Preface
- In RT-Thread, there are many operations for global interrupts. Everyone knows the [disable] and [enable] of global interrupts.
- It is necessary to correctly understand the disable and enable of the global interrupt.
- As follows, to ensure the correct operation of the linked list, [interrupt protection]
/* lock interrupt */
temp = rt_hw_interrupt_disable();
/* remove from old list */
rt_list_remove(&(object->list));
/* unlock interrupt */
rt_hw_interrupt_enable(temp);
Problem and analysis:
- After rt_hw_interrupt_disable, can the interrupt be triggered?
- After actual verification, and analysis of the implementation method of rt_hw_interrupt_disable: The following conclusions are obtained:
- rt_hw_interrupt_disable only shields the global [interrupt request], and configures the enabled interrupt, and it can still [interrupt], but the interrupt processing function ISR will not be executed temporarily.
- In other words, if the hardware configuration enables interrupts, after rt_hw_interrupt_disable, it still has the [interrupt] function. For example, you configure a GPIO external interrupt and enable the interrupt. After rt_hw_interrupt_disable, an external interrupt can be generated, but the execution [ISR] is not triggered.
- [Key]: After rt_hw_interrupt_disable, the triggered interrupt, ISR cannot be executed temporarily, but the hardware normally sets the [pending] interrupt flag bit.
- After rt_hw_interrupt_enable, the interrupt of [pending] will execute the interrupt [ISR] handler.
Concept combing
- Single interrupt generation: configure to enable hardware interrupts, such as buttons, GPIO external interrupts, press to trigger interrupts, hardware sets [pending] interrupt flag bit, and executes interrupts [ISR]
- Disabling a single interrupt: Configure to disable hardware interrupts, such as a button, and pressing it will no longer trigger the interrupt. Even if there is an [ISR] interrupt processing function, it will not be executed.
- Prohibition of global interrupts: that is, no physical interrupts will be triggered anymore, [this function is not found]!
- Global interrupt request shielding: rt_hw_interrupt_disable, the triggered interrupt can only set the [pending] interrupt flag bit, and the CPU continues to execute the operation.
- For mutually exclusive or critical operations, you can use the method of shielding the global interrupt request + processing + opening the global interrupt request, allowing the operation to complete the operation in an exclusive manner.
Sort out the sleep process in RT-Thread PM
- Sleep operation is performed in: idle thread (lowest thread priority)
- Before entering SLEEP, global interrupt request is prohibited! rt_hw_interrupt_disable
- Pin processing before sleep, etc., enter sleep, CPU stops running, [freeze] is here! !
- During sleep, it can still be triggered by [enable interrupts], such as key GPIO external interrupt, LPTimer, etc., and wake up the CPU.
- After the CPU wakes up, continue to the next operation, [Processing after sleep]
- PM exits sleep and turns on the global interrupt request rt_hw_interrupt_enable. At this time, the response interrupt ISR is turned on.
- According to the interrupt [pending] flag bit, enter the wake-up interrupt processing function [ISR] and execute it.
- In this way, during deep sleep, the system clock is stopped and the pins are turned off. When waking up, the clock and pins can be restored! [The whole process of sleep is complete]
- Don't worry about being cut out by interruption, or even task switching, causing the system to run on the wrong clock.
- Don't worry about turning off the global interrupt request, buttons, LPTimer, etc., can not trigger the interrupt and wake up the MCU.
![2021-02-24_142531.png](https://img-blog.csdnimg.cn/img_convert/3c5d49c06fd5a947cb0b9a58b8758a00.png)
to sum up
- Need a correct understanding of interrupt triggering and execution flow
- Correctly understand the operation of RT-Thead global interrupt: rt_hw_interrupt_disable and rt_hw_interrupt_enable