modbus: rework RS-485 transceiver DE,RE GPIO configuration

Follow modern way to configure DE,RE GPIO using
struct gpio_dt_spec.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
This commit is contained in:
Johann Fischer 2021-03-17 16:58:05 +01:00 committed by Carles Cufí
parent dcbadd24c2
commit f943a5581d
3 changed files with 30 additions and 41 deletions

View file

@ -15,29 +15,29 @@ LOG_MODULE_REGISTER(modbus, CONFIG_MODBUS_LOG_LEVEL);
#define DT_DRV_COMPAT zephyr_modbus_serial
#define MB_RTU_DEFINE_GPIO_CFG(n, d) \
static struct mb_rtu_gpio_config d##_cfg_##n = { \
.name = DT_INST_GPIO_LABEL(n, d), \
.pin = DT_INST_GPIO_PIN(n, d), \
.flags = DT_INST_GPIO_FLAGS(n, d), \
#define MB_RTU_DEFINE_GPIO_CFG(inst, prop) \
static struct gpio_dt_spec prop##_cfg_##inst = { \
.port = DEVICE_DT_GET(DT_INST_PHANDLE(inst, prop)), \
.pin = DT_INST_GPIO_PIN(inst, prop), \
.dt_flags = DT_INST_GPIO_FLAGS(inst, prop), \
};
#define MB_RTU_DEFINE_GPIO_CFGS(n) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(n, de_gpios), \
(MB_RTU_DEFINE_GPIO_CFG(n, de_gpios)), ()) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(n, re_gpios), \
(MB_RTU_DEFINE_GPIO_CFG(n, re_gpios)), ())
#define MB_RTU_DEFINE_GPIO_CFGS(inst) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, de_gpios), \
(MB_RTU_DEFINE_GPIO_CFG(inst, de_gpios)), ()) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, re_gpios), \
(MB_RTU_DEFINE_GPIO_CFG(inst, re_gpios)), ())
DT_INST_FOREACH_STATUS_OKAY(MB_RTU_DEFINE_GPIO_CFGS)
#define MB_RTU_ASSIGN_GPIO_CFG(n, d) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(n, d), \
(&d##_cfg_##n), (NULL))
#define MB_RTU_ASSIGN_GPIO_CFG(inst, prop) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, prop), \
(&prop##_cfg_##inst), (NULL))
#define MODBUS_DT_GET_SERIAL_DEV(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), \
#define MODBUS_DT_GET_SERIAL_DEV(inst) { \
.dev_name = DT_INST_BUS_LABEL(inst), \
.de = MB_RTU_ASSIGN_GPIO_CFG(inst, de_gpios), \
.re = MB_RTU_ASSIGN_GPIO_CFG(inst, re_gpios), \
},
#ifdef CONFIG_MODBUS_SERIAL
@ -46,9 +46,9 @@ static struct modbus_serial_config modbus_serial_cfg[] = {
};
#endif
#define MODBUS_DT_GET_DEV(n) { \
.iface_name = DT_INST_LABEL(n), \
.cfg = &modbus_serial_cfg[n], \
#define MODBUS_DT_GET_DEV(inst) { \
.iface_name = DT_INST_LABEL(inst), \
.cfg = &modbus_serial_cfg[inst], \
},
#define DEFINE_MODBUS_RAW_ADU(x, _) { \

View file

@ -80,13 +80,6 @@
/* Modbus ADU constants */
#define MODBUS_ADU_PROTO_ID 0x0000
struct mb_rtu_gpio_config {
const char *name;
const struct device *dev;
gpio_pin_t pin;
gpio_dt_flags_t flags;
};
struct modbus_serial_config {
/* UART device name */
const char *dev_name;
@ -97,9 +90,9 @@ struct modbus_serial_config {
/* Pointer to current position in buffer */
uint8_t *uart_buf_ptr;
/* Pointer to driver enable (DE) pin config */
struct mb_rtu_gpio_config *de;
struct gpio_dt_spec *de;
/* Pointer to receiver enable (nRE) pin config */
struct mb_rtu_gpio_config *re;
struct gpio_dt_spec *re;
/* RTU timer to detect frame end point */
struct k_timer rtu_timer;
/* Number of bytes received or to send */

View file

@ -33,7 +33,7 @@ static void modbus_serial_tx_on(struct modbus_context *ctx)
struct modbus_serial_config *cfg = ctx->cfg;
if (cfg->de != NULL) {
gpio_pin_set(cfg->de->dev, cfg->de->pin, 1);
gpio_pin_set(cfg->de->port, cfg->de->pin, 1);
}
uart_irq_tx_enable(cfg->dev);
@ -45,7 +45,7 @@ static void modbus_serial_tx_off(struct modbus_context *ctx)
uart_irq_tx_disable(cfg->dev);
if (cfg->de != NULL) {
gpio_pin_set(cfg->de->dev, cfg->de->pin, 0);
gpio_pin_set(cfg->de->port, cfg->de->pin, 0);
}
}
@ -54,7 +54,7 @@ static void modbus_serial_rx_on(struct modbus_context *ctx)
struct modbus_serial_config *cfg = ctx->cfg;
if (cfg->re != NULL) {
gpio_pin_set(cfg->re->dev, cfg->re->pin, 1);
gpio_pin_set(cfg->re->port, cfg->re->pin, 1);
}
uart_irq_rx_enable(cfg->dev);
@ -66,7 +66,7 @@ static void modbus_serial_rx_off(struct modbus_context *ctx)
uart_irq_rx_disable(cfg->dev);
if (cfg->re != NULL) {
gpio_pin_set(cfg->re->dev, cfg->re->pin, 0);
gpio_pin_set(cfg->re->port, cfg->re->pin, 0);
}
}
@ -443,26 +443,22 @@ static int configure_gpio(struct modbus_context *ctx)
struct modbus_serial_config *cfg = ctx->cfg;
if (cfg->de != NULL) {
cfg->de->dev = device_get_binding(cfg->de->name);
if (cfg->de->dev == NULL) {
if (!device_is_ready(cfg->de->port)) {
return -ENODEV;
}
if (gpio_pin_configure(cfg->de->dev, cfg->de->pin,
GPIO_OUTPUT_INACTIVE | cfg->de->flags)) {
if (gpio_pin_configure_dt(cfg->de, GPIO_OUTPUT_INACTIVE)) {
return -EIO;
}
}
if (cfg->re != NULL) {
cfg->re->dev = device_get_binding(cfg->re->name);
if (cfg->re->dev == NULL) {
if (!device_is_ready(cfg->re->port)) {
return -ENODEV;
}
if (gpio_pin_configure(cfg->re->dev, cfg->re->pin,
GPIO_OUTPUT_INACTIVE | cfg->re->flags)) {
if (gpio_pin_configure_dt(cfg->re, GPIO_OUTPUT_INACTIVE)) {
return -EIO;
}
}