Preemptive and periodic tasks

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

Preemptive and periodic tasks

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).

Preemptive and periodic tasks

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.

Preemptive and periodic tasks

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

Preemptive and periodic tasks

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,

Preemptive and periodic tasks

Thanks again…actually I tried with the method you mentioned to introduce delays first, but I got same issue.

Preemptive and periodic tasks

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….. Regards.