ESP32 FreeRTOS vTaskSuspend fails with print statements

nalagind
Posts: 1
Joined: Sat Dec 24, 2022 10:35 pm

ESP32 FreeRTOS vTaskSuspend fails with print statements

Postby nalagind » 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:
  1. int count = 1;
  2.  
  3. TaskHandle_t redHandle;
  4.  
  5. void setup() {
  6.   // put your setup code here, to run once:
  7.   Serial.begin(115200);
  8.  
  9.   xTaskCreatePinnedToCore(
  10.     red
  11.     , "red"
  12.     , 1024
  13.     , NULL
  14.     , 1
  15.     , &redHandle
  16.     , 0);
  17.  
  18.   xTaskCreatePinnedToCore(
  19.     green
  20.     , "green"
  21.     , 1024
  22.     , NULL
  23.     , 1
  24.     , NULL
  25.     , 1);
  26. }
  27.  
  28. void loop() {
  29.   // put your main code here, to run repeatedly:
  30.  
  31. }
  32.  
  33. void red(void* param) {
  34.   pinMode(13, OUTPUT);
  35.  
  36.   while (true) {
  37.     count++;
  38.     Serial.println(count);
  39.  
  40.     digitalWrite(13, HIGH);
  41.     vTaskDelay(500 / portTICK_PERIOD_MS);
  42.     digitalWrite(13, LOW);
  43.     vTaskDelay(500 / portTICK_PERIOD_MS);
  44.   }
  45. }
  46.  
  47. void green(void* param) {
  48.   pinMode(27, OUTPUT);
  49.  
  50.   while (true) {
  51.     // Serial.println(count);
  52.     if (count > 5) {
  53.       vTaskSuspend(redHandle);
  54.     }
  55.    
  56.     digitalWrite(27, HIGH);
  57.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  58.     digitalWrite(27, LOW);
  59.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  60.   }
  61. }

savage
Posts: 23
Joined: Mon Jul 15, 2019 9:24 pm

Re: ESP32 FreeRTOS vTaskSuspend fails with print statements

Postby savage » Wed Dec 28, 2022 3:44 pm

Does it help if you mark the global variable as volatile?

Code: Select all

volatile int count = 1;

Who is online

Users browsing this forum: Baidu [Spider] and 74 guests