-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi.c
80 lines (64 loc) · 1.43 KB
/
spi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* File Name: spi.c
* Project: MSP430 Programing
* File Created: 25-08-2022 10:53:56:PM
* Author: <<KhangKT>>
* AuthorEmail: [email protected]
* Phone:0964991713
*/
#include <msp430g2553.h>
#include <stdint.h>
#include "Include/spi.h"
volatile uint8_t spi_buf = 0;
#define SCLK BIT5
#define MISO BIT6
#define MOSI BIT7
#define CS BIT2
void spi_init()
{
UCB0CTL1 = UCSWRST;
UCB0CTL0 = UCSYNC + UCSPB + UCMSB + UCCKPH; // UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master,2 bit stop
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 = 0x02; // Frequency CPU / 2 (16Mhz / 2 = 8 Mhz SPI)
UCB0BR1 = 0;
P1SEL |= SCLK | MISO | MOSI; // P1.6 is MISO and P1.7 is MOSI
P1SEL2 |= SCLK | MISO | MOSI;
P1DIR |= SCLK | MOSI;
P1DIR &= ~MISO;
P2DIR |= CS; // | CS2; // P2.2 CS (chip select)
P2OUT |= CS; // | CS2;
UCB0CTL1 &= ~UCSWRST; // Initialize USCI state machine
}
void spi_txready()
{
while (!(IFG2 & UCB0TXIFG))
; // TX buffer ready?
}
void spi_rxready()
{
while (!(IFG2 & UCB0RXIFG))
; // RX Received?
}
void spi_send(uint8_t data)
{
spi_txready();
UCB0TXBUF = data; // Send data over SPI to Slave
}
void spi_recv()
{
spi_rxready();
spi_buf = UCB0RXBUF; // Store received data
}
void spi_transfer(uint8_t data)
{
spi_send(data);
spi_recv();
}
void spi_chipEnable()
{
P2OUT &= ~CS;
}
void spi_chipDisable()
{
P2OUT |= CS;
}