Level measurement using range finder and lora technology

Dependencies:   Cayenne-LPP SDBlockDevice

Committer:
wamae
Date:
Wed Jun 26 10:35:50 2019 +0000
Revision:
0:f930f0440fd5
better copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wamae 0:f930f0440fd5 1 #ifndef _DSA_STANDBY_H_
wamae 0:f930f0440fd5 2 #define _DSA_STANDBY_H_
wamae 0:f930f0440fd5 3
wamae 0:f930f0440fd5 4 #include "mbed.h"
wamae 0:f930f0440fd5 5 #include "rtc_api_hal.h"
wamae 0:f930f0440fd5 6
wamae 0:f930f0440fd5 7 enum WakeupType {
wamae 0:f930f0440fd5 8 WAKEUP_RESET,
wamae 0:f930f0440fd5 9 WAKEUP_TIMER,
wamae 0:f930f0440fd5 10 WAKEUP_PIN
wamae 0:f930f0440fd5 11 };
wamae 0:f930f0440fd5 12
wamae 0:f930f0440fd5 13 static RTC_HandleTypeDef RtcHandle;
wamae 0:f930f0440fd5 14
wamae 0:f930f0440fd5 15 void rtc_set_wake_up_timer_s(uint32_t delta)
wamae 0:f930f0440fd5 16 {
wamae 0:f930f0440fd5 17 uint32_t clock = RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
wamae 0:f930f0440fd5 18
wamae 0:f930f0440fd5 19 // HAL_RTCEx_SetWakeUpTimer_IT will assert that delta is 0xFFFF at max
wamae 0:f930f0440fd5 20 if (delta > 0xFFFF) {
wamae 0:f930f0440fd5 21 delta -= 0x10000;
wamae 0:f930f0440fd5 22 clock = RTC_WAKEUPCLOCK_CK_SPRE_17BITS;
wamae 0:f930f0440fd5 23 }
wamae 0:f930f0440fd5 24
wamae 0:f930f0440fd5 25 RtcHandle.Instance = RTC;
wamae 0:f930f0440fd5 26
wamae 0:f930f0440fd5 27 HAL_StatusTypeDef status = HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, delta, clock);
wamae 0:f930f0440fd5 28
wamae 0:f930f0440fd5 29 if (status != HAL_OK) {
wamae 0:f930f0440fd5 30 printf("Set wake up timer failed: %d\n", status);
wamae 0:f930f0440fd5 31 NVIC_SystemReset();
wamae 0:f930f0440fd5 32 }
wamae 0:f930f0440fd5 33 }
wamae 0:f930f0440fd5 34
wamae 0:f930f0440fd5 35 WakeupType get_wakeup_type() {
wamae 0:f930f0440fd5 36
wamae 0:f930f0440fd5 37 if(READ_BIT(RTC->ISR, RTC_ISR_WUTF))
wamae 0:f930f0440fd5 38 return WAKEUP_TIMER;
wamae 0:f930f0440fd5 39
wamae 0:f930f0440fd5 40 // this is set by timer too, but that's checked already
wamae 0:f930f0440fd5 41 // above.
wamae 0:f930f0440fd5 42 if(READ_BIT(PWR->CSR, PWR_CSR_WUF))
wamae 0:f930f0440fd5 43 return WAKEUP_PIN;
wamae 0:f930f0440fd5 44
wamae 0:f930f0440fd5 45 return WAKEUP_RESET;
wamae 0:f930f0440fd5 46 }
wamae 0:f930f0440fd5 47
wamae 0:f930f0440fd5 48 void standby(int seconds) {
wamae 0:f930f0440fd5 49 printf("Going to sleep!\n");
wamae 0:f930f0440fd5 50
wamae 0:f930f0440fd5 51 core_util_critical_section_enter();
wamae 0:f930f0440fd5 52
wamae 0:f930f0440fd5 53 // Clear wakeup flag, just in case.
wamae 0:f930f0440fd5 54 SET_BIT(PWR->CR, PWR_CR_CWUF);
wamae 0:f930f0440fd5 55
wamae 0:f930f0440fd5 56 // Enable wakeup timer.
wamae 0:f930f0440fd5 57 rtc_set_wake_up_timer_s(seconds);
wamae 0:f930f0440fd5 58
wamae 0:f930f0440fd5 59 // Enable debug interface working in standby. Causes power consumption to increase drastically while in standby.
wamae 0:f930f0440fd5 60 //HAL_DBGMCU_EnableDBGStandbyMode();
wamae 0:f930f0440fd5 61
wamae 0:f930f0440fd5 62 HAL_PWR_EnterSTANDBYMode();
wamae 0:f930f0440fd5 63
wamae 0:f930f0440fd5 64 // this should not happen...
wamae 0:f930f0440fd5 65 rtc_deactivate_wake_up_timer();
wamae 0:f930f0440fd5 66 core_util_critical_section_exit();
wamae 0:f930f0440fd5 67
wamae 0:f930f0440fd5 68 // something went wrong, let's reset
wamae 0:f930f0440fd5 69 NVIC_SystemReset();
wamae 0:f930f0440fd5 70 }
wamae 0:f930f0440fd5 71
wamae 0:f930f0440fd5 72 #endif // _DSA_STANDBY_H_