# IDF-FreeRTOS xTaskGetCoreID() outputs trash value

**URL:** <https://forums.freertos.org/t/idf-freertos-xtaskgetcoreid-outputs-trash-value/19833>\
**Category:** Espressif\
**Tags:** debug\
**Created:** [April 18, 2024, 2:50pm UTC](https://forums.freertos.org/t/idf-freertos-xtaskgetcoreid-outputs-trash-value/19833 "2024-04-18T14:50:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bushpulbek](https://avatars.discourse-cdn.com/v4/letter/b/34f0e0/32.png) [@bushpulbek](https://forums.freertos.org/u/bushpulbek)\
**Post date:** [April 18, 2024, 2:50pm UTC](https://forums.freertos.org/t/idf-freertos-xtaskgetcoreid-outputs-trash-value/19833/1 "2024-04-18T14:50:03Z")

</div>

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:

```auto
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](https://d2v6vdsk2p900z.cloudfront.net/original/2X/7/77031ee9c9ba06e7f7f77c33323cf09b57d89185.png)

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?

---

<div class="post-metadata">

**Author:** ![bushpulbek](https://avatars.discourse-cdn.com/v4/letter/b/34f0e0/32.png) [@bushpulbek](https://forums.freertos.org/u/bushpulbek)\
**Post date:** [April 18, 2024, 2:50pm UTC](https://forums.freertos.org/t/idf-freertos-xtaskgetcoreid-outputs-trash-value/19833/2 "2024-04-18T14:50:59Z")

</div>

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

 ![Снимок экрана 2024-04-18 185029](https://d2v6vdsk2p900z.cloudfront.net/original/2X/7/7a84127da04b783d4fa69e4988d875b5ea553c28.png)

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 19, 2024, 7:17am UTC](https://forums.freertos.org/t/idf-freertos-xtaskgetcoreid-outputs-trash-value/19833/3 "2024-04-19T07:17:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bushpulbek](https://avatars.discourse-cdn.com/v4/letter/b/34f0e0/32.png) [@bushpulbek](https://forums.freertos.org/u/bushpulbek)\
**Post date:** [April 19, 2024, 9:25am UTC](https://forums.freertos.org/t/idf-freertos-xtaskgetcoreid-outputs-trash-value/19833/4 "2024-04-19T09:25:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 19, 2024, 9:27am UTC](https://forums.freertos.org/t/idf-freertos-xtaskgetcoreid-outputs-trash-value/19833/5 "2024-04-19T09:27:04Z")

</div>

Thank you for reporting back!
