IDF-FreeRTOS xTaskGetCoreID() outputs trash value

Hello everybody!

When creating 3 tasks on ESP32:

  • void vEmulateLoad(void *pvParameter)
  • void vReceiveTask(void *pvParameter)
  • void vTransmitTask(void *pvParameter)

The first one pinned to Core 0 stops working after creating two others pinned to Core 1.
The code is presented below:

static QueueHandle_t xControlQueue = NULL;
static TaskHandle_t xReceiveTask = NULL, xTransmitTask = NULL;
static TaskHandle_t xEmulateLoad = NULL;

typedef struct {
  void *data;
  uint32_t data_len;
} message;

void vEmulateLoad(void *pvParameters) {
  message msg;

  /*SIMPLE DATA GENERATION*/
  srand(time(NULL));
  uint32_t arr_size = (uint32_t)pow(2, PACKETS_UNIQUE_NUM);
  uint8_t arr[arr_size];
  for (uint8_t i = 0; i < arr_size; ++i) {
    arr[i] = (uint8_t)rand();
  }
  msg.data = (void *)arr;
  msg.data_len = sizeof(arr);

  for (;;) {
    if (xQueueSend(xControlQueue, &msg, pdMS_TO_TICKS(100)) != pdTRUE)
      ESP_LOGE(TAG, "[FreeRTOS] xControlQueue: FULL");
    else
      ESP_LOGI(TAG, "[FreeRTOS] xControlQueue: Item added");

    vTaskDelay(pdMS_TO_TICKS(1000));
  }
  vTaskDelete(NULL);
}

void app_main(void){
BaseType_t xReturned __attribute__((unused));

  // Initialize radio
  state = radio.beginFSK(FSK_FREQ, FSK_BITRATE, FSK_FREQDEV, FSK_RXBW,
                         FSK_TXPOWER, FSK_PREAMBLE);
  assert(state == RADIOLIB_ERR_NONE && "[SX1276] Failed to begin()");
  radio.setDio0Action(ISR_opDone, RISING);
  ESP_LOGI(TAG, "[SX1276] Chip is set");

  // Initialize xControlQueue
  xControlQueue = xQueueCreate(QUEUE_SIZE, sizeof(message));
  assert(xControlQueue && "[FreeRTOS] Failed to create xControlQueue");
  ESP_LOGI(TAG, "[FreeRTOS] xControlQueue is set");

  // Initialize task emulating the load
  xReturned = xTaskCreatePinnedToCore(vEmulateLoad, "xEmulateLoad", 4096, NULL,
                                      (tskIDLE_PRIORITY + 1), &xEmulateLoad, 0);
  assert(xReturned == pdPASS && "[FreeRTOS] Failed to create xEmulateLoad");
  ESP_LOGI(TAG, "[FreeRTOS] xEmulateLoad is set");

  // Initialize RX Task
  xReturned = xTaskCreatePinnedToCore(vReceiveTask, "xReceiveTask", 4096, NULL,
                                      (tskIDLE_PRIORITY + 1), &xReceiveTask, 1);
  assert(xReturned == pdPASS && "[FreeRTOS] Failed to create xReceiveTask");
  ESP_LOGI(TAG, "[FreeRTOS] xReceiveTask is set");

  // Initialize TX Task
  xReturned =
      xTaskCreatePinnedToCore(vTransmitTask, "xTransmitTask", 4096, NULL,
                              (tskIDLE_PRIORITY + 1), &xTransmitTask, 1);
  assert(xReturned == pdPASS && "[FreeRTOS] Failed to create xTransmitTask");
  ESP_LOGI(TAG, "[FreeRTOS] xTransmitTask is set");

  printf("Core %d: xReceiveTask\n", xTaskGetCoreID(xReceiveTask));
  printf("Core %d: xTransmitTask\n", xTaskGetCoreID(xTransmitTask));
  printf("Core %d: xEmulateLoad\n", xTaskGetCoreID(xEmulateLoad));
}

As you can see, vEmulateLoad() simply puts an item in the xControlQueue.
I can’t show the code of vReceiveTask() and vTransmitTask() but I can say that they’re responsible for half-duplex radiocommunication and after creation, they both go in Blocked State.

  • vReceiveTask() is in Blocked State until Notification from ISR;
  • vTransmitTask() is in Blocked State until Notification from vReceiveTask().

The result is given below:
Снимок экрана 2024-04-18 183256

I suppose I somehow broke some memory access rules/logic but I am totally confused, and any clue would be a relief.
Any suggestions on the source of the problem?

If I pin all 3 tasks on the same core (Core 1, for example) the following outputs:

It seems like some memory corruption. Can you try to disable parts of your application to narrow down the problematic part?

Hello!

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.

Thank you for reporting back!