背景:
在stm32的标准库还叫STM32F10xxx_FWLib_V2.0.3的那个年代
文件 STM32F10xFWLib_V2.0.3/FWLib/library/inc/stm32f10x_nvic.h
中有对System Handlers的定义。具体内容如下:
/* System Handlers -----------------------------------------------------------*/
#define SystemHandler_NMI ((u32)0x00001F) /* NMI Handler */
#define SystemHandler_HardFault ((u32)0x000000) /* Hard Fault Handler */
#define SystemHandler_MemoryManage ((u32)0x043430) /* Memory Manage Handler */
#define SystemHandler_BusFault ((u32)0x547931) /* Bus Fault Handler */
#define SystemHandler_UsageFault ((u32)0x24C232) /* Usage Fault Handler */
#define SystemHandler_SVCall ((u32)0x01FF40) /* SVCall Handler */
#define SystemHandler_DebugMonitor ((u32)0x0A0080) /* Debug Monitor Handler */
#define SystemHandler_PSV ((u32)0x02829C) /* PSV Handler */
#define SystemHandler_SysTick ((u32)0x02C39A) /* SysTick Handler */
第一眼看到这些宏定义的值很奇怪,也没啥规律,既然叫Handler,是不是和地址有关,但是地址怎么给定义成固定值了,比较违反常理,应该不是。
再接着搜索引用它们的地方,发现主要有2种类型:
1)作为参数传给NVIC_xxxx()。
2) 这些宏定义会有不同的组合。
作为参数传给api时,如下:
/*******************************************************************************
* Function Name : NVIC_SetSystemHandlerPendingBit
* Description : Sets System Handler pending bit.
* Input : - SystemHandler: specifies the system handler pending bit
* to be set.
* This parameter can be one of the following values:
* - SystemHandler_NMI
* - SystemHandler_PSV
* - SystemHandler_SysTick
* Output : None
* Return : None
*******************************************************************************/
void NVIC_SetSystemHandlerPendingBit(u32 SystemHandler)
{u32 tmp = 0x00;/* Check the parameters */assert_param(IS_SET_PENDING_SYSTEM_HANDLER(SystemHandler));/* Get the System Handler pending bit position */tmp = SystemHandler & (u32)0x1F;/* Set the corresponding System Handler pending bit */SCB->ICSR |= ((u32)0x01 << tmp);
}
这里面其实只用到了它的低5位,使用的掩码为0x1F,对应bit[4:0]。以SystemHandler_SysTick=0x02039A为例, 0x02039A & 0x1F = 0x1A,0x1A即为十进制的26。然后再看SCB->ICSR寄存器
第26位即为设置SysTick异常状态为Pending状态的控制位。
同理,当将SystemHandler_SysTick用在不同的NVIC_xxx()时,会使用他不同位域的值,也对应着不同的寄存器。
如下高亮的SystemHandler_SysTick被分配组合在不同的组中使用。自然就会组合出一个没啥规律的数值,看起来像是个随机数。其他的宏定义也是类似。
附:AN2953文档,描述如何将没有使用CMSIS的标准库移植到使用了CMSIS的标准库。
https://www.st.com/en/embedded-software/stsw-stm32023.html
Cortex-M核心来自ARM公司,st使用了Cortex-M作为核心,再添加一些诸如spi,i2c等这些外设做成了stm32fxxx这样的Soc芯片。
下面这些Exception是来自Cortex-M3的异常,这里称它们为Exception。
/****** Cortex-M3 Processor Exceptions Numbers ***************************************************/NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */MemoryManagement_IRQn = -12, /*!< 4 Cortex-M3 Memory Management Interrupt */BusFault_IRQn = -11, /*!< 5 Cortex-M3 Bus Fault Interrupt */UsageFault_IRQn = -10, /*!< 6 Cortex-M3 Usage Fault Interrupt */SVCall_IRQn = -5, /*!< 11 Cortex-M3 SV Call Interrupt */DebugMonitor_IRQn = -4, /*!< 12 Cortex-M3 Debug Monitor Interrupt */PendSV_IRQn = -2, /*!< 14 Cortex-M3 Pend SV Interrupt */SysTick_IRQn = -1, /*!< 15 Cortex-M3 System Tick Interrupt */
下面这些是来自Cortex-M之外的模块的中断,称之为Interrupt.
/****** STM32 specific Interrupt Numbers *********************************************************/WWDG_IRQn = 0, /*!< Window WatchDog Interrupt */PVD_IRQn = 1, /*!< PVD through EXTI Line detection Interrupt */TAMPER_IRQn = 2, /*!< Tamper Interrupt */RTC_IRQn = 3, /*!< RTC global Interrupt */FLASH_IRQn = 4, /*!< FLASH global Interrupt */RCC_IRQn = 5, /*!< RCC global Interrupt */EXTI0_IRQn = 6, /*!< EXTI Line0 Interrupt */EXTI1_IRQn = 7, /*!< EXTI Line1 Interrupt */EXTI2_IRQn = 8, /*!< EXTI Line2 Interrupt */EXTI3_IRQn = 9, /*!< EXTI Line3 Interrupt */EXTI4_IRQn = 10, /*!< EXTI Line4 Interrupt */DMA1_Channel1_IRQn = 11, /*!< DMA1 Channel 1 global Interrupt */DMA1_Channel2_IRQn = 12, /*!< DMA1 Channel 2 global Interrupt */DMA1_Channel3_IRQn = 13, /*!< DMA1 Channel 3 global Interrupt */DMA1_Channel4_IRQn = 14, /*!< DMA1 Channel 4 global Interrupt */DMA1_Channel5_IRQn = 15, /*!< DMA1 Channel 5 global Interrupt */DMA1_Channel6_IRQn = 16, /*!< DMA1 Channel 6 global Interrupt */DMA1_Channel7_IRQn = 17, /*!< DMA1 Channel 7 global Interrupt */ADC1_2_IRQn = 18, /*!< ADC1 et ADC2 global Interrupt */USB_HP_CAN1_TX_IRQn = 19, /*!< USB High Priority or CAN1 TX Interrupts */USB_LP_CAN1_RX0_IRQn = 20, /*!< USB Low Priority or CAN1 RX0 Interrupts */CAN1_RX1_IRQn = 21, /*!< CAN1 RX1 Interrupt */CAN1_SCE_IRQn = 22, /*!< CAN1 SCE Interrupt */EXTI9_5_IRQn = 23, /*!< External Line[9:5] Interrupts */TIM1_BRK_IRQn = 24, /*!< TIM1 Break Interrupt */TIM1_UP_IRQn = 25, /*!< TIM1 Update Interrupt */TIM1_TRG_COM_IRQn = 26, /*!< TIM1 Trigger and Commutation Interrupt */TIM1_CC_IRQn = 27, /*!< TIM1 Capture Compare Interrupt */TIM2_IRQn = 28, /*!< TIM2 global Interrupt */TIM3_IRQn = 29, /*!< TIM3 global Interrupt */
#ifndef STM32F10X_LDTIM4_IRQn = 30, /*!< TIM4 global Interrupt */
#endif I2C1_EV_IRQn = 31, /*!< I2C1 Event Interrupt */I2C1_ER_IRQn = 32, /*!< I2C1 Error Interrupt */
#ifndef STM32F10X_LDI2C2_EV_IRQn = 33, /*!< I2C2 Event Interrupt */I2C2_ER_IRQn = 34, /*!< I2C2 Error Interrupt */
#endif SPI1_IRQn = 35, /*!< SPI1 global Interrupt */SPI2_IRQn = 36, /*!< SPI2 global Interrupt */USART1_IRQn = 37, /*!< USART1 global Interrupt */USART2_IRQn = 38, /*!< USART2 global Interrupt */
#ifndef STM32F10X_LD USART3_IRQn = 39, /*!< USART3 global Interrupt */
#endif EXTI15_10_IRQn = 40, /*!< External Line[15:10] Interrupts */RTCAlarm_IRQn = 41, /*!< RTC Alarm through EXTI Line Interrupt */USBWakeUp_IRQn = 42, /*!< USB WakeUp from suspend through EXTI Line Interrupt */
#ifdef STM32F10X_HDTIM8_BRK_IRQn = 43, /*!< TIM8 Break Interrupt */TIM8_UP_IRQn = 44, /*!< TIM8 Update Interrupt */TIM8_TRG_COM_IRQn = 45, /*!< TIM8 Trigger and Commutation Interrupt */TIM8_CC_IRQn = 46, /*!< TIM8 Capture Compare Interrupt */ADC3_IRQn = 47, /*!< ADC3 global Interrupt */FSMC_IRQn = 48, /*!< FSMC global Interrupt */SDIO_IRQn = 49, /*!< SDIO global Interrupt */TIM5_IRQn = 50, /*!< TIM5 global Interrupt */SPI3_IRQn = 51, /*!< SPI3 global Interrupt */UART4_IRQn = 52, /*!< UART4 global Interrupt */UART5_IRQn = 53, /*!< UART5 global Interrupt */TIM6_IRQn = 54, /*!< TIM6 global Interrupt */TIM7_IRQn = 55, /*!< TIM7 global Interrupt */DMA2_Channel1_IRQn = 56, /*!< DMA2 Channel 1 global Interrupt */DMA2_Channel2_IRQn = 57, /*!< DMA2 Channel 2 global Interrupt */DMA2_Channel3_IRQn = 58, /*!< DMA2 Channel 3 global Interrupt */DMA2_Channel4_5_IRQn = 59 /*!< DMA2 Channel 4 and Channel 5 global Interrupt */
#endif
在没有使用CMSIS之前,stm32的标准库里面是不怎么区分cortex-M之内和之外的nvic处理。
在使用CMSIS之后,标准库的目录结构分成2部分:
如名字所示,CMSIS中主要是处理跟cortex-m相关的,STM32F10x_StdPeriph_Driver里面存放的标准外设相关的。
此时在含有CMSIS的库中如果想设置SystemTick为Pending状态,根据examples中的使用方法就是直接操作寄存器:
如果觉得这样不够优雅,可以自己再给它封装一个类似NVIC_SetSystemHandlerPendingBit()的api就行了。