From b0b5ab017c60959e597a4de9f7ba2ccbfdb96ad7 Mon Sep 17 00:00:00 2001 From: Alex Lisitsyn Date: Fri, 13 Aug 2021 22:46:09 +0800 Subject: [PATCH] freemodbus: fix issues when modbus master and slave work simultaneously fix slave destroy bug when master and slave init simultaneously add additional timer group options for master fix master and slave use the same timer group index (https://github.com/espressif/esp-idf/issues/4699) * Original commit: espressif/esp-idf@9693ef49180a805099ef4a5aeb92eaa0afc7ca29 --- components/freemodbus/Kconfig | 25 +++++++++++++++---- components/freemodbus/modbus/mb.c | 2 +- components/freemodbus/port/port.h | 2 ++ components/freemodbus/port/porttimer.c | 3 +-- components/freemodbus/port/porttimer_m.c | 14 ++++------- .../modbus_controller/mbc_serial_slave.c | 2 +- 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/components/freemodbus/Kconfig b/components/freemodbus/Kconfig index cbdba58..a28e5f4 100644 --- a/components/freemodbus/Kconfig +++ b/components/freemodbus/Kconfig @@ -187,25 +187,40 @@ menu "Modbus configuration" Modbus stack event processing time. config FMB_TIMER_PORT_ENABLED - bool "Modbus slave stack use timer for 3.5T symbol time measurement" + bool "Modbus stack use timer for 3.5T symbol time measurement" default n help If this option is set the Modbus stack uses timer for T3.5 time measurement. Else the internal UART TOUT timeout is used for 3.5T symbol time measurement. config FMB_TIMER_GROUP - int "Modbus Timer group number" + int "Slave Timer group number" range 0 1 default 0 help - Modbus Timer group number that is used for timeout measurement. + Modbus slave Timer group number that is used for timeout measurement. config FMB_TIMER_INDEX - int "Modbus Timer index in the group" + int "Slave Timer index in the group" range 0 1 default 0 help - Modbus Timer Index in the group that is used for timeout measurement. + Modbus slave Timer Index in the group that is used for timeout measurement. + + config FMB_MASTER_TIMER_GROUP + int "Master Timer group number" + range 0 1 + default FMB_TIMER_GROUP + help + Modbus master Timer group number that is used for timeout measurement. + + config FMB_MASTER_TIMER_INDEX + int "Master Timer index" + range 0 1 + default FMB_TIMER_INDEX + help + Modbus master Timer Index in the group that is used for timeout measurement. + Note: Modbus master and slave should have different timer index to be able to work simultaneously. config FMB_TIMER_ISR_IN_IRAM bool "Place timer interrupt handler into IRAM" diff --git a/components/freemodbus/modbus/mb.c b/components/freemodbus/modbus/mb.c index be00e55..2572201 100644 --- a/components/freemodbus/modbus/mb.c +++ b/components/freemodbus/modbus/mb.c @@ -54,7 +54,7 @@ #endif #ifndef MB_PORT_HAS_CLOSE -#define MB_PORT_HAS_CLOSE 0 +#define MB_PORT_HAS_CLOSE 1 #endif /* ----------------------- Static variables ---------------------------------*/ diff --git a/components/freemodbus/port/port.h b/components/freemodbus/port/port.h index 79fb007..e67f56c 100644 --- a/components/freemodbus/port/port.h +++ b/components/freemodbus/port/port.h @@ -61,6 +61,8 @@ #define MB_TCP_FRAME_LOG_BUFSIZE (256) +#define MB_PORT_HAS_CLOSE (1) // Define to explicitly close port on destroy + // Define number of timer reloads per 1 mS #define MB_TIMER_TICS_PER_MS (20UL) diff --git a/components/freemodbus/port/porttimer.c b/components/freemodbus/port/porttimer.c index 2568a62..4417b44 100644 --- a/components/freemodbus/port/porttimer.c +++ b/components/freemodbus/port/porttimer.c @@ -159,8 +159,7 @@ vMBPortTimersDisable(void) void vMBPortTimerClose(void) { #ifdef CONFIG_FMB_TIMER_PORT_ENABLED - ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex)); - ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex)); + ESP_ERROR_CHECK(timer_deinit(usTimerGroupIndex, usTimerIndex)); ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle)); #endif } diff --git a/components/freemodbus/port/porttimer_m.c b/components/freemodbus/port/porttimer_m.c index a96bc4e..ac41f4a 100644 --- a/components/freemodbus/port/porttimer_m.c +++ b/components/freemodbus/port/porttimer_m.c @@ -50,16 +50,13 @@ #define MB_TIMER_DIVIDER ((TIMER_BASE_CLK / 1000000UL) * MB_TICK_TIME_US - 1) // divider for 50uS #define MB_TIMER_WITH_RELOAD (1) -// Timer group and timer number to measure time (configurable in KConfig) -#define MB_TIMER_INDEX (CONFIG_FMB_TIMER_INDEX) -#define MB_TIMER_GROUP (CONFIG_FMB_TIMER_GROUP) - /* ----------------------- Variables ----------------------------------------*/ static USHORT usT35TimeOut50us; -static const USHORT usTimerIndex = MB_TIMER_INDEX; // Initialize Modbus Timer index used by stack, -static const USHORT usTimerGroupIndex = MB_TIMER_GROUP; // Timer group index used by stack -static timer_isr_handle_t xTimerIntHandle; // Timer interrupt handle +// Initialize Modbus Timer group and index used by stack +static const USHORT usTimerIndex = CONFIG_FMB_MASTER_TIMER_INDEX; +static const USHORT usTimerGroupIndex = CONFIG_FMB_MASTER_TIMER_GROUP; +static timer_isr_handle_t xTimerIntHandle; // Timer interrupt handle static timg_dev_t *MB_TG[2] = { &TIMERG0, &TIMERG1 }; @@ -211,7 +208,6 @@ vMBMasterPortTimersDisable() void vMBMasterPortTimerClose(void) { - ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex)); - ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex)); + ESP_ERROR_CHECK(timer_deinit(usTimerGroupIndex, usTimerIndex)); ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle)); } diff --git a/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c b/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c index cf45730..b45e839 100644 --- a/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c +++ b/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c @@ -173,7 +173,7 @@ static esp_err_t mbc_serial_slave_destroy(void) mb_error = eMBClose(); MB_SLAVE_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack close failure returned (0x%x).", (uint32_t)mb_error); - + mbs_interface_ptr = NULL; vMBPortSetMode((UCHAR)MB_PORT_INACTIVE); return ESP_OK; }