From 731f8d473610f04ec3d0cf53cb10dd373b467f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Stenberg?= Date: Thu, 8 Feb 2024 15:47:32 +0100 Subject: [PATCH] modbus: Respect CONFIG_UART_USE_RUNTIME_CONFIGURE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only perform runtime UART configuration if it is enabled. Signed-off-by: Björn Stenberg --- subsys/modbus/Kconfig | 2 +- subsys/modbus/modbus_serial.c | 94 ++++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 41 deletions(-) diff --git a/subsys/modbus/Kconfig b/subsys/modbus/Kconfig index 38c7d761c076eb..4c49d6c0985194 100644 --- a/subsys/modbus/Kconfig +++ b/subsys/modbus/Kconfig @@ -44,7 +44,7 @@ config MODBUS_SERIAL default y depends on SERIAL && SERIAL_HAS_DRIVER depends on DT_HAS_ZEPHYR_MODBUS_SERIAL_ENABLED - select UART_USE_RUNTIME_CONFIGURE + imply UART_USE_RUNTIME_CONFIGURE help Enable Modbus over serial line support. diff --git a/subsys/modbus/modbus_serial.c b/subsys/modbus/modbus_serial.c index a8b65291781658..13d59c8154a193 100644 --- a/subsys/modbus/modbus_serial.c +++ b/subsys/modbus/modbus_serial.c @@ -444,6 +444,58 @@ static int configure_gpio(struct modbus_context *ctx) return 0; } +static inline int configure_uart(struct modbus_context *ctx, + struct modbus_iface_param *param) +{ + struct modbus_serial_config *cfg = ctx->cfg; + struct uart_config uart_cfg = { + .baudrate = param->serial.baud, + .flow_ctrl = UART_CFG_FLOW_CTRL_NONE, + }; + + if (ctx->mode == MODBUS_MODE_ASCII) { + uart_cfg.data_bits = UART_CFG_DATA_BITS_7; + } else { + uart_cfg.data_bits = UART_CFG_DATA_BITS_8; + } + + switch (param->serial.parity) { + case UART_CFG_PARITY_ODD: + case UART_CFG_PARITY_EVEN: + uart_cfg.parity = param->serial.parity; + uart_cfg.stop_bits = UART_CFG_STOP_BITS_1; + break; + case UART_CFG_PARITY_NONE: + /* Use of no parity requires 2 stop bits */ + uart_cfg.parity = param->serial.parity; + uart_cfg.stop_bits = UART_CFG_STOP_BITS_2; + break; + default: + return -EINVAL; + } + + if (ctx->client) { + /* Allow custom stop bit settings only in client mode */ + switch (param->serial.stop_bits_client) { + case UART_CFG_STOP_BITS_0_5: + case UART_CFG_STOP_BITS_1: + case UART_CFG_STOP_BITS_1_5: + case UART_CFG_STOP_BITS_2: + uart_cfg.stop_bits = param->serial.stop_bits_client; + break; + default: + return -EINVAL; + } + } + + if (uart_configure(cfg->dev, &uart_cfg) != 0) { + LOG_ERR("Failed to configure UART"); + return -EINVAL; + } + + return 0; +} + void modbus_serial_rx_disable(struct modbus_context *ctx) { modbus_serial_rx_off(ctx); @@ -505,7 +557,6 @@ int modbus_serial_init(struct modbus_context *ctx, struct modbus_serial_config *cfg = ctx->cfg; const uint32_t if_delay_max = 3500000; const uint32_t numof_bits = 11; - struct uart_config uart_cfg; switch (param.mode) { case MODBUS_MODE_RTU: @@ -521,49 +572,12 @@ int modbus_serial_init(struct modbus_context *ctx, return -ENODEV; } - uart_cfg.baudrate = param.serial.baud, - uart_cfg.flow_ctrl = UART_CFG_FLOW_CTRL_NONE; - - if (ctx->mode == MODBUS_MODE_ASCII) { - uart_cfg.data_bits = UART_CFG_DATA_BITS_7; - } else { - uart_cfg.data_bits = UART_CFG_DATA_BITS_8; - } - - switch (param.serial.parity) { - case UART_CFG_PARITY_ODD: - case UART_CFG_PARITY_EVEN: - uart_cfg.parity = param.serial.parity; - uart_cfg.stop_bits = UART_CFG_STOP_BITS_1; - break; - case UART_CFG_PARITY_NONE: - /* Use of no parity requires 2 stop bits */ - uart_cfg.parity = param.serial.parity; - uart_cfg.stop_bits = UART_CFG_STOP_BITS_2; - break; - default: - return -EINVAL; - } - - if (ctx->client) { - /* Allow custom stop bit settings only in client mode */ - switch (param.serial.stop_bits_client) { - case UART_CFG_STOP_BITS_0_5: - case UART_CFG_STOP_BITS_1: - case UART_CFG_STOP_BITS_1_5: - case UART_CFG_STOP_BITS_2: - uart_cfg.stop_bits = param.serial.stop_bits_client; - break; - default: + if (IS_ENABLED(CONFIG_UART_USE_RUNTIME_CONFIGURE)) { + if (configure_uart(ctx, ¶m) != 0) { return -EINVAL; } } - if (uart_configure(cfg->dev, &uart_cfg) != 0) { - LOG_ERR("Failed to configure UART"); - return -EINVAL; - } - if (param.serial.baud <= 38400) { cfg->rtu_timeout = (numof_bits * if_delay_max) / param.serial.baud;