STM32 Learning Notes (ix) external interrupts, Standby mode, and event wake-up

Source: Internet
Author: User

Learning knowledge only needs to accumulate and improve, but how to explain the knowledge system requires deep cognition and systematic understanding. External interrupt and event learning is not very difficult, but it is related to STM32 Power control section, it is worth serious understanding, in this article I will take the actual code as an example of the detailed explanation of these content, I hope to each reader to help.

 1. External interrupts

If you have learned the Systick system clock tick experiment, master the knowledge of the cortex-m3 interrupt, then the external interrupt is better understood, and the Systick interrupt, the external interrupt is also when the signal is triggered, if the interrupt screen register allows triggering, there will be an interrupt, When the CPU finds the interrupt vector table and finds the entry function, the relevant code is executed correctly, because the external interrupt itself is based on the rising or falling edge of the common Gpio port, so in this case the key is used as the hardware circuit for testing the external interrupt.

According to the working principle diagram: KEY1 ~ PC4; KEY2 ~ PB10;

KEY3 ~ PC13; KEY4 ~ PA0;

where key1,2,3 as the external interrupt pin, KEY4 as the wake-up pin, followed by the explanation.

Knowing the schematic of the work, it is necessary to determine its corresponding interrupt line number, this in the reference manual external interrupt/Event Line Image section is described in detail, here:

From the above can be seen, the same position in different areas of the pin common in the same break number, and so on, the above four pins respectively corresponding to the interrupt line number is EXIT4, EXIT10, Exit13,exit0, here is also a point of attention: That is, a middle wire can only be reversed to a pin, which requires the design of external interrupt hardware circuitry should not be repeated, such as PA0 and PB0 can not be designed as an external interrupt.

  Knowing this, if you are familiar with the STM32 design process, then you know that the order is:

1. External interrupt pin GPIO initialization, code as follows:

/***************************************************************** function:exti_gpio_config* Description:                External interrupt trigger corresponding GPIO port configuration KEY1 PC4 External interrupt 4 KEY2 PB10 external interrupt KEY3 PC13 external interrupt 13 KEY4 PA0 Wakeup Wake-up event * Input: None * Output: No **************************************************************** */voidExti_gpio_config (void) {gpio_inittypedef gpio_initstructure; Rcc_apb2periphclockcmd (Rcc_apb2periph_gpioa|Rcc_apb2periph_gpiob| RCC_APB2PERIPH_GPIOC |Rcc_apb2periph_afio, ENABLE); Gpio_initstructure.gpio_mode=Gpio_mode_ipu; Gpio_initstructure.gpio_speed=Gpio_speed_50mhz; Gpio_initstructure.gpio_pin=Exti_key1_pin; Gpio_init (Exti_key1_port,&gpio_initstructure);        Gpio_extilineconfig (Exti_key1_portsource, Exti_key1_pinsource); //re-use Gpio as an external middle hole trigger portGpio_initstructure.gpio_pin=Exti_key1_pin; Gpio_init (Exti_key2_port,&gpio_initstructure);        Gpio_extilineconfig (Exti_key2_portsource, Exti_key2_pinsource); Gpio_initstructure.gpio_pin=Exti_key3_pin; Gpio_init (Exti_key3_port,&gpio_initstructure);        Gpio_extilineconfig (Exti_key3_portsource, Exti_key3_pinsource); Gpio_initstructure.gpio_mode=GPIO_MODE_IPD; Gpio_initstructure.gpio_pin=Exti_key4_pin; Gpio_init (Exti_key4_port,&gpio_initstructure); Gpio_extilineconfig (Exti_key4_portsource, Exti_key4_pinsource);}

2. Peripheral Exti Initialization

3. Allow interrupt triggering in interrupt screen register

/***************************************************************** function:exti_mode_config* Description: External interrupt trigger Configuration and vector table open corresponding interrupt where KEY1 as interrupt into _WFE mode KEY2 and KEY3 as normal interrupt lit corresponding LED light KEY4 as an event used to wake the CPU * Input: None * Output: No *****************************************************************/voidExti_mode_config (void) {exti_inittypedef exti_initstructure;        Nvic_inittypedef nvic_initstructure;        Exti_deinit (); Exti_initstructure.exti_mode= Exti_mode_interrupt;//external key trigger interruptExti_initstructure.exti_trigger = exti_trigger_falling;//Falling Edge TriggerExti_initstructure.exti_linecmd = ENABLE;//External interrupt EnableExti_initstructure.exti_line = Exti_key1_line;//outside the wire break number 4Exti_init (&exti_initstructure); Exti_initstructure.exti_line= Exti_key2_line;//outside the wire break number tenExti_init (&exti_initstructure); Exti_initstructure.exti_line= Exti_key3_line;//External break numberExti_init (&exti_initstructure); Exti_initstructure.exti_mode= Exti_mode_event;//External Key Trigger eventExti_initstructure.exti_line = Exti_key4_line;//outside the wire break number 0Exti_init (&exti_initstructure);                                //pwr_wakeuppincmd (ENABLE); //pa0 as wake-up pin enablenvic_setvectortable (Nvic_vecttab_flash,0x0);//vector table in Flash, offset 0Nvic_prioritygroupconfig (nvic_prioritygroup_2); /*activates the corresponding interrupt line number in the vector table*/Nvic_initstructure.nvic_irqchannelcmd=ENABLE; Nvic_initstructure.nvic_irqchannelsubpriority=0; Nvic_initstructure.nvic_irqchannelpreemptionpriority=0; Nvic_initstructure.nvic_irqchannel=exti_key1_irqn; Nvic_init (&nvic_initstructure); //nvic_initstructure.nvic_irqchannelsubpriority = 0;//nvic_initstructure.nvic_irqchannelpreemptionpriority = 1;//Nvic_initstructure.nvic_irqchannel = exti0_irqn; //Nvic_init (&nvic_initstructure);nvic_initstructure.nvic_irqchannelsubpriority=0; Nvic_initstructure.nvic_irqchannelpreemptionpriority=2; Nvic_initstructure.nvic_irqchannel=exti_key2_3_irqn; Nvic_init (&nvic_initstructure);}

4. Interrupt function Processing

The key PC4 corresponds to the external interrupt entry, because the key is high by default, when the button is pressed, it will produce a falling edge signal, triggering the interrupt, at this time the CPU in the Interrupt vector table Query external interrupt entry address, such as PC4 corresponding entry address is void Exti4_irqhandler (void ), start execution of the interrupt, the implementation process can refer to the Systick chapter.

/***************************************************************** function:exti4_irqhandler* Description: Outside Part Interrupt 4 entry function for LED light and system enter _WFE mode * Input: None * Output: No ****************************************************** ***********/voidExti4_irqhandler (void) {led_light_up (0);                                      Exti_clearflag (exti_line4); //Clear the break number 4Pwr_enterstopmode (Pwr_regulator_lowpower, PWR_STOPENTRY_WFE);//Enter WFE stop low power mode only event can wake upSysteminit ();//exit Stop mode to initialize the clock because switching to the internal clock during sleepLed1_off ();}

In the Stm32 header and startup file design, the break number 10~15 share the same entry function, so it needs to be judged in the interrupt to determine if the signal triggered the interrupt.

 ****** function:exti15_10_irqhandler* Description: External interrupt 10 and 13 A shared interrupt entry that determines the PIN that triggers the interrupt and executes the code by triggering a post-state check. * Input: None * Output: no ****************************************************************  */ void  Exti15_10_irqhandler (void      = Exti_getitstatus (EXTI_LINE10); //     get the state of external interrupt 10      Exti13_status = Exti_getitstatus (EXTI_LINE13);  
If

Led_light_up (1

If

Led_light_up (2
}
/**/
}

The external interrupt involves not much knowledge, but careful observation of the code above, you will find that KEY4 (PA0) is not configured to interrupt, and configured for the event, and is set to wake-up pin, KEY1 (PC4) in the interrupt not only lit the LED, also called Pwr_enterstopmode (Pwr_regulator_lowpower, PWR_STOPENTRY_WFE); This feature is actually useful, involving low power and external event wakeup, which I'll explain in detail below.

2. Differences and linkages between external events and interruptions

external events and interrupts are triggered by a pin signal, for reference:

This diagram can be very intuitive to see the difference between interrupts and events, when the external signal input, if through the event mask register, then the event signal into the pulse trigger, triggering a pulse signal, directly to the corresponding peripheral, for triggering, this is a pure hardware process, understanding the DMA should know that this way does not require CPU involvement , but this also has its drawbacks, such as a single function, only to provide a signal, can not provide information, which can only produce the specified function of the event. If the blocking register is interrupted, it is sent directly to the CPU, resulting in interrupts, such as entering the upper entry function to begin processing. From this, it can be seen that the event is a purely hardware-triggered process, related to the design support of the CPU itself, while the interruption can be software to achieve a variety of functions, while the low-power mode and event wake-up is one of the events supported by Stm32.

3. Low power mode and event wake-up

The low-power mode is an important feature of the embedded design for removable devices, where the CPU shuts down the clock, which reduces battery consumption and prolongs single-use times.

The above entry is the shutdown-low power-wfe mode, so it can only be awakened by the key 4 event. In fact, the low-power mode is still waiting for the machine mode and sleep mode, here is not used, and later if used to explain. Interrupts cannot be awakened, in addition, when the CPU exits the stop mode, it is forced to switch to the internal clock, so after entering the stop mode, add systeminit () to reconfigure the system clock to avoid clock changes. Specific code reference: http://files.cnblogs.com/files/zc110747/7.EXTI_LED.7z

STM32 Learning Notes (ix) external interrupts, Standby mode, and event wake-up

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.