Page 1 of 1

On which core my code is running?

Posted: Sat Jun 13, 2020 8:35 am
by MoustachedBird
Hello everyone, i'm new using ESP-IDF (FreeRTOS) and I want to know if it is possible to identify on which core my code is running?

If I understand correctly FreeRTOS uses the two cores automatically, it knows which one is free and which is not, so it runs a segment of a task (or a complete it depends how long the task is) on the available core.

I see that some users identify the core selecting it previously (something like the following code), so now I'm not sure if FreeRTOS selects the core automatically or if I have to select it manually.

Code: Select all

/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"

#define core_0 0
#define core_1 1

void hello_task_core_1(void *pvParameter)
{
	while(1){
	printf("Hello world from core %d!\n", xPortGetCoreID() );
    vTaskDelay(872 / portTICK_PERIOD_MS);
    fflush(stdout);
	} 
}

void hello_task_core_0(void *pvParameter)
{
	while(1) {
	printf("Hello world from core %d!\n", xPortGetCoreID() );
    vTaskDelay(1323 / portTICK_PERIOD_MS);
    fflush(stdout);
	}
}

void app_main()
{
    nvs_flash_init();
	xTaskCreatePinnedToCore(&hello_task_core_0, "core1_task", 1024*4, NULL, configMAX_PRIORITIES - 1, NULL, core_0);
	xTaskCreatePinnedToCore(&hello_task_core_1, "core0_task", 1024*4, NULL, configMAX_PRIORITIES - 1, NULL, core_1);
}
I mean, it's possible to do something like this?

Code: Select all

#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_system.h"
#include "nvs_flash.h"

void task1(void *pvParameter){
    while(1) {
        printf("Running task 1\n");
        printf("Hello world from core %d!\n", xPortGetCoreID() );
        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }
    vTaskDelete(NULL);
}

void task2(void *pvParameter) {
    while(1) {
        printf("Running task 2\n");
        printf("Hello world from core %d!\n", xPortGetCoreID() );
        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }
    vTaskDelete(NULL);
}

void task3(void *pvParameter) {
    while(1) {
        printf("Running task 3\n");
        printf("Hello world from core %d!\n", xPortGetCoreID() );
        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }
    vTaskDelete(NULL);
}

void task4(void *pvParameter) {
    while(1) {
        printf("Running task 4\n");
        printf("Hello world from core %d!\n", xPortGetCoreID() );
        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }
    vTaskDelete(NULL);
}

void app_main() {
     nvs_flash_init();
     xTaskCreate(&task1, "task1", 1024, NULL, 1, NULL);
     xTaskCreate(&task2, "task2", 1024, NULL, 2, NULL);
     xTaskCreate(&task3, "task3", 1024, NULL, 3, NULL);
     xTaskCreate(&task4, "task4", 1024, NULL, 4, NULL);
     
     //vTaskStartScheduler();
}


Re: On which core my code is running?

Posted: Sat Jun 13, 2020 10:24 am
by ESP_Sprite
Both are valid and have their use. Pinning to a certain core will give you more control over what tasks get executed when, which can be useful in situations where e.g. you want to have low latency. Letting tasks roam free can give you more overall CPU power, as there's a larger chance the CPUs will be actively working on something.