modbus: get interface index according to interface name

Add function to get Modbus RTU interface index according
to interface name. This can be used to clearly identify
interfaces in the application.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
This commit is contained in:
Johann Fischer 2020-12-28 16:19:19 +01:00 committed by Carles Cufí
commit b0518d6100
8 changed files with 41 additions and 6 deletions

View file

@ -334,6 +334,18 @@ struct mbs_rtu_user_callbacks {
int (*holding_reg_wr_fp)(uint16_t addr, float reg);
};
/**
* @brief Get Modbus RTU interface index according to interface name
*
* If there is more than one interface, it can be used to clearly
* identify interfaces in the application.
*
* @param iface_name Modbus RTU interface name
*
* @retval Modbus RTU interface index or negative error value.
*/
int mb_rtu_iface_get_by_name(const char *iface_name);
/**
* @brief Configure Modbus Interface as server
*

View file

@ -9,7 +9,7 @@
modbus0 {
compatible = "zephyr,modbus-serial";
label = "MODBUS";
label = "MODBUS0";
status = "okay";
de-gpios = <&arduino_header 15 GPIO_ACTIVE_LOW>; /* D9 */
};

View file

@ -9,7 +9,7 @@
modbus0 {
compatible = "zephyr,modbus-serial";
label = "MODBUS";
label = "MODBUS0";
status = "okay";
de-gpios = <&arduino_header 15 GPIO_ACTIVE_LOW>; /* D9 */
};

View file

@ -102,8 +102,16 @@ static struct mbs_rtu_user_callbacks mbs_cbs = {
static int init_modbus_server(void)
{
const uint8_t iface = 0;
const uint32_t mb_rtu_br = 19200;
const char iface_name[] = {DT_PROP(DT_INST(0, zephyr_modbus_serial), label)};
int iface;
iface = mb_rtu_iface_get_by_name(iface_name);
if (iface < 0) {
LOG_ERR("Failed to get iface index for %s", iface_name);
return iface;
}
return mb_rtu_cfg_server(iface, 1, mb_rtu_br, UART_CFG_PARITY_NONE,
&mbs_cbs, false);

View file

@ -49,6 +49,7 @@ DT_INST_FOREACH_STATUS_OKAY(MB_RTU_DEFINE_GPIO_CFGS)
(&d##_cfg_##n), (NULL))
#define MODBUS_DT_GET_DEV(n) { \
.iface_name = DT_INST_LABEL(n), \
.dev_name = DT_INST_BUS_LABEL(n), \
.de = MB_RTU_ASSIGN_GPIO_CFG(n, de_gpios), \
.re = MB_RTU_ASSIGN_GPIO_CFG(n, re_gpios), \
@ -663,6 +664,7 @@ static struct mb_rtu_context *mb_cfg_iface(const uint8_t iface,
k_timer_init(&ctx->rtu_timer, mb_rtu_tmr_handler, NULL);
k_timer_user_data_set(&ctx->rtu_timer, ctx);
LOG_DBG("Modbus interface %s initialized", ctx->iface_name);
return ctx;
}
@ -696,6 +698,17 @@ int mb_rtu_cfg_server(const uint8_t iface, const uint8_t node_addr,
return 0;
}
int mb_rtu_iface_get_by_name(const char *iface_name)
{
for (int i = 0; i < ARRAY_SIZE(mb_ctx_tbl); i++) {
if (strcmp(iface_name, mb_ctx_tbl[i].iface_name) == 0) {
return i;
}
}
return -ENODEV;
}
int mb_rtu_cfg_client(const uint8_t iface,
const uint32_t baud, const enum uart_config_parity parity,
const uint32_t rx_timeout,

View file

@ -89,6 +89,8 @@ struct mb_rtu_gpio_config {
#define MB_RTU_STATE_CONFIGURED 0
struct mb_rtu_context {
/* Interface name */
const char *iface_name;
/* UART device name */
const char *dev_name;
/* UART device */

View file

@ -9,7 +9,7 @@
modbus0 {
compatible = "zephyr,modbus-serial";
label = "MODBUS";
label = "MODBUS0";
status = "okay";
de-gpios = <&gpiob 22 GPIO_ACTIVE_LOW>; /* red LED */
re-gpios = <&gpioe 26 GPIO_ACTIVE_LOW>; /* green LED */
@ -22,7 +22,7 @@
modbus1 {
compatible = "zephyr,modbus-serial";
label = "MODBUS";
label = "MODBUS1";
status = "okay";
};
};

View file

@ -9,7 +9,7 @@
modbus0 {
compatible = "zephyr,modbus-serial";
label = "MODBUS";
label = "MODBUS0";
status = "okay";
};
};