Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT




Loading

Preemptive and periodic tasks

Posted by Manuel Andres on April 1, 2013
Hi,

I noticed that if preemptive option is enable there is no way to block the highest priority task.
How can I implement a system with the following behavior?: periodic tasks that at the end of their execution allows other tasks to run. However, if a new period of a higher priority task starts, it preempts the currently running task.

Regards,
Manuel

RE: Preemptive and periodic tasks

Posted by Richard Damon on April 1, 2013
A task (even the highest priority one) "blocks" if it blocks in a call to a Delay operation, or waits on a semaphore or queue.

Typically, most tasks will have some operation in their loop which blocks, while they wait for the next time they should do more work. The only exception will be lowest priority tasks which intend to use any remaining time doing something. (They need to be lowest as any task lower won't get time).


RE: Preemptive and periodic tasks

Posted by Manuel Andres on April 1, 2013
Hi,

Thanks for the quick response.
I enabled the preemptive mode and I suspended the higher priority task using the method vTaskSuspend(NULL) inside of itself. However, the system is stuck on that method. Other tasks did not start to run.

RE: Preemptive and periodic tasks

Posted by Manuel Andres on April 1, 2013
I can see that pxCurrentTCB corresponds to my second task, but it never starts its execution. It is stuck on portYield method that Timer Interrupt executes. To be more specific "return" of portYield macro jumps again and again to the instruction just after "call _vTaskSwitchContext" on file portASM_PIC24.S

RE: Preemptive and periodic tasks

Posted by Richard Damon on April 1, 2013
That sounds like something has corrupted your system. Some likely causes:

Stack overrun
Interrupts using not FromISR APIs (or non-interrupts using a FromISR API incorrectly)
Interrupts with wrong priorities

Also, as a side note, using vTaskSuspend this way is a bit unusual, and I find I use vTaskSuspend only in very unusual cases. Normally a task is waiting for SOMETHING, either time (so you use a vTaskDelay or vTaskDelayUntil) or is waiting for some data (so you get data from a Queue) or it is waiting to be signaled via a semaphore. These tend to be more reliable than using Suspend and Resume,

RE: Preemptive and periodic tasks

Posted by Manuel Andres on April 1, 2013
Thanks again...actually I tried with the method you mentioned to introduce delays first, but I got same issue.

RE: Preemptive and periodic tasks

Posted by Richard on April 1, 2013
Where did you get your PIC24 project from?

How did you put your PIC24 project together?

Have you tried building and running the official demo application for the PIC24 that comes in the FreeRTOS zip file download?

Do you have stack overflow detection turned on?

Did you start with something very simple to get confidence that you had integrated the code correctly? I would suggest starting by turning pre-emption off, then creating two tasks that do nothing but yield to each other. For example you can start with just the following two tasks:

volatile unsigned long ul1, ul2;

void vTask1( void *pvParameters )
{
for( ;; )
{
ul1++;
taskYIELD();
}
}

void vTask2( void *pvParameters )
{
for( ;; )
{
ul2++;
taskYIELD();
}
}


Create these two tasks using:

portBASE_TYPE xTaskCreate( 
vTask1,
"T1",
configMINIMAL_STACK_SIZE,
NULL,
1,
NULL;
);

portBASE_TYPE xTaskCreate(
vTask2,
"T2",
configMINIMAL_STACK_SIZE,
NULL,
1,
NULL;
);



Start the scheduler, let it run for a while (you may be able to do that in the MPLAB simulator), then pause the debugger. Now if you inspect the ul1 and ul2 variables they should have approximately the same value. Next if you inspect xTickCount (defined in tasks.c) you should find it has incremented.

If that works, then it shows you can yield from one task to another, then you can turn preemption on and take out the calls to taskYIELD() and try it again.....

[you may also benefit from reading one of the tutorial books]

Regards.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS