diff --git a/components/freemodbus/Kconfig b/components/freemodbus/Kconfig index 226ba32f2f..df77036112 100644 --- a/components/freemodbus/Kconfig +++ b/components/freemodbus/Kconfig @@ -51,6 +51,29 @@ menu "Modbus configuration" Modbus UART driver event task priority. The priority of Modbus controller task is equal to (CONFIG_FMB_SERIAL_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 bool "Modbus controller slave ID support" default n diff --git a/components/freemodbus/port/port.h b/components/freemodbus/port/port.h index f53863f9a3..2405ce5452 100644 --- a/components/freemodbus/port/port.h +++ b/components/freemodbus/port/port.h @@ -28,6 +28,7 @@ #define MB_PORT_TAG "MB_PORT_COMMON" +#define MB_PORT_TASK_AFFINITY (CONFIG_FMB_PORT_TASK_AFFINITY) #define MB_PORT_HAS_CLOSE (1) // Define to explicitly close port on destroy #define MB_PORT_CHECK(a, ret_val, str, ...) \ diff --git a/components/freemodbus/port/portserial.c b/components/freemodbus/port/portserial.c index 2f3029bda7..3a90e2278c 100644 --- a/components/freemodbus/port/portserial.c +++ b/components/freemodbus/port/portserial.c @@ -258,8 +258,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 diff --git a/components/freemodbus/port/portserial_m.c b/components/freemodbus/port/portserial_m.c index 9a5d85ba9e..7fb7d0f47f 100644 --- a/components/freemodbus/port/portserial_m.c +++ b/components/freemodbus/port/portserial_m.c @@ -251,8 +251,10 @@ BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, MB_PORT_CHECK((xErr == ESP_OK), FALSE, "mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", (uint32_t)xErr); // 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 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 ecd6435f03..623cbdedd2 100644 --- a/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c +++ b/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c @@ -738,12 +738,13 @@ esp_err_t mbc_serial_master_create(mb_port_type_t port_type, void** handler) MB_MASTER_CHECK((mbm_opts->mbm_event_group != NULL), ESP_ERR_NO_MEM, "mb event group error."); // Create modbus controller task - status = xTaskCreate((void*)&modbus_master_task, + status = xTaskCreatePinnedToCore((void*)&modbus_master_task, "modbus_matask", MB_CONTROLLER_STACK_SIZE, NULL, // No parameters MB_CONTROLLER_PRIORITY, - &mbm_opts->mbm_task_handle); + &mbm_opts->mbm_task_handle, + MB_PORT_TASK_AFFINITY); if (status != pdPASS) { vTaskDelete(mbm_opts->mbm_task_handle); MB_MASTER_CHECK((status == pdPASS), ESP_ERR_NO_MEM, 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 61682d63f9..e79771ec5f 100644 --- a/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c +++ b/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c @@ -481,12 +481,13 @@ esp_err_t mbc_serial_slave_create(mb_port_type_t port_type, void** handler) MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL), ESP_ERR_NO_MEM, "mb notify queue creation error."); // Create Modbus controller task - status = xTaskCreate((void*)&modbus_slave_task, + status = xTaskCreatePinnedToCore((void*)&modbus_slave_task, "modbus_slave_task", MB_CONTROLLER_STACK_SIZE, NULL, MB_CONTROLLER_PRIORITY, - &mbs_opts->mbs_task_handle); + &mbs_opts->mbs_task_handle, + MB_PORT_TASK_AFFINITY); if (status != pdPASS) { vTaskDelete(mbs_opts->mbs_task_handle); MB_SLAVE_CHECK((status == pdPASS), ESP_ERR_NO_MEM,