Skip to content

Commit fa74767

Browse files
committed
Allow selecting in IDF the running core for Arduino's core tasks
1 parent d922557 commit fa74767

File tree

6 files changed

+95
-15
lines changed

6 files changed

+95
-15
lines changed

Kconfig.projbuild

+64
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,70 @@ config AUTOSTART_ARDUINO
1919
If disabled, you can call initArduino() to run any preparations
2020
required by the framework
2121

22+
choice ARDUINO_RUNNING_CORE
23+
bool "Core on which Arduino's setup() and loop() are running"
24+
default ARDUINO_RUN_CORE1
25+
help
26+
Select on which core Arduino's setup() and loop() functions run
27+
28+
config ARDUINO_RUN_CORE0
29+
bool "CORE 0"
30+
config ARDUINO_RUN_CORE1
31+
bool "CORE 1"
32+
config ARDUINO_RUN_NO_AFFINITY
33+
bool "BOTH"
34+
35+
endchoice
36+
37+
config ARDUINO_RUNNING_CORE
38+
int
39+
default 0 if ARDUINO_RUN_CORE0
40+
default 1 if ARDUINO_RUN_CORE1
41+
default -1 if ARDUINO_RUN_NO_AFFINITY
42+
43+
choice ARDUINO_EVENT_RUNNING_CORE
44+
bool "Core on which Arduino's event handler is running"
45+
default ARDUINO_EVENT_RUN_CORE1
46+
help
47+
Select on which core Arduino's WiFi.onEvent() run
48+
49+
config ARDUINO_EVENT_RUN_CORE0
50+
bool "CORE 0"
51+
config ARDUINO_EVENT_RUN_CORE1
52+
bool "CORE 1"
53+
config ARDUINO_EVENT_RUN_NO_AFFINITY
54+
bool "BOTH"
55+
56+
endchoice
57+
58+
config ARDUINO_EVENT_RUNNING_CORE
59+
int
60+
default 0 if ARDUINO_EVENT_RUN_CORE0
61+
default 1 if ARDUINO_EVENT_RUN_CORE1
62+
default -1 if ARDUINO_EVENT_RUN_NO_AFFINITY
63+
64+
choice ARDUINO_UDP_RUNNING_CORE
65+
bool "Core on which Arduino's UDP is running"
66+
default ARDUINO_UDP_RUN_CORE1
67+
help
68+
Select on which core Arduino's UDP run
69+
70+
config ARDUINO_UDP_RUN_CORE0
71+
bool "CORE 0"
72+
config ARDUINO_UDP_RUN_CORE1
73+
bool "CORE 1"
74+
config ARDUINO_UDP_RUN_NO_AFFINITY
75+
bool "BOTH"
76+
77+
endchoice
78+
79+
config ARDUINO_UDP_RUNNING_CORE
80+
int
81+
default 0 if ARDUINO_UDP_RUN_CORE0
82+
default 1 if ARDUINO_UDP_RUN_CORE1
83+
default -1 if ARDUINO_UDP_RUN_NO_AFFINITY
84+
85+
2286
config DISABLE_HAL_LOCKS
2387
bool "Disable mutex locks for HAL"
2488
default "n"

cores/esp32/esp32-hal-misc.c

+18
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ void disableCore1WDT(){
111111
}
112112
#endif
113113

