From 4fa0a9b4b2e30c4a705c3e9049678d3441bdf7a1 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 21 Dec 2020 15:32:45 +0200 Subject: [PATCH] drivers: serial: ns16550: Remove hard-coded max instance count Instead of having a hard-coded maximum instance count, introduce a Kconfig variable for it. The inclusion of the per-instance header files is solved by having them chain-include each other with a pre-processor condition that checks if the current header file is the last one or not. Signed-off-by: Johan Hedberg --- drivers/serial/CMakeLists.txt | 4 +++- drivers/serial/Kconfig.ns16550 | 7 +++++++ drivers/serial/uart_ns16550.c | 7 ++++--- drivers/serial/uart_ns16550_port_x.h | 5 +++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/drivers/serial/CMakeLists.txt b/drivers/serial/CMakeLists.txt index 14dceab37e2..21ffa7836dc 100644 --- a/drivers/serial/CMakeLists.txt +++ b/drivers/serial/CMakeLists.txt @@ -42,7 +42,9 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE uart_handlers.c) if(CONFIG_UART_NS16550) zephyr_library_sources(uart_ns16550.c) - foreach(NUM RANGE 0 3) + math(EXPR max_index "${CONFIG_UART_NS16550_MAX_INSTANCES} - 1") + foreach(NUM RANGE 0 ${max_index}) + math(EXPR NEXT_NUM "${NUM} + 1") configure_file( uart_ns16550_port_x.h ${PROJECT_BINARY_DIR}/include/generated/uart_ns16550_port_${NUM}.h diff --git a/drivers/serial/Kconfig.ns16550 b/drivers/serial/Kconfig.ns16550 index f00356c60f2..1f0e004cc88 100644 --- a/drivers/serial/Kconfig.ns16550 +++ b/drivers/serial/Kconfig.ns16550 @@ -11,6 +11,13 @@ menuconfig UART_NS16550 if UART_NS16550 +config UART_NS16550_MAX_INSTANCES + int "Maximum number of supported driver instances" + range 1 32 + default 4 + help + The maximum number of supported driver instances in device tree. + config UART_NS16550_LINE_CTRL bool "Enable Serial Line Control for Apps" depends on UART_LINE_CTRL diff --git a/drivers/serial/uart_ns16550.c b/drivers/serial/uart_ns16550.c index 6b599192de5..19b4dd64300 100644 --- a/drivers/serial/uart_ns16550.c +++ b/drivers/serial/uart_ns16550.c @@ -976,7 +976,8 @@ static const struct uart_driver_api uart_ns16550_driver_api = { #endif }; +/* The instance-specific header files are chained together (each instance + * includes the next one, unless it's the last instance) so we only need to + * include the first instance. + */ #include -#include -#include -#include diff --git a/drivers/serial/uart_ns16550_port_x.h b/drivers/serial/uart_ns16550_port_x.h index dfc09dd168d..89d7a3c6c91 100644 --- a/drivers/serial/uart_ns16550_port_x.h +++ b/drivers/serial/uart_ns16550_port_x.h @@ -130,3 +130,8 @@ static void irq_config_func_@NUM@(const struct device *dev) #endif #endif + +/* Include subsequent instances */ +#if @NUM@ < (CONFIG_UART_NS16550_MAX_INSTANCES - 1) +#include +#endif