From e1374b9c7ca82dbee871abaa2ccd2974ff0a68ef Mon Sep 17 00:00:00 2001 From: aleks Date: Wed, 28 Jul 2021 10:07:17 +0200 Subject: [PATCH] freemodbus: add affinity option for modbus stack tasks --- components/freemodbus/Kconfig | 23 +++++++++++++++++++++++ components/freemodbus/port/port.h | 3 +++ components/freemodbus/port/portserial.c | 6 ++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/components/freemodbus/Kconfig b/components/freemodbus/Kconfig index 44c3f9c1a0..6ec3e1c9e8 100644 --- a/components/freemodbus/Kconfig +++ b/components/freemodbus/Kconfig @@ -33,6 +33,29 @@ menu "Modbus configuration" Modbus UART driver event task priority. The priority of Modbus controller task is equal to (CONFIG_MB_SERIAL_TASK_PRIO - 1). + choice MB_PORT_TASK_AFFINITY + prompt "Modbus task affinity" + default MB_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 MB_PORT_TASK_AFFINITY_NO_AFFINITY + bool "No affinity" + config MB_PORT_TASK_AFFINITY_CPU0 + bool "CPU0" + config MB_PORT_TASK_AFFINITY_CPU1 + bool "CPU1" + + endchoice + + config MB_PORT_TASK_AFFINITY + hex + default FREERTOS_NO_AFFINITY if MB_PORT_TASK_AFFINITY_NO_AFFINITY || FREERTOS_UNICORE + default 0x0 if MB_PORT_TASK_AFFINITY_CPU0 + default 0x1 if MB_PORT_TASK_AFFINITY_CPU1 + config MB_CONTROLLER_SLAVE_ID_SUPPORT bool "Modbus controller slave ID support" default n diff --git a/components/freemodbus/port/port.h b/components/freemodbus/port/port.h index 0d8173a654..459b395e56 100644 --- a/components/freemodbus/port/port.h +++ b/components/freemodbus/port/port.h @@ -66,6 +66,9 @@ #define EXIT_CRITICAL_SECTION( ) { vMBPortExitCritical(); \ ESP_LOGD(MB_PORT_TAG,"%s: Port exit critical", __func__); } +// The task affinity for Modbus stack tasks +#define MB_PORT_TASK_AFFINITY (CONFIG_MB_PORT_TASK_AFFINITY) + typedef char BOOL; typedef unsigned char UCHAR; diff --git a/components/freemodbus/port/portserial.c b/components/freemodbus/port/portserial.c index dee5fdd365..322b1aaa91 100644 --- a/components/freemodbus/port/portserial.c +++ b/components/freemodbus/port/portserial.c @@ -241,8 +241,10 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, "mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", (uint32_t)xErr); #endif // Create a task to handle UART events - BaseType_t xStatus = xTaskCreate(vUartTask, "uart_queue_task", MB_SERIAL_TASK_STACK_SIZE, - NULL, MB_SERIAL_TASK_PRIO, &xMbTaskHandle); + BaseType_t xStatus = xTaskCreatePinnedToCore(vUartTask, "uart_queue_task", + MB_SERIAL_TASK_STACK_SIZE, + NULL, MB_SERIAL_TASK_PRIO, + &xMbTaskHandle, MB_PORT_TASK_AFFINITY); if (xStatus != pdPASS) { vTaskDelete(xMbTaskHandle); // Force exit from function with failure