ESP32 FreeRTOS vTaskSuspend fails with print statements
Posted: Sat Dec 24, 2022 11:16 pm
Have only been a few days into FreeRTOS on Arduino ESP32, so please bear with me :)
I encountered a strange behavior when trying out a simple FreeRTOS code running on a ESP32 WROVER module. At the start global variable count initiates at 1. The two tasks are:
- red: increments the global variable count every 1 s, and toggles a red LED every 0.5 s. It is pinned to core 0. Note that there's a print statement printing the count value.
- green: suspends the red tasks once count is greater than 5, and toggles a green LED every 1 s. It is pinned to core 1.
Very strangly, with the print statement in green (line 51) commented out, vTaskSuspend(redHandle) does not suspend the red task as expected once count is over 5. Both LEDs keep toggling and the count continues to increment. It seems that only when the count is printed out with line 51 in the green task will red be actually suspended.
I'm not sure if this is a bug, a multicore issue, or a collision deep under between Serial and FreeRTOS. Can someone explain?
Full sketch as follows:
I encountered a strange behavior when trying out a simple FreeRTOS code running on a ESP32 WROVER module. At the start global variable count initiates at 1. The two tasks are:
- red: increments the global variable count every 1 s, and toggles a red LED every 0.5 s. It is pinned to core 0. Note that there's a print statement printing the count value.
- green: suspends the red tasks once count is greater than 5, and toggles a green LED every 1 s. It is pinned to core 1.
Very strangly, with the print statement in green (line 51) commented out, vTaskSuspend(redHandle) does not suspend the red task as expected once count is over 5. Both LEDs keep toggling and the count continues to increment. It seems that only when the count is printed out with line 51 in the green task will red be actually suspended.
I'm not sure if this is a bug, a multicore issue, or a collision deep under between Serial and FreeRTOS. Can someone explain?
Full sketch as follows:
- int count = 1;
- TaskHandle_t redHandle;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- xTaskCreatePinnedToCore(
- red
- , "red"
- , 1024
- , NULL
- , 1
- , &redHandle
- , 0);
- xTaskCreatePinnedToCore(
- green
- , "green"
- , 1024
- , NULL
- , 1
- , NULL
- , 1);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- }
- void red(void* param) {
- pinMode(13, OUTPUT);
- while (true) {
- count++;
- Serial.println(count);
- digitalWrite(13, HIGH);
- vTaskDelay(500 / portTICK_PERIOD_MS);
- digitalWrite(13, LOW);
- vTaskDelay(500 / portTICK_PERIOD_MS);
- }
- }
- void green(void* param) {
- pinMode(27, OUTPUT);
- while (true) {
- // Serial.println(count);
- if (count > 5) {
- vTaskSuspend(redHandle);
- }
- digitalWrite(27, HIGH);
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- digitalWrite(27, LOW);
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- }
- }