How to Configure the Priority Group of the Cortex-M4 Processor of an STM32F407G Discovery Board in C

STM32F407G discovery board



In this article, we show how to configure the priority group of the M4 cortex processor of an STM32F407G discovery board in C.

The priority group specifies how priorities are made within the processor code.

There are 2 parts to a priority group: the preemption priority and the subpriority.

This configuration doesn't actually set the priority for a particular system exception but specifies how priorities are made.

When there are multiple system exceptions, the preemption priority determines which exception should be executed first (take priority).

The exception with the lowest priority number has the greatest priority and gets executed first.

However, when multiple exceptions have the preemption priority, then the subpriority is looked at. If the exception with the lowest number subpriority gets executed.

There are 5 different priority groupings: NVIC_PRIORITYGROUP_0, NVICPRIORITYGROUP_1, NVIC_PRIORITYGROUP_2, NVICPRIORITYGROUP_3, and NVIC_PRIORITYGROUP_4.

Each group has 4 assigned bits to specify the preemption priority and the subpriority.

NVIC_PRIORITYGROUP_0 has 0 bits for the the preemption priority and 4 bits for the subpriority.

NVIC_PRIORITYGROUP_1 has 1 bit for the preemption priority and 3 bits for the subpriority.

NVIC_PRIORITYGROUP_2 has 2 bits for the preemption priority and 2 bits for the subpriority.

NVIC_PRIORITYGROUP_3 has 3 bits for the preemption priority and 1 bit for the subpriority.

NVIC_PRIORITYGROUP_4 has 4 bits for the preemption priority and 0 bits for the subpriority.

By default, if you don't configure this priority grouping, it will be NVIC_PRIORITYGROUP_4 by default.

Just to show how this works, if the NVIC_PRIORITYGROUP_4 is selected, there are 4 bits for the preemption priority. This means that preemption priority values can range from 0 to 15, with 0 being the highest priority and 15 being the lowest.

So the code to set the priority group for the processor is shown below.

This code goes within the msp.c file, which for an STM32F4xx discovery board is, stm32f4xx_hal_msp.c





So this is a basic program.

At the top of the code, you must have an include statement for the stm32f4xx_hal.h header file.

Then we have our function, HAL_MspInit()

Within this function, we initialize low-level processor configurations.

The stm32f4xx_hal_cortex.h within the STM32F4xx_HAL_Driver directory is the header file which contains the functions for low-level processor configurations.

The HAL_NVIC_SetPriorityGrouping() function allows us to select the priority group for the processor of the STM32F4xx board.

In this case, we select NVIC_PRIORITYGROUP_3, which has 3 bits for the preemption priority and 1 bit for the subpriority.

We enable the MemManage fault, Bus fault, and Usage fault with the line, SCB->SHCSR |= 0x7 << 16;

We then set the priority of the exceptions through the HAL_NVIC_SetPriority() function

The first parameter it the system exception, the second is the preemption priority, and the third is the subpriority.

If you are using NVIC_PRIORITYGROUP_4, which has no bits for the subpriority, you can simply specifiy 0 as its value, but it accepts no value for it.

The only other thing that's important to be done is the main.c file.

This main.c file needs the HAL_Init() function, which then goes to our mcp.c file through the HAL_MspInit() function and initializes the low-level configurations of the processor.

The code below is all that is needed for the main.c file within the Core directory.





So we must include 2 header files within this file.

We then have our HAL_Init(), which is the responsible for many initialization configurations, including low-level configuration settings for our processor.

And this is how to configure the priority group for the cortex-M4 processor in an STM32F407G microcontroller board.



Related Resources





HTML Comment Box is loading comments...