This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to create two task with different task priority?

SDK version: 14.2.0



I use "ble_app_hrs_freertos" as code base and modify it to create a "ble_app_template_freertos" project.

"softdevice_task" is already in this project (uxPriority = 2).

I can use "xTaskCreate" to create a new task "task1" and the uxPriority is 1, and work normally.

And I modify "softdevice_taskuxPriority to 5, and "task1" uxPriority is 1 it works normally too.

But when I add a new task "task2" and the uxPriority set to 2, the system not work.

And I found when this project only have "softdevice_task"(uxPriority = 5) and "task1"(uxPriority = 2), it doesn't work too.

How to solve this issue?

The code config below doesn't work.

int main(void)
{
    bool erase_bonds;

    clock_init();
    
    if (pdPASS != xTaskCreate(task1_thread, "TASK1", 128, p_context, 1, &m_task1_thread))
    {
        APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    }

    if (pdPASS != xTaskCreate(task2_thread, "TASK2", 128, p_context, 2, &m_task2_thread))
    {
        APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    }
    
    // Activate deep sleep mode.
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

    // Configure and initialize the BLE stack.
    ble_stack_init();

    // Initialize modules.
    timers_init();
    buttons_leds_init(&erase_bonds);
    gap_params_init();
    gatt_init();
    advertising_init();
    services_init();
    sensor_simulator_init();
    conn_params_init();
    peer_manager_init();
    application_timers_start();

    // Create a FreeRTOS task for the BLE stack.
    // The task will run advertising_start() before entering its loop.
    nrf_sdh_freertos_init(advertising_start, &erase_bonds);

    // Start FreeRTOS scheduler.
    vTaskStartScheduler();

    while (true)
    {
        APP_ERROR_HANDLER(NRF_ERROR_FORBIDDEN);
    }
}

Parents Reply Children
  • Hi Aryan

    I modify the configTOTAL_HEAP_SIZE from 4096 to 8192, but still not working.
    But when I modify configMAX_PRIORITIES from 3 to 4 it works!! And I can change any uxPriority value what I want.

    Did you know why?

  • my bad, I should have seen this.

    Look at this link. You cannot use priorities for task which is more than (configMAX_PRIORITIES - 1). Since you were trying to use the numbers more than that scheduler was working in a unpredictable way. Next SDK we have updated the FreeRTOS kernel to 10.0.0 where i think you get an assert if you use illegal priority numbers to task, helping us to catch the error faster.

  • Hello Aryan & Cooper,

    I am new to Embedded Studio & Nordic; I have tried different way to create two another thread with this project. But system crashed every time.

    If I can comment out:

    // nrf_sdh_freertos_init(advertising_start, &erase_bonds);,

    and keep task1_thread, but comment out task2_thread; then task1_thread is running correct.

    I changed configMAX_PRIORITIES 6; did not see difference. 

     I changed configTOTAL_HEAP_SIZE to 8192; did not see difference

    I am using:

    SEGGER Embedded Studio for ARM
    Release 3.40  Build 2018052200.36079
    Windows x64

    nRF52840 DK

    C:\Nordic_Semi\nRF5_SDK_15.0.0_a53641a\examples\ble_peripheral\ble_app_hrs_freertos\pca10056\s140\ses as base project.

    The test that I created:

    if (pdPASS != xTaskCreate(task1_thread, "TASK1", 128, NULL, 3, &m_task1_thread))
    {
    APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    }
    else
    NRF_LOG_INFO("SPT FreeRTOS created.");

    Again, I must comment out:

    // nrf_sdh_freertos_init(advertising_start, &erase_bonds);,

    to make it work.

    Please help if you can, I am really appreciated.

    Donald

Related