stephen mathenge / Mbed OS Level_estimation_Maesurement

Dependencies:   Cayenne-LPP SDBlockDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers standby.h Source File

standby.h

00001 #ifndef _DSA_STANDBY_H_
00002 #define _DSA_STANDBY_H_
00003 
00004 #include "mbed.h"
00005 #include "rtc_api_hal.h"
00006 
00007 enum WakeupType {
00008     WAKEUP_RESET,
00009     WAKEUP_TIMER,
00010     WAKEUP_PIN
00011 };
00012 
00013 static RTC_HandleTypeDef RtcHandle;
00014 
00015 void rtc_set_wake_up_timer_s(uint32_t delta)
00016 {
00017     uint32_t clock = RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
00018 
00019     // HAL_RTCEx_SetWakeUpTimer_IT will assert that delta is 0xFFFF at max
00020     if (delta > 0xFFFF) {
00021         delta -= 0x10000;
00022         clock = RTC_WAKEUPCLOCK_CK_SPRE_17BITS;
00023     }
00024 
00025     RtcHandle.Instance = RTC;
00026 
00027     HAL_StatusTypeDef status = HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, delta, clock);
00028 
00029     if (status != HAL_OK) {
00030         printf("Set wake up timer failed: %d\n", status);
00031         NVIC_SystemReset();
00032      }
00033 }
00034 
00035 WakeupType get_wakeup_type() {
00036 
00037     if(READ_BIT(RTC->ISR, RTC_ISR_WUTF))
00038         return WAKEUP_TIMER;
00039 
00040     // this is set by timer too, but that's checked already
00041     // above.
00042     if(READ_BIT(PWR->CSR, PWR_CSR_WUF))
00043         return WAKEUP_PIN;
00044 
00045     return WAKEUP_RESET;
00046 }
00047 
00048 void standby(int seconds) {
00049     printf("Going to sleep!\n");
00050 
00051     core_util_critical_section_enter();
00052 
00053     // Clear wakeup flag, just in case.
00054     SET_BIT(PWR->CR, PWR_CR_CWUF);
00055 
00056     // Enable wakeup timer.
00057     rtc_set_wake_up_timer_s(seconds);
00058 
00059     // Enable debug interface working in standby. Causes power consumption to increase drastically while in standby.
00060     //HAL_DBGMCU_EnableDBGStandbyMode();
00061 
00062     HAL_PWR_EnterSTANDBYMode();
00063 
00064     // this should not happen...
00065     rtc_deactivate_wake_up_timer();
00066     core_util_critical_section_exit();
00067 
00068     // something went wrong, let's reset
00069     NVIC_SystemReset();
00070 }
00071 
00072 #endif // _DSA_STANDBY_H_