42 #include "isr_compat.h"
44 signed char tx_byte_ctr, rx_byte_ctr;
45 unsigned char rx_buf[2];
46 unsigned char* tx_buf_ptr;
47 unsigned char* rx_buf_ptr;
48 unsigned char receive_data;
49 unsigned char transmit_data1;
50 unsigned char transmit_data2;
51 volatile unsigned int i;
63 i2c_receiveinit(uint8_t slave_address) {
65 UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC;
66 UCB1CTL1 = UCSSEL_2 | UCSWRST;
67 UCB1BR0 = I2C_PRESC_400KHZ_LSB;
68 UCB1BR1 = I2C_PRESC_400KHZ_MSB;
69 UCB1I2CSA = slave_address;
75 #if I2C_RX_WITH_INTERRUPT
90 i2c_transmitinit(uint8_t slave_address) {
92 UCB1CTL0 |= (UCMST | UCMODE_3 | UCSYNC);
93 UCB1CTL1 = UCSSEL_2 + UCSWRST;
94 UCB1BR0 = I2C_PRESC_400KHZ_LSB;
95 UCB1BR1 = I2C_PRESC_400KHZ_MSB;
96 UCB1I2CSA = slave_address;
111 static volatile uint8_t rx_byte_tot = 0;
113 i2c_receive_n(uint8_t byte_ctr, uint8_t *rx_buf) {
115 rx_byte_tot = byte_ctr;
116 rx_byte_ctr = byte_ctr;
119 while ((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG))
120 PRINTFDEBUG (
"____ UCTXSTT not clear OR NACK received\n");
122 #if I2C_RX_WITH_INTERRUPT
123 PRINTFDEBUG(
" RX Interrupts: YES \n");
126 if(rx_byte_tot == 1){
129 while(UCB1CTL1 & UCTXSTT)
130 PRINTFDEBUG (
"____ STT clear wait\n");
140 uint8_t n_received = 0;
142 PRINTFDEBUG(
" RX Interrupts: NO \n");
146 while (rx_byte_ctr > 0){
147 if (UC1IFG & UCB1RXIFG) {
148 rx_buf[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
150 UC1IFG &= ~UCB1RXIFG;
170 return (UCB1STAT & UCBBUSY);
178 I2C_PxSEL |= (I2C_SDA | I2C_SCL);
179 I2C_PxSEL2 |= (I2C_SDA | I2C_SCL);
180 I2C_PxDIR |= I2C_SCL;
181 I2C_PxDIR &= ~I2C_SDA;
182 I2C_PxREN |= (I2C_SDA | I2C_SCL);
183 I2C_PxOUT |= (I2C_SDA | I2C_SCL);
188 I2C_PxSEL &= ~(I2C_SDA | I2C_SCL);
189 I2C_PxSEL2 &= ~(I2C_SDA | I2C_SCL);
190 I2C_PxREN &= ~(I2C_SDA | I2C_SCL);
191 I2C_PxOUT &= ~(I2C_SDA | I2C_SCL);
203 static volatile uint8_t tx_byte_tot = 0;
205 i2c_transmit_n(uint8_t byte_ctr, uint8_t *tx_buf) {
206 tx_byte_tot = byte_ctr;
207 tx_byte_ctr = byte_ctr;
209 UCB1CTL1 |= UCTR + UCTXSTT;
213 ISR(USCIAB1TX, i2c_tx_interrupt)
216 if (UC1IFG & UCB1TXIFG) {
217 if (tx_byte_ctr == 0) {
219 UC1IFG &= ~UCB1TXIFG;
222 UCB1TXBUF = tx_buf_ptr[tx_byte_tot - tx_byte_ctr];
227 #if I2C_RX_WITH_INTERRUPT
228 else if (UC1IFG & UCB1RXIFG){
229 rx_buf_ptr[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
231 if (rx_byte_ctr == 1){
233 if (rx_byte_tot != 1)
235 UC1IFG &= ~UCB1RXIFG;
241 ISR(USCIAB1RX, i2c_rx_interrupt)
243 if(UCB1STAT & UCNACKIFG) {
244 PRINTFDEBUG(
"!!! NACK received in RX\n");
246 UCB1STAT &= ~UCNACKIFG;