1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 23:32:44 +01:00

cpu/atmega2560: reworked UART implementation

This commit is contained in:
Hauke Petersen 2016-03-10 11:11:42 +01:00 committed by kYc0o
parent e57d511499
commit ccf7cde82d

View File

@ -19,216 +19,115 @@
* @} * @}
*/ */
#include "board.h"
#include "cpu.h" #include "cpu.h"
#include "thread.h"
#include "sched.h" #include "sched.h"
#include "thread.h"
#include "periph/uart.h" #include "periph/uart.h"
#include "periph_conf.h"
/** /**
* @brief Allocate memory to store the callback functions. * @brief Configured device map
* @{
*/ */
static uart_isr_ctx_t config[UART_NUMOF]; #if UART_NUMOF
static mega_uart_t *dev[] = {
#ifdef UART_0
UART_0,
#endif
#ifdef UART_1
UART_1,
#endif
#ifdef UART_2
UART_2,
#endif
#ifdef UART_3
UART_3
#endif
};
#else
/* fallback if no UART is defined */
static const mega_uart_t *dev[] = { NULL };
#endif
static int init_base(uart_t uart, uint32_t baudrate); /**
* @brief Allocate memory to store the callback functions.
*/
static uart_isr_ctx_t isr_ctx[UART_NUMOF];
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
{ {
/* initialize basic functionality */ /* make sure the given device is valid */
int res = init_base(uart, baudrate); if (uart >= UART_NUMOF) {
return -1;
if (res != 0) {
return res;
} }
/* register callbacks */ /* register interrupt context */
config[uart].rx_cb = rx_cb; isr_ctx[uart].rx_cb = rx_cb;
config[uart].arg = arg; isr_ctx[uart].arg = arg;
/* configure interrupts and enable RX interrupt */ /* disable and reset UART */
switch (uart) { dev[uart]->CSRB = 0;
#if UART_0_EN dev[uart]->CSRA = 0;
case UART_0: /* configure UART to 8N1 mode */
UART0_RX_IRQ_EN; dev[uart]->CSRC = (1 << UCSZ00) | (1 << UCSZ01);
break; /* set clock divider */
#endif /* UART_0_EN */ dev[uart]->BRR = CLOCK_CORECLOCK / (16 * baudrate);
#if UART_1_EN /* enable RX and TX and the RX interrupt */
dev[uart]->CSRB = ((1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0));
case UART_1:
UART1_RX_IRQ_EN;
break;
#endif /* UART_1_EN */
#if UART_2_EN
case UART_2:
UART2_RX_IRQ_EN;
break;
#endif /* UART_2_EN */
#if UART_3_EN
case UART_3:
UART3_RX_IRQ_EN;
break;
#endif /* UART_3_EN */
}
return 0;
}
static int init_base(uart_t uart, uint32_t baudrate)
{
uint16_t clock_divider = CLOCK_CORECLOCK / (16 * baudrate);
switch (uart) {
#if UART_0_EN
case UART_0:
/* enable RX and TX */
UART0_RX_TX_EN;
/* use 8 Bit characters */
UART0_SET_8BIT_SIZE;
/* set clock divider */
UART0_BAUD_RATE_L = clock_divider;
UART0_BAUD_RATE_H = (clock_divider >> 8);
break;
#endif /* UART_0 */
#if UART_1_EN
case UART_1:
/* enable RX and TX */
UART1_RX_TX_EN;
/* use 8 Bit characters */
UART1_SET_8BIT_SIZE;
/* set clock divider */
UART1_BAUD_RATE_L = clock_divider;
UART1_BAUD_RATE_H = (clock_divider >> 8);
break;
#endif /* UART_1 */
#if UART_2_EN
case UART_2:
/* enable RX and TX */
UART2_RX_TX_EN;
/* use 8 Bit characters */
UART2_SET_8BIT_SIZE;
/* set clock divider */
UART2_BAUD_RATE_L = clock_divider;
UART2_BAUD_RATE_H = (clock_divider >> 8);
break;
#endif /* UART_2 */
#if UART_3_EN
case UART_3:
/* enable RX and TX */
UART3_RX_TX_EN;
/* use 8 Bit characters */
UART3_SET_8BIT_SIZE;
/* set clock divider */
UART3_BAUD_RATE_L = clock_divider;
UART3_BAUD_RATE_H = (clock_divider >> 8);
break;
#endif /* UART_3 */
default:
(void)clock_divider; /* this makes cppcheck happy */
}
return 0; return 0;
} }
void uart_write(uart_t uart, const uint8_t *data, size_t len) void uart_write(uart_t uart, const uint8_t *data, size_t len)
{ {
switch (uart) { for (size_t i = 0; i < len; i++) {
#if UART_0_EN while (!(dev[uart]->CSRA & (1 << UDRE0)));
case UART_0: dev[uart]->DR = data[i];
for (unsigned i = 0; i < len; i++) {
while (!UART0_DTREG_EMPTY);
UART0_DATA_REGISTER = data[i];
}
break;
#endif /* UART_0_EN */
#if UART_1_EN
case UART_1:
for (unsigned i = 0; i < len; i++) {
while (!UART1_DTREG_EMPTY);
UART1_DATA_REGISTER = data[i];
}
break;
#endif /* UART_1_EN */
#if UART_2_EN
case UART_2:
for (unsigned i = 0; i < len; i++) {
while (!UART2_DTREG_EMPTY);
UART2_DATA_REGISTER = data[i];
}
break;
#endif /* UART_2_EN */
#if UART_3_EN
case UART_3:
for (unsigned i = 0; i < len; i++) {
while (!UART3_DTREG_EMPTY);
UART3_DATA_REGISTER = data[i];
}
break;
#endif /* UART_3_EN */
} }
} }
static inline void isr_handler(int num)
#if UART_0_EN
ISR(USART0_RX_vect, ISR_BLOCK)
{ {
__enter_isr(); isr_ctx[num].rx_cb(isr_ctx[num].arg, dev[num]->DR);
config[UART_0].rx_cb(config[UART_0].arg, UART0_DATA_REGISTER);
if (sched_context_switch_request) { if (sched_context_switch_request) {
thread_yield(); thread_yield();
} }
__exit_isr();
} }
#endif /* UART_0_EN */
#if UART_1_EN #ifdef UART_0_ISR
ISR(USART1_RX_vect, ISR_BLOCK) ISR(UART_0_ISR, ISR_BLOCK)
{ {
__enter_isr(); __enter_isr();
config[UART_1].rx_cb(config[UART_1].arg, UART1_DATA_REGISTER); isr_handler(0);
if (sched_context_switch_request) {
thread_yield();
}
__exit_isr(); __exit_isr();
} }
#endif /* UART_1_EN */ #endif /* UART_0_ISR */
#if UART_2_EN #if UART_1_ISR
ISR(USART2_RX_vect, ISR_BLOCK) ISR(UART_1_ISR, ISR_BLOCK)
{ {
__enter_isr(); __enter_isr();
config[UART_2].rx_cb(config[UART_2].arg, UART2_DATA_REGISTER); isr_handler(1);
if (sched_context_switch_request) {
thread_yield();
}
__exit_isr(); __exit_isr();
} }
#endif /* UART_2_EN */ #endif /* UART_1_ISR */
#if UART_3_EN #if UART_2_ISR
ISR(USART3_RX_vect, ISR_BLOCK) ISR(UART_2_ISR, ISR_BLOCK)
{ {
__enter_isr(); __enter_isr();
config[UART_3].rx_cb(config[UART_3].arg, UART3_DATA_REGISTER); isr_handler(2);
if (sched_context_switch_request) {
thread_yield();
}
__exit_isr(); __exit_isr();
} }
#endif /* UART_3_EN */ #endif /* UART_2_ISR */
#if UART_3_ISR
ISR(UART_3_ISR, ISR_BLOCK)
{
__enter_isr();
isr_handler(3);
__exit_isr();
}
#endif /* UART_3_ISR */