cpu/samd21: adapted UART driver
This commit is contained in:
parent
07a7d93690
commit
a93f15b5cb
@ -27,15 +27,6 @@
|
|||||||
#include "periph/uart.h"
|
#include "periph/uart.h"
|
||||||
#include "periph/gpio.h"
|
#include "periph/gpio.h"
|
||||||
|
|
||||||
/**
|
|
||||||
* @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_isr_ctx_t;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Allocate memory to store the callback functions
|
* @brief Allocate memory to store the callback functions
|
||||||
*/
|
*/
|
||||||
@ -53,18 +44,18 @@ static inline SercomUsart *_uart(uart_t dev)
|
|||||||
return uart_config[dev].dev;
|
return uart_config[dev].dev;
|
||||||
}
|
}
|
||||||
|
|
||||||
int uart_init(uart_t uart, uint32_t baudrate,
|
static int init_base(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 */
|
/* initialize basic functionality */
|
||||||
int res = uart_init_blocking(uart, baudrate);
|
int res = init_base(uart, baudrate);
|
||||||
if (res != 0) {
|
if (res != 0) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* register callbacks */
|
/* register callbacks */
|
||||||
uart_ctx[uart].rx_cb = rx_cb;
|
uart_ctx[uart].rx_cb = rx_cb;
|
||||||
uart_ctx[uart].tx_cb = tx_cb;
|
|
||||||
uart_ctx[uart].arg = arg;
|
uart_ctx[uart].arg = arg;
|
||||||
/* configure interrupts and enable RX interrupt */
|
/* configure interrupts and enable RX interrupt */
|
||||||
_uart(uart)->INTENSET.reg = SERCOM_USART_INTENSET_RXC;
|
_uart(uart)->INTENSET.reg = SERCOM_USART_INTENSET_RXC;
|
||||||
@ -72,7 +63,7 @@ int uart_init(uart_t uart, uint32_t baudrate,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int uart_init_blocking(uart_t uart, uint32_t baudrate)
|
static int init_base(uart_t uart, uint32_t baudrate)
|
||||||
{
|
{
|
||||||
uint32_t baud;
|
uint32_t baud;
|
||||||
SercomUsart *dev;
|
SercomUsart *dev;
|
||||||
@ -112,29 +103,12 @@ int uart_init_blocking(uart_t uart, uint32_t baudrate)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_tx_begin(uart_t uart)
|
void uart_write(uart_t uart, const uint8_t *data, size_t len)
|
||||||
{
|
|
||||||
_uart(uart)->INTENSET.reg = SERCOM_USART_INTENSET_TXC;
|
|
||||||
}
|
|
||||||
|
|
||||||
int uart_read_blocking(uart_t uart, char *data)
|
|
||||||
{
|
|
||||||
while(!(_uart(uart)->INTFLAG.reg & SERCOM_USART_INTFLAG_RXC));
|
|
||||||
*data = (char)_uart(uart)->DATA.reg;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int uart_write(uart_t uart, char data)
|
|
||||||
{
|
|
||||||
_uart(uart)->DATA.reg = (uint8_t)data;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int uart_write_blocking(uart_t uart, char data)
|
|
||||||
{
|
{
|
||||||
|
for (size_t i = 0; i < len; i++) {
|
||||||
while (!(_uart(uart)->INTFLAG.reg & SERCOM_USART_INTFLAG_DRE));
|
while (!(_uart(uart)->INTFLAG.reg & SERCOM_USART_INTFLAG_DRE));
|
||||||
_uart(uart)->DATA.reg = (uint8_t)data;
|
_uart(uart)->DATA.reg = data[i];
|
||||||
return 1;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_poweron(uart_t uart)
|
void uart_poweron(uart_t uart)
|
||||||
@ -163,11 +137,6 @@ static inline void irq_handler(int dev)
|
|||||||
/* interrupt flag is cleared by reading the data register */
|
/* interrupt flag is cleared by reading the data register */
|
||||||
uart_ctx[dev].rx_cb(uart_ctx[dev].arg, (char)(uart->DATA.reg));
|
uart_ctx[dev].rx_cb(uart_ctx[dev].arg, (char)(uart->DATA.reg));
|
||||||
}
|
}
|
||||||
else if (uart->INTFLAG.reg & SERCOM_USART_INTFLAG_TXC) {
|
|
||||||
if (uart_ctx[dev].tx_cb(uart_ctx[dev].arg) == 0) {
|
|
||||||
uart->INTENCLR.reg = SERCOM_USART_INTENCLR_TXC;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (uart->INTFLAG.reg & SERCOM_USART_INTFLAG_ERROR) {
|
else if (uart->INTFLAG.reg & SERCOM_USART_INTFLAG_ERROR) {
|
||||||
/* clear error flag */
|
/* clear error flag */
|
||||||
uart->INTFLAG.reg = SERCOM_USART_INTFLAG_ERROR;
|
uart->INTFLAG.reg = SERCOM_USART_INTFLAG_ERROR;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user