serial: sam: add support for interrupt driven serial

Implements interrupt driven UART for the serial driver.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
Signed-off-by: Justin Watson <jwatson5@gmail.com>
This commit is contained in:
Justin Watson 2018-02-19 21:58:07 -08:00 committed by Kumar Gala
commit c1ce290d45
8 changed files with 323 additions and 12 deletions

View file

@ -21,6 +21,8 @@
#define CONFIG_UART_SAM_PORT_0_NAME ATMEL_SAM_UART_400E0800_LABEL
#define CONFIG_UART_SAM_PORT_0_BAUD_RATE ATMEL_SAM_UART_400E0800_CURRENT_SPEED
#define CONFIG_UART_SAM_PORT_0_IRQ ATMEL_SAM_UART_400E0800_IRQ_0
#define CONFIG_UART_SAM_PORT_0_IRQ_PRIO ATMEL_SAM_UART_400E0800_IRQ_0_PRIORITY
#define CONFIG_USART_SAM_PORT_0_NAME ATMEL_SAM_USART_40098000_LABEL
#define CONFIG_USART_SAM_PORT_0_BAUD_RATE ATMEL_SAM_USART_40098000_CURRENT_SPEED

View file

@ -22,8 +22,12 @@
#define CONFIG_UART_SAM_PORT_0_NAME ATMEL_SAM_UART_400E0600_LABEL
#define CONFIG_UART_SAM_PORT_0_BAUD_RATE ATMEL_SAM_UART_400E0600_CURRENT_SPEED
#define CONFIG_UART_SAM_PORT_0_IRQ ATMEL_SAM_UART_400E0600_IRQ_0
#define CONFIG_UART_SAM_PORT_0_IRQ_PRIO ATMEL_SAM_UART_400E0600_IRQ_0_PRIORITY
#define CONFIG_UART_SAM_PORT_1_NAME ATMEL_SAM_UART_400E0800_LABEL
#define CONFIG_UART_SAM_PORT_1_BAUD_RATE ATMEL_SAM_UART_400E0800_CURRENT_SPEED
#define CONFIG_UART_SAM_PORT_1_IRQ ATMEL_SAM_UART_400E0800_IRQ_0
#define CONFIG_UART_SAM_PORT_1_IRQ_PRIO ATMEL_SAM_UART_400E0800_IRQ_0_PRIORITY
#define CONFIG_USART_SAM_PORT_0_NAME ATMEL_SAM_USART_40024000_LABEL
#define CONFIG_USART_SAM_PORT_0_BAUD_RATE ATMEL_SAM_USART_40024000_CURRENT_SPEED
#define CONFIG_USART_SAM_PORT_1_NAME ATMEL_SAM_USART_40028000_LABEL

View file

@ -29,14 +29,24 @@
#define CONFIG_UART_SAM_PORT_0_NAME ATMEL_SAM_UART_400E0800_LABEL
#define CONFIG_UART_SAM_PORT_0_BAUD_RATE ATMEL_SAM_UART_400E0800_CURRENT_SPEED
#define CONFIG_UART_SAM_PORT_0_IRQ ATMEL_SAM_UART_400E0800_IRQ_0
#define CONFIG_UART_SAM_PORT_0_IRQ_PRIO ATMEL_SAM_UART_400E0800_IRQ_0_PRIORITY
#define CONFIG_UART_SAM_PORT_1_NAME ATMEL_SAM_UART_400E0A00_LABEL
#define CONFIG_UART_SAM_PORT_1_BAUD_RATE ATMEL_SAM_UART_400E0A00_CURRENT_SPEED
#define CONFIG_UART_SAM_PORT_1_IRQ ATMEL_SAM_UART_400E0A00_IRQ_0
#define CONFIG_UART_SAM_PORT_1_IRQ_PRIO ATMEL_SAM_UART_400E0A00_IRQ_0_PRIORITY
#define CONFIG_UART_SAM_PORT_2_NAME ATMEL_SAM_UART_400E1A00 _LABEL
#define CONFIG_UART_SAM_PORT_2_BAUD_RATE ATMEL_SAM_UART_400E1A00_CURRENT_SPEED
#define CONFIG_UART_SAM_PORT_2_IRQ ATMEL_SAM_UART_400E1A00_IRQ_0
#define CONFIG_UART_SAM_PORT_2_IRQ_PRIO ATMEL_SAM_UART_400E1A00_IRQ_0_PRIORITY
#define CONFIG_UART_SAM_PORT_3_NAME ATMEL_SAM_UART_400E1C00_LABEL
#define CONFIG_UART_SAM_PORT_3_BAUD_RATE ATMEL_SAM_UART_400E1C00_CURRENT_SPEED
#define CONFIG_UART_SAM_PORT_3_IRQ ATMEL_SAM_UART_400E1C00_IRQ_0
#define CONFIG_UART_SAM_PORT_3_IRQ_PRIO ATMEL_SAM_UART_400E1C00_IRQ_0_PRIORITY
#define CONFIG_UART_SAM_PORT_4_NAME ATMEL_SAM_UART_400E1E00_LABEL
#define CONFIG_UART_SAM_PORT_4_BAUD_RATE ATMEL_SAM_UART_400E1E00_CURRENT_SPEED
#define CONFIG_UART_SAM_PORT_4_IRQ ATMEL_SAM_UART_400E1E00_IRQ_0
#define CONFIG_UART_SAM_PORT_4_IRQ_PRIO ATMEL_SAM_UART_400E1E00_IRQ_0_PRIORITY
#define CONFIG_USART_SAM_PORT_0_NAME ATMEL_SAM_USART_40024000_LABEL
#define CONFIG_USART_SAM_PORT_0_BAUD_RATE ATMEL_SAM_USART_40024000_CURRENT_SPEED
#define CONFIG_USART_SAM_PORT_1_NAME ATMEL_SAM_USART_40028000_LABEL

View file

