freemodbus: add affinity option for modbus stack tasks

This commit is contained in:
aleks 2021-07-28 07:45:13 +02:00 committed by Alex Lisitsyn
parent ba15ac8634
commit 33fe673e85
8 changed files with 47 additions and 13 deletions

View File

@ -113,6 +113,29 @@ menu "Modbus configuration"
Modbus port data processing task priority. Modbus port data processing task priority.
The priority of Modbus controller task is equal to (CONFIG_FMB_PORT_TASK_PRIO - 1). The priority of Modbus controller task is equal to (CONFIG_FMB_PORT_TASK_PRIO - 1).
choice FMB_PORT_TASK_AFFINITY
prompt "Modbus task affinity"
default FMB_PORT_TASK_AFFINITY_CPU0
depends on !FREERTOS_UNICORE
help
Allows setting the core affinity of the Modbus controller task, i.e. whether the task is pinned to
particular CPU, or allowed to run on any CPU.
config FMB_PORT_TASK_AFFINITY_NO_AFFINITY
bool "No affinity"
config FMB_PORT_TASK_AFFINITY_CPU0
bool "CPU0"
config FMB_PORT_TASK_AFFINITY_CPU1
bool "CPU1"
endchoice
config FMB_PORT_TASK_AFFINITY
hex
default FREERTOS_NO_AFFINITY if FMB_PORT_TASK_AFFINITY_NO_AFFINITY || FREERTOS_UNICORE
default 0x0 if FMB_PORT_TASK_AFFINITY_CPU0
default 0x1 if FMB_PORT_TASK_AFFINITY_CPU1
config FMB_CONTROLLER_SLAVE_ID_SUPPORT config FMB_CONTROLLER_SLAVE_ID_SUPPORT
bool "Modbus controller slave ID support" bool "Modbus controller slave ID support"
default y default y
@ -165,7 +188,7 @@ menu "Modbus configuration"
config FMB_TIMER_PORT_ENABLED config FMB_TIMER_PORT_ENABLED
bool "Modbus slave stack use timer for 3.5T symbol time measurement" bool "Modbus slave stack use timer for 3.5T symbol time measurement"
default y default n
help help
If this option is set the Modbus stack uses timer for T3.5 time measurement. 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. Else the internal UART TOUT timeout is used for 3.5T symbol time measurement.

View File

@ -50,6 +50,9 @@
#define MB_TCP_STACK_SIZE (CONFIG_FMB_PORT_TASK_STACK_SIZE) #define MB_TCP_STACK_SIZE (CONFIG_FMB_PORT_TASK_STACK_SIZE)
#define MB_TCP_TASK_PRIO (CONFIG_FMB_PORT_TASK_PRIO) #define MB_TCP_TASK_PRIO (CONFIG_FMB_PORT_TASK_PRIO)
// The task affinity for Modbus stack tasks
#define MB_PORT_TASK_AFFINITY (CONFIG_FMB_PORT_TASK_AFFINITY)
#define MB_TCP_READ_TIMEOUT_MS (100) // read timeout in mS #define MB_TCP_READ_TIMEOUT_MS (100) // read timeout in mS
#define MB_TCP_READ_TIMEOUT (pdMS_TO_TICKS(MB_TCP_READ_TIMEOUT_MS)) #define MB_TCP_READ_TIMEOUT (pdMS_TO_TICKS(MB_TCP_READ_TIMEOUT_MS))
#define MB_TCP_SEND_TIMEOUT_MS (500) // send event timeout in mS #define MB_TCP_SEND_TIMEOUT_MS (500) // send event timeout in mS

View File

