转自:https://blog.csdn.net/xukai871105/article/details/53516857
前言
本文将说明在FreeRTOS嵌入式操作系统中,如何设置STM32 Cortex M3和M4系列MCU的中断优先级。
总结
- 【1】STM32L1系列,STM32F1系列,STM32F4系列,设置NVIC时需要使用NVIC_PriorityGroup_4。
- 【2】抢占优先级的要比“MAX”更大,而比“LOWEST”更小。例如 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY= , configLIBRARY_LOWEST_INTERRUPT_PRIORITY= ,那么NVIC抢占优先级设置的具体值应该比MAX=5更大(包括等于),比LOWEST=15更小(包括等于),也就是说5,6,7,8,9,10,11,12,13,14,15肯定是合法值。
- 【3】FreeRTOS中,数值越大优先级越高,这种优先级可以成为逻辑优先级。Cortex M3/M4中断中,数值越大优先级越低,这种优先级成为中断优先级。两者相反,所以才会出现比"MAX"更大而比“LOWEST”更小的情况。
示意代码
【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设置】
【示例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 = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
【示例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 = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);