From 2cff3ac0ee43b0412a82da764feb5d348aa9cc44 Mon Sep 17 00:00:00 2001 From: aleks Date: Mon, 4 May 2026 08:58:09 +0200 Subject: [PATCH] fix: address maximum api blocking time --- Kconfig | 13 ++++++++++++- .../mb_controller/common/esp_modbus_master.c | 9 ++++++++- modbus/mb_controller/common/mbc_master.h | 18 ++++++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Kconfig b/Kconfig index f799740..514f989 100644 --- a/Kconfig +++ b/Kconfig @@ -71,12 +71,23 @@ menu "Modbus configuration" config FMB_MASTER_TIMEOUT_MS_RESPOND int "Slave respond timeout (Milliseconds)" - default 10000 + default 5000 range 150 30000 help If master sends a frame which is not broadcast, it has to wait some time for slave response. if slave is not respond in this time, the master will process timeout error. + config FMB_MASTER_MAX_API_BLOCKING_TIME_MS + int "Modbus TCP master API blocking threshold (ms)" + range 3000 200000 + default 6000 + help + This option represents the maximum API blocking time used by controller API calls. + The master uses timer to handle slave response timeout as defined by FMB_MASTER_TIMEOUT_MS_RESPOND + or overridden by corresponded master configuration field. The actual API blocking time is determined + as a shortest time between these two values. This API blocking time is separated with + FMB_MASTER_TIMEOUT_MS_RESPOND intentionally to prevent possible misconfiguration and must not be less than it. + config FMB_MASTER_DELAY_MS_CONVERT int "Slave conversion delay (Milliseconds)" default 200 diff --git a/modbus/mb_controller/common/esp_modbus_master.c b/modbus/mb_controller/common/esp_modbus_master.c index 1d9010b..e0072d4 100644 --- a/modbus/mb_controller/common/esp_modbus_master.c +++ b/modbus/mb_controller/common/esp_modbus_master.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2016-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -203,6 +203,13 @@ esp_err_t mbc_master_start(void *ctx) MB_RETURN_ON_FALSE(ctx, ESP_ERR_INVALID_STATE, TAG, "Master interface is not correctly initialized."); mbm_controller_iface_t *mbm_controller = MB_MASTER_GET_IFACE(ctx); + + if ((mbm_controller->opts.comm_opts.common_opts.response_tout_ms > 0) && + (mbm_controller->opts.comm_opts.common_opts.response_tout_ms > CONFIG_FMB_MASTER_MAX_API_BLOCKING_TIME_MS)) { + mbm_controller->opts.comm_opts.common_opts.response_tout_ms = CONFIG_FMB_MASTER_MAX_API_BLOCKING_TIME_MS + 500; + ESP_LOGW(TAG, "Slave response time option in master is incorrect, setting to max value."); + } + MB_RETURN_ON_FALSE(mbm_controller->start, ESP_ERR_INVALID_STATE, TAG, "Master interface is not correctly initialized."); error = mbm_controller->start(ctx); diff --git a/modbus/mb_controller/common/mbc_master.h b/modbus/mb_controller/common/mbc_master.h index 40d8073..837d57d 100644 --- a/modbus/mb_controller/common/mbc_master.h +++ b/modbus/mb_controller/common/mbc_master.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2016-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -22,16 +22,26 @@ #include "mb_utils.h" #include "mb_master.h" +#include "sdkconfig.h" + #ifdef __cplusplus extern "C" { #endif /* ----------------------- Defines ------------------------------------------*/ +#define MB_MIN_RESP_DELAY_MS (300) // Set the maximum resource waiting time, the actual time of resource release -// will be dependent on response time set by timer + conversion time if the command is received -#define MB_MAX_RESP_DELAY_MS (3000) -#define MB_MIN_RESP_DELAY_MS (300) +// will be dependent on response time set by timer + conversion time if the command is received. +#if defined(CONFIG_FMB_MASTER_MAX_API_BLOCKING_TIME_MS) +#define MB_MAX_RESP_DELAY_MS (CONFIG_FMB_MASTER_MAX_API_BLOCKING_TIME_MS) +_Static_assert( + (CONFIG_FMB_MASTER_MAX_API_BLOCKING_TIME_MS) >= (CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND), + "CONFIG_FMB_MASTER_MAX_API_BLOCKING_TIME_MS must be >= CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND" +); +#else +#define MB_MAX_RESP_DELAY_MS (CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND) +#endif /** * @brief Modbus controller handler structure