Last active
February 11, 2018 08:48
-
-
Save alexbrickwedde/21bd84693f0749a6ac8121bb72026dd1 to your computer and use it in GitHub Desktop.
STM32 sleep with RTC alarm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void subSeconds (RTC_TimeTypeDef *ptTime, uint32_t p_uiSeconds) | |
{ | |
int8_t iHours = p_uiSeconds / 3600; | |
int8_t iMinutes = (p_uiSeconds / 60) % 60; | |
int8_t iSeconds = p_uiSeconds % 60; | |
if (iSeconds > ptTime->Seconds) | |
{ | |
iMinutes++; | |
iSeconds -= 60; | |
} | |
if (iMinutes > ptTime->Minutes) | |
{ | |
iHours++; | |
iMinutes -= 60; | |
} | |
if (iHours > ptTime->Hours) | |
{ | |
// iDays++; | |
iHours -= 24; | |
} | |
ptTime->Seconds -= iSeconds; | |
ptTime->Minutes -= iMinutes; | |
ptTime->Hours -= iHours; | |
} | |
uint8_t addSecToTime(RTC_TimeTypeDef *time, uint32_t seconds) | |
{ | |
time->Seconds += seconds % 60; | |
if (time->Seconds > 59) | |
{ | |
time->Seconds -= 60; | |
time->Minutes += 1; | |
} | |
time->Minutes += (seconds / 60) % 60; | |
if (time->Minutes > 59) | |
{ | |
time->Minutes -= 60; | |
time->Hours += 1; | |
} | |
time->Hours += (seconds / (60 * 60)) % 24; | |
if (time->Hours > 23) | |
{ | |
time->Hours -= 24; | |
} | |
return 1; | |
} | |
uint8_t CheckTimer_Expired() | |
{ | |
RTC_DateTypeDef tDate; | |
if (HAL_RTC_GetDate(&hrtc, &tDate, FORMAT_BIN) != HAL_OK) | |
{ | |
DebugBreak (); | |
} | |
if ((tDate.Date == 3) && (tDate.Month == 6) && (tDate.Year == 16)) | |
{ | |
return (0); | |
} | |
dbg_write_str("ex"); | |
return (1); | |
} | |
uint32_t DelayTimer_RTC(uint32_t uiSeconds) | |
{ | |
HAL_NVIC_DisableIRQ(RTC_IRQn); | |
RTC_AlarmTypeDef alarm; | |
alarm.AlarmTime.Hours = 0; | |
alarm.AlarmTime.Minutes = 0; | |
alarm.AlarmTime.Seconds = 0; | |
subSeconds (&alarm.AlarmTime, uiSeconds); | |
RTC_DateTypeDef tDate; | |
tDate.WeekDay = RTC_WEEKDAY_FRIDAY; | |
tDate.Date = 3; | |
tDate.Month = 6; | |
tDate.Year = 16; | |
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc); | |
if(HAL_RTC_WaitForSynchro(&hrtc) != HAL_OK) | |
{ | |
DebugBreak (); | |
} | |
if (HAL_RTC_SetDate(&hrtc, &tDate, FORMAT_BIN) != HAL_OK) | |
{ | |
DebugBreak (); | |
} | |
if (HAL_RTC_SetTime(&hrtc, &alarm.AlarmTime, FORMAT_BIN) != HAL_OK) | |
{ | |
DebugBreak (); | |
} | |
__HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc); | |
HAL_NVIC_EnableIRQ(RTC_IRQn); | |
__GPIOA_CLK_DISABLE(); | |
__GPIOB_CLK_DISABLE(); | |
while(1) | |
{ | |
HAL_IWDG_Refresh (&hiwdg); | |
if (HAL_RTC_GetTime(&hrtc, &alarm.AlarmTime, FORMAT_BIN) != HAL_OK) | |
{ | |
DebugBreak (); | |
} | |
if (CheckTimer_Expired()) | |
{ | |
break; | |
} | |
uint32_t uiStep = 86399 - ((((alarm.AlarmTime.Hours * 60) + alarm.AlarmTime.Minutes) * 60 ) + alarm.AlarmTime.Seconds); | |
if (uiStep > 10) | |
{ | |
uiStep = 10; | |
} | |
else if (uiStep < 2) | |
{ | |
break; | |
} | |
__GPIOA_CLK_DISABLE(); | |
__GPIOA_CLK_DISABLE(); | |
RCC_OscInitTypeDef RCC_OscInitStruct; | |
RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
RCC_PeriphCLKInitTypeDef PeriphClkInit; | |
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI; | |
RCC_OscInitStruct.HSIState = RCC_HSI_ON; | |
RCC_OscInitStruct.HSICalibrationValue = 16; | |
RCC_OscInitStruct.LSIState = RCC_LSI_ON; | |
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; | |
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL7; | |
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK | |
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; | |
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; | |
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | |
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; | |
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; | |
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC; | |
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; | |
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); | |
addSecToTime(&alarm.AlarmTime, uiStep); | |
alarm.Alarm = RTC_ALARM_A; | |
if (HAL_RTC_SetAlarm_IT(&hrtc, &alarm, FORMAT_BIN) != HAL_OK) | |
{ | |
DebugBreak(); | |
} | |
HAL_EnableDBGStopMode(); | |
HAL_SuspendTick(); | |
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU | PWR_FLAG_SB); | |
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); | |
HAL_ResumeTick(); | |
SystemClock_Config(); | |
} | |
__GPIOA_CLK_ENABLE(); | |
__GPIOB_CLK_ENABLE(); | |
dbg_write_str("-\r"); | |
return (HAL_OK); | |
} | |
void main() | |
{ | |
....... | |
HAL_PWR_EnableBkUpAccess(); | |
__HAL_RCC_BKP_CLK_ENABLE(); | |
__HAL_RCC_RTC_ENABLE(); | |
HAL_NVIC_SetPriority(RTC_IRQn, 0, 0); | |
HAL_NVIC_EnableIRQ(RTC_IRQn); | |
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0); | |
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); | |
........ | |
while(1) | |
{ | |
HAL_IWDG_Refresh (&hiwdg); | |
DoSomeAction(); | |
DelayTimer_RTC (60); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment