39 #include "dev/uart1.h"
40 #include "dev/watchdog.h"
44 static int (*uart1_input_handler)(
unsigned char c);
45 static volatile uint8_t rx_in_progress;
47 static volatile uint8_t transmitting;
49 #ifdef UART1_CONF_TX_WITH_INTERRUPT
50 #define TX_WITH_INTERRUPT UART1_CONF_TX_WITH_INTERRUPT
52 #define TX_WITH_INTERRUPT 0
55 #ifdef UART1_CONF_RX_WITH_DMA
56 #define RX_WITH_DMA UART1_CONF_RX_WITH_DMA
62 #warning RX_WITH_DMA ENABLED - WILL NOT WORK WITH MSPSIM / COOJA!
69 static uint8_t txbuf_data[TXBUFSIZE];
75 static uint8_t
rxbuf[RXBUFSIZE];
76 static uint16_t last_size;
77 static struct ctimer rxdma_timer;
80 handle_rxdma_timer(
void *ptr)
84 while(last_size != size) {
87 uart1_input_handler((
unsigned char)
rxbuf[RXBUFSIZE - last_size]);
89 if(last_size == 0) last_size = RXBUFSIZE;
100 return ((~ UTCTL1) & TXEPT) | rx_in_progress | transmitting;
104 uart1_set_input(
int (*input)(
unsigned char c))
109 uart1_input_handler = input;
113 uart1_writeb(
unsigned char c)
116 #if TX_WITH_INTERRUPT
125 if(transmitting == 0) {
136 while((IFG2 & UTXIFG1) == 0);
148 uart1_init(
unsigned long ubr)
155 UCTL1 = SWRST | CHAR;
171 #if F_CPU == 3900000ul
173 case UART1_BAUD2UBR(115200ul):
176 case UART1_BAUD2UBR(57600ul):
179 case UART1_BAUD2UBR(38400ul):
182 case UART1_BAUD2UBR(19200ul):
185 case UART1_BAUD2UBR(9600ul):
189 #elif F_CPU == 2457600ul
191 case UART1_BAUD2UBR(115200ul):
194 case UART1_BAUD2UBR(57600ul):
203 #error Unsupported CPU speed in uart1.c
209 ME2 |= (UTXE1 | URXE1);
222 #if TX_WITH_INTERRUPT
230 DMACTL0 = DMA0TSEL_9;
233 DMA0SA = (
unsigned int) &RXBUF1;
234 DMA0DA = (
unsigned int) &
rxbuf;
236 last_size = RXBUFSIZE;
237 DMA0CTL = DMADT_4 + DMASBDB + DMADSTINCR_3 + DMAEN + DMAREQ;
239 msp430_add_lpm_req(MSP430_REQUIRE_LPM1);
247 #ifdef __IAR_SYSTEMS_ICC__
248 #pragma vector=UART1RX_VECTOR
251 interrupt(UART1RX_VECTOR)
253 uart1_rx_interrupt(
void)
256 ENERGEST_ON(ENERGEST_TYPE_IRQ);
258 if(!(URXIFG1 & IFG2)) {
271 if(uart1_input_handler !=
NULL) {
272 if(uart1_input_handler(c)) {
279 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
283 #if TX_WITH_INTERRUPT
284 interrupt(UART1TX_VECTOR)
285 uart1_tx_interrupt(
void)
287 ENERGEST_ON(ENERGEST_TYPE_IRQ);
295 ENERGEST_OFF(ENERGEST_TYPE_IRQ);