From 85299614134f69d2631dc1857a0a7f19447b40d0 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 * Original commit: espressif/esp-idf@4bac558ab3bcac96033f1ffa99acde78eb5c82ed --- 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 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/components/freemodbus/port/porttimer.c b/components/freemodbus/port/porttimer.c index c68e171..a98a5d2 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 static timg_dev_t *MB_TG[2] = {&TIMERG0, &TIMERG1}; @@ -121,7 +123,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); @@ -154,6 +157,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 55c561c..b2e1723 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 timg_dev_t *MB_TG[2] = { &TIMERG0, &TIMERG1 }; @@ -128,7 +127,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); @@ -164,7 +163,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; } @@ -211,4 +210,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 30aae20..b162312 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 5c6b05f..6a125d9 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; }