114+
BaseType_t xTaskCreateUniversal( TaskFunction_t pxTaskCode,
115+
const char * const pcName,
116+
const uint32_t usStackDepth,
117+
void * const pvParameters,
118+
UBaseType_t uxPriority,
119+
TaskHandle_t * const pxCreatedTask,
120+
const BaseType_t xCoreID ){
121+
#ifndef CONFIG_FREERTOS_UNICORE
122+
if(xCoreID >= 0 && xCoreID < 2) {
123+
return xTaskCreatePinnedToCore(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask, xCoreID);
124+
} else {
125+
#endif
126+
return xTaskCreate(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask);
127+
#ifndef CONFIG_FREERTOS_UNICORE
128+
}
129+
#endif
130+
}
131+
114132
unsigned long IRAM_ATTR micros()
115133
{
116134
return (unsigned long) (esp_timer_get_time());

cores/esp32/esp32-hal.h

+10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ void enableCore1WDT();
9090
void disableCore1WDT();
9191
#endif
9292

93+
//if xCoreID < 0 or CPU is unicore, it will use xTaskCreate, else xTaskCreatePinnedToCore
94+
//allows to easily handle all possible situations without repetitive code
95+
BaseType_t xTaskCreateUniversal( TaskFunction_t pxTaskCode,
96+
const char * const pcName,
97+
const uint32_t usStackDepth,
98+
void * const pvParameters,
99+
UBaseType_t uxPriority,
100+
TaskHandle_t * const pxCreatedTask,
101+
const BaseType_t xCoreID );
102+
93103
unsigned long micros();
94104
unsigned long millis();
95105
void delay(uint32_t);

cores/esp32/main.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ TaskHandle_t loopTaskHandle = NULL;
77

88
#if CONFIG_AUTOSTART_ARDUINO
99

10-
#if CONFIG_FREERTOS_UNICORE
11-
#define ARDUINO_RUNNING_CORE 0
12-
#else
13-
#define ARDUINO_RUNNING_CORE 1
14-
#endif
15-
1610
bool loopTaskWDTEnabled;
1711

1812
void loopTask(void *pvParameters)
@@ -30,7 +24,7 @@ extern "C" void app_main()
3024
{
3125
loopTaskWDTEnabled = false;
3226
initArduino();
33-
xTaskCreatePinnedToCore(loopTask, "loopTask", 8192, NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE);
27+
xTaskCreateUniversal(loopTask, "loopTask", 8192, NULL, 1, &loopTaskHandle, CONFIG_ARDUINO_RUNNING_CORE);
3428
}
3529

3630
#endif

libraries/AsyncUDP/src/AsyncUDP.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static bool _udp_task_start(){
150150
}
151151
}
152152
if(!_udp_task_handle){
153-
xTaskCreate(_udp_task, "async_udp", 4096, NULL, 3, (TaskHandle_t*)&_udp_task_handle);
153+
xTaskCreateUniversal(_udp_task, "async_udp", 4096, NULL, 3, (TaskHandle_t*)&_udp_task_handle, CONFIG_ARDUINO_UDP_RUNNING_CORE);
154154
if(!_udp_task_handle){
155155
return false;
156156
}

libraries/WiFi/src/WiFiGeneric.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ extern "C" {
5050

5151
#include "sdkconfig.h"
5252

53-
#if CONFIG_FREERTOS_UNICORE
54-
#define ARDUINO_RUNNING_CORE 0
55-
#else
56-
#define ARDUINO_RUNNING_CORE 1
57-
#endif
58-
5953
static xQueueHandle _network_event_queue;
6054
static TaskHandle_t _network_event_task_handle = NULL;
6155
static EventGroupHandle_t _network_event_group = NULL;
@@ -96,7 +90,7 @@ static bool _start_network_event_task(){
9690
}
9791
}
9892
if(!_network_event_task_handle){
99-
xTaskCreatePinnedToCore(_network_event_task, "network_event", 4096, NULL, ESP_TASKD_EVENT_PRIO - 1, &_network_event_task_handle, ARDUINO_RUNNING_CORE);
93+
xTaskCreateUniversal(_network_event_task, "network_event", 4096, NULL, ESP_TASKD_EVENT_PRIO - 1, &_network_event_task_handle, CONFIG_ARDUINO_EVENT_RUNNING_CORE);
10094
if(!_network_event_task_handle){
10195
log_e("Network Event Task Start Failed!");
10296
return false;

0 commit comments

Comments
 (0)