From 5bcb7e26d75982fe97497197aac916850dddc7f2 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 17 Sep 2018 12:23:09 +0800 Subject: [PATCH 1/2] freertos: add Kconfig FREERTOS_NO_AFFINITY constant Use in place of tskNO_AFFINITY in Kconfig files --- components/freertos/Kconfig | 6 ++++++ components/freertos/include/freertos/task.h | 3 +++ components/freertos/port.c | 2 ++ 3 files changed, 11 insertions(+) diff --git a/components/freertos/Kconfig b/components/freertos/Kconfig index 16c7c4bc87..683ea02ac0 100644 --- a/components/freertos/Kconfig +++ b/components/freertos/Kconfig @@ -10,6 +10,12 @@ config FREERTOS_UNICORE This is needed when e.g. another process needs complete control over the second core. +# This invisible config value sets the value of tskNO_AFFINITY in task.h. +# Intended to be used as a constant from other Kconfig files. +# Value is (32-bit) INT_MAX. +config FREERTOS_NO_AFFINITY + hex + default 0x7FFFFFFF choice FREERTOS_CORETIMER prompt "Xtensa timer to use as the FreeRTOS tick source" diff --git a/components/freertos/include/freertos/task.h b/components/freertos/include/freertos/task.h index 31df0bdd10..441ed90816 100644 --- a/components/freertos/include/freertos/task.h +++ b/components/freertos/include/freertos/task.h @@ -93,6 +93,9 @@ extern "C" { #define tskKERNEL_VERSION_MINOR 2 #define tskKERNEL_VERSION_BUILD 0 +/** + * @brief Argument of xTaskCreatePinnedToCore indicating that task has no affinity + */ #define tskNO_AFFINITY INT_MAX /** diff --git a/components/freertos/port.c b/components/freertos/port.c index 98fe13d364..4c99b1936b 100644 --- a/components/freertos/port.c +++ b/components/freertos/port.c @@ -123,6 +123,8 @@ extern void _xt_coproc_init(void); #define SYSTICK_INTR_ID (ETS_INTERNAL_TIMER1_INTR_SOURCE+ETS_INTERNAL_INTR_SOURCE_OFF) #endif +_Static_assert(tskNO_AFFINITY == CONFIG_FREERTOS_NO_AFFINITY, "incorrect tskNO_AFFINITY value"); + /*-----------------------------------------------------------*/ unsigned port_xSchedulerRunning[portNUM_PROCESSORS] = {0}; // Duplicate of inaccessible xSchedulerRunning; needed at startup to avoid counting nesting From 5d1ccb950191ebcdfccd06fe69cabdc6b9447ffe Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 1 Aug 2018 12:33:43 +0300 Subject: [PATCH 2/2] lwip: allow setting LwIP tasks affinity via sdkconfig In some cases applications need to ensure that WiFi/BT related tasks run on CPU1. This option can be used to set task affinity in such case. https://github.com/espressif/esp-idf/issues/2233#issuecomment-409220381 --- components/lwip/Kconfig | 25 +++++++++++++++++++ .../lwip/port/esp32/freertos/sys_arch.c | 11 ++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index f8802d1177..f4b0483516 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -443,6 +443,31 @@ config TCPIP_TASK_STACK_SIZE Configure TCP/IP task stack size, used by LWIP to process multi-threaded TCP/IP operations. Setting this stack too small will result in stack overflow crashes. +choice TCPIP_TASK_AFFINITY + prompt "TCP/IP task affinity" + default TCPIP_TASK_AFFINITY_NO_AFFINITY + help + Allows setting LwIP tasks affinity, i.e. whether the task is pinned to + CPU0, pinned to CPU1, or allowed to run on any CPU. + Currently this applies to "TCP/IP" task and "Ping" task. + +config TCPIP_TASK_AFFINITY_NO_AFFINITY + bool "No affinity" +config TCPIP_TASK_AFFINITY_CPU0 + bool "CPU0" +config TCPIP_TASK_AFFINITY_CPU1 + bool "CPU1" + depends on !FREERTOS_UNICORE + +endchoice + +config TCPIP_TASK_AFFINITY + hex + default FREERTOS_NO_AFFINITY if TCPIP_TASK_AFFINITY_NO_AFFINITY + default 0x0 if TCPIP_TASK_AFFINITY_CPU0 + default 0x1 if TCPIP_TASK_AFFINITY_CPU1 + + menuconfig PPP_SUPPORT bool "Enable PPP support (new/experimental)" default n diff --git a/components/lwip/port/esp32/freertos/sys_arch.c b/components/lwip/port/esp32/freertos/sys_arch.c index 2c56969ade..07830c35a8 100644 --- a/components/lwip/port/esp32/freertos/sys_arch.c +++ b/components/lwip/port/esp32/freertos/sys_arch.c @@ -426,16 +426,17 @@ sys_mbox_free(sys_mbox_t *mbox) sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio) { - xTaskHandle CreatedTask; + xTaskHandle created_task; portBASE_TYPE result; - result = xTaskCreate(thread, name, stacksize, arg, prio, &CreatedTask); + result = xTaskCreatePinnedToCore(thread, name, stacksize, arg, prio, &created_task, + CONFIG_TCPIP_TASK_AFFINITY); - if (result == pdPASS) { - return CreatedTask; - } else { + if (result != pdPASS) { return NULL; } + + return created_task; } /*-----------------------------------------------------------------------------------*/