@ -249,8 +249,10 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
uart_set_always_rx_timeout(ucUartNumber, true); uart_set_always_rx_timeout(ucUartNumber, true);
// Create a task to handle UART events // Create a task to handle UART events
BaseType_t xStatus = xTaskCreate(vUartTask, "uart_queue_task", MB_SERIAL_TASK_STACK_SIZE, BaseType_t xStatus = xTaskCreatePinnedToCore(vUartTask, "uart_queue_task",
NULL, MB_SERIAL_TASK_PRIO, &xMbTaskHandle); MB_SERIAL_TASK_STACK_SIZE,
NULL, MB_SERIAL_TASK_PRIO,
&xMbTaskHandle, MB_PORT_TASK_AFFINITY);
if (xStatus != pdPASS) { if (xStatus != pdPASS) {
vTaskDelete(xMbTaskHandle); vTaskDelete(xMbTaskHandle);
// Force exit from function with failure // Force exit from function with failure

View File

@ -243,8 +243,10 @@ BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
uart_set_always_rx_timeout(ucUartNumber, true); uart_set_always_rx_timeout(ucUartNumber, true);
// Create a task to handle UART events // Create a task to handle UART events
BaseType_t xStatus = xTaskCreate(vUartTask, "uart_queue_task", MB_SERIAL_TASK_STACK_SIZE, BaseType_t xStatus = xTaskCreatePinnedToCore(vUartTask, "uart_queue_task",
NULL, MB_SERIAL_TASK_PRIO, &xMbTaskHandle); MB_SERIAL_TASK_STACK_SIZE,
NULL, MB_SERIAL_TASK_PRIO,
&xMbTaskHandle, MB_PORT_TASK_AFFINITY);
if (xStatus != pdPASS) { if (xStatus != pdPASS) {
vTaskDelete(xMbTaskHandle); vTaskDelete(xMbTaskHandle);
// Force exit from function with failure // Force exit from function with failure

View File

@ -671,12 +671,13 @@ esp_err_t mbc_serial_master_create(void** handler)
MB_MASTER_CHECK((mbm_opts->mbm_event_group != NULL), MB_MASTER_CHECK((mbm_opts->mbm_event_group != NULL),
ESP_ERR_NO_MEM, "mb event group error."); ESP_ERR_NO_MEM, "mb event group error.");
// Create modbus controller task // Create modbus controller task
status = xTaskCreate((void*)&modbus_master_task, status = xTaskCreatePinnedToCore((void*)&modbus_master_task,
"modbus_matask", "modbus_matask",
MB_CONTROLLER_STACK_SIZE, MB_CONTROLLER_STACK_SIZE,
NULL, // No parameters NULL, // No parameters
MB_CONTROLLER_PRIORITY, MB_CONTROLLER_PRIORITY,
&mbm_opts->mbm_task_handle); &mbm_opts->mbm_task_handle,
MB_PORT_TASK_AFFINITY);
if (status != pdPASS) { if (status != pdPASS) {
vTaskDelete(mbm_opts->mbm_task_handle); vTaskDelete(mbm_opts->mbm_task_handle);
MB_MASTER_CHECK((status == pdPASS), ESP_ERR_NO_MEM, MB_MASTER_CHECK((status == pdPASS), ESP_ERR_NO_MEM,

View File

@ -212,12 +212,13 @@ esp_err_t mbc_serial_slave_create(void** handler)
MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL), MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL),
ESP_ERR_NO_MEM, "mb notify queue creation error."); ESP_ERR_NO_MEM, "mb notify queue creation error.");
// Create Modbus controller task // Create Modbus controller task
status = xTaskCreate((void*)&modbus_slave_task, status = xTaskCreatePinnedToCore((void*)&modbus_slave_task,
"modbus_slave_task", "modbus_slave_task",
MB_CONTROLLER_STACK_SIZE, MB_CONTROLLER_STACK_SIZE,
NULL, NULL,
MB_CONTROLLER_PRIORITY, MB_CONTROLLER_PRIORITY,
&mbs_opts->mbs_task_handle); &mbs_opts->mbs_task_handle,
MB_PORT_TASK_AFFINITY);
if (status != pdPASS) { if (status != pdPASS) {
vTaskDelete(mbs_opts->mbs_task_handle); vTaskDelete(mbs_opts->mbs_task_handle);
MB_SLAVE_CHECK((status == pdPASS), ESP_ERR_NO_MEM, MB_SLAVE_CHECK((status == pdPASS), ESP_ERR_NO_MEM,

View File

@ -122,12 +122,13 @@ xMBMasterTCPPortInit( USHORT usTCPPort )
} }
// Create task for packet processing // Create task for packet processing
BaseType_t xErr = xTaskCreate(vMBTCPPortMasterTask, BaseType_t xErr = xTaskCreatePinnedToCore(vMBTCPPortMasterTask,
"tcp_master_task", "tcp_master_task",
MB_TCP_STACK_SIZE, MB_TCP_STACK_SIZE,
NULL, NULL,
MB_TCP_TASK_PRIO, MB_TCP_TASK_PRIO,
&xMbPortConfig.xMbTcpTaskHandle); &xMbPortConfig.xMbTcpTaskHandle,
MB_PORT_TASK_AFFINITY);
if (xErr != pdTRUE) if (xErr != pdTRUE)
{ {
ESP_LOGE(MB_TCP_MASTER_PORT_TAG, "TCP master task creation failure."); ESP_LOGE(MB_TCP_MASTER_PORT_TAG, "TCP master task creation failure.");

View File

@ -185,12 +185,13 @@ esp_err_t mbc_tcp_slave_create(void** handler)
MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL), MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL),
ESP_ERR_NO_MEM, "mb notify queue creation error."); ESP_ERR_NO_MEM, "mb notify queue creation error.");
// Create Modbus controller task // Create Modbus controller task
status = xTaskCreate((void*)&modbus_tcp_slave_task, status = xTaskCreatePinnedToCore((void*)&modbus_tcp_slave_task,
"modbus_tcp_slave_task", "modbus_tcp_slave_task",
MB_CONTROLLER_STACK_SIZE, MB_CONTROLLER_STACK_SIZE,
NULL, NULL,
MB_CONTROLLER_PRIORITY, MB_CONTROLLER_PRIORITY,
&mbs_opts->mbs_task_handle); &mbs_opts->mbs_task_handle,
MB_PORT_TASK_AFFINITY);
if (status != pdPASS) { if (status != pdPASS) {
vTaskDelete(mbs_opts->mbs_task_handle); vTaskDelete(mbs_opts->mbs_task_handle);
MB_SLAVE_CHECK((status == pdPASS), ESP_ERR_NO_MEM, MB_SLAVE_CHECK((status == pdPASS), ESP_ERR_NO_MEM,