From fa74767b2e4b4808439f81b97cb454e1118621f8 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Sat, 13 Apr 2019 17:13:13 +0200 Subject: [PATCH] Allow selecting in IDF the running core for Arduino's core tasks --- Kconfig.projbuild | 64 +++++++++++++++++++++++++++++ cores/esp32/esp32-hal-misc.c | 18 ++++++++ cores/esp32/esp32-hal.h | 10 +++++ cores/esp32/main.cpp | 8 +--- libraries/AsyncUDP/src/AsyncUDP.cpp | 2 +- libraries/WiFi/src/WiFiGeneric.cpp | 8 +--- 6 files changed, 95 insertions(+), 15 deletions(-) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index d97876d24..97f41979c 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -19,6 +19,70 @@ config AUTOSTART_ARDUINO If disabled, you can call initArduino() to run any preparations required by the framework +choice ARDUINO_RUNNING_CORE + bool "Core on which Arduino's setup() and loop() are running" + default ARDUINO_RUN_CORE1 + help + Select on which core Arduino's setup() and loop() functions run + + config ARDUINO_RUN_CORE0 + bool "CORE 0" + config ARDUINO_RUN_CORE1 + bool "CORE 1" + config ARDUINO_RUN_NO_AFFINITY + bool "BOTH" + +endchoice + +config ARDUINO_RUNNING_CORE + int + default 0 if ARDUINO_RUN_CORE0 + default 1 if ARDUINO_RUN_CORE1 + default -1 if ARDUINO_RUN_NO_AFFINITY + +choice ARDUINO_EVENT_RUNNING_CORE + bool "Core on which Arduino's event handler is running" + default ARDUINO_EVENT_RUN_CORE1 + help + Select on which core Arduino's WiFi.onEvent() run + + config ARDUINO_EVENT_RUN_CORE0 + bool "CORE 0" + config ARDUINO_EVENT_RUN_CORE1 + bool "CORE 1" + config ARDUINO_EVENT_RUN_NO_AFFINITY + bool "BOTH" + +endchoice + +config ARDUINO_EVENT_RUNNING_CORE + int + default 0 if ARDUINO_EVENT_RUN_CORE0 + default 1 if ARDUINO_EVENT_RUN_CORE1 + default -1 if ARDUINO_EVENT_RUN_NO_AFFINITY + +choice ARDUINO_UDP_RUNNING_CORE + bool "Core on which Arduino's UDP is running" + default ARDUINO_UDP_RUN_CORE1 + help + Select on which core Arduino's UDP run + + config ARDUINO_UDP_RUN_CORE0 + bool "CORE 0" + config ARDUINO_UDP_RUN_CORE1 + bool "CORE 1" + config ARDUINO_UDP_RUN_NO_AFFINITY + bool "BOTH" + +endchoice + +config ARDUINO_UDP_RUNNING_CORE + int + default 0 if ARDUINO_UDP_RUN_CORE0 + default 1 if ARDUINO_UDP_RUN_CORE1 + default -1 if ARDUINO_UDP_RUN_NO_AFFINITY + + config DISABLE_HAL_LOCKS bool "Disable mutex locks for HAL" default "n" diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c index f3175c9d0..ae06edc33 100644 --- a/cores/esp32/esp32-hal-misc.c +++ b/cores/esp32/esp32-hal-misc.c @@ -111,6 +111,24 @@ void disableCore1WDT(){ } #endif +BaseType_t xTaskCreateUniversal( TaskFunction_t pxTaskCode, + const char * const pcName, + const uint32_t usStackDepth, + void * const pvParameters, + UBaseType_t uxPriority, + TaskHandle_t * const pxCreatedTask, + const BaseType_t xCoreID ){ +#ifndef CONFIG_FREERTOS_UNICORE + if(xCoreID >= 0 && xCoreID < 2) { + return xTaskCreatePinnedToCore(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask, xCoreID); + } else { +#endif + return xTaskCreate(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask); +#ifndef CONFIG_FREERTOS_UNICORE + } +#endif +} + unsigned long IRAM_ATTR micros() { return (unsigned long) (esp_timer_get_time()); diff --git a/cores/esp32/esp32-hal.h b/cores/esp32/esp32-hal.h index ef87a0441..4b5fb2c2e 100644 --- a/cores/esp32/esp32-hal.h +++ b/cores/esp32/esp32-hal.h @@ -90,6 +90,16 @@ void enableCore1WDT(); void disableCore1WDT(); #endif +//if xCoreID < 0 or CPU is unicore, it will use xTaskCreate, else xTaskCreatePinnedToCore +//allows to easily handle all possible situations without repetitive code +BaseType_t xTaskCreateUniversal( TaskFunction_t pxTaskCode, + const char * const pcName, + const uint32_t usStackDepth, + void * const pvParameters, + UBaseType_t uxPriority, + TaskHandle_t * const pxCreatedTask, + const BaseType_t xCoreID ); + unsigned long micros(); unsigned long millis(); void delay(uint32_t); diff --git a/cores/esp32/main.cpp b/cores/esp32/main.cpp index edba0e7d4..3a455c8a8 100644 --- a/cores/esp32/main.cpp +++ b/cores/esp32/main.cpp @@ -7,12 +7,6 @@ TaskHandle_t loopTaskHandle = NULL; #if CONFIG_AUTOSTART_ARDUINO -#if CONFIG_FREERTOS_UNICORE -#define ARDUINO_RUNNING_CORE 0 -#else -#define ARDUINO_RUNNING_CORE 1 -#endif - bool loopTaskWDTEnabled; void loopTask(void *pvParameters) @@ -30,7 +24,7 @@ extern "C" void app_main() { loopTaskWDTEnabled = false; initArduino(); - xTaskCreatePinnedToCore(loopTask, "loopTask", 8192, NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE); + xTaskCreateUniversal(loopTask, "loopTask", 8192, NULL, 1, &loopTaskHandle, CONFIG_ARDUINO_RUNNING_CORE); } #endif diff --git a/libraries/AsyncUDP/src/AsyncUDP.cpp b/libraries/AsyncUDP/src/AsyncUDP.cpp index 481860511..63ccc114d 100644 --- a/libraries/AsyncUDP/src/AsyncUDP.cpp +++ b/libraries/AsyncUDP/src/AsyncUDP.cpp @@ -150,7 +150,7 @@ static bool _udp_task_start(){ } } if(!_udp_task_handle){ - xTaskCreate(_udp_task, "async_udp", 4096, NULL, 3, (TaskHandle_t*)&_udp_task_handle); + xTaskCreateUniversal(_udp_task, "async_udp", 4096, NULL, 3, (TaskHandle_t*)&_udp_task_handle, CONFIG_ARDUINO_UDP_RUNNING_CORE); if(!_udp_task_handle){ return false; } diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index b0468e256..9da2861ca 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -50,12 +50,6 @@ extern "C" { #include "sdkconfig.h" -#if CONFIG_FREERTOS_UNICORE -#define ARDUINO_RUNNING_CORE 0 -#else -#define ARDUINO_RUNNING_CORE 1 -#endif - static xQueueHandle _network_event_queue; static TaskHandle_t _network_event_task_handle = NULL; static EventGroupHandle_t _network_event_group = NULL; @@ -96,7 +90,7 @@ static bool _start_network_event_task(){ } } if(!_network_event_task_handle){ - xTaskCreatePinnedToCore(_network_event_task, "network_event", 4096, NULL, ESP_TASKD_EVENT_PRIO - 1, &_network_event_task_handle, ARDUINO_RUNNING_CORE); + xTaskCreateUniversal(_network_event_task, "network_event", 4096, NULL, ESP_TASKD_EVENT_PRIO - 1, &_network_event_task_handle, CONFIG_ARDUINO_EVENT_RUNNING_CORE); if(!_network_event_task_handle){ log_e("Network Event Task Start Failed!"); return false; -- GitLab