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