One of the tasks attached to a core doesn't run

JosephWheel
Posts: 4
Joined: Sat Feb 03, 2024 12:12 am

One of the tasks attached to a core doesn't run

Postby JosephWheel » Sat Feb 03, 2024 12:20 am

Hey everyone! I have an ESP32-S3 (attached to a Sunton display) and I want it to perform two tasks: displaying video that an ESP32-CAM transmits and blinking an LED (below is the main code):
  1. #include <Arduino.h>
  2.  
  3. #include "tasks.hpp"
  4.  
  5. TaskHandle_t video;
  6. TaskHandle_t led;
  7.  
  8. void setup() {
  9.     Serial.begin(115200);
  10.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  11.  
  12.     Serial.println();
  13.     Serial.println("---SICABS Indoor---");
  14.     Serial.println();
  15.  
  16.     xTaskCreatePinnedToCore(videoProjection, "Video projection", 200000, NULL, 1, &video, 1);
  17.     xTaskCreatePinnedToCore(blinkLED, "blink LED", 200000, NULL, 1, &led, 0);
  18. }
  19.  
  20. void loop() {
  21.     vTaskDelete(NULL);
  22. }
The task for displaying video looks like this:

  1. void videoProjection(void* parameter) {
  2.     gfx.init();
  3.     gfx.setRotation(0);
  4.     gfx.setColorDepth(16);
  5.     gfx.setBrightness(127);
  6.     gfx.fillScreen(TFT_BLACK);
  7.     gfx.setSwapBytes(true);
  8.  
  9.     gfx.setFont(&fonts::lgfxJapanGothic_16);
  10.     gfx.drawString("SICABS Indoor", 0, 0);
  11.  
  12.     // JPEG decoder
  13.     TJpgDec.setJpgScale(1);
  14.     TJpgDec.setCallback(tft_output);
  15.  
  16.     // WiFi
  17.     WiFi.softAP(wifi_ssid, wifi_password);
  18.     IPAddress IP = WiFi.softAPIP();
  19.  
  20.     // WebSockets
  21.     server.listen(SERVER_PORT);
  22.  
  23.     Serial.print("Video task running on core ");
  24.     Serial.println(xPortGetCoreID());
  25.     for (;;) {
  26.         if (server.poll()) {
  27.             // if (!client.available()) {  // Always overwrite the client to the new one
  28.             client = server.accept();
  29.             client.onMessage(onMessageCallback);
  30.             client.onEvent(onEventsCallback);
  31.             client.send("Hello Client!");
  32.             // }
  33.         }
  34.  
  35.         if (client.available()) {
  36.             client.poll();
  37.         }
  38.     }
  39. }
And the task for blinking an LED looks like this:
  1. void blinkLED(void *parameter) {
  2.     int time = 1000;
  3.     pinMode(led_pin, OUTPUT);
  4.     Serial.print("Blinking LED task running on core ");
  5.     Serial.println(xPortGetCoreID());
  6.     for (;;) {
  7.         digitalWrite(led_pin, HIGH);
  8.         vTaskDelay(time / portTICK_PERIOD_MS);
  9.         digitalWrite(led_pin, LOW);
  10.         vTaskDelay(time / portTICK_PERIOD_MS);
  11.     }
  12. }
Something I don't get is that the first declared task in the main code is the one that runs (no matter the priority or the core), which means that if I declare the video transmission task first, that only task runs. But if I declare the blinking LED first, that only task runs.

I've already tried by changing their running core, stack size and even running them both on the same core and deleting the setup() and loop() functions.

Any idea why this happens? Thanks in advance :cry:

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: One of the tasks attached to a core doesn't run

Postby ESP_Sprite » Sat Feb 03, 2024 8:30 am

You request a total of 400K of stack space for the tasks. Likely you don't have that much internal memory available.

MicroController
Posts: 1702
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: One of the tasks attached to a core doesn't run

Postby MicroController » Sat Feb 03, 2024 11:30 am

Note that xTaskCreate tells you whether or not it was able to actually create the task via its return value:
If the task was created successfully then pdPASS is returned. Otherwise errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY is returned.
https://www.freertos.org/a00125.html

JosephWheel
Posts: 4
Joined: Sat Feb 03, 2024 12:12 am

Re: One of the tasks attached to a core doesn't run

Postby JosephWheel » Sat Feb 03, 2024 4:10 pm

ESP_Sprite wrote:
Sat Feb 03, 2024 8:30 am
You request a total of 400K of stack space for the tasks. Likely you don't have that much internal memory available.
It seems to be true, I allocated 100000 bytes each and it worked. :)

JosephWheel
Posts: 4
Joined: Sat Feb 03, 2024 12:12 am

Re: One of the tasks attached to a core doesn't run

Postby JosephWheel » Sat Feb 03, 2024 4:30 pm

MicroController wrote:
Sat Feb 03, 2024 11:30 am
Note that xTaskCreate tells you whether or not it was able to actually create the task via its return value:
If the task was created successfully then pdPASS is returned. Otherwise errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY is returned.
https://www.freertos.org/a00125.html
Thank you! I changed the setup() function like this:
  1.     if (xTaskCreatePinnedToCore(videoProjection, "Video projection", 200000, NULL, 2, &video, 1) != pdPASS) {
  2.         Serial.println("video not allocated :(");
  3.     }
  4.  
  5.     if (xTaskCreatePinnedToCore(blinkLED, "blink LED", 200000, NULL, 1, &led, 0) != pdPASS) {
  6.         Serial.println("LED blinking not allocated :(");
  7.     }
And I got the following output:
LED blinking not allocated :(
[ 1338][esp32-hal-i2c.c:75] i2cInit(): Initialising I2C Master: sda=19 scl=20 freq=100000
[ 1340][esp32-hal-i2c.c:75] i2cInit(): Initialising I2C Master: sda=19 scl=20 freq=100000
Video task running on core 1


So I allocated 100k for each task and I got both running :)

MicroController
Posts: 1702
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: One of the tasks attached to a core doesn't run

Postby MicroController » Sun Feb 04, 2024 3:21 am

100kb of stack per task is way too much. 4-16kb per task is sufficient for almost all tasks.

Who is online

Users browsing this forum: aygh4266 and 153 guests