modbus: Respect CONFIG_UART_USE_RUNTIME_CONFIGURE

Only perform runtime UART configuration if it is enabled.

Signed-off-by: Björn Stenberg <bjorn@haxx.se>
This commit is contained in:
Björn Stenberg 2024-02-08 15:47:32 +01:00 committed by Carles Cufí
parent 5a0ecb9641
commit 731f8d4736
2 changed files with 55 additions and 41 deletions

View file

@ -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.

View file

@ -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,47 +572,10 @@ 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:
if (IS_ENABLED(CONFIG_UART_USE_RUNTIME_CONFIGURE)) {
if (configure_uart(ctx, &param) != 0) {
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;
}
if (param.serial.baud <= 38400) {