Mike Fiore
/
stm32f4-stop-mode
testing stop mode on stm32f4 devices
main.cpp@0:424b9fd546c0, 2016-01-20 (annotated)
- Committer:
- mfiore
- Date:
- Wed Jan 20 20:57:45 2016 +0000
- Revision:
- 0:424b9fd546c0
remove rtos library from project and device will no longer wake up from stop mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:424b9fd546c0 | 1 | #include "mbed.h" |
mfiore | 0:424b9fd546c0 | 2 | #include <time.h> |
mfiore | 0:424b9fd546c0 | 3 | #include <rtc_api.h> |
mfiore | 0:424b9fd546c0 | 4 | |
mfiore | 0:424b9fd546c0 | 5 | int is_rtc_enabled(void) { |
mfiore | 0:424b9fd546c0 | 6 | return (RTC ->ISR & RTC_ISR_INITS ) && (RTC ->ISR & RTC_ISR_RSF ); |
mfiore | 0:424b9fd546c0 | 7 | } |
mfiore | 0:424b9fd546c0 | 8 | |
mfiore | 0:424b9fd546c0 | 9 | int main() { |
mfiore | 0:424b9fd546c0 | 10 | // attach our custom RTC enabled function so time can survive across sleeps & resets |
mfiore | 0:424b9fd546c0 | 11 | attach_rtc(&rtc_read, &rtc_write, &rtc_init, &is_rtc_enabled); |
mfiore | 0:424b9fd546c0 | 12 | if (!is_rtc_enabled()) |
mfiore | 0:424b9fd546c0 | 13 | set_time(0); |
mfiore | 0:424b9fd546c0 | 14 | RTC_HandleTypeDef RtcHandle; |
mfiore | 0:424b9fd546c0 | 15 | RtcHandle.Instance = RTC; |
mfiore | 0:424b9fd546c0 | 16 | |
mfiore | 0:424b9fd546c0 | 17 | int interval = 4; |
mfiore | 0:424b9fd546c0 | 18 | |
mfiore | 0:424b9fd546c0 | 19 | while(true) { |
mfiore | 0:424b9fd546c0 | 20 | |
mfiore | 0:424b9fd546c0 | 21 | __PWR_CLK_ENABLE(); |
mfiore | 0:424b9fd546c0 | 22 | HAL_PWR_EnableBkUpAccess(); |
mfiore | 0:424b9fd546c0 | 23 | |
mfiore | 0:424b9fd546c0 | 24 | HAL_PWREx_EnableMainRegulatorLowVoltage(); |
mfiore | 0:424b9fd546c0 | 25 | HAL_PWREx_EnableFlashPowerDown(); |
mfiore | 0:424b9fd546c0 | 26 | HAL_PWREx_EnableLowRegulatorLowVoltage(); |
mfiore | 0:424b9fd546c0 | 27 | |
mfiore | 0:424b9fd546c0 | 28 | // make sure RTC and LSE stay on while we're asleep |
mfiore | 0:424b9fd546c0 | 29 | RCC ->BDCR |= RCC_BDCR_LSEON; |
mfiore | 0:424b9fd546c0 | 30 | RCC ->BDCR |= RCC_BDCR_RTCEN; |
mfiore | 0:424b9fd546c0 | 31 | |
mfiore | 0:424b9fd546c0 | 32 | printf("configuring wakeup timer for %lu seconds from now\r\n", interval); |
mfiore | 0:424b9fd546c0 | 33 | |
mfiore | 0:424b9fd546c0 | 34 | HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, interval, RTC_WAKEUPCLOCK_CK_SPRE_16BITS ); |
mfiore | 0:424b9fd546c0 | 35 | |
mfiore | 0:424b9fd546c0 | 36 | printf("entering sleep mode %08x\r\n", RTC->ISR); |
mfiore | 0:424b9fd546c0 | 37 | fflush(stdout); |
mfiore | 0:424b9fd546c0 | 38 | |
mfiore | 0:424b9fd546c0 | 39 | // clear any pending interrupts |
mfiore | 0:424b9fd546c0 | 40 | EXTI ->PR = (1 << 22); |
mfiore | 0:424b9fd546c0 | 41 | |
mfiore | 0:424b9fd546c0 | 42 | HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI ); |
mfiore | 0:424b9fd546c0 | 43 | |
mfiore | 0:424b9fd546c0 | 44 | HAL_PWREx_DisableMainRegulatorLowVoltage(); |
mfiore | 0:424b9fd546c0 | 45 | HAL_PWREx_DisableFlashPowerDown(); |
mfiore | 0:424b9fd546c0 | 46 | HAL_PWREx_DisableLowRegulatorLowVoltage(); |
mfiore | 0:424b9fd546c0 | 47 | |
mfiore | 0:424b9fd546c0 | 48 | __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RtcHandle, RTC_FLAG_WUTF); |
mfiore | 0:424b9fd546c0 | 49 | |
mfiore | 0:424b9fd546c0 | 50 | // reconfigure PLL |
mfiore | 0:424b9fd546c0 | 51 | SetSysClock(); |
mfiore | 0:424b9fd546c0 | 52 | wait(1.0); |
mfiore | 0:424b9fd546c0 | 53 | printf("wake\r\n"); |
mfiore | 0:424b9fd546c0 | 54 | } |
mfiore | 0:424b9fd546c0 | 55 | |
mfiore | 0:424b9fd546c0 | 56 | return 0; |
mfiore | 0:424b9fd546c0 | 57 | } |