ISR management and FreeRTOS

jradoslav wrote on Tuesday, November 25, 2014:

Dear FreeRTOS developers

I have questions about event creation/memory management in ISR subroutines.

I need to create memory block(Event) in ISR and then send event pointer it to queue.

before i have written this:
ISR()
{

Event* e = pvPortmalloc();
fillEvent(e,&ISRData);
sendQueueFromISR(queue,e);

}

then I`ve read some docs that memory allocation in ISR is not good choice.

this code block seems to be better:

ISR()
{
    
    Event* e;
    if(receiveFromISR(freeMemEventQ,&e))
    {
        sendMemoryRequirementForNextEvent(); -- requirements queue which process other mem generator task
        fillEvent(e,&ISRData);
        sendQueueFromISR(queue,e);
    }

}

My questions are:

1 How many calls *****FromISR routines can be used in ISR?
2 how much ticks consume *****FromISR, I`m using onlu sizeof(pointer) items
3 How can i use higher priority context switching (portYIELD) with more then 1 FromISR call in ISR subroutine.
4 how can i use higherPriorityWoken variable with more then 1 FromISR call in ISR subroutine.
5 portYIELD can be used only at end of ISR subroutine ?
6 How most time-ticks can consume ISR subroutine for safe FreeRTOS running
7 it is absolutely impossible to use malloc in ISR without memory generator task?

ISR
{
BaseType_t hpw1=false,hpw2=false;

FromISR(q,data,&hpw1);
FromISR(q,data,&hpw2);

portYIELDFromISR(hpw1|hpw2);

}

OR

ISR
{
BaseType_t hpw1;

FromISR(q,data,&hpw1); 
//hpw1 = true

FromISR(q,data,&hpw2);
//hpw2 = false

portYIELDFromISR( what? );

}

Thank you

rtel wrote on Tuesday, November 25, 2014:

Allocating memory in an interrupt is rarely a good thing as it can be non deterministic. Although FreeRTOS has 5 difference memory allocation schemes to choose from none of them are designed to be called from an interrupt as they use scheduler locks (so interrupts remain enabled while the allocation takes place). It would be possible to edit one of those schemes to make it safe to use from an interrupt, but I think there could be better options available.

For example:

  • If the block being allocated is always the same size then pre-allocate a pool of blocks. Then you can just grab a free block that has already been allocated, rather than allocate a block in the interrupt itself.

  • Allocate a block outside of the interrupt so it is ready for use inside the interrupt. When the block gets used, allocate another, again from outside the interrupt, so the interrupt always has a block ready.

  • Defer the interrupt processing to a task. You can do that using a semaphore, or using the centralised deferred interrupt processing mechanism. See the following link - note if you want the processing to happen immediately after the interrupt exits you will need to ensure the timer task has a priority above that of any other application tasks.
    http://www.freertos.org/xTimerPendFunctionCallFromISR.html
    http://www.freertos.org/a00110.html#configTIMER_TASK_PRIORITY

1 How many calls */**FromISR routines can be used in ISR?

There is no limit. You can use the same “xHigherPriorityTaskWoken”
parameter to each as the API functions will only ever set the value to
pdTRUE - leaving it unchanged in all other cases.

2 how much ticks consume /***FromISR, I`m using onlu sizeof(pointer)
items

It will not consume any ticks (that is, it won’t last longer than a tick
period, unless you have configTICK_RATE_MS set to something extremely
fast). It will of course consume some time.

3 How can i use higher priority context switching (portYIELD) with
more then 1 FromISR call in ISR subroutine.

As above - you can just pass the address of the same
xHigherPriorityTaskWoken variable into each. You only need to initialise
the variable to pdFALSE once, before the first ‘FromISR’ call.

4 how can i use higherPriorityWoken variable with more then 1 FromISR
call in ISR subroutine.

I think I have answered that twice now ;o)

5 portYIELD can be used only at end of ISR subroutine ?

That deepens on the port you are using. For most ports it can be called
anywhere. For a few ports it is essential it is called at the end.

Regards.

jradoslav wrote on Thursday, November 27, 2014:

Thank you for your comprehensive answer about ISR management. what are for me very important information.

xTimerPendFunctionCallFromISR is very usable and i think that can be used as event dispatch function

i solve event item generator problem with 1 memory requirement queue/memory pointer generator task, which generate memory allocation point from requirement item

this requirement item contains pointer to a destination queue and size of mem allocation requirement.

struct memReqItem
{
uint32_t memSize;
xQueueHandle destinationQueue;

}

if this item generator task receive from 1 requirement queue , then allocate memory (memSize)
and starst pointer sends to destination pointer queue (destinationQueue). ISR uses this queue and sends next requirement.

So. for each ISR is created one destinationQueue that is used only in depended ISR.

this pointer fills with ISR data and can be used as argument pointer in xTimerPendFunctionCallFromISR as dispatcher calls.