当前位置:网站首页>Hal library setting STM32 interrupt
Hal library setting STM32 interrupt
2022-07-01 03:07:00 【two thousand and twenty-one point zero nine】
This blog knowledge comes from Mr. Wei Dongshan's 7 Internet of things course .
One 、 The meaning of interruption
Usually , hold CPU Internally generated Our emergency is called abnormal , Such as illegal instructions ( Divide by zero )、 Address access cross-border, etc ; Put the CPU External on-chip peripherals generate Our emergency is called interrupt , such as GPIO Pin level change 、 Timer overflow, etc . The effects of exceptions and interrupts are basically the same , Is to suspend the current task , Give priority to emergencies , therefore Generally, interrupts and exceptions are collectively referred to as interrupts .
Cortex-M3 Kernel has 256 There are exceptions and interrupts , Which number 1~15 It's a system exception , 16~256 It's an external interrupt , Here's the picture .
Two 、 Interrupt priority
So many interruptions , Caused some new problems . For example, two interrupts occur at the same time , Which interrupt task should be executed first ? Another example is when an interrupt occurs , Another more urgent interruption , It's time to continue with the original interrupt , Or execute a new emergency interrupt ?
To address these issues , Cortex-M3 The kernel has a peripheral dedicated to managing interrupts NVIC( Nested Vectored Interrupt Controller, Nested vector interrupt controller ) , Control the nesting and scheduling of interrupts through priority . NVIC Is a total interrupt controller , Whether it's an exception from the kernel or an external interrupt from a peripheral , All by NVIC Unified management .
No. in the above figure 1、2、3 Interrupt priority is fixed and cannot be modified , Others can be modified .
stay Cortex-M3 in , Split the priority into preemptive priority ( Preempt Priority) And sub priorities (Subpriority) , Each interrupt needs to specify these two levels , Interrupts with high priority can interrupt interrupts with low priority , Implement interrupt nesting . Interrupt nesting is interrupt interrupt .
All programmable interrupts need to specify preemptive priority and sub priority , Preemption priority determines whether interrupt nesting can be generated , The sub priority determines the interrupt response sequence , If the two priorities are the same, look at the position of the interrupt in the interrupt exception table , The more forward, the more responsive .
Preemption priority is high ( Small value ) Interrupt preemption priority is low ( Large value ) Interrupt handling function of , And then execute the interrupt handling function with high priority , After execution, continue to execute the interrupted low priority processing function .
When two interrupts have the same preemptive priority , That is, the two interrupts will have no nested relationship , When an interruption comes , If at this time CPU Processing another interrupt , Then the later interrupt can be processed only after the previous interrupt processing function is processed , When these two interrupts arrive at the same time , Then the interrupt controller will decide which one to deal with first according to their sub priority .
If the priority of two interrupts is set to the same , Then whoever triggers it first will execute it first ; If it is triggered at the same time , Then, according to the location of the interrupt exception table ( Lean forward ) To decide who will execute first .
summary ,1、 Interrupts with high preemption priority can interrupt interrupts with low preemption priority ;
2、 The preemption priority of two interrupts is the same , Later, the interrupt must wait until the previous interrupt is processed ;
3、 The preemption priority of two interrupts is the same , At the same time produce , If the sub priority is higher, execute first ;
4、 preemption 、 Same sub priority 、 At the same time produce , Determined by the default position of the interrupt vector table (STM32 Interrupt exception table , The following table ).
Cortex-M3 The design has 256 Kind of interrupt , But most MCU It doesn't take so many interrupts , such as STM32F103
The series only has 70 There are exceptions and interrupts , The top 10 It's an exception , Back 60 One is an external interrupt , Above table .
stay HAL Library startup file “ startup_stm32f103xb.s”, You can see the defined interrupt vector table , The following code snippet shows .
__Vectors DCD __initial_sp ; Top of Stack
DCD Reset_Handler ; Reset Handler
DCD NMI_Handler ; NMI Handler
DCD HardFault_Handler ; Hard Fault Handler
DCD MemManage_Handler ; MPU Fault Handler
DCD BusFault_Handler ; Bus Fault Handler
DCD UsageFault_Handler ; Usage Fault Handler
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD SVC_Handler ; SVCall Handler
DCD DebugMon_Handler ; Debug Monitor Handler
DCD 0 ; Reserved
DCD PendSV_Handler ; PendSV Handler
DCD SysTick_Handler ; SysTick Handler
; External Interrupts
DCD WWDG_IRQHandler ; Window Watchdog
DCD PVD_IRQHandler ; PVD through EXTI Line detect
DCD TAMPER_IRQHandler ; Tamper
DCD RTC_IRQHandler ; RTC
DCD FLASH_IRQHandler ; Flash
DCD RCC_IRQHandler ; RCC
DCD EXTI0_IRQHandler ; EXTI Line 0
DCD EXTI1_IRQHandler ; EXTI Line 1
DCD EXTI2_IRQHandler ; EXTI Line 2
DCD EXTI3_IRQHandler ; EXTI Line 3
DCD EXTI4_IRQHandler ; EXTI Line 4
DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1
DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2
DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3
DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4
DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5
DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6
DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7
DCD ADC1_2_IRQHandler ; ADC1_2
DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TX
DCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0
DCD CAN1_RX1_IRQHandler ; CAN1 RX1
DCD CAN1_SCE_IRQHandler ; CAN1 SCE
DCD EXTI9_5_IRQHandler ; EXTI Line 9..5
DCD TIM1_BRK_IRQHandler ; TIM1 Break
DCD TIM1_UP_IRQHandler ; TIM1 Update
DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation
DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare
DCD TIM2_IRQHandler ; TIM2
DCD TIM3_IRQHandler ; TIM3
DCD TIM4_IRQHandler ; TIM4
DCD I2C1_EV_IRQHandler ; I2C1 Event
DCD I2C1_ER_IRQHandler ; I2C1 Error
DCD I2C2_EV_IRQHandler ; I2C2 Event
DCD I2C2_ER_IRQHandler ; I2C2 Error
DCD SPI1_IRQHandler ; SPI1
DCD SPI2_IRQHandler ; SPI2
DCD USART1_IRQHandler ; USART1
DCD USART2_IRQHandler ; USART2
DCD USART3_IRQHandler ; USART3
DCD EXTI15_10_IRQHandler ; EXTI Line 15..10
DCD RTC_Alarm_IRQHandler ; RTC Alarm through EXTI Line
DCD USBWakeUp_IRQHandler ; USB Wakeup from suspend
__Vectors_End
3、 ... and 、 The concept of break line
Of the same number IO The port will share a break line , All interrupts will be summarized into the same interrupt line ,PA0 As an interrupt source ,PB0、PC0...PG0 It cannot be used as an interrupt source .
Four 、cubeMX Set button interrupt in
1、 Set up PA0 To interrupt 0
2、 Interrupt selection falling edge trigger , That is, when the key is pressed .
3、 Enable medium break 0, Set interrupt priority group , And priorities .
4、EXTI0, Code initialization
5、 Program analysis
When there is an interruption , From main() Jump to the interrupt handler in EXTIO_IRQHandler() in , And then execute HAL_GPIO_EXTI_IRQHandler().
stay HAL_GPIO_EXTI_IRQHandler() in , It will first determine whether the incoming pin has generated an external interrupt , Confirm that the pin generates an interrupt , Then clear the interrupt flag , Call again “ HAL_GPIO_EXTI_Callback()” Callback function . In this callback function , By judging the input pin , Complete the corresponding user operations .
The callback function does not implement , We need to achieve it ourselves .
There are two ways of writing
static uint8_t Flag;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == KEY_Pin)
{
Flag =~ Flag;
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,Flag?GPIO_PIN_RESET:GPIO_PIN_SET);
}
}
The second way is to negate the flag bit in the interrupt callback function , Because the interrupt function should be executed for as long as possible , This does not affect other interrupts .
gpio.c
static uint8_t Flag;
uint8_t Key_Get_flag(void)
{
return Flag;
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == KEY_Pin)
{
Flag =~ Flag;
}
}
gpio.h
/* USER CODE BEGIN Prototypes */
//void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);
uint8_t Key_Get_flag(void);
/* USER CODE END Prototypes */
main().c
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(Key_Get_flag())
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
}
}
/* USER CODE END 3 */
}
边栏推荐
猜你喜欢
EtherCAT原理概述
Let's just say I can use thousands of expression packs
Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
UE4 rendering pipeline learning notes
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
XXL job User Guide
Restcloud ETL practice data row column conversion
最好用的信任关系自动化脚本(shell)
Share Creators萌芽人才培養計劃來了!
随机推荐
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
PCB defect detection based on OpenCV and image subtraction
Huawei operator level router configuration example | BGP VPLS configuration example
C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Mouse over effect II
Catch 222222
How to verify whether the contents of two files are the same
STM32——一线协议之DS18B20温度采样
访问url 404 的错误
Poj-3486-computers[dynamic planning]
[applet project development -- Jingdong Mall] classified navigation area of uni app
ctfshow爆破wp
[PR # 5 A] two way running (state pressure DP)
Mouse over effect I
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
EtherCAT原理概述
Mouse over effect VI
MySQL knowledge points