diff --git a/cpu/sam3/periph/uart.c b/cpu/sam3/periph/uart.c index 07da80ddd..95c2cad47 100644 --- a/cpu/sam3/periph/uart.c +++ b/cpu/sam3/periph/uart.c @@ -28,63 +28,47 @@ #define ENABLE_DEBUG (0) #include "debug.h" -/* guard file in case no UART device was specified */ -#if UART_NUMOF - -/** - * @brief Each UART device has to store two callbacks. - */ -typedef struct { - uart_rx_cb_t rx_cb; - uart_tx_cb_t tx_cb; - void *arg; -} uart_conf_t; - /** * @brief Allocate memory to store the callback functions. */ -static uart_conf_t uart_config[UART_NUMOF]; +static uart_isr_ctx_t uart_config[UART_NUMOF]; +static int init_base(uart_t uart, uint32_t baudrate); -int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, uart_tx_cb_t tx_cb, void *arg) +int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) { /* initialize basic functionality */ - int res = uart_init_blocking(uart, baudrate); + int res = init_base(uart, baudrate); if (res != 0) { return res; } /* register callbacks */ uart_config[uart].rx_cb = rx_cb; - uart_config[uart].tx_cb = tx_cb; uart_config[uart].arg = arg; /* configure interrupts and enable RX interrupt */ switch (uart) { #if UART_0_EN case UART_0: - NVIC_SetPriority(UART_0_IRQ, UART_IRQ_PRIO); NVIC_EnableIRQ(UART_0_IRQ); UART_0_DEV->UART_IER = UART_IER_RXRDY; break; #endif #if UART_1_EN case UART_1: - NVIC_SetPriority(UART_1_IRQ, UART_IRQ_PRIO); NVIC_EnableIRQ(UART_1_IRQ); UART_1_DEV->US_IER = US_IER_RXRDY; break; #endif #if UART_2_EN case UART_2: - NVIC_SetPriority(UART_2_IRQ, UART_IRQ_PRIO); NVIC_EnableIRQ(UART_2_IRQ); UART_2_DEV->US_IER = US_IER_RXRDY; break; #endif #if UART_3_EN case UART_3: - NVIC_SetPriority(UART_3_IRQ, UART_IRQ_PRIO); NVIC_EnableIRQ(UART_3_IRQ); UART_3_DEV->US_IER = US_IER_RXRDY; break; @@ -93,7 +77,7 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, uart_tx_cb_t t return 0; } -int uart_init_blocking(uart_t uart, uint32_t baudrate) +static int init_base(uart_t uart, uint32_t baudrate) { switch (uart) { #if UART_0_EN @@ -168,120 +152,39 @@ int uart_init_blocking(uart_t uart, uint32_t baudrate) return 0; } -void uart_tx_begin(uart_t uart) +void uart_write(uart_t uart, const uint8_t *data, size_t len) { - switch (uart) { -#if UART_0_EN - case UART_0: - UART_0_DEV->UART_IER = UART_IER_TXRDY; - break; -#endif -#if UART_1_EN - case UART_1: - UART_1_DEV->US_IER = US_IER_TXRDY; - break; -#endif -#if UART_2_EN - case UART_2: - UART_2_DEV->US_IER = US_IER_TXRDY; - break; -#endif -#if UART_3_EN - case UART_3: - UART_3_DEV->US_IER = US_IER_TXRDY; - break; -#endif - } -} - -int uart_write(uart_t uart, char data) -{ - switch (uart) { -#if UART_0_EN - case UART_0: - UART_0_DEV->UART_THR = data; - break; -#endif -#if UART_1_EN - case UART_1: - UART_1_DEV->US_THR = data; - break; -#endif -#if UART_2_EN - case UART_2: - UART_2_DEV->US_THR = data; - break; -#endif -#if UART_3_EN - case UART_3: - UART_3_DEV->US_THR = data; - break; -#endif - } - return 1; -} + Uart *dev; -int uart_read_blocking(uart_t uart, char *data) -{ switch (uart) { #if UART_0_EN case UART_0: - while (!(UART_0_DEV->UART_SR & UART_SR_RXRDY)); - *data = (char)UART_0_DEV->UART_RHR; + dev = (Uart *)UART_0_DEV; break; #endif #if UART_1_EN case UART_1: - while (!(UART_1_DEV->US_CSR & US_CSR_RXRDY)); - *data = (char)UART_1_DEV->US_RHR; + dev = (Uart *)UART_1_DEV; break; #endif #if UART_2_EN case UART_2: - while (!(UART_2_DEV->US_CSR & US_CSR_RXRDY)); - *data = (char)UART_2_DEV->US_RHR; + dev = (Uart *)UART_2_DEV; break; #endif #if UART_3_EN case UART_3: - while (!(UART_3_DEV->US_CSR & US_CSR_RXRDY)); - *data = (char)UART_3_DEV->US_RHR; + dev = (Uart *)UART_3_DEV; break; #endif + default: + return; } - return 1; -} - -int uart_write_blocking(uart_t uart, char data) -{ - switch (uart) { -#if UART_0_EN - case UART_0: - while (!(UART_0_DEV->UART_SR & UART_SR_TXRDY)); - UART_0_DEV->UART_THR = data; - break; -#endif -#if UART_1_EN - case UART_1: - while(!(UART_1_DEV->US_CSR & US_CSR_TXRDY)); - UART_1_DEV->US_THR = data; - break; -#endif -#if UART_2_EN - case UART_2: - while(!(UART_2_DEV->US_CSR & US_CSR_TXRDY)); - UART_2_DEV->US_THR = data; - break; -#endif -#if UART_3_EN - case UART_3: - while(!(UART_3_DEV->US_CSR & US_CSR_TXRDY)); - UART_3_DEV->US_THR = data; - break; -#endif + for (size_t i = 0; i < len; i++) { + while (!(dev->UART_SR & UART_SR_TXRDY)); + dev->UART_THR = data[i]; } - return 1; } void uart_poweron(uart_t uart) @@ -343,11 +246,6 @@ void UART_0_ISR(void) char data = (char)UART_0_DEV->UART_RHR; uart_config[UART_0].rx_cb(uart_config[UART_0].arg, data); } - if ((UART_0_DEV->UART_SR & UART_SR_TXRDY) && (UART_0_DEV->UART_IMR & UART_IMR_TXRDY)) { - if (uart_config[UART_0].tx_cb(uart_config[UART_0].arg) == 0) { - UART_0_DEV->UART_IDR = UART_IDR_TXRDY; - } - } if (sched_context_switch_request) { thread_yield(); } @@ -361,11 +259,6 @@ void UART_1_ISR(void) char data = (char)UART_1_DEV->US_RHR; uart_config[UART_1].rx_cb(uart_config[UART_1].arg, data); } - if ((UART_1_DEV->US_CSR & US_CSR_TXRDY) && (UART_1_DEV->US_IMR & US_IMR_TXRDY)) { - if (uart_config[UART_1].tx_cb(uart_config[UART_1].arg) == 0) { - UART_1_DEV->US_IDR = US_IDR_TXRDY; - } - } if (sched_context_switch_request) { thread_yield(); } @@ -379,11 +272,6 @@ void UART_2_ISR(void) char data = (char)UART_2_DEV->US_RHR; uart_config[UART_2].rx_cb(uart_config[UART_2].arg, data); } - if ((UART_2_DEV->US_CSR & US_CSR_TXRDY) && (UART_2_DEV->US_IMR & US_IMR_TXRDY)) { - if (uart_config[UART_2].tx_cb(uart_config[UART_2].arg) == 0) { - UART_2_DEV->US_IDR = US_IDR_TXRDY; - } - } if (sched_context_switch_request) { thread_yield(); } @@ -397,15 +285,8 @@ void UART_3_ISR(void) char data = (char)UART_3_DEV->US_RHR; uart_config[UART_3].rx_cb(uart_config[UART_3].arg, data); } - if ((UART_3_DEV->US_CSR & US_CSR_TXRDY) && (UART_3_DEV->US_IMR & US_IMR_TXRDY)) { - if (uart_config[UART_3].tx_cb(uart_config[UART_3].arg) == 0) { - UART_3_DEV->US_IDR = US_IDR_TXRDY; - } - } if (sched_context_switch_request) { thread_yield(); } } #endif - -#endif /* UART_NUMOF */