modbus: use enum for MODBUS mode

Use enum for MODBUS mode.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
This commit is contained in:
Johann Fischer 2021-03-01 14:36:35 +01:00 committed by Carles Cufí
parent 5319c7b042
commit 19d0b474c1
2 changed files with 33 additions and 14 deletions

View file

@ -356,11 +356,18 @@ static void mb_tx_rtu_frame(struct modbus_context *ctx)
void mb_tx_frame(struct modbus_context *ctx)
{
if (IS_ENABLED(CONFIG_MODBUS_ASCII_MODE) &&
(ctx->ascii_mode == true)) {
mb_tx_ascii_frame(ctx);
} else {
switch (ctx->mode) {
case MODBUS_MODE_RTU:
mb_tx_rtu_frame(ctx);
break;
case MODBUS_MODE_ASCII:
if (IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
mb_tx_ascii_frame(ctx);
break;
}
default:
LOG_ERR("Unknown MODBUS mode");
return;
}
}
@ -370,7 +377,7 @@ void mb_tx_frame(struct modbus_context *ctx)
*/
static void mb_cb_handler_rx(struct modbus_context *ctx)
{
if ((ctx->ascii_mode == true) &&
if ((ctx->mode == MODBUS_MODE_ASCII) &&
IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
uint8_t c;
@ -460,11 +467,18 @@ static void mb_rx_handler(struct k_work *item)
mb_rx_disable(ctx);
if (IS_ENABLED(CONFIG_MODBUS_ASCII_MODE) &&
(ctx->ascii_mode == true)) {
ctx->rx_frame_err = mb_rx_ascii_frame(ctx);
} else {
switch (ctx->mode) {
case MODBUS_MODE_RTU:
ctx->rx_frame_err = mb_rx_rtu_frame(ctx);
break;
case MODBUS_MODE_ASCII:
if (IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
ctx->rx_frame_err = mb_rx_ascii_frame(ctx);
break;
}
default:
LOG_ERR("Unknown MODBUS mode");
return;
}
ctx->uart_buf_ctr = 0;
@ -513,7 +527,7 @@ static int mb_configure_uart(struct modbus_context *ctx,
uart_cfg.baudrate = baudrate,
uart_cfg.flow_ctrl = UART_CFG_FLOW_CTRL_NONE;
if (ctx->ascii_mode == true) {
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;
@ -634,7 +648,7 @@ static struct modbus_context *mb_cfg_iface(const uint8_t iface,
ctx->rxwait_to = rx_timeout;
ctx->node_addr = node_addr;
ctx->client = client;
ctx->ascii_mode = ascii_mode;
ctx->mode = ascii_mode ? MODBUS_MODE_ASCII : MODBUS_MODE_RTU;
ctx->mbs_user_cb = NULL;
k_mutex_init(&ctx->iface_lock);
@ -743,7 +757,7 @@ int modbus_disable(const uint8_t iface)
ctx->rxwait_to = 0;
ctx->node_addr = 0;
ctx->ascii_mode = false;
ctx->mode = MODBUS_MODE_RTU;
ctx->mbs_user_cb = NULL;
atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);

View file

@ -86,6 +86,11 @@ struct mb_rtu_gpio_config {
gpio_dt_flags_t flags;
};
enum modbus_mode {
MODBUS_MODE_RTU,
MODBUS_MODE_ASCII,
};
#define MODBUS_STATE_CONFIGURED 0
struct modbus_context {
@ -95,8 +100,8 @@ struct modbus_context {
const char *dev_name;
/* UART device */
const struct device *dev;
/* True if ASCII mode is enabled */
bool ascii_mode;
/* MODBUS mode */
enum modbus_mode mode;
/* True if interface is configured as client */
bool client;
/* Amount of time client is willing to wait for response from server */