Merge branch 'bugfix/modbus_allow_master_slave_work_simultaneously_v41' into 'release/v4.1'

freemodbus: fix issues when modbus master and slave work simultaneously (backport v4.1)

See merge request espressif/esp-idf!14951
This commit is contained in:
Jiang Jiang Jian
2022-06-19 23:23:24 +08:00
6 changed files with 27 additions and 35 deletions

View File

@@ -159,18 +159,33 @@ menu "Modbus configuration"
Else the internal UART TOUT timeout is used for 3.5T symbol time measurement. Else the internal UART TOUT timeout is used for 3.5T symbol time measurement.
config FMB_TIMER_GROUP config FMB_TIMER_GROUP
int "Modbus Timer group number" int "Slave Timer group number"
range 0 1 range 0 1
default 0 default 0
help 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 config FMB_TIMER_INDEX
int "Modbus Timer index in the group" int "Slave Timer index in the group"
range 0 1 range 0 1
default 0 default 0
help 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 config FMB_TIMER_ISR_IN_IRAM
bool "Place timer interrupt handler into IRAM" bool "Place timer interrupt handler into IRAM"

View File

@@ -54,7 +54,7 @@
#endif #endif
#ifndef MB_PORT_HAS_CLOSE #ifndef MB_PORT_HAS_CLOSE
#define MB_PORT_HAS_CLOSE 0 #define MB_PORT_HAS_CLOSE 1
#endif #endif
/* ----------------------- Static variables ---------------------------------*/ /* ----------------------- Static variables ---------------------------------*/

View File

@@ -55,25 +55,9 @@
/* ----------------------- Modbus includes ----------------------------------*/ /* ----------------------- Modbus includes ----------------------------------*/
/* ----------------------- Variables ----------------------------------------*/ /* ----------------------- Variables ----------------------------------------*/
static UCHAR ucPortMode = 0;
/* ----------------------- Start implementation -----------------------------*/ /* ----------------------- Start implementation -----------------------------*/
UCHAR
ucMBPortGetMode( void )
{
return ucPortMode;
}
void
vMBPortSetMode( UCHAR ucMode )
{
ENTER_CRITICAL_SECTION();
ucPortMode = ucMode;
EXIT_CRITICAL_SECTION();
}
void void
vMBMasterPortClose( void ) vMBMasterPortClose( void )
{ {
@@ -84,4 +68,3 @@ vMBMasterPortClose( void )
vMBMasterPortTimerClose( ); vMBMasterPortTimerClose( );
vMBMasterPortEventClose( ); vMBMasterPortEventClose( );
} }

View File

@@ -147,8 +147,7 @@ vMBPortTimersDisable(void)
void vMBPortTimerClose(void) void vMBPortTimerClose(void)
{ {
#ifdef CONFIG_FMB_TIMER_PORT_ENABLED #ifdef CONFIG_FMB_TIMER_PORT_ENABLED
ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex)); ESP_ERROR_CHECK(timer_deinit(usTimerGroupIndex, usTimerIndex));
ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex));
ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle)); ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle));
#endif #endif
} }

View File

@@ -50,16 +50,13 @@
#define MB_TIMER_DIVIDER ((TIMER_BASE_CLK / 1000000UL) * MB_TICK_TIME_US - 1) // divider for 50uS #define MB_TIMER_DIVIDER ((TIMER_BASE_CLK / 1000000UL) * MB_TICK_TIME_US - 1) // divider for 50uS
#define MB_TIMER_WITH_RELOAD (1) #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 ----------------------------------------*/ /* ----------------------- Variables ----------------------------------------*/
static USHORT usT35TimeOut50us; static USHORT usT35TimeOut50us;
static const USHORT usTimerIndex = MB_TIMER_INDEX; // Initialize Modbus Timer index used by stack, // Initialize Modbus Timer group and index used by stack
static const USHORT usTimerGroupIndex = MB_TIMER_GROUP; // Timer group index used by stack static const USHORT usTimerIndex = CONFIG_FMB_MASTER_TIMER_INDEX;
static timer_isr_handle_t xTimerIntHandle; // Timer interrupt handle static const USHORT usTimerGroupIndex = CONFIG_FMB_MASTER_TIMER_GROUP;
static timer_isr_handle_t xTimerIntHandle; // Timer interrupt handle
/* ----------------------- static functions ---------------------------------*/ /* ----------------------- static functions ---------------------------------*/
@@ -194,7 +191,6 @@ vMBMasterPortTimersDisable()
void vMBMasterPortTimerClose(void) void vMBMasterPortTimerClose(void)
{ {
ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex)); ESP_ERROR_CHECK(timer_deinit(usTimerGroupIndex, usTimerIndex));
ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex));
ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle)); ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle));
} }

View File

@@ -28,7 +28,7 @@
#include "port_serial_slave.h" #include "port_serial_slave.h"
// Shared pointer to interface structure // Shared pointer to interface structure
static mb_slave_interface_t* mbs_interface_ptr = NULL; // &default_interface_inst; static mb_slave_interface_t* mbs_interface_ptr = NULL;
// Modbus task function // Modbus task function
static void modbus_slave_task(void *pvParameters) static void modbus_slave_task(void *pvParameters)
@@ -36,7 +36,6 @@ static void modbus_slave_task(void *pvParameters)
// Modbus interface must be initialized before start // Modbus interface must be initialized before start
MB_SLAVE_ASSERT(mbs_interface_ptr != NULL); MB_SLAVE_ASSERT(mbs_interface_ptr != NULL);
mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts; mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
MB_SLAVE_ASSERT(mbs_opts != NULL); MB_SLAVE_ASSERT(mbs_opts != NULL);
// Main Modbus stack processing cycle // Main Modbus stack processing cycle
for (;;) { for (;;) {