52 #include "dev/uart1.h"
53 #include "dev/watchdog.h"
59 static int (*uart1_input_handler)(
unsigned char c);
61 static volatile uint8_t transmitting;
63 #ifdef UART1_CONF_TX_WITH_INTERRUPT
64 #define TX_WITH_INTERRUPT UART1_CONF_TX_WITH_INTERRUPT
66 #define TX_WITH_INTERRUPT 1
72 #ifdef UART1_CONF_TX_BUFSIZE
73 #define UART1_TX_BUFSIZE UART1_CONF_TX_BUFSIZE
75 #define UART1_TX_BUFSIZE 64
79 static uint8_t txbuf_data[UART1_TX_BUFSIZE];
90 uart1_set_input(
int (*input)(
unsigned char c))
92 uart1_input_handler = input;
96 uart1_writeb(
unsigned char c)
108 if(transmitting == 0) {
111 INT_SC1FLAG = INT_SCTXFREE;
112 INT_SC1CFG |= INT_SCTXFREE;
118 while((INT_SC1FLAG & INT_SCTXFREE) == 0);
123 INT_SC1FLAG = INT_SCTXFREE;
135 uart1_init(
unsigned long ubr)
138 GPIO_PBCFGL &= 0xF00F;
139 GPIO_PBCFGL |= 0x0490;
141 u16_t uartper = (
u32_t)24e6/(2*ubr);
142 u32_t rest = (
u32_t)24e6%(2*ubr);
146 if(rest > (2*ubr)/4 && rest < (3*2*ubr)/4){
149 else if(rest >= (3*2*ubr)/4){
153 SC1_UARTPER = uartper;
155 SC1_UARTCFG = SC_UART8BIT;
157 SC1_MODE = SC1_MODE_UART;
159 SC1_INTMODE = SC_RXVALLEVEL | SC_TXFREELEVEL;
161 INT_SC1CFG = INT_SCRXVAL;
165 #if TX_WITH_INTERRUPT
170 INT_SC1FLAG = 0xFFFF;
172 INT_CFGSET = INT_SC1;
175 void uart1_rx_interrupt(
void);
176 void uart1_tx_interrupt(
void);
181 ENERGEST_ON(ENERGEST_TYPE_IRQ);
183 if(INT_SC1FLAG & INT_SCRXVAL){
184 uart1_rx_interrupt();
185 INT_SC1FLAG = INT_SCRXVAL;
187 #if TX_WITH_INTERRUPT
188 else if(INT_SC1FLAG & INT_SCTXFREE){
189 uart1_tx_interrupt();
190 INT_SC1FLAG = INT_SCTXFREE;
196 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
200 void uart1_rx_interrupt(
void)
206 if(uart1_input_handler !=
NULL) {
207 uart1_input_handler(c);
212 #if TX_WITH_INTERRUPT
213 void uart1_tx_interrupt(
void)
218 INT_SC1CFG &= ~INT_SCTXFREE;