How to feed a dog in sleep mode of stm32?

Source: Internet
Author: User

The independent watchdog (iwdg) and low-power mode are often used in stm32 development. The watchdog is used to detect and solve faults caused by software errors, the low power consumption mode is used to save energy when the CPU does not need to continue running. The clock of the independent watchdog is provided by the independent RC oscillator (stm32f10x is generally 40 kHz). It is still valid even when the main clock fails, so it can work in stopped and standby modes. Moreover, once the independent watchdog is started, it cannot be stopped except for system reset. However, when the MCU enters the low-power mode, the system will reset frequently because the CPU stops running and cannot be fed to the dog. So how can we solve this problem? Is it impossible for an independent watchdog and a low-power mode to be used simultaneously?

In sleep mode, a good way is to use RTC to wake up regularly to feed the dog. For example, the interval between the reset of the watchdog is 10 s. Set the RTC alarm clock interruption time to 5 s before entering sleep mode. In this way, the dog is awakened once every 5s. You can solve this problem very well.

While (1) {// execute Task 1 (); Task 2 ();//.. // feed the dog dev_iwdg_feed (); // enters the standby mode switch if (m_benterstandbymode) {// enable external interruption, gpiob3, used to wake up dev_exti_enable (true) from the standby mode of MCU ); enterstopmode: // set the RTC alarm, which is triggered once every 5 seconds */dev_rtc_setalarm (5); // enter the stop mode (low power consumption ), pwr_enterstopmode (pwr_regulator_lowpower, pwr_stopentry_wfi) is awakened when an external interrupt is triggered; // If (dev_rtc_isalarm () {// dev_iwdg_feed (); // after the dog is fed, continue to enter the stop mode goto enterstopmode;} // disable the external interruption dev_exti_enable (false); // recover the system clock dev_clk_restore () after waking up in the stop mode ();}}

The complete reference code is as follows:

//************************************** **************************************** * **************/STM32F10x StopMode RTC Feed Dog // compiler: keil UV3 // 2013-01-04, by friehood //************************************ **************************************** * ***************** # include "stm32f10x_lib.h" # include "platform_config.h" static Boolean g_bRTCAlarm = FALSE; /******************************** **************************************** * ****** Function Name: RCC_Configuration * Description: Configures the different system clocks. * Input: None * Output: None * Return: none *************************************** **************************************** /void RCC_Configuration (void) {/* RCC system reset (for debug purpose) */RCC_DeInit ();/* Enable HSE */RCC_HSEConfig (RCC_HSE_ON);/* Wait till H SE is ready */if (RCC_WaitForHSEStartUp () = SUCCESS) {/* Enable Prefetch Buffer */FLASH_PrefetchBufferCmd (FLASH_PrefetchBuffer_Enable); // FLASH timing control // recommended value: SYSCLK = 0 ~ 24 MHz Latency = 0 // SYSCLK = 24 ~ 48 MHz Latency = 1 // SYSCLK = 48 ~ 72 MHz Latency = 2 // FLASH_SetLatency (FLASH_Latency_1); // warning: Changing to 1 will affect the DMA value (for example, the ADC acquisition value will be misplaced) FLASH_SetLatency (FLASH_Latency_2 ); /* HCLK = SYSCLK */RCC_HCLKConfig (response);/* PCLK2 = HCLK */RCC_PCLK2Config (response);/* PCLK1 = HCLK/2 */RCC_PCLK1Config (response ); /* PLLCLK = 12 MHz * 3 = 36 MHz */RCC_PLLConfig (RCC_PLLSource_HSE_Div1, RCC_PLLMul_3);/* Enable PLL */RCC_PLLCmd (ENABLE);/* Wait til L PLL is ready */while (RCC_GetFlagStatus (RCC_FLAG_PLLRDY) = RESET) {}/ * Select PLL as system clock source */RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK ); /* Wait till PLL is used as system clock source */while (RCC_GetSYSCLKSource ()! = 0x08) {}}/* Enable PWR and BKP clock */RCC_APB1PeriphClockCmd (temperature | temperature, ENABLE);/* Enable AFIO clock */RCC_APB2PeriphClockCmd (temperature, ENABLE );} /*************************************** **************************************** * Function Name: NVIC_Configuration * Description: Configures the nested vectored interrupt controller. * Input: None * Output: None * Return: none *************************************** **************************************** /void NVIC_Configuration (void) {NVIC_InitTypeDef NVIC_InitStructure; # ifdef VECT_TAB_RAM/* Set the Vector Table base location at 0x20000000 */NVIC_SetVectorTable (NVIC_VectTab_RAM, 0x0 ); # else/* VECT_TAB_FLASH * // * Set the Vector Table base location at 0x08000000 */NVIC_SetVectorTable (NVIC_VectTab_FLASH, 0x0); # endif/* Configure one bit for preemption priority */NVIC_PriorityGroupConfig (NVIC_PriorityGroup_1 );} /*************************************** **************************************** * Function Name: invalid ick_configuration * Description: Configures the specified ick to generate an interrupt each 1 millisecond. * Input: None * Output: None * Return: none *************************************** ***** * **********************************/Void SysTick_Configuration (void) {/* Select AHB clock (HCLK) as your ick clock source */SysTick_CLKSourceConfig (optional);/* Set Your ick Priority to 3 */NVIC_SystemHandlerPriorityConfig (SystemHandler_SysTick, 3, 0 ); /* SysTick interrupt each 1 ms with HCLK equal to 72 MHz */SysTick_SetReload (72000);/* Enable the specified ick Interrupt */SysTick_ITConf Ig (ENABLE );} /*************************************** **************************************** * Function Name: delay * Description: Inserts a delay time. * Input: nTime: specifies the delay time length, in milliseconds. * Output: None * Return: none *************************************** **************************************** /void Delay (u32 nTime) {/* Enable the specified ick Counter */SysTick_CounterC Md (effecick_counter_enable); TimingDelay = nTime; while (TimingDelay! = 0);/* Disable the specified ick Counter */SysTick_CounterCmd (SysTick_Counter_Disable);/* Clear the specified ick Counter */SysTick_CounterCmd (SysTick_Counter_Clear );} /*************************************** **************************************** * Function Name: RTC_Configuration * Description: Configures RTC clock source and prescaler. * Input: None * Output: None * Return: None ************************ **************************************** * **************/Void RTC_Configuration (void) {EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;/* RTC clock source configuration failed * // * Allow access to BKP Domain */PWR_BackupAccessCmd (ENABLE ); /* Reset Backup Domain */BKP_DeInit ();/* Enable the lsi osc */RCC_LSICmd (ENABLE);/* Wait till LSI is read Y */while (RCC_GetFlagStatus (RCC_FLAG_LSIRDY) = RESET) {}/ * Select the RTC Clock Source */RCC_RTCCLKConfig (RCC_RTCCLKSource_LSI ); /* Enable the RTC Clock */RCC_RTCCLKCmd (ENABLE);/* RTC configuration restart * // * Wait for rtc apb registers synchronisation */RTC_WaitForSynchro ();/* Set RTC prescaler: set RTC period to 1sec */RTC_SetPrescaler (40000);/* Wa It until last write operation on RTC registers has finished */RTC_WaitForLastTask ();/* Enable the RTC Alarm interrupt */RTC_ITConfig (RTC_IT_ALR, ENABLE ); /* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask ();/* Configure EXTI Line17 (RTC Alarm) to generate an interrupt on rising edge */EXTI_ClearITPendingBit (EXTI_Line17); EXTI_InitStructure.EXTI_Line = EXTI_Line17; Token = token; token = EXTI_Trigger_Rising; token = ENABLE; EXTI_Init (& EXTI_InitStructure);/* Enable the RTC Interrupt */token = RTCAlarm_IRQChannel; token = 0; token = 0; token QChannelCmd = ENABLE; NVIC_Init (& NVIC_InitStructure );} /*************************************** **************************************** * Function Name: RTCAlarm_IRQHandler * Description: This function handles RTC Alarm interrupt request. * Input: None * Output: None * Return: none *************************************** **************************************** /void RTCAlarm_IRQHandler (void) {if (RTC_Ge TITStatus (RTC_IT_ALR )! = RESET) {/* Set the RTC alarm flag */g_bRTCAlarm = TRUE;/* Clear EXTI line17 pending bit */EXTI_ClearITPendingBit (EXTI_Line17 ); /* Check if the Wake-Up flag is set */if (PWR_GetFlagStatus (PWR_FLAG_WU )! = RESET) {/* Clear Wake Up flag */PWR_ClearFlag (PWR_FLAG_WU);}/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask (); /* Clear RTC Alarm interrupt pending bit */RTC_ClearITPendingBit (RTC_IT_ALR);/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask ();}} /*************************************** **************************************** * Function Name: dev_rtc_setAlarm * Description: sets the RTC alarm. * Input: alert time * Output: None * Return: none *************************************** **************************************** /void dev_rtc_setAlarm (u32 AlarmValue) {/* Clear the rtc sec flag */RTC_ClearFlag (RTC_FLAG_SEC);/* Wait clear RTC flag sccess */while (RTC_GetFlagStatus (RTC_FLAG_SEC) = RESET ); /* Wait until last write operation on RTC r Egisters has finished */RTC_WaitForLastTask ();/* Sets the RTC alarm value */RTC_SetAlarm (RTC_GetCounter () + AlarmValue ); /* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask ();} /*************************************** **************************************** * Function Name: dev_rtc_isAlarm * Description: whether to trigger * Input: None * Output: None * Return: TRUE: triggered, FALSE, ************************************** **************************************** */Boolean dev_rtc_isAlarm (void) {if (g_bRTCAlarm) {/* Clear the RTC alarm flag */g_bRTCAlarm = FALSE; return TRUE;} return FALSE;} void dev_iwdg_init (void) {/* Enable write access to IWDG_PR and IWDG_RLR registers */IWDG_WriteAccessCmd (IWDG_WriteAccess_Enable);/* IWDG counter clock: 40 KHz (LSI)/256 = 0.15625 KHz */I WDG_SetPrescaler (IWDG_Prescaler_256);/* Set counter reload value to 1562 */IWDG_SetReload (1562); // 10 s/* Reload iwdgdg counter */IWDG_ReloadCounter (); /* Enable IWDG (the LSI oscillator will be enabled by hardware) */IWDG_Enable ();} void dev_iwdg_feed (void) {IWDG_ReloadCounter ();} /*************************************** **************************************** * Function Name: dev_clk_restore * Descripti On: Restore system clock after wake-up from STOP: enable HSE, PLL * and select PLL as system clock source. * Input: None * Output: None * Return: none *************************************** **************************************** /void dev_clk_restore (void) {/* Enable HSE */RCC_HSEConfig (RCC_HSE_ON);/* Wait till HSE is ready */HSEStartUpStatus = RCC_WaitForHSEStartUp (); if (HSEStartUpStatus = SU CCESS) {/* Enable PLL */RCC_PLLCmd (ENABLE);/* Wait till PLL is ready */while (RCC_GetFlagStatus (RCC_FLAG_PLLRDY) = RESET) {}/* Select PLL as system clock source */RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);/* Wait till PLL is used as system clock source */while (RCC_GetSYSCLKSource ()! = 0x08) {}}}/*********************************** **************************************** * *** Function Name: EXTI_Configuration * Description: Configures EXTI Line3. * Input: None * Output: None * Return: none *************************************** **************************************** /void EXIT_Configuration (void) {EXTI_InitTypeDef EXTI_InitStructure; GPIO_EXTILineConfig (GPIO_PortSourceGPIOB, GP Io_pinsoure3); reverse (EXTI_Line3); reverse = EXTI_Line3; reverse = reverse; reverse = ENABLE; EXTI_Init (& EXTI_InitStructure);} void dev_exti_enable (Boolean bEnable) {NVIC_InitTypeDef NVIC_InitStructure;/* Clear the Key Button EXTI line pending bit */EXTI_Cl EarITPendingBit (EXTI_Line3); NVIC_ClearIRQChannelPendingBit (EXTI3_IRQChannel); NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel; cost = 0; cost = bEnable? ENABLE: DISABLE; NVIC_Init (& NVIC_InitStructure );} /*************************************** **************************************** * Function Name: main * Description: Main program. * Input: None * Output: None * Return: none *************************************** **************************************** /int main (void) {/* System Clocks Configuration */RCC_Configuration ();/* using C configuration */NVIC_Configuration ();/* Configure RTC clock source and prescaler */RTC_Configuration (); /* Configure the specified ick to generate an interrupt each 1 millisecond */SysTick_Configuration ();/* Configures EXTI Line3 */EXIT_Configuration ();/* IWDG initialize */dev_iwdg_init (); while (1) {// execute Task 1 (); Task 2 ();//.. // feed the dog dev_iwdg_feed (); // enters the standby mode switch if (m_bEnterStandByMode) {// enable external interruption, GPIOB3, used to wake up dev_exti_enable (TRUE) from the standby mode of MCU ); ENTERSTOPMODE: // set the RTC alarm, which is triggered once every 5 seconds */dev_rtc_setAlarm (5); // enter the stop mode (low power consumption ), PWR_EnterSTOPMode (PWR_Regulator_LowPower, PWR_STOPEntry_WFI) is awakened when an external interrupt is triggered; // if (dev_rtc_isAlarm () {// dev_iwdg_feed (); // after the dog is fed, continue to enter the stop mode goto ENTERSTOPMODE;} // disable the external interruption dev_exti_enable (FALSE); // recover the system clock dev_clk_restore () after waking up in the stop mode ();}}}

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.