#include "STM32_StopMode_RTC_wakeup.h"
#define RTC_IRQChannel RTC_IRQn
static ErrorStatus HSEStartUpStatus;
//RTC闹钟初始化:启动时钟、配置LSI做RTC时钟、设置预分频40000得到1Hz
void StopMode_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* RTC clock source configuration ----------------------------------------*/
/* 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);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(40000);
/* 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_Mode = EXTI_Mode_Event;
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
/*******************************************************************************
* Function Name : SYSCLKConfig_STOP
* Description : Configures system clock after wake-up from STOP: enable HSE, PLL
* and select PLL as system clock source.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void StopMode_SYSCLKConfig_STOP(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)
{
}
}
}
//设置待机时间并进入停止,闹钟事件发生自动退出Stop模式
//进入STOP模式,闹钟唤醒后跳出函数运行
//RAM、GPIO状态保持
//s为单位秒
void StopMode_EnterStopMode(u32 s)
{
//设置待机时间
RTC_SetAlarm(RTC_GetCounter() + s);
RTC_WaitForLastTask();
/* LDO不关掉,闹钟事件唤醒*/
PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFE);
//唤醒后重新配置系统时钟
StopMode_SYSCLKConfig_STOP();
}
- 1
- 2
前往页