• AVR Freaks

Helpful ReplyHot!freeRTOS preemptive scheduling not able to run other tasks

Author
ullagaddi
Starting Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2019/05/23 21:38:14
  • Location: 0
  • Status: offline
2020/07/12 11:09:45 (permalink)
0

freeRTOS preemptive scheduling not able to run other tasks

Hi,
I'm new to freeRTOS and I'm stuck in a single task execution.
I've created simple led toggle task. LED1 should toggle in app task and LED2 in sys task.
Attached harmony configurations for your reference.
 
I've not used delay function. Instead I've just created a for loop to verify preemptive functionality of RTOS.
When I debug/free run this code, after scheduler is started, first task executed is app task. and this will continue to run without preempting. i.e, LED1 toggles continuously but LED2 never toggles. 
 
I've following queries:
1. After scheduler is started, the first task to be executed is sys task. why app task executed?
2. Since I've set tick rate to 1ms, after 1ms the context switch should happen. why it is not happening?
3. What are the function names for 1ms tick interrupt and context switching that I need to monitor in Task.c file?
 
Harmony version: 2.06
mcu: PIC32MZ2048EFG100
 
My apologies if my understanding about rtos concepts are wrong and please correct me.
 
void SYS_Tasks ( void )
{
    /* Create OS Thread for Sys Tasks. */
    xTaskCreate((TaskFunction_t) _SYS_Tasks,
                "Sys Tasks",
                256, NULL, 1, NULL);

 
 
    /* Create OS Thread for APP Tasks. */
    xTaskCreate((TaskFunction_t) _APP_Tasks,
                "APP Tasks",
                256, NULL, 1, NULL);

    /**************
     * Start RTOS *
     **************/
    vTaskStartScheduler(); /* This function never returns. */
}


/*******************************************************************************
  Function:
    void _SYS_Tasks ( void )

  Summary:
    Maintains state machines of system modules.
*/
static void _SYS_Tasks ( void)
{
    int i;
    while(1)
    {
        /* Maintain system services */

        /* Maintain Device Drivers */
 
        LED2Toggle();
        for(i = 0; i < 10000000; i++);
        i++;
        i++;

        /* Maintain Middleware */

        /* Task Delay */
    }
}

 
 

/*******************************************************************************
  Function:
    void _APP_Tasks ( void )

  Summary:
    Maintains state machine of APP.
*/

static void _APP_Tasks(void)
{
    int i;
    while(1)
    {
        LED1Toggle();
        for(i = 0; i < 10000000; i++);
        i++;
        i++;
    }
}

Attached Image(s)

#1
rjc101
Super Member
  • Total Posts : 133
  • Reward points : 0
  • Joined: 2016/07/08 14:56:34
  • Location: 0
  • Status: offline
Re: freeRTOS preemptive scheduling not able to run other tasks 2020/07/12 12:26:32 (permalink) ☄ Helpfulby ullagaddi 2020/07/12 21:24:21
0
This snippet from the FreeRTOS website should explain what is happening, where tasks of equal priority don't get CPU time unless a task yields.
configUSE_TIME_SLICING

By default (if configUSE_TIME_SLICING is not defined, or if configUSE_TIME_SLICING is defined as 1) FreeRTOS uses prioritised preemptive scheduling with time slicing. That means the RTOS scheduler will always run the highest priority task that is in the Ready state, and will switch between tasks of equal priority on every RTOS tick interrupt. If configUSE_TIME_SLICING is set to 0 then the RTOS scheduler will still run the highest priority task that is in the Ready state, but will not switch between tasks of equal priority just because a tick interrupt has occurred.

 
 This setting should be in the FreeRTOS config tree in Harmony v2.06, failing that change the setting in FreeRTOSConfig.h within your project.  By default this is set to 0 in the file.
#2
ullagaddi
Starting Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2019/05/23 21:38:14
  • Location: 0
  • Status: offline
Re: freeRTOS preemptive scheduling not able to run other tasks 2020/07/12 21:28:55 (permalink)
0
Thankyou.
 
I've enabled time slicing and now context switch is happening and it is working as expected.
 
But why app task is executing first and not sys task?
 
 
#3
rjc101
Super Member
  • Total Posts : 133
  • Reward points : 0
  • Joined: 2016/07/08 14:56:34
  • Location: 0
  • Status: offline
Re: freeRTOS preemptive scheduling not able to run other tasks 2020/07/12 23:45:16 (permalink)
4 (1)
I think FreeRTOS maintains a list (array) of tasks ready to run per priority. This is worked through from high to low, decreasing the index each context switch. If you swap the task create order around the sys task would b run first.
#4
LdB_ECM
Super Member
  • Total Posts : 404
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: freeRTOS preemptive scheduling not able to run other tasks 2020/07/13 19:49:15 (permalink)
4 (1)
Also tick the box that says "use task delay"
Then get rid of this junk you are wasting CPU time
for(i =0; i <10000000; i++);

On FreeRTOS you have vTaskDelay that sleeps the thread so it uses no CPU time
https://www.freertos.org/a00127.html
 
#5
Jump to:
© 2020 APG vNext Commercial Version 4.5