Quality RTOS & Embedded Software

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


Generic ISR dispatch routine

Posted by *anonymous on December 24, 2006

In order to have a single generic ISR dispatch routine, I have added a function to tasks.c that returns the priority of the highest priority ready task.

unsigned portBASE_TYPE TopPriority(void) {     return uxTopReadyPriority; }

My generic dispatch routine captures this priority before dispatching to the proper ISR. When the ISR returns, it tests whether the highest priority has changed. If so, it calls for a context switch, else it doesn't.

void PortIRQDispatch(void) {     portENTER_SWITCHING_ISR();         unsigned    portBASE_TYPE prevTopPriority = TopPriority();


    portEXIT_SWITCHING_ISR(prevTopPriority<TopPriority()); }

I wonder if anyone has any comments on issues/problems this may raise.

One point is that this subverts time slicing between tasks of equal priority. Using <= rather than < in the final test would be similar to time slicing, although not quite the same.

Generic ISR dispatch routine

Posted by nobody on December 24, 2006

I'm not sure I follow the object of this code, what you are trying to achieve? 

If you want to only cause a switch to another task when there is a task of higher priority than the running task then you could add the code to vTaskSwitchContext in tasks.c to only execute the while loop if the top ready priority is greater than the running priority.

If this is just to have commonality in your IRQs and to prevent the application writer having to use the ENTER/EXIT macros then I think the code is good.  You of coarse have a few little expenses in an extra layer of indirection.  Also you are going to save and restore the context even when the task does not need it saved and restored.  Each IRQ in the system will save the context.

Different ports handle this in different ways.  Some only save the context when a switch is actually required which is really nice as this is the most optimal.  From the macros you quote I assume you are using the ARM port.  This itself uses two different techniques.  Looking at the STR750 (and I think the ARM9) ports you will see that there is an IRQ wrapper  written in assembler that saves the context before calling the IRQ C function, then restores it again prior to exiting the ASM wrapper (on completion of the interrupt).  The portEND_SWITCHING_ISR macro then becomes a simple call to vTaskSwitchContext() if a switch is required.  Take a look at the port page for STR750/RIDE at the section "Interrupt service routines".

Generic ISR dispatch routine

Posted by *anonymous on December 24, 2006

I am using an LPC2xxx. The VIC priority vectored interrupts and the FIQ interrupt are handled separately and each in there own way. In my case, they all operate outside the realm of FreeRTOS, never triggering context switches.

My PortIRQDispatch() is the common routine entered off of the VIC default IRQ. All of these have to go through such a common handler. So it can perform operations common to all of the default vector ISRs. I was looking for a common way of determining if a context switch was required on exiting the ISR.

I really don't like to tamper with FreeRTOS, so I look for ways to work with it with as few changes as possible. My TopPriority() function added into tasks.c seemed like a pretty inocuous change that gave me what I wanted. But I wonder if others might have tried a similar stunt and run into some subtle 'gotcha'.

In this particular case, I'm not sure tampering vTaskSwitchContext() would be a good thing to do. I think it would mean that taskYIELD() wouldn't perform as expected.

Thanks for the feedback.

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

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

Latest News

FreeRTOS v10.2.1 is available for immediate download. MIT licensed, includes 64-bit RISC-V, NXP Cortex-M33 demo & Nuvoton Cortex-M23 demo.

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Cadence Tensilica Cortes

Espressif ESP32

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

Xilinx Microblaze and Zynq partner