Change from: https://blog.csdn.net/xukai871105/article/details/53516857
Preface
This article will show how to set the interrupt priority of STM32 Cortex M3 and M4 Series MCU in FreeRTOS embedded operating system.
summary
- [1] STM32L1 series, STM32F1 series and STM32F4 series. NVIC? Prioritygroup? 4 is required when setting NVIC.
- [2] the priority of preemption is larger than "MAX" and smaller than "low". For example, if "configlibrary" MAX "syscall" interrupt "priority = 5," configlibrary "low" interrupt "priority = 15", then the specific value of NVIC preemption priority setting should be larger (including equal) than MAX=5, and smaller (including equal) than low = 15, that is to say, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 must be legal.
- [3] in FreeRTOS, the higher the value is, the higher the priority is. This priority can be a logical priority. In Cortex M3/M4 interrupt, the higher the value, the lower the priority, which becomes the interrupt priority. On the contrary, they are bigger than "MAX" and smaller than "lost".
Schematic code
[FreeRTOSConfig.h]
/* Use the system definition, if there is one */ #ifdef __NVIC_PRIO_BITS #define configPRIO_BITS __NVIC_PRIO_BITS #else #define configPRIO_BITS 4 /* 15 priority levels */ #endif #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15 #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5 /* The lowest priority. */ #define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) ) /* Priority 5, or 95 as only the top four bits are implemented. */ /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!! See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */ #define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
[NVIC settings]
[example A]
NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel = XXXX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_LOWEST_INTERRUPT_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
[example A]
NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel = XXXX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_LOWEST_INTERRUPT_PRIORITY-1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);