Page 1 of 1

xTaskGetCoreID() outputs trash

Posted: Thu Apr 18, 2024 1:55 pm
by bushpulbek
Hello, everybody!

When creating 3 tasks:
  • void vReceiveTask(void *pvParameters)
  • void vTransmitTask(void *pvParameters)
  • void vEmulateLoad(void *pvParameters)
The last one pinned to Core 0 stops working after two other pinned to Core 1 are created and started running. The piece of code for creating those is given below:
  1. static QueueHandle_t xControlQueue = NULL;
  2. static TaskHandle_t xReceiveTask = NULL, xTransmitTask = NULL;
  3. static TaskHandle_t xEmulateLoad = NULL;
  4.  
  5. typedef struct {
  6.   void *data;
  7.   uint32_t data_len;
  8. } message;
  9.  
  10. void vEmulateLoad(void *pvParameters) {
  11.   message msg;
  12.  
  13.   /*SIMPLE DATA GENERATION*/
  14.   srand(time(NULL));
  15.   uint32_t arr_size = (uint32_t)pow(2, PACKETS_UNIQUE_NUM);
  16.   uint8_t arr[arr_size];
  17.   for (uint8_t i = 0; i < arr_size; ++i) {
  18.     arr[i] = (uint8_t)rand();
  19.   }
  20.   msg.data = (void *)arr;
  21.   msg.data_len = sizeof(arr);
  22.  
  23.   for (;;) {
  24.     if (xQueueSend(xControlQueue, &msg, pdMS_TO_TICKS(100)) != pdTRUE)
  25.       ESP_LOGE(TAG, "[FreeRTOS] xControlQueue: FULL");
  26.     else
  27.       ESP_LOGI(TAG, "[FreeRTOS] xControlQueue: Item added");
  28.  
  29.     vTaskDelay(pdMS_TO_TICKS(1000));
  30.   }
  31.   vTaskDelete(NULL);
  32. }
  33.  
  34. void app_main(void){
  35.  BaseType_t xReturned __attribute__((unused));
  36.  
  37. // Initialize xControlQueue
  38.   xControlQueue = xQueueCreate(QUEUE_SIZE, sizeof(message));
  39.   assert(xControlQueue && "[FreeRTOS] Failed to create xControlQueue");
  40.   ESP_LOGI(TAG, "[FreeRTOS] xControlQueue is set");
  41.  
  42.   // Initialize task emulating the load
  43.   xReturned = xTaskCreatePinnedToCore(vEmulateLoad, "xEmulateLoad", 4096, NULL,
  44.                                       (tskIDLE_PRIORITY + 1), &xEmulateLoad, 0);
  45.   assert(xReturned == pdPASS && "[FreeRTOS] Failed to create xEmulateLoad");
  46.   ESP_LOGI(TAG, "[FreeRTOS] xEmulateLoad is set");
  47.  
  48.   // Initialize RX Task
  49.   xReturned = xTaskCreatePinnedToCore(vReceiveTask, "xReceiveTask", 4096, NULL,
  50.                                       (tskIDLE_PRIORITY + 1), &xReceiveTask, 1);
  51.   assert(xReturned == pdPASS && "[FreeRTOS] Failed to create xReceiveTask");
  52.   ESP_LOGI(TAG, "[FreeRTOS] xReceiveTask is set");
  53.  
  54.   // Initialize TX Task
  55.   xReturned =
  56.       xTaskCreatePinnedToCore(vTransmitTask, "xTransmitTask", 4096, NULL,
  57.                               (tskIDLE_PRIORITY + 1), &xTransmitTask, 1);
  58.   assert(xReturned == pdPASS && "[FreeRTOS] Failed to create xTransmitTask");
  59.   ESP_LOGI(TAG, "[FreeRTOS] xTransmitTask is set");
  60.  
  61.   printf("Core %d: xReceiveTask\n", xTaskGetCoreID(xReceiveTask));
  62.   printf("Core %d: xTransmitTask\n", xTaskGetCoreID(xTransmitTask));
  63.   printf("Core %d: xEmulateLoad\n", xTaskGetCoreID(xEmulateLoad));
  64. }
As you can see vEmulateLoad() just puts items into the xControlQueue.
Regarding 2 other tasks: I can't share their code but I can say that they're responsible for half-duplex radiocommunication and both of them go to the blocked state after creation:
  • vReceiveTask() is in Blocked state until a Notification comes from ISR
  • vTransmitTask() is in Blocked state until a Notification comes from vReceiveTask()
The output in the monitor is the following:
Снимок экрана 2024-04-18 183256.png
Снимок экрана 2024-04-18 183256.png (19.98 KiB) Viewed 304 times
When trying to pin all three tasks on the same core (Core 1, for example) I get the following error:
Снимок экрана 2024-04-18 185029.png
Снимок экрана 2024-04-18 185029.png (62.33 KiB) Viewed 304 times
Despite me being proud of breaking FreeRTOS and my nervous system (XD), solving such an issue would be a great relief.
Any suggestions where I could make a mistake?

Re: xTaskGetCoreID() outputs trash

Posted: Fri Apr 19, 2024 9:26 am
by bushpulbek
Hello, everybody!

Yes, indeed, as it was suggested the fault was memory-related. The stack allocated to one of the tasks got overflowed during the runtime and, thus, couldn’t be detected either by assert or compiler.

Therefore, the solution was to increase the number of bytes allocated for the task at the stage of creation.