cpu/saml21: adapted UART driver

This commit is contained in:
Hauke Petersen 2015-10-20 16:27:05 +02:00
parent a93f15b5cb
commit 65673663b5

View File

@ -21,53 +21,30 @@
* @}
*/
#include "board.h"
#include "cpu.h"
#include "periph/uart.h"
#include "periph_conf.h"
#include "sched.h"
#include "thread.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 Unified interrupt handler for all UART devices
*
* @param uartnum the number of the UART that triggered the ISR
* @param uart the UART device that triggered the ISR
*/
static inline void irq_handler(uart_t uartnum, SercomUsart *uart);
#include "periph/uart.h"
/**
* @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 uint64_t _long_division(uint64_t n, uint64_t d);
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, uart_tx_cb_t tx_cb, void *arg)
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, 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 */
@ -81,7 +58,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)
{
/* Calculate the BAUD value */
uint64_t temp1 = ((16 * ((uint64_t)baudrate)) << 32);
@ -134,54 +111,42 @@ int uart_init_blocking(uart_t uart, uint32_t baudrate)
break;
#endif
default:
(void)baud_calculated;
return -1;
}
uart_poweron(uart);
return 0;
}
void uart_tx_begin(uart_t uart)
void uart_write(uart_t uart, const uint8_t *data, size_t len)
{
}
void uart_tx_end(uart_t uart)
{
}
int uart_write(uart_t uart, char data)
{
switch (uart) {
case UART_0:
UART_0_DEV.DATA.reg = (uint8_t)data;
break;
}
return 1;
}
int uart_read_blocking(uart_t uart, char *data)
{
switch (uart) {
case UART_0:
while (UART_0_DEV.INTFLAG.bit.RXC == 0);
*data = (char)(0x00ff & UART_0_DEV.DATA.reg);
break;
}
return 1;
}
int uart_write_blocking(uart_t uart, char data)
{
switch (uart) {
case UART_0:
if (uart == UART_0) {
for (size_t i = 0; i < len; i++) {
while (UART_0_DEV.INTFLAG.bit.DRE == 0);
while(UART_0_DEV.SYNCBUSY.bit.ENABLE);
UART_0_DEV.DATA.reg = (uint8_t)data;
UART_0_DEV.DATA.reg = data[i];
while (UART_0_DEV.INTFLAG.reg & SERCOM_USART_INTFLAG_TXC);
break;
}
}
}
static inline void irq_handler(uint8_t uartnum, SercomUsart *dev)
{
if (dev->INTFLAG.bit.RXC) {
/* cleared by reading DATA regiser */
char data = (char)dev->DATA.reg;
uart_config[uartnum].rx_cb(uart_config[uartnum].arg, data);
}
else if (dev->INTFLAG.bit.ERROR) {
/* clear error flag */
dev->INTFLAG.bit.ERROR = 1;
}
if (sched_context_switch_request) {
thread_yield();
}
return 1;
}
void uart_poweron(uart_t uart)
@ -203,35 +168,6 @@ void UART_0_ISR(void)
}
#endif
static inline void irq_handler(uint8_t uartnum, SercomUsart *dev)
{
if (dev->INTFLAG.bit.RXC) {
/* cleared by reading DATA regiser */
char data = (char)dev->DATA.reg;
uart_config[uartnum].rx_cb(uart_config[uartnum].arg, data);
}
else if (dev->INTFLAG.bit.TXC) {
if (uart_config[uartnum].tx_cb(uart_config[uartnum].arg) == 0) {
/* TXC flag is also cleared by writing data to DATA register */
if (dev->INTFLAG.bit.TXC) {
/* cleared by writing 1 to TXC */
dev->INTFLAG.bit.TXC = 1;
}
}
}
else if (dev->INTFLAG.bit.ERROR) {
/* clear error flag */
dev->INTFLAG.bit.ERROR = 1;
}
if (sched_context_switch_request) {
thread_yield();
}
}
static uint64_t _long_division(uint64_t n, uint64_t d)
{
int32_t i;
@ -253,5 +189,3 @@ static uint64_t _long_division(uint64_t n, uint64_t d)
return q;
}
#endif /* UART_NUMOF */