Skip to content

Commit

Permalink
modbus: Respect CONFIG_UART_USE_RUNTIME_CONFIGURE
Browse files Browse the repository at this point in the history
Only perform runtime UART configuration if it is enabled.

Signed-off-by: Björn Stenberg <[email protected]>
  • Loading branch information
zagor authored and carlescufi committed Apr 10, 2024
1 parent 5a0ecb9 commit 731f8d4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 41 deletions.
2 changes: 1 addition & 1 deletion subsys/modbus/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
94 changes: 54 additions & 40 deletions subsys/modbus/modbus_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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, &param) != 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;
Expand Down

0 comments on commit 731f8d4

Please sign in to comment.