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
  • My test code is really simple like below.

    task1.c

    #include "task1.h"
    
    #include "FreeRTOS.h"
    #include "task.h"
    #include "semphr.h"
    
    #include "nrf_drv_gpiote.h"
    
    static TaskHandle_t m_task1_thread;
    SemaphoreHandle_t task1_sem;
    
    static void task1_thread(void * arg)
    {
    	task1_sem = xSemaphoreCreateBinary();
    	if (NULL == task1_sem)
    	{
    			APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    	}
    	
    	uint32_t err_code;
    	nrf_drv_gpiote_out_config_t pin_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
    	err_code = nrf_drv_gpiote_out_init(LED_4, &pin_config);
    	APP_ERROR_CHECK(err_code);
    	
    	while(1)
    	{
    		xSemaphoreTake(task1_sem, 1024);
    		
    		nrf_drv_gpiote_out_toggle(LED_4);
    	}
    }
    
    void task1_thread_init(void * p_context)
    {
    	if (pdPASS != xTaskCreate(task1_thread, "TASK1", 128, p_context, 1, &m_task1_thread))
        {
            APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
        }
    }
    

    task2.c

    #include "task2.h"
    
    #include "FreeRTOS.h"
    #include "task.h"
    #include "semphr.h"
    
    #include "nrf_drv_gpiote.h"
    
    static TaskHandle_t m_task2_thread;
    SemaphoreHandle_t task2_sem;
    
    static void task2_thread(void * arg)
    {
    	task2_sem = xSemaphoreCreateBinary();
    	if (NULL == task2_sem)
    	{
    		APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    	}
    	
    	uint32_t err_code;
    	nrf_drv_gpiote_out_config_t pin_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
    	err_code = nrf_drv_gpiote_out_init(LED_3, &pin_config);
    	APP_ERROR_CHECK(err_code);
    	
    	while(1)
    	{
    		xSemaphoreTake(task2_sem, 512));
    		
    		nrf_drv_gpiote_out_toggle(LED_3);
    	}
    }
    
    void task2_thread_init(void * p_context)
    {
    		if (pdPASS != xTaskCreate(task2_thread, "TASK2", 128, p_context, 2, &m_task2_thread))
        {
            APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
        }
    }
    

  •  The task which is neither executing my last  in html coding program,It's not in running about Create (task1_thread, "TASK1", 128, p_context, high priority or equal priority task is executing appear under the  priority section. I have the learning option with experts at Phd dissertation writing service.

  • Hi Aryan

    Have you tested my code?
    Got the same result or work normally?

    I try to found the issue, but still not working.

  • I disabled the logs and it worked just fine. for logs , i think you need to increase the heap size.  configTOTAL_HEAP_SIZE

  • 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?

Related