43 #include "isr_compat.h"
46 #include "dev/watchdog.h"
48 #include "isr_compat.h"
50 #include "platform-conf.h"
55 #define PRINTF(...) printf(__VA_ARGS__)
60 signed char tx_byte_ctr, rx_byte_ctr;
61 unsigned char rx_buf[2];
62 unsigned char* tx_buf_ptr;
63 unsigned char* rx_buf_ptr;
64 unsigned char receive_data;
65 unsigned char transmit_data1;
66 unsigned char transmit_data2;
67 volatile unsigned int i;
71 static int (*uart1_input_handler)(
unsigned char c);
73 static volatile uint8_t serial_transmitting;
75 #ifdef UART1_CONF_TX_WITH_INTERRUPT
76 #define TX_WITH_INTERRUPT UART1_CONF_TX_WITH_INTERRUPT
78 #define TX_WITH_INTERRUPT 1
98 i2c_receiveinit(uint8_t slave_address) {
100 UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC;
101 UCB1CTL1 = UCSSEL_2 | UCSWRST;
102 UCB1BR0 = I2C_PRESC_400KHZ_LSB;
103 UCB1BR1 = I2C_PRESC_400KHZ_MSB;
104 UCB1I2CSA = slave_address;
108 UCB1CTL1 &= ~UCSWRST;
109 UCB1I2CIE = UCNACKIE;
110 #if I2C_RX_WITH_INTERRUPT
125 i2c_transmitinit(uint8_t slave_address) {
127 UCB1CTL0 |= (UCMST | UCMODE_3 | UCSYNC);
128 UCB1CTL1 = UCSSEL_2 + UCSWRST;
129 UCB1BR0 = I2C_PRESC_400KHZ_LSB;
130 UCB1BR1 = I2C_PRESC_400KHZ_MSB;
131 UCB1I2CSA = slave_address;
133 UCB1CTL1 &= ~UCSWRST;
134 UCB1I2CIE = UCNACKIE;
146 static volatile uint8_t rx_byte_tot = 0;
148 i2c_receive_n(uint8_t byte_ctr, uint8_t *rx_buf) {
150 rx_byte_tot = byte_ctr;
151 rx_byte_ctr = byte_ctr;
154 while ((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG));
157 #if I2C_RX_WITH_INTERRUPT
158 PRINTF(
" RX Interrupts: YES \n");
161 if(rx_byte_tot == 1){
164 while(UCB1CTL1 & UCTXSTT);
175 uint8_t n_received = 0;
181 while (rx_byte_ctr > 0){
182 if (UC1IFG & UCB1RXIFG) {
183 rx_buf[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
185 UC1IFG &= ~UCB1RXIFG;
205 return (UCB1STAT & UCBBUSY);
213 I2C_PxSEL |= (I2C_SDA | I2C_SCL);
214 I2C_PxSEL2 |= (I2C_SDA | I2C_SCL);
215 I2C_PxDIR |= I2C_SCL;
216 I2C_PxDIR &= ~I2C_SDA;
217 I2C_PxREN |= (I2C_SDA | I2C_SCL);
218 I2C_PxOUT |= (I2C_SDA | I2C_SCL);
223 I2C_PxSEL &= ~(I2C_SDA | I2C_SCL);
224 I2C_PxSEL2 &= ~(I2C_SDA | I2C_SCL);
225 I2C_PxREN &= ~(I2C_SDA | I2C_SCL);
226 I2C_PxOUT &= ~(I2C_SDA | I2C_SCL);
238 static volatile uint8_t tx_byte_tot = 0;
240 i2c_transmit_n(uint8_t byte_ctr, uint8_t *tx_buf) {
241 tx_byte_tot = byte_ctr;
242 tx_byte_ctr = byte_ctr;
244 UCB1CTL1 |= UCTR + UCTXSTT;
252 return (UCA1STAT & UCBUSY) | serial_transmitting;
256 uart1_set_input(
int (*
input)(
unsigned char c))
258 PRINTF(
"UART1 input handler set\n");
259 uart1_input_handler =
input;
264 uart1_writeb(
unsigned char c)
290 while((UCA1STAT & UCBUSY));
293 RS485_TXEN_PORT(OUT) |= BV(RS485_TXEN_PIN);
295 while((UCA1STAT & UCBUSY));
296 RS485_TXEN_PORT(OUT) &= ~BV(RS485_TXEN_PIN);
298 PRINTF(
"char written to UCA1TXBUF\n");
304 uart1_writearray(
unsigned char* c,
int length)
306 RS485_TXEN_PORT(OUT) |= BV(RS485_TXEN_PIN);
308 for(i=0;i<length;i++)
313 while((UCA1STAT & UCBUSY));
314 RS485_TXEN_PORT(OUT) &= ~BV(RS485_TXEN_PIN);
325 PRINTF(
"UART1 pin init\n");
329 UART1_RX_PORT(
SEL) |= BV(UART1_RX_PIN);
330 UART1_RX_PORT(DIR) &= ~BV(UART1_RX_PIN);
331 UART1_TX_PORT(
SEL) |= BV(UART1_TX_PIN);
332 UART1_TX_PORT(DIR) |= BV(UART1_TX_PIN);
335 RS485_TXEN_PORT(DIR) |= BV(RS485_TXEN_PIN);
336 RS485_TXEN_PORT(OUT) &= ~BV(RS485_TXEN_PIN);
348 PRINTF(
"UART1 init\n");
351 UCA1CTL1 |= UCSSEL_3;
357 UCA1CTL1 &= ~UCSWRST;
359 serial_transmitting = 0;
364 UCA1CTL1 &= ~UCSWRST;
372 ISR(USCIAB1TX, uart1_i2c_tx_interrupt)
375 if (UC1IFG & UCB1TXIFG) {
377 UC1IFG &= ~UCB1TXIFG;
378 if (tx_byte_ctr == 0) {
382 UCB1TXBUF = tx_buf_ptr[tx_byte_tot - tx_byte_ctr];
387 #if I2C_RX_WITH_INTERRUPT //TODO: Is this right as we are in the TX interrupt?
388 else if (UC1IFG & UCB1RXIFG){
389 UC1IFG &= ~UCB1RXIFG;
390 PRINTF(
"USCIAB1TX: UCB1RXIFG\n");
391 rx_buf_ptr[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
393 if (rx_byte_ctr == 1){
395 if (rx_byte_tot != 1)
400 #if TX_WITH_INTERRUPT
401 else if(IFG2 & UCA1TXIFG) {
403 PRINTF(
"USCIAB1TX: UCA1TXIFG\n");
405 serial_transmitting = 0;
406 RS485_TXEN_PORT(OUT) &= ~BV(RS485_TXEN_PIN);
414 ISR(USCIAB1RX, uart1_i2c_rx_interrupt)
419 #if I2C_RX_WITH_INTERRUPT
420 if(UCB1STAT & UCNACKIFG) {
422 UCB1STAT &= ~UCNACKIFG;
425 if( UC1IFG & UCA1RXIFG){
426 if(UCA1STAT & UCRXERR) {
432 if(uart1_input_handler !=
NULL) {
433 if(uart1_input_handler(c)) {
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
static void input(void)
Process a received 6lowpan packet.
void i2c_enable(void)
Configure serial controller in I2C mode and set I2C speed.
int ringbuf_elements(struct ringbuf *r)
Get the number of elements currently in the ring buffer.
#define NULL
The null pointer.
Structure that holds the state of a ring buffer.
void watchdog_periodic(void)
Writes the WDT clear sequence.
I2C communication device driver header file for Zolertia Z1 sensor node.
void i2c_disable(void)
Configure serial controller in disabled mode.
void uart1_pin_init(void)
Configure pins for RS232 port.
Header file for the ring buffer library