Skip to content

Commit

Permalink
drivers: serial: sam: usart: convert to DT macros
Browse files Browse the repository at this point in the history
Convert driver to use new device tree macros.

see zephyrproject-rtos#23107

Signed-off-by: Gerson Fernando Budke <[email protected]>
  • Loading branch information
nandojve committed Apr 12, 2020
1 parent 994f9f6 commit 1a426cf
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 209 deletions.
194 changes: 60 additions & 134 deletions drivers/serial/usart_sam.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*
* Copyright (c) 2018 Justin Watson
* Copyright (c) 2016 Piotr Mienkowski
* Copyright (c) 2020 Gerson Fernando Budke <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -12,41 +14,15 @@
* are currently implemented.
*/

#define DT_DRV_COMPAT atmel_sam_usart

#include <errno.h>
#include <sys/__assert.h>
#include <device.h>
#include <init.h>
#include <soc.h>
#include <drivers/uart.h>

/*
* Verify Kconfig configuration
*/

#if CONFIG_USART_SAM_PORT_0 == 1

#if DT_USART_SAM_PORT_0_BAUD_RATE == 0
#error "DT_USART_SAM_PORT_0_BAUD_RATE has to be bigger than 0"
#endif

#endif

#if CONFIG_USART_SAM_PORT_1 == 1

#if DT_USART_SAM_PORT_1_BAUD_RATE == 0
#error "DT_USART_SAM_PORT_1_BAUD_RATE has to be bigger than 0"
#endif

#endif

#if CONFIG_USART_SAM_PORT_2 == 1

#if DT_USART_SAM_PORT_2_BAUD_RATE == 0
#error "DT_USART_SAM_PORT_2_BAUD_RATE has to be bigger than 0"
#endif

#endif

/* Device constant configuration parameters */
struct usart_sam_dev_cfg {
Usart *regs;
Expand Down Expand Up @@ -349,125 +325,75 @@ static const struct uart_driver_api usart_sam_driver_api = {
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};

/* USART0 */

#ifdef CONFIG_USART_SAM_PORT_0

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* Forward declare function */
static void usart0_sam_irq_config_func(struct device *port);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

static const struct usart_sam_dev_cfg usart0_sam_config = {
.regs = USART0,
.periph_id = DT_USART_SAM_PORT_0_PERIPHERAL_ID,
.pin_rx = PIN_USART0_RXD,
.pin_tx = PIN_USART0_TXD,

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = usart0_sam_irq_config_func,
#endif
};
#define USART_SAM_IRQ_CALLBACK(n) \
.irq_config_func = usart##n##_sam_irq_config_func,

static struct usart_sam_dev_data usart0_sam_data = {
.baud_rate = DT_USART_SAM_PORT_0_BAUD_RATE,
};
#define USART_SAM_IRQ_CONNECT(n) \
do { \
IRQ_CONNECT(DT_IRQN(DT_NODELABEL(usart##n)), \
DT_IRQ(DT_NODELABEL(usart##n), priority), \
usart_sam_isr, DEVICE_GET(usart##n##_sam), 0); \
irq_enable(DT_IRQN(DT_NODELABEL(usart##n))); \
} while (0)

DEVICE_AND_API_INIT(usart0_sam, DT_USART_SAM_PORT_0_NAME, &usart_sam_init,
&usart0_sam_data, &usart0_sam_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &usart_sam_driver_api);
#define USART_SAM_IRQ_HANDLER_DECL(n) \
static void usart##n##_sam_irq_config_func(struct device *port);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void usart0_sam_irq_config_func(struct device *port)
{
IRQ_CONNECT(DT_USART_SAM_PORT_0_IRQ,
DT_USART_SAM_PORT_0_IRQ_PRIO,
usart_sam_isr,
DEVICE_GET(usart0_sam), 0);
irq_enable(DT_USART_SAM_PORT_0_IRQ);
#define USART_SAM_IRQ_HANDLER(n) \
static void usart##n##_sam_irq_config_func(struct device *port) \
{ \
USART_SAM_IRQ_CONNECT(n); \
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

#endif
#else

/* USART1 */
#define USART_SAM_IRQ_CALLBACK(n)
#define USART_SAM_IRQ_HANDLER_DECL(n)
#define USART_SAM_IRQ_HANDLER(n)

#ifdef CONFIG_USART_SAM_PORT_1

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* Forward declare function */
static void usart1_sam_irq_config_func(struct device *port);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

static const struct usart_sam_dev_cfg usart1_sam_config = {
.regs = USART1,
.periph_id = DT_USART_SAM_PORT_1_PERIPHERAL_ID,
.pin_rx = PIN_USART1_RXD,
.pin_tx = PIN_USART1_TXD,

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = usart1_sam_irq_config_func,
#define USART_PROP(n, p) \
UTIL_AND(DT_NODE_HAS_PROP(DT_NODELABEL(usart##n), p), \
DT_PROP(DT_NODELABEL(usart##n), p))

#define USART_SAM_DEVICE_INIT(n) \
BUILD_ASSERT(USART_PROP(n, current_speed), \
"usart"#n" current-speed property should exists and" \
" has to be bigger than 0."); \
BUILD_ASSERT(USART_PROP(n, peripheral_id), \
"usart"#n" peripheral_id property should exists."); \
\
USART_SAM_IRQ_HANDLER_DECL(n) \
static const struct usart_sam_dev_cfg usart##n##_sam_config = { \
.regs = (Usart *)DT_REG_ADDR(DT_NODELABEL(usart##n)), \
.periph_id = USART_PROP(n, peripheral_id), \
.pin_rx = PIN_USART##n##_RXD, \
.pin_tx = PIN_USART##n##_TXD, \
USART_SAM_IRQ_CALLBACK(n) \
}; \
\
static struct usart_sam_dev_data usart##n##_sam_data = { \
.baud_rate = USART_PROP(n, current_speed), \
}; \
\
DEVICE_AND_API_INIT(usart##n##_sam, DT_LABEL(DT_NODELABEL(usart##n)), \
&usart_sam_init, &usart##n##_sam_data, \
&usart##n##_sam_config, PRE_KERNEL_1, \
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&usart_sam_driver_api); \
USART_SAM_IRQ_HANDLER(n)

#if DT_HAS_NODE(DT_NODELABEL(usart0))
USART_SAM_DEVICE_INIT(0);
#endif
};

static struct usart_sam_dev_data usart1_sam_data = {
.baud_rate = DT_USART_SAM_PORT_1_BAUD_RATE,
};

DEVICE_AND_API_INIT(usart1_sam, DT_USART_SAM_PORT_1_NAME, &usart_sam_init,
&usart1_sam_data, &usart1_sam_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &usart_sam_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void usart1_sam_irq_config_func(struct device *port)
{
IRQ_CONNECT(DT_USART_SAM_PORT_1_IRQ,
DT_USART_SAM_PORT_1_IRQ_PRIO,
usart_sam_isr,
DEVICE_GET(usart1_sam), 0);
irq_enable(DT_USART_SAM_PORT_1_IRQ);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

#if DT_HAS_NODE(DT_NODELABEL(usart1))
USART_SAM_DEVICE_INIT(1);
#endif

/* USART2 */

#ifdef CONFIG_USART_SAM_PORT_2

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/* Forward declare function */
static void usart2_sam_irq_config_func(struct device *port);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

static const struct usart_sam_dev_cfg usart2_sam_config = {
.regs = USART2,
.periph_id = DT_USART_SAM_PORT_2_PERIPHERAL_ID,
.pin_rx = PIN_USART2_RXD,
.pin_tx = PIN_USART2_TXD,

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = usart2_sam_irq_config_func,
#endif
};

static struct usart_sam_dev_data usart2_sam_data = {
.baud_rate = DT_USART_SAM_PORT_2_BAUD_RATE,
};

DEVICE_AND_API_INIT(usart2_sam, DT_USART_SAM_PORT_2_NAME, &usart_sam_init,
&usart2_sam_data, &usart2_sam_config, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &usart_sam_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void usart2_sam_irq_config_func(struct device *port)
{
IRQ_CONNECT(DT_USART_SAM_PORT_2_IRQ,
DT_USART_SAM_PORT_2_IRQ_PRIO,
usart_sam_isr,
DEVICE_GET(usart2_sam), 0);
irq_enable(DT_USART_SAM_PORT_2_IRQ);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

#if DT_HAS_NODE(DT_NODELABEL(usart2))
USART_SAM_DEVICE_INIT(2);
#endif
12 changes: 0 additions & 12 deletions soc/arm/atmel_sam/sam3x/dts_fixup.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,4 @@
#define DT_I2C_1_IRQ_PRI DT_ATMEL_SAM_I2C_TWI_40090000_IRQ_0_PRIORITY
#define DT_I2C_1_PERIPHERAL_ID DT_ATMEL_SAM_I2C_TWI_40090000_PERIPHERAL_ID

#define DT_USART_SAM_PORT_0_NAME DT_ATMEL_SAM_USART_40098000_LABEL
#define DT_USART_SAM_PORT_0_BAUD_RATE DT_ATMEL_SAM_USART_40098000_CURRENT_SPEED
#define DT_USART_SAM_PORT_1_NAME DT_ATMEL_SAM_USART_4009C000_LABEL
#define DT_USART_SAM_PORT_1_BAUD_RATE DT_ATMEL_SAM_USART_4009C000_CURRENT_SPEED
#define DT_USART_SAM_PORT_2_NAME DT_ATMEL_SAM_USART_400A0000_LABEL
#define DT_USART_SAM_PORT_2_BAUD_RATE DT_ATMEL_SAM_USART_400A0000_CURRENT_SPEED
#define DT_USART_SAM_PORT_3_NAME DT_ATMEL_SAM_USART_400A4000_LABEL
#define DT_USART_SAM_PORT_3_BAUD_RATE DT_ATMEL_SAM_USART_400A4000_CURRENT_SPEED

#define DT_FLASH_DEV_BASE_ADDRESS DT_ATMEL_SAM_FLASH_CONTROLLER_400E0A00_BASE_ADDRESS
#define DT_FLASH_DEV_NAME DT_ATMEL_SAM_FLASH_CONTROLLER_400E0A00_LABEL

/* End of SoC Level DTS fixup file */
13 changes: 0 additions & 13 deletions soc/arm/atmel_sam/sam4e/dts_fixup.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,4 @@
#define DT_SPI_0_IRQ_PRI DT_ATMEL_SAM_SPI_40088000_IRQ_0_PRIORITY
#define DT_SPI_0_PERIPHERAL_ID DT_ATMEL_SAM_SPI_40088000_PERIPHERAL_ID

#define DT_USART_SAM_PORT_0_NAME DT_ATMEL_SAM_USART_400A0000_LABEL
#define DT_USART_SAM_PORT_0_BAUD_RATE DT_ATMEL_SAM_USART_400A0000_CURRENT_SPEED
#define DT_USART_SAM_PORT_0_IRQ DT_ATMEL_SAM_USART_400A0000_IRQ_0
#define DT_USART_SAM_PORT_0_IRQ_PRIO DT_ATMEL_SAM_USART_400A0000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_0_PERIPHERAL_ID DT_ATMEL_SAM_USART_400A0000_PERIPHERAL_ID
#define DT_USART_SAM_PORT_1_NAME DT_ATMEL_SAM_USART_400A4000_LABEL
#define DT_USART_SAM_PORT_1_BAUD_RATE DT_ATMEL_SAM_USART_400A4000_CURRENT_SPEED
#define DT_USART_SAM_PORT_1_IRQ DT_ATMEL_SAM_USART_400A4000_IRQ_0
#define DT_USART_SAM_PORT_1_IRQ_PRIO DT_ATMEL_SAM_USART_400A4000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_1_PERIPHERAL_ID DT_ATMEL_SAM_USART_400A4000_PERIPHERAL_ID

#define DT_FLASH_DEV_BASE_ADDRESS DT_ATMEL_SAM_FLASH_CONTROLLER_400E0A00_BASE_ADDRESS
#define DT_FLASH_DEV_NAME DT_ATMEL_SAM_FLASH_CONTROLLER_400E0A00_LABEL
/* End of SoC Level DTS fixup file */
18 changes: 0 additions & 18 deletions soc/arm/atmel_sam/sam4s/dts_fixup.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,4 @@
#define DT_SPI_0_IRQ_PRI DT_ATMEL_SAM_SPI_40008000_IRQ_0_PRIORITY
#define DT_SPI_0_PERIPHERAL_ID DT_ATMEL_SAM_SPI_40008000_PERIPHERAL_ID

#define DT_USART_SAM_PORT_0_NAME DT_ATMEL_SAM_USART_40024000_LABEL
#define DT_USART_SAM_PORT_0_BAUD_RATE DT_ATMEL_SAM_USART_40024000_CURRENT_SPEED
#define DT_USART_SAM_PORT_1_NAME DT_ATMEL_SAM_USART_40028000_LABEL
#define DT_USART_SAM_PORT_1_BAUD_RATE DT_ATMEL_SAM_USART_40028000_CURRENT_SPEED

#define DT_USART_SAM_PORT_0_NAME DT_ATMEL_SAM_USART_40024000_LABEL
#define DT_USART_SAM_PORT_0_BAUD_RATE DT_ATMEL_SAM_USART_40024000_CURRENT_SPEED
#define DT_USART_SAM_PORT_0_IRQ DT_ATMEL_SAM_USART_40024000_IRQ_0
#define DT_USART_SAM_PORT_0_IRQ_PRIO DT_ATMEL_SAM_USART_40024000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_0_PERIPHERAL_ID DT_ATMEL_SAM_USART_40024000_PERIPHERAL_ID
#define DT_USART_SAM_PORT_1_NAME DT_ATMEL_SAM_USART_40028000_LABEL
#define DT_USART_SAM_PORT_1_BAUD_RATE DT_ATMEL_SAM_USART_40028000_CURRENT_SPEED
#define DT_USART_SAM_PORT_1_IRQ DT_ATMEL_SAM_USART_40028000_IRQ_0
#define DT_USART_SAM_PORT_1_IRQ_PRIO DT_ATMEL_SAM_USART_40028000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_1_PERIPHERAL_ID DT_ATMEL_SAM_USART_40028000_PERIPHERAL_ID

#define DT_FLASH_DEV_BASE_ADDRESS DT_ATMEL_SAM_FLASH_CONTROLLER_400E0A00_BASE_ADDRESS
#define DT_FLASH_DEV_NAME DT_ATMEL_SAM_FLASH_CONTROLLER_400E0A00_LABEL
/* End of SoC Level DTS fixup file */
16 changes: 0 additions & 16 deletions soc/arm/atmel_sam/same70/dts_fixup.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,6 @@
#define DT_SPI_1_IRQ_PRI DT_ATMEL_SAM_SPI_40058000_IRQ_0_PRIORITY
#define DT_SPI_1_PERIPHERAL_ID DT_ATMEL_SAM_SPI_40058000_PERIPHERAL_ID

#define DT_USART_SAM_PORT_0_NAME DT_ATMEL_SAM_USART_40024000_LABEL
#define DT_USART_SAM_PORT_0_BAUD_RATE DT_ATMEL_SAM_USART_40024000_CURRENT_SPEED
#define DT_USART_SAM_PORT_0_IRQ DT_ATMEL_SAM_USART_40024000_IRQ_0
#define DT_USART_SAM_PORT_0_IRQ_PRIO DT_ATMEL_SAM_USART_40024000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_0_PERIPHERAL_ID DT_ATMEL_SAM_USART_40024000_PERIPHERAL_ID
#define DT_USART_SAM_PORT_1_NAME DT_ATMEL_SAM_USART_40028000_LABEL
#define DT_USART_SAM_PORT_1_BAUD_RATE DT_ATMEL_SAM_USART_40028000_CURRENT_SPEED
#define DT_USART_SAM_PORT_1_IRQ DT_ATMEL_SAM_USART_40028000_IRQ_0
#define DT_USART_SAM_PORT_1_IRQ_PRIO DT_ATMEL_SAM_USART_40028000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_1_PERIPHERAL_ID DT_ATMEL_SAM_USART_40028000_PERIPHERAL_ID
#define DT_USART_SAM_PORT_2_NAME DT_ATMEL_SAM_USART_4002C000_LABEL
#define DT_USART_SAM_PORT_2_BAUD_RATE DT_ATMEL_SAM_USART_4002C000_CURRENT_SPEED
#define DT_USART_SAM_PORT_2_IRQ DT_ATMEL_SAM_USART_4002C000_IRQ_0
#define DT_USART_SAM_PORT_2_IRQ_PRIO DT_ATMEL_SAM_USART_4002C000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_2_PERIPHERAL_ID DT_ATMEL_SAM_USART_4002C000_PERIPHERAL_ID

#define DT_ADC_0_BASE_ADDRESS DT_ATMEL_SAM_AFEC_4003C000_BASE_ADDRESS
#define DT_ADC_0_IRQ DT_ATMEL_SAM_AFEC_4003C000_IRQ_0
#define DT_ADC_0_IRQ_PRI DT_ATMEL_SAM_AFEC_4003C000_IRQ_0_PRIORITY
Expand Down
16 changes: 0 additions & 16 deletions soc/arm/atmel_sam/samv71/dts_fixup.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,6 @@
#define DT_SPI_1_IRQ_PRI DT_ATMEL_SAM_SPI_40058000_IRQ_0_PRIORITY
#define DT_SPI_1_PERIPHERAL_ID DT_ATMEL_SAM_SPI_40058000_PERIPHERAL_ID

#define DT_USART_SAM_PORT_0_NAME DT_ATMEL_SAM_USART_40024000_LABEL
#define DT_USART_SAM_PORT_0_BAUD_RATE DT_ATMEL_SAM_USART_40024000_CURRENT_SPEED
#define DT_USART_SAM_PORT_0_IRQ DT_ATMEL_SAM_USART_40024000_IRQ_0
#define DT_USART_SAM_PORT_0_IRQ_PRIO DT_ATMEL_SAM_USART_40024000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_0_PERIPHERAL_ID DT_ATMEL_SAM_USART_40024000_PERIPHERAL_ID
#define DT_USART_SAM_PORT_1_NAME DT_ATMEL_SAM_USART_40028000_LABEL
#define DT_USART_SAM_PORT_1_BAUD_RATE DT_ATMEL_SAM_USART_40028000_CURRENT_SPEED
#define DT_USART_SAM_PORT_1_IRQ DT_ATMEL_SAM_USART_40028000_IRQ_0
#define DT_USART_SAM_PORT_1_IRQ_PRIO DT_ATMEL_SAM_USART_40028000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_1_PERIPHERAL_ID DT_ATMEL_SAM_USART_40028000_PERIPHERAL_ID
#define DT_USART_SAM_PORT_2_NAME DT_ATMEL_SAM_USART_4002C000_LABEL
#define DT_USART_SAM_PORT_2_BAUD_RATE DT_ATMEL_SAM_USART_4002C000_CURRENT_SPEED
#define DT_USART_SAM_PORT_2_IRQ DT_ATMEL_SAM_USART_4002C000_IRQ_0
#define DT_USART_SAM_PORT_2_IRQ_PRIO DT_ATMEL_SAM_USART_4002C000_IRQ_0_PRIORITY
#define DT_USART_SAM_PORT_2_PERIPHERAL_ID DT_ATMEL_SAM_USART_4002C000_PERIPHERAL_ID

#define DT_ADC_0_BASE_ADDRESS DT_ATMEL_SAM_AFEC_4003C000_BASE_ADDRESS
#define DT_ADC_0_IRQ DT_ATMEL_SAM_AFEC_4003C000_IRQ_0
#define DT_ADC_0_IRQ_PRI DT_ATMEL_SAM_AFEC_4003C000_IRQ_0_PRIORITY
Expand Down

0 comments on commit 1a426cf

Please sign in to comment.