37 #include "dev/uart1.h"
38 #include "dev/watchdog.h"
41 #include "isr_compat.h"
43 static int (*uart1_input_handler)(
unsigned char c);
44 static volatile uint8_t rx_in_progress;
46 static volatile uint8_t transmitting;
48 #ifdef UART1_CONF_TX_WITH_INTERRUPT
49 #define TX_WITH_INTERRUPT UART1_CONF_TX_WITH_INTERRUPT
51 #define TX_WITH_INTERRUPT 0
54 #ifdef UART1_CONF_RX_WITH_DMA
55 #define RX_WITH_DMA UART1_CONF_RX_WITH_DMA
64 static uint8_t txbuf_data[TXBUFSIZE];
70 static uint8_t
rxbuf[RXBUFSIZE];
71 static uint16_t last_size;
72 static struct ctimer rxdma_timer;
75 handle_rxdma_timer(
void *ptr)
79 while(last_size != size) {
82 uart1_input_handler((
unsigned char)
rxbuf[RXBUFSIZE - last_size]);
84 if(last_size == 0) last_size = RXBUFSIZE;
95 return ((~ UTCTL1) & TXEPT) | rx_in_progress | transmitting;
99 uart1_set_input(
int (*input)(
unsigned char c))
104 uart1_input_handler = input;
108 uart1_writeb(
unsigned char c)
111 #if TX_WITH_INTERRUPT
120 if(transmitting == 0) {
131 while((IFG2 & UTXIFG1) == 0);
150 UCTL1 = SWRST | CHAR;
166 #if F_CPU == 3900000ul
168 case UART1_BAUD2UBR(115200ul):
171 case UART1_BAUD2UBR(57600ul):
174 case UART1_BAUD2UBR(38400ul):
177 case UART1_BAUD2UBR(19200ul):
180 case UART1_BAUD2UBR(9600ul):
184 #elif F_CPU == 2457600ul
186 case UART1_BAUD2UBR(115200ul):
189 case UART1_BAUD2UBR(57600ul):
198 #error Unsupported CPU speed in uart1.c
204 ME2 |= (UTXE1 | URXE1);
217 #if TX_WITH_INTERRUPT
225 DMACTL0 = DMA0TSEL_9;
228 DMA0SA = (
unsigned int) &RXBUF1;
229 DMA0DA = (
unsigned int) &
rxbuf;
231 last_size = RXBUFSIZE;
232 DMA0CTL = DMADT_4 + DMASBDB + DMADSTINCR_3 + DMAEN + DMAREQ;
234 msp430_add_lpm_req(MSP430_REQUIRE_LPM1);
239 ISR(UART1RX, uart1_rx_interrupt)
242 ENERGEST_ON(ENERGEST_TYPE_IRQ);
244 if(!(URXIFG1 & IFG2)) {
257 if(uart1_input_handler !=
NULL) {
258 if(uart1_input_handler(c)) {
265 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
269 #if TX_WITH_INTERRUPT
270 ISR(UART1TX, uart1_tx_interrupt)
272 ENERGEST_ON(ENERGEST_TYPE_IRQ);
280 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Header file for the ring buffer library
Structure that holds the state of a ring buffer.
#define NULL
The null pointer.
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Header file for the energy estimation mechanism
int ringbuf_elements(struct ringbuf *r)
Get the number of elements currently in the ring buffer.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
void watchdog_periodic(void)
Writes the WDT clear sequence.
tcirc_buf rxbuf
The RX circular buffer, for storing characters from serial port.
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Header file for the callback timer
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
#define CLOCK_SECOND
A second, measured in system clock time.