1 | #include "msp430g2553.h" // device specific header
|
2 | #include "TI_USCI_I2C_master.h"
|
3 |
|
4 | signed char byteCtr;
|
5 | unsigned char *TI_receive_field;
|
6 | unsigned char *TI_transmit_field;
|
7 |
|
8 | void TI_USCI_I2C_receiveinit(unsigned char slave_address, unsigned char prescale)
|
9 | {
|
10 | P1SEL |= SDA_PIN + SCL_PIN; // Assign I2C pins to USCI_B0 P3???
|
11 | UCB0CTL1 = UCSWRST; // Enable SW reset
|
12 | UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
|
13 | UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
|
14 | UCB0BR0 = prescale; // set prescaler
|
15 | UCB0BR1 = 0;
|
16 | UCB0I2CSA = slave_address; // set slave address
|
17 | UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
|
18 | UCB0I2CIE = UCNACKIE;
|
19 | IE2 = UCB0RXIE; // Enable RX interrupt
|
20 | }
|
21 |
|
22 | void TI_USCI_I2C_transmitinit(unsigned char slave_address, unsigned char prescale)
|
23 | {
|
24 | P1SEL |= SDA_PIN + SCL_PIN; // Assign I2C pins to USCI_B0 P3???
|
25 | UCB0CTL1 = UCSWRST; // Enable SW reset
|
26 | UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
|
27 | UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
|
28 | UCB0BR0 = prescale; // set prescaler
|
29 | UCB0BR1 = 0;
|
30 | UCB0I2CSA = slave_address; // Set slave address
|
31 | UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
|
32 | UCB0I2CIE = UCNACKIE;
|
33 | IE2 = UCB0TXIE; // Enable TX ready interrupt
|
34 | }
|
35 |
|
36 | void TI_USCI_I2C_receive(unsigned char byteCount, unsigned char *field)
|
37 | {
|
38 | TI_receive_field = field;
|
39 | if ( byteCount == 1 )
|
40 | {
|
41 | byteCtr = 0 ;
|
42 | __disable_interrupt();
|
43 | UCB0CTL1 |= UCTXSTT; // I2C start condition
|
44 | while (UCB0CTL1 & UCTXSTT); // Start condition sent?
|
45 | UCB0CTL1 |= UCTXSTP; // I2C stop condition
|
46 | __enable_interrupt();
|
47 | }
|
48 | else if ( byteCount > 1 )
|
49 | {
|
50 | byteCtr = byteCount - 2 ;
|
51 | UCB0CTL1 |= UCTXSTT; // I2C start condition
|
52 | }
|
53 | else
|
54 | while (1); // illegal parameter
|
55 | }
|
56 |
|
57 | void TI_USCI_I2C_transmit(unsigned char byteCount, unsigned char *field)
|
58 | {
|
59 | TI_transmit_field = field;
|
60 | byteCtr = byteCount;
|
61 | UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
|
62 | }
|
63 |
|
64 | unsigned char TI_USCI_I2C_slave_present(unsigned char slave_address)
|
65 | {
|
66 | unsigned char ie2_bak, slaveadr_bak, ucb0i2cie, returnValue;
|
67 | ucb0i2cie = UCB0I2CIE; // restore old UCB0I2CIE
|
68 | ie2_bak = IE2; // store IE2 register
|
69 | slaveadr_bak = UCB0I2CSA; // store old slave address
|
70 | UCB0I2CIE &= ~ UCNACKIE; // no NACK interrupt
|
71 | UCB0I2CSA = slave_address; // set slave address
|
72 | IE2 &= ~(UCB0TXIE + UCB0RXIE); // no RX or TX interrupts
|
73 | __disable_interrupt();
|
74 | UCB0CTL1 |= UCTR + UCTXSTT + UCTXSTP; // I2C TX, start condition
|
75 | while (UCB0CTL1 & UCTXSTP); // wait for STOP condition
|
76 |
|
77 | returnValue = !(UCB0STAT & UCNACKIFG);
|
78 | __enable_interrupt();
|
79 | IE2 = ie2_bak; // restore IE2
|
80 | UCB0I2CSA = slaveadr_bak; // restore old slave address
|
81 | UCB0I2CIE = ucb0i2cie; // restore old UCB0CTL1
|
82 | return returnValue; // return whether or not
|
83 | // a NACK occured
|
84 | }
|
85 |
|
86 | unsigned char TI_USCI_I2C_notready()
|
87 | {
|
88 | return (UCB0STAT & UCBBUSY);
|
89 | }
|
90 |
|
91 |
|
92 | #pragma vector = USCIAB0RX_VECTOR
|
93 | __interrupt void USCIAB0RX_ISR(void)
|
94 | {
|
95 | if (UCB0STAT & UCNACKIFG)
|
96 | { // send STOP if slave sends NACK
|
97 | UCB0CTL1 |= UCTXSTP;
|
98 | UCB0STAT &= ~UCNACKIFG;
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 | #pragma vector = USCIAB0TX_VECTOR
|
104 | __interrupt void USCIAB0TX_ISR(void)
|
105 | {
|
106 | if (IFG2 & UCB0RXIFG)
|
107 | {
|
108 | if ( byteCtr == 0 )
|
109 | {
|
110 | UCB0CTL1 |= UCTXSTP; // I2C stop condition
|
111 | *TI_receive_field = UCB0RXBUF;
|
112 | TI_receive_field++;
|
113 | }
|
114 | else
|
115 | {
|
116 | *TI_receive_field = UCB0RXBUF;
|
117 | TI_receive_field++;
|
118 | byteCtr--;
|
119 | }
|
120 | }
|
121 | else
|
122 | {
|
123 | if (byteCtr == 0)
|
124 | {
|
125 | UCB0CTL1 |= UCTXSTP; // I2C stop condition
|
126 | IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
|
127 | }
|
128 | else
|
129 | {
|
130 | UCB0TXBUF = *TI_transmit_field;
|
131 | TI_transmit_field++;
|
132 | byteCtr--;
|
133 | }
|
134 | }
|
135 | }
|