久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標(biāo)題:
STM32 休眠模式下如何喂狗?
[打印本頁]
作者:
51黑黑黑
時間:
2016-3-4 17:51
標(biāo)題:
STM32 休眠模式下如何喂狗?
在STM32開發(fā)中經(jīng)常會用到獨立看門狗(IWDG)和低功耗模式,看門狗是為了檢測和解決由軟件錯誤引起的故障,低功耗模式是為了在CPU不需要繼續(xù)運行時進入到休眠模式用以節(jié)省電能。其中獨立看門狗的時鐘由獨立的RC振蕩器(STM32F10x一般為40kHz)提供,即使在主時鐘出現(xiàn)故障時,也仍然有效,因此可以在停止和待機模式下工作。而且獨立看門狗一旦啟動,除了系統(tǒng)復(fù)位,它不能再被停止。但這樣引發(fā)的一個問題是當(dāng)MCU進入到低功耗模式后由于CPU停止運行無法喂狗,會導(dǎo)致系統(tǒng)頻繁復(fù)位。那如何解決這個問題呢,難道獨立看門狗和低功耗模式?jīng)]法同時使用?
一個很好的方式是在休眠模式下通過RTC定時喚醒來喂狗,喂完夠在進入繼續(xù)進入到休眠模式。比如看門狗復(fù)位的時間間隔為10s。那么在進入休眠模式前設(shè)置RTC鬧鐘中斷時間為5s。這樣每隔5s喚醒一次喂一次狗。便可以很好的解決這個問題。
while(1)
{
// 執(zhí)行任務(wù)
Task1();
Task2();
// ..
// 喂狗
dev_iwdg_feed();
// 進入待機模式開關(guān)
if(m_bEnterStandByMode)
{
// 使能外部中斷,GPIOB3,用以MCU從待機模式喚醒
dev_exti_enable(TRUE);
ENTERSTOPMODE:
// 設(shè)置RTC鬧鐘,5秒鐘產(chǎn)生一次RTC鬧鐘中斷*/
dev_rtc_setAlarm(5);
// 進入停止模式(低功耗),直至外部中斷觸發(fā)時被喚醒
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
// 是否是RTC鬧鐘中斷喚醒
if(dev_rtc_isAlarm())
{
// 喂狗
dev_iwdg_feed();
// 喂完狗繼續(xù)進入停止模式
goto ENTERSTOPMODE;
}
// 禁止外部中斷
dev_exti_enable(FALSE);
// 從停止模式喚醒后恢復(fù)系統(tǒng)時鐘
dev_clk_restore();
}
}
復(fù)制代碼
以下是完整的參考代碼:
//**********************************************************************************************
// 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 HSE is ready */
if(RCC_WaitForHSEStartUp() == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//FLASH時序控制
//推薦值:SYSCLK = 0~24MHz Latency=0
// SYSCLK = 24~48MHz Latency=1
// SYSCLK = 48~72MHz Latency=2
//FLASH_SetLatency(FLASH_Latency_1); //警告:修改為1會對DMA值有影響(如ADC采集值會錯位)
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 12MHz * 3 = 36 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_3);
/* 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)
{
}
}
/* Enable PWR and BKP clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Enable AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, 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 : SysTick_Configuration
* Description : Configures the SysTick to generate an interrupt each 1 millisecond.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SysTick_Configuration(void)
{
/* Select AHB clock(HCLK) as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
/* Set SysTick Priority to 3 */
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
/* SysTick interrupt each 1ms with HCLK equal to 72MHz */
SysTick_SetReload(72000);
/* Enable the SysTick Interrupt */
SysTick_ITConfig(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 SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
TimingDelay = nTime;
while(TimingDelay != 0);
/* Disable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* Clear the SysTick 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 ------------------------------------------*/
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
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 -------------------------------------------------------*/
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(40000);
/* Wait 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;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable the RTC Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = 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_GetITStatus(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 : 設(shè)置RTC鬧鐘.
* Input : 鬧鐘時間
* 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 registers 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 : RTC鬧鐘是否觸發(fā)
* Input : None
* Output : None
* Return : TRUE:已觸發(fā),F(xiàn)ALSE,未觸發(fā)
*******************************************************************************/
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: 40KHz(LSI) / 256 = 0.15625 KHz */
IWDG_SetPrescaler(IWDG_Prescaler_256);
/* Set counter reload value to 1562 */
IWDG_SetReload(1562); // 10s
/* Reload IWDG 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
* Description : 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 == SUCCESS)
{
/* 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,GPIO_PinSource3);
EXTI_ClearITPendingBit(EXTI_Line3);
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void dev_exti_enable(Boolean bEnable)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Clear the Key Button EXTI line pending bit */
EXTI_ClearITPendingBit(EXTI_Line3);
NVIC_ClearIRQChannelPendingBit(EXTI3_IRQChannel);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = 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();
/* NVIC configuration */
NVIC_Configuration();
/* Configure RTC clock source and prescaler */
RTC_Configuration();
/* Configure the SysTick to generate an interrupt each 1 millisecond */
SysTick_Configuration();
/* Configures EXTI Line3 */
EXIT_Configuration();
/* IWDG initialize*/
dev_iwdg_init();
while(1)
{
// 執(zhí)行任務(wù)
Task1();
Task2();
// ..
// 喂狗
dev_iwdg_feed();
// 進入待機模式開關(guān)
if(m_bEnterStandByMode)
{
// 使能外部中斷,GPIOB3,用以MCU從待機模式喚醒
dev_exti_enable(TRUE);
ENTERSTOPMODE:
// 設(shè)置RTC鬧鐘,5秒鐘產(chǎn)生一次RTC鬧鐘中斷*/
dev_rtc_setAlarm(5);
// 進入停止模式(低功耗),直至外部中斷觸發(fā)時被喚醒
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
// 是否是RTC鬧鐘中斷喚醒
if(dev_rtc_isAlarm())
{
// 喂狗
dev_iwdg_feed();
// 喂完狗繼續(xù)進入停止模式
goto ENTERSTOPMODE;
}
// 禁止外部中斷
dev_exti_enable(FALSE);
// 從停止模式喚醒后恢復(fù)系統(tǒng)時鐘
dev_clk_restore();
}
}
}
復(fù)制代碼
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
日韩一区二区视频
|
亚洲 中文 欧美 日韩 在线观看
|
国产免费一区
|
欧美综合一区二区
|
黄网站免费在线观看
|
91香蕉
|
91亚洲一区
|
av黄色在线观看
|
亚洲综合精品
|
欧美不卡视频
|
国产精品免费观看
|
欧美日韩国产一区二区三区不卡
|
cao在线
|
国产黄色大片在线观看
|
爱草在线
|
欧美激情视频一区二区三区免费
|
中文字幕在线人
|
男人的天堂视频网站
|
亚洲免费视频一区
|
综合第一页
|
黄色大片在线视频
|
久久99精品久久久久婷婷
|
久草中文在线观看
|
天堂在线网
|
国产精品日韩
|
中文字幕福利视频
|
国产精品无码久久久久
|
亚洲精品久久久久久宅男
|
国产精品久久久久一区二区三区
|
久久久在线视频
|
亚洲国产精品久久久
|
日韩成人在线免费观看
|
久久精品成人一区
|
午夜丰满寂寞少妇精品
|
伊人色综合久久天天五月婷
|
久久国产精品亚洲
|
国产亚洲成av人在线观看导航
|
亚洲精品视频在线看
|
国内精品久久久久
|
99久久亚洲
|
精品九九久久
|