SysTick interrupt priority

samfpetersen wrote on Wednesday, January 06, 2016:

I’m using FreeRTOS V8.2.1 on STM32F4 and my question is about the SysTick interrupt priority. The FreeRTOS source says that it should be the “lowest priority interrupt”, and it explicitly sets it to the lowest priority in xPortStartScheduler(). This seems backwards to me, shouldn’t it be the highest?

Further complicating things is the STM32’s HAL libraries, which have their own systick implementation… It specifies that you should set the systick interrupt to the highest priority.

By the way, I’m referring to logically highest not numerically highest, and I’m aware that they are inverted


rtel wrote on Wednesday, January 06, 2016:

The interrupt that absolutely must be the lowest priority is the PendSV interrupt. The SysTick interrupt could be higher, but could interfer with application interrupts if it is.

When the ST HAL is used with FreeRTOS, like it can be with the STM32 Cube software supplied by ST, then ST call the FreeRTOS tick handler from their HAL tick handler. That means you must not install the FreeRTOS handler. However, I do it the other way around - installing the FreeRTOS handler as normal, then calling the HAL tick handler from the FreeRTOS tick hook function.


rtel wrote on Thursday, January 07, 2016:

Actually - I would like to correct my previous post. The SysTick handler does need to run at the lowest priority. This is because the critical section within the handler itself assumes it is running at the lowest priority, so saves time by not saving the old priority mask and then restoring the previous priority mask.

samfpetersen wrote on Friday, January 08, 2016:

The conflict I encounter is that the ST HAL SysTick must be at the highest priority, since there are HAL layer ISRs that wait on a timeout (which is bad, but it is what it is). So to fix this, I’m setting the SysTick interrupt to the highest priority, and in that ISR I’m calling the ST HAL tick function and setting another interrupt at the lowest priority. In the ISR of that interrupt, I’m calling the FreeRTOS tick function.

Seems hacky, but I can’t think of a better way to do this at the moment. Thoughts?


janoshnosh wrote on Wednesday, November 09, 2016:

@Sam Petersen
I totally agree with that strategy. I have no understanding for why the SysTick interrupt should be of lowest priority. In my opinion, FreeRTOS wouldn’t have anything to do with a real-time system anymore, if it doesn’t keep counting milliseconds.
We have the same issue here, that the HAL-ISR-delays make all time calculations based on the FreeRTOS tick invalid. And ridiculous.

owainkopin wrote on Friday, December 22, 2017:

@Richard Barry

what is logic of having systick as lowest priority isr?
Surely systick is used for driving timers on various semaphore/mailbox operations that may time out; and also scheduler is run off the back of the systick. Is not systick kind of the God process controlling just about everything?

I can also see the view that we may have processes run off the back of ISR’s that must be serviced in a timely fashion independent of the RTOS; they may be higher priority than systick. But ISR’s that are posting semapores etc; should they not run lower than systick?

I am working on a system with around 10 isr sources; these are currently all higher priority than systick and I regularly see the systick being preempted. As a general rule of thumb is it not better to let systick run; do its stuff and then process other isrs?

rtel wrote on Friday, December 22, 2017:

I think it is only the PenSV that needs to run at the lowest priority,
it is just recommended to run SysTick at the lowest too to leave a lot
of higher priority interrupts available for applications that require
them to be more real time (deterministic). Some libraries (STM32Cube?)
poll the tick inside an ISR (not good practice in my humble opinion) in
which cases having the tick at the lowest priority can actually cause a
deadlock. It is also generally more efficient if the tick is only
incremented after other interrupts have completed, rather than intermix
ISRs that are using the time and having the time changing.

karltsai wrote on Monday, April 23, 2018:

I’m new to this and here is my strategy:

  1. install the FreeRTOS systick handler and bump it to higher priority that HAL requires
  2. make sure the priority mask and whatever house-keeping necessary are done properly in tje systick handler
  3. rewrite/overwrite HAL_Delay and HAL_GetTick functions to use FreeRTOS version. Both of them are weak so not a problem. There is no need to keep 2 sets of system ticks

Can someone see any problem of this approach?

rtel wrote on Monday, April 23, 2018:

First, I’m guessing you are using STM32. Is that correct?

Second - what is it you are trying to do? If you want to use both the
ST drivers and FreeRTOS at the same time then the ST way of doing that
is to call the FreeRTOS tick handler from their tick handler. The
FreeRTOS way of doing it is to install its own tick handler (SysTick)
then call the ST handler from the FreeRTOS tick hook function. Either
way, if you are using the ST driver, it is likely you are going to need
both to execute.

karltsai wrote on Monday, April 23, 2018:

  1. yes that is the case
  2. I understand both approaches. calling from HAL leaves FRTOS vounerable as it didn’t have enough protection running in higher priority. calling from FRTOS breaks the HAL when it is expecting tick count to increment which never does unless the FRTOS priority is bumped.
  3. I’m trying to address both problems in a seamless way. i.e.: with minimal impact to HAL as well as FreeRTOS code base.

rtel wrote on Monday, April 23, 2018:

Unfortunately I think it is necessary to bump the SysTick priority up
when using the HAL drivers as some wait for the tick count to change
from within HAL interrupts themselves (not something that would normally
be recommended).

karltsai wrote on Monday, April 23, 2018:

this is the rewrite to break the barrier between HAL and FRTOS

#include <FreeRTOS.h>
#include <task.h>

// HAL_Delay is used mostly in device init and config
void HAL_Delay(uint32_t Delay)

uint32_t HAL_GetTick(void)
return xTaskGetTickCount();

karltsai wrote on Monday, April 23, 2018:

Already addressed that in (1)