@ -1,6 +1,7 @@
# Kconfig - Atmel SAM UART configuration options
#
# Copyright (c) 2017 Piotr Mienkowski
# Copyright (c) 2018 Justin Watson
# SPDX-License-Identifier: Apache-2.0
#
@ -8,6 +9,7 @@ menuconfig UART_SAM
bool "Atmel SAM MCU family UART driver"
depends on SOC_FAMILY_SAM
select SERIAL_HAS_DRIVER
select SERIAL_SUPPORT_INTERRUPT
default n
help
This option enables the UARTx driver for Atmel SAM MCUs.

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2017 Piotr Mienkowski
* Copyright (c) 2018 Justin Watson
* SPDX-License-Identifier: Apache-2.0
*/
@ -68,11 +69,19 @@ struct uart_sam_dev_cfg {
u32_t periph_id;
struct soc_gpio_pin pin_rx;
struct soc_gpio_pin pin_tx;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_config_func_t irq_config_func;
#endif
};
/* Device run time data */
struct uart_sam_dev_data {
u32_t baud_rate;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_callback_t irq_cb; /* Interrupt Callback */
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};
#define DEV_CFG(dev) \
@ -122,6 +131,10 @@ static int uart_sam_init(struct device *dev)
/* Enable receiver and transmitter */
uart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
cfg->irq_config_func(dev);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
return 0;
}
@ -152,6 +165,26 @@ static unsigned char uart_sam_poll_out(struct device *dev, unsigned char c)
return c;
}
static int uart_sam_err_check(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
int errors = 0;
if (uart->UART_SR & UART_SR_OVRE) {
errors |= UART_ERROR_OVERRUN;
}
if (uart->UART_SR & UART_SR_PARE) {
errors |= UART_ERROR_PARITY;
}
if (uart->UART_SR & UART_SR_FRAME) {
errors |= UART_ERROR_FRAMING;
}
return errors;
}
static int baudrate_set(Uart *const uart, u32_t baudrate,
u32_t mck_freq_hz)
{
@ -173,19 +206,180 @@ static int baudrate_set(Uart *const uart, u32_t baudrate,
return 0;
}
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static int uart_sam_fifo_fill(struct device *dev, const uint8_t *tx_data,
int size)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
/* Wait for transmitter to be ready. */
while ((uart->UART_SR & UART_SR_TXRDY) == 0)
;
uart->UART_THR = *tx_data;
return 1;
}
static int uart_sam_fifo_read(struct device *dev, uint8_t *rx_data,
const int size)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
int bytes_read;
bytes_read = 0;
while (bytes_read < size) {
if (uart->UART_SR & UART_SR_RXRDY) {
rx_data[bytes_read] = uart->UART_RHR;
bytes_read++;
} else {
break;
}
}
return bytes_read;
}
static void uart_sam_irq_tx_enable(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
uart->UART_IER = UART_IER_TXRDY;
}
static void uart_sam_irq_tx_disable(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
uart->UART_IDR = UART_IDR_TXRDY;
}
static int uart_sam_irq_tx_ready(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
return (uart->UART_SR & UART_SR_TXRDY);
}
static void uart_sam_irq_rx_enable(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
uart->UART_IER = UART_IER_RXRDY;
}
static void uart_sam_irq_rx_disable(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
uart->UART_IDR = UART_IDR_RXRDY;
}
static int uart_sam_irq_tx_complete(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
return !(uart->UART_SR & UART_SR_TXRDY);
}
static int uart_sam_irq_rx_ready(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
return (uart->UART_SR & UART_SR_RXRDY);
}
static void uart_sam_irq_err_enable(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
uart->UART_IER = UART_IER_OVRE | UART_IER_FRAME | UART_IER_PARE;
}
static void uart_sam_irq_err_disable(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
uart->UART_IDR = UART_IDR_OVRE | UART_IDR_FRAME | UART_IDR_PARE;
}
static int uart_sam_irq_is_pending(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
return ((uart->UART_SR & UART_SR_TXRDY)
| (uart->UART_SR & UART_SR_RXRDY));
}
static int uart_sam_irq_update(struct device *dev)
{
volatile Uart * const uart = DEV_CFG(dev)->regs;
return (uart->UART_SR & UART_SR_TXEMPTY);
}
static void uart_sam_irq_callback_set(struct device *dev,
uart_irq_callback_t cb)
{
struct uart_sam_dev_data *const dev_data = DEV_DATA(dev);
dev_data->irq_cb = cb;
}
static void uart_sam_isr(void *arg)
{
struct device *dev = arg;
struct uart_sam_dev_data *const dev_data = DEV_DATA(dev);
if (dev_data->irq_cb) {
dev_data->irq_cb(dev);
}
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const struct uart_driver_api uart_sam_driver_api = {
.poll_in = uart_sam_poll_in,
.poll_out = uart_sam_poll_out,
.err_check = uart_sam_err_check,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.fifo_fill = uart_sam_fifo_fill,
.fifo_read = uart_sam_fifo_read,
.irq_tx_enable = uart_sam_irq_tx_enable,
.irq_tx_disable = uart_sam_irq_tx_disable,
.irq_tx_ready = uart_sam_irq_tx_ready,
.irq_rx_enable = uart_sam_irq_rx_enable,
.irq_rx_disable = uart_sam_irq_rx_disable,
.irq_tx_complete = uart_sam_irq_tx_complete,
.irq_rx_ready = uart_sam_irq_rx_ready,
.irq_err_enable = uart_sam_irq_err_enable,
.irq_err_disable = uart_sam_irq_err_disable,
.irq_is_pending = uart_sam_irq_is_pending,
.irq_update = uart_sam_irq_update,
.irq_callback_set = uart_sam_irq_callback_set,
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};
/* UART0 */
#ifdef CONFIG_UART_SAM_PORT_0
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* Forward declare function */
static void uart0_sam_irq_config_func(struct device *port);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const struct uart_sam_dev_cfg uart0_sam_config = {
.regs = UART0,
.periph_id = ID_UART0,
.pin_rx = PIN_UART0_RXD,
.pin_tx = PIN_UART0_TXD,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = uart0_sam_irq_config_func,
#endif
};
static struct uart_sam_dev_data uart0_sam_data = {
@ -195,16 +389,37 @@ static struct uart_sam_dev_data uart0_sam_data = {
DEVICE_AND_API_INIT(uart0_sam, CONFIG_UART_SAM_PORT_0_NAME, &uart_sam_init,
&uart0_sam_data, &uart0_sam_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &uart_sam_driver_api);
#endif
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void uart0_sam_irq_config_func(struct device *port)
{
IRQ_CONNECT(CONFIG_UART_SAM_PORT_0_IRQ,
CONFIG_UART_SAM_PORT_0_IRQ_PRIO,
uart_sam_isr,
DEVICE_GET(uart0_sam), 0);
irq_enable(CONFIG_UART_SAM_PORT_0_IRQ);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#endif /* CONFIG_UART_SAM_PORT_0 */
/* UART1 */
#ifdef CONFIG_UART_SAM_PORT_1
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* Forward declare function */
static void uart1_sam_irq_config_func(struct device *port);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const struct uart_sam_dev_cfg uart1_sam_config = {
.regs = UART1,
.periph_id = ID_UART1,
.pin_rx = PIN_UART1_RXD,
.pin_tx = PIN_UART1_TXD,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = uart1_sam_irq_config_func,
#endif
};
static struct uart_sam_dev_data uart1_sam_data = {
@ -214,16 +429,38 @@ static struct uart_sam_dev_data uart1_sam_data = {
DEVICE_AND_API_INIT(uart1_sam, CONFIG_UART_SAM_PORT_1_NAME, &uart_sam_init,
&uart1_sam_data, &uart1_sam_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &uart_sam_driver_api);
#endif
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void uart1_sam_irq_config_func(struct device *port)
{
IRQ_CONNECT(CONFIG_UART_SAM_PORT_1_IRQ,
CONFIG_UART_SAM_PORT_1_IRQ_PRIO,
uart_sam_isr,
DEVICE_GET(uart1_sam), 0);
irq_enable(CONFIG_UART_SAM_PORT_1_IRQ);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#endif /* CONFIG_UART_SAM_PORT_1 */
/* UART2 */
#ifdef CONFIG_UART_SAM_PORT_2
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* Forward declare function */
static void uart2_sam_irq_config_func(struct device *port);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const struct uart_sam_dev_cfg uart2_sam_config = {
.regs = UART2,
.periph_id = ID_UART2,
.pin_rx = PIN_UART2_RXD,
.pin_tx = PIN_UART2_TXD,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = uart2_sam_irq_config_func,
#endif
};
static struct uart_sam_dev_data uart2_sam_data = {
@ -233,16 +470,38 @@ static struct uart_sam_dev_data uart2_sam_data = {
DEVICE_AND_API_INIT(uart2_sam, CONFIG_UART_SAM_PORT_2_NAME, &uart_sam_init,
&uart2_sam_data, &uart2_sam_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &uart_sam_driver_api);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void uart2_sam_irq_config_func(struct device *port)
{
IRQ_CONNECT(CONFIG_UART_SAM_PORT_2_IRQ,
CONFIG_UART_SAM_PORT_2_IRQ_PRIO,
uart_sam_isr,
DEVICE_GET(uart2_sam), 0);
irq_enable(CONFIG_UART_SAM_PORT_2_IRQ);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#endif
/* UART3 */
#ifdef CONFIG_UART_SAM_PORT_3
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* Forward declare function */
static void uart3_sam_irq_config_func(struct device *port);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const struct uart_sam_dev_cfg uart3_sam_config = {
.regs = UART3,
.periph_id = ID_UART3,
.pin_rx = PIN_UART3_RXD,
.pin_tx = PIN_UART3_TXD,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = uart3_sam_irq_config_func,
#endif
};
static struct uart_sam_dev_data uart3_sam_data = {
@ -252,16 +511,38 @@ static struct uart_sam_dev_data uart3_sam_data = {
DEVICE_AND_API_INIT(uart3_sam, CONFIG_UART_SAM_PORT_3_NAME, &uart_sam_init,
&uart3_sam_data, &uart3_sam_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &uart_sam_driver_api);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void uart3_sam_irq_config_func(struct device *port)
{
IRQ_CONNECT(CONFIG_UART_SAM_PORT_3_IRQ,
CONFIG_UART_SAM_PORT_3_IRQ_PRIO,
uart_sam_isr,
DEVICE_GET(uart3_sam), 0);
irq_enable(CONFIG_UART_SAM_PORT_3_IRQ);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#endif
/* UART4 */
#ifdef CONFIG_UART_SAM_PORT_4
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* Forward declare function */
static void uart4_sam_irq_config_func(struct device *port);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const struct uart_sam_dev_cfg uart4_sam_config = {
.regs = UART4,
.periph_id = ID_UART4,
.pin_rx = PIN_UART4_RXD,
.pin_tx = PIN_UART4_TXD,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = uart4_sam_irq_config_func,
#endif
};
static struct uart_sam_dev_data uart4_sam_data = {
@ -271,4 +552,16 @@ static struct uart_sam_dev_data uart4_sam_data = {
DEVICE_AND_API_INIT(uart4_sam, CONFIG_UART_SAM_PORT_4_NAME, &uart_sam_init,
&uart4_sam_data, &uart4_sam_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &uart_sam_driver_api);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void uart4_sam_irq_config_func(struct device *port)
{
IRQ_CONNECT(CONFIG_UART_SAM_PORT_4_IRQ,
CONFIG_UART_SAM_PORT_4_IRQ_PRIO,
uart_sam_isr,
DEVICE_GET(uart4_sam), 0);
irq_enable(CONFIG_UART_SAM_PORT_4_IRQ);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#endif

View file

@ -55,7 +55,7 @@
uart0: uart@400E0800 {
compatible = "atmel,sam-uart";
reg = <0x400E0800 0x124>;
interrupts = <8 0>;
interrupts = <8 1>;
peripheral-id = <8>;
status = "disabled";
label = "UART_0";

View file

@ -55,7 +55,7 @@
uart0: uart@400E0600 {
compatible = "atmel,sam-uart";
reg = <0x400E0600 0x200>;
interrupts = <8 0>;
interrupts = <8 1>;
peripheral-id = <8>;
status = "disabled";
label = "UART_0";
@ -64,7 +64,7 @@
uart1: uart@400E0800 {
compatible = "atmel,sam-uart";
reg = <0x400E0800 0x200>;
interrupts = <9 0>;
interrupts = <9 1>;
peripheral-id = <9>;
status = "disabled";
label = "UART_1";
@ -73,7 +73,7 @@
usart0: usart@40024000 {
compatible = "atmel,sam-usart";
reg = <0x40024000 0x130>;
interrupts = <14 0>;
interrupts = <14 1>;
peripheral-id = <14>;
status = "disabled";
label = "USART_0";
@ -82,7 +82,7 @@
usart1: usart@40028000 {
compatible = "atmel,sam-usart";
reg = <0x40028000 0x130>;
interrupts = <15 0>;
interrupts = <15 1>;
peripheral-id = <15>;
status = "disabled";
label = "USART_1";

View file

@ -73,7 +73,7 @@
uart0: uart@400E0800 {
compatible = "atmel,sam-uart";
reg = <0x400E0800 0x100>;
interrupts = <7 0>;
interrupts = <7 1>;
peripheral-id = <7>;
status = "disabled";
label = "UART_0";
@ -82,7 +82,7 @@
uart1: uart@400E0A00 {
compatible = "atmel,sam-uart";
reg = <0x400E0A00 0x100>;
interrupts = <8 0>;
interrupts = <8 1>;
peripheral-id = <8>;
status = "disabled";
label = "UART_1";
@ -91,7 +91,7 @@
uart2: uart@400E1A00 {
compatible = "atmel,sam-uart";
reg = <0x400E1A00 0x100>;
interrupts = <44 0>;
interrupts = <44 1>;
peripheral-id = <44>;
status = "disabled";
label = "UART_2";
@ -100,7 +100,7 @@
uart3: uart@400E1C00 {
compatible = "atmel,sam-uart";
reg = <0x400E1C00 0x100>;
interrupts = <45 0>;
interrupts = <45 1>;
peripheral-id = <45>;
status = "disabled";
label = "UART_3";
@ -109,7 +109,7 @@
uart4: uart@400E1E00 {
compatible = "atmel,sam-uart";
reg = <0x400E1E00 0x100>;
interrupts = <46 0>;
interrupts = <46 1>;
peripheral-id = <46>;
status = "disabled";
label = "UART_4";