From 4bac558ab3bcac96033f1ffa99acde78eb5c82ed Mon Sep 17 00:00:00 2001 From: Alex Lisitsyn Date: Thu, 31 Oct 2019 23:23:24 +0800 Subject: [PATCH] freemodbus: fix a bug with destroy function of modbus controller and fix port destroy functions adds timer interrupt handle and free it in vMBXXXPortTimerClose() in master and slave timer port assign modbus controller interface pointer to NULL in destroy function after free --- components/freemodbus/port/porttimer.c | 7 +++++-- components/freemodbus/port/porttimer_m.c | 14 +++++++------- .../modbus_controller/mbc_serial_master.c | 1 + .../modbus_controller/mbc_serial_slave.c | 2 +- .../protocols/modbus_master/main/sense_modbus.c | 8 ++++---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/components/freemodbus/port/porttimer.c b/components/freemodbus/port/porttimer.c index 4898e0ca0..95ecf0f3a 100644 --- a/components/freemodbus/port/porttimer.c +++ b/components/freemodbus/port/porttimer.c @@ -50,6 +50,7 @@ #include "mbport.h" #include "driver/timer.h" #include "port_serial_slave.h" +#include "sdkconfig.h" #ifdef CONFIG_FMB_TIMER_PORT_ENABLED @@ -63,6 +64,7 @@ static const USHORT usTimerIndex = CONFIG_FMB_TIMER_INDEX; // Modbus Timer index used by stack static const USHORT usTimerGroupIndex = CONFIG_FMB_TIMER_GROUP; // Modbus Timer group index used by stack +static timer_isr_handle_t xTimerIntHandle; // Timer interrupt handle /* ----------------------- Start implementation -----------------------------*/ static void IRAM_ATTR vTimerGroupIsr(void *param) @@ -109,7 +111,8 @@ BOOL xMBPortTimersInit(USHORT usTim1Timerout50us) "failure to set alarm failure, timer_set_alarm_value() returned (0x%x).", (uint32_t)xErr); // Register ISR for timer - xErr = timer_isr_register(usTimerGroupIndex, usTimerIndex, vTimerGroupIsr, (void*)(uint32_t)usTimerIndex, ESP_INTR_FLAG_IRAM, NULL); + xErr = timer_isr_register(usTimerGroupIndex, usTimerIndex, vTimerGroupIsr, + (void*)(uint32_t)usTimerIndex, ESP_INTR_FLAG_LOWMED, &xTimerIntHandle); MB_PORT_CHECK((xErr == ESP_OK), FALSE, "timer set value failure, timer_isr_register() returned (0x%x).", (uint32_t)xErr); @@ -142,6 +145,6 @@ 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(esp_intr_free(xTimerIntHandle)); #endif } - diff --git a/components/freemodbus/port/porttimer_m.c b/components/freemodbus/port/porttimer_m.c index af0ded085..7a4897d10 100644 --- a/components/freemodbus/port/porttimer_m.c +++ b/components/freemodbus/port/porttimer_m.c @@ -51,16 +51,15 @@ #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 - -#define MB_TIMER_IO_LED 0 +#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 /* ----------------------- static functions ---------------------------------*/ @@ -110,7 +109,7 @@ BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us) (uint32_t)xErr); // Register ISR for timer xErr = timer_isr_register(usTimerGroupIndex, usTimerIndex, - vTimerGroupIsr, (void*)(uint32_t)usTimerIndex, ESP_INTR_FLAG_IRAM, NULL); + vTimerGroupIsr, (void*)(uint32_t)usTimerIndex, ESP_INTR_FLAG_LOWMED, &xTimerIntHandle); MB_PORT_CHECK((xErr == ESP_OK), FALSE, "timer set value failure, timer_isr_register() returned (0x%x).", (uint32_t)xErr); @@ -146,7 +145,7 @@ static BOOL xMBMasterPortTimersEnable(USHORT usTimerTics50us) MB_PORT_CHECK((xErr == ESP_OK), FALSE, "timer start failure, timer_start() returned (0x%x).", (uint32_t)xErr); - //ESP_LOGD(MB_PORT_TAG,"%s Init timer.", __func__); + ESP_LOGD(MB_PORT_TAG,"%s Init timer.", __func__); return TRUE; } @@ -193,4 +192,5 @@ void vMBMasterPortTimerClose(void) { ESP_ERROR_CHECK(timer_pause(usTimerGroupIndex, usTimerIndex)); ESP_ERROR_CHECK(timer_disable_intr(usTimerGroupIndex, usTimerIndex)); -} + ESP_ERROR_CHECK(esp_intr_free(xTimerIntHandle)); +} \ No newline at end of file diff --git a/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c b/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c index 30aae200f..b162312f9 100644 --- a/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c +++ b/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c @@ -140,6 +140,7 @@ static esp_err_t mbc_serial_master_destroy(void) MB_MASTER_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack close failure returned (0x%x).", (uint32_t)mb_error); free(mbm_interface_ptr); // free the memory allocated for options + mbm_interface_ptr = NULL; return ESP_OK; } 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 5c6b05f37..6a125d997 100644 --- a/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c +++ b/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c @@ -133,7 +133,7 @@ static esp_err_t mbc_serial_slave_destroy(void) MB_SLAVE_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack close failure returned (0x%x).", (uint32_t)mb_error); free(mbs_interface_ptr); - + mbs_interface_ptr = NULL; return ESP_OK; } diff --git a/examples/protocols/modbus_master/main/sense_modbus.c b/examples/protocols/modbus_master/main/sense_modbus.c index 8d1ab3539..c2830fe73 100644 --- a/examples/protocols/modbus_master/main/sense_modbus.c +++ b/examples/protocols/modbus_master/main/sense_modbus.c @@ -59,16 +59,16 @@ static void* sense_modbus_get_param_data(const mb_parameter_descriptor_t* param_ switch(param_descriptor->mb_param_type) { case MB_PARAM_HOLDING: - instance_ptr = (void*)(&holding_reg_params + param_descriptor->param_offset - 1); + instance_ptr = (void*)((uint32_t)&holding_reg_params + param_descriptor->param_offset - 1); break; case MB_PARAM_INPUT: - instance_ptr = (void*)(&input_reg_params + param_descriptor->param_offset - 1); + instance_ptr = (void*)((uint32_t)&input_reg_params + param_descriptor->param_offset - 1); break; case MB_PARAM_COIL: - instance_ptr = (void*)(&coil_reg_params + param_descriptor->param_offset - 1); + instance_ptr = (void*)((uint32_t)&coil_reg_params + param_descriptor->param_offset - 1); break; case MB_PARAM_DISCRETE: - instance_ptr = (void*)(&discrete_reg_params + param_descriptor->param_offset - 1); + instance_ptr = (void*)((uint32_t)&discrete_reg_params + param_descriptor->param_offset - 1); break; default: instance_ptr = NULL;