STM32L151,使用STM32CUBE生成的代码,系统使用HSI时钟,PLL到48MHz,RTC使用LSI。RTC配置用于WAKEUP。
RTC初始化函数如下:
/* RTC init function */
void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
/**Initialize RTC and set the Time and Date
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&hrtc);
sTime.Hours = 0x0;
sTime.Minutes = 0x0;
sTime.Seconds = 0x0;
sTime.TimeFormat = RTC_HOURFORMAT12_AM;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 0x1;
sDate.Year = 0x15;
HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);
/**Enable the WakeUp
*/
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, RTC_WAKEUP_COUNTER, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
RTC->ISR &= (~RTC_ISR_WUTF);
}
进入STOP模式代码:
void EnterStopMode(void){
#if !defined( BASE_STATION )
if((MainState & (MainState_AcOk | MainState_Charging)) == 0){
MainState = (MainState_TypeDef)((MainState & 0xff00) | MainState_Stop);
key_exti();
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
}
#endif
}
在执行完HAL_PWR_EnterSTOPMode后,系统的电流从29mA下降到14mA。但从此不再有响应,RTC也无法唤醒。经测试,如果注释掉HAL_PWR_EnterSTOPMode后,RTC的wakeup中断是有正常产生的。 |