NAMote72 Utility Application – Serial Terminal Monitor control for NAMote72 (note: this application replaces the previous na_mote1 test code application)
Dependencies: SX127x lib_gps lib_mma8451q lib_mpl3115a2 lib_sx9500 mbed
Fork of na_mote1 by
See wiki Page for a detailed
This is a link to the wiki page
rtc_alarm.c@10:a3e2c558c003, 2015-06-25 (annotated)
- Committer:
- dudmuck
- Date:
- Thu Jun 25 22:29:28 2015 +0000
- Revision:
- 10:a3e2c558c003
added deepsleep test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudmuck | 10:a3e2c558c003 | 1 | /* mbed |
dudmuck | 10:a3e2c558c003 | 2 | * Copyright (c) |
dudmuck | 10:a3e2c558c003 | 3 | * |
dudmuck | 10:a3e2c558c003 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dudmuck | 10:a3e2c558c003 | 5 | * you may not use this file except in compliance with the License. |
dudmuck | 10:a3e2c558c003 | 6 | * You may obtain a copy of the License at |
dudmuck | 10:a3e2c558c003 | 7 | * |
dudmuck | 10:a3e2c558c003 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dudmuck | 10:a3e2c558c003 | 9 | * |
dudmuck | 10:a3e2c558c003 | 10 | * Unless required by applicable law or agreed to in writing, software |
dudmuck | 10:a3e2c558c003 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dudmuck | 10:a3e2c558c003 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dudmuck | 10:a3e2c558c003 | 13 | * See the License for the specific language governing permissions and |
dudmuck | 10:a3e2c558c003 | 14 | * limitations under the License. |
dudmuck | 10:a3e2c558c003 | 15 | */ |
dudmuck | 10:a3e2c558c003 | 16 | #include "rtc_alarm.h" |
dudmuck | 10:a3e2c558c003 | 17 | #include <stdio.h> |
dudmuck | 10:a3e2c558c003 | 18 | |
dudmuck | 10:a3e2c558c003 | 19 | /* |
dudmuck | 10:a3e2c558c003 | 20 | The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC |
dudmuck | 10:a3e2c558c003 | 21 | Wakeup event, a tamper event, a time-stamp event, or a comparator event, |
dudmuck | 10:a3e2c558c003 | 22 | without depending on an external interrupt (Auto-wakeup mode). |
dudmuck | 10:a3e2c558c003 | 23 | |
dudmuck | 10:a3e2c558c003 | 24 | (+) RTC auto-wakeup (AWU) from the Stop mode |
dudmuck | 10:a3e2c558c003 | 25 | (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to: |
dudmuck | 10:a3e2c558c003 | 26 | (+++) Configure the EXTI Line 17 to be sensitive to rising edges (Interrupt |
dudmuck | 10:a3e2c558c003 | 27 | or Event modes) and Enable the RTC Alarm Interrupt using the HAL_RTC_SetAlarm_IT() |
dudmuck | 10:a3e2c558c003 | 28 | function |
dudmuck | 10:a3e2c558c003 | 29 | (+++) Configure the RTC to generate the RTC alarm using the HAL_RTC_Init() |
dudmuck | 10:a3e2c558c003 | 30 | and HAL_RTC_SetTime() functions. |
dudmuck | 10:a3e2c558c003 | 31 | (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it |
dudmuck | 10:a3e2c558c003 | 32 | is necessary to: |
dudmuck | 10:a3e2c558c003 | 33 | (+++) Configure the EXTI Line 19 to be sensitive to rising edges (Interrupt or Event modes) and |
dudmuck | 10:a3e2c558c003 | 34 | Enable the RTC Tamper or time stamp Interrupt using the HAL_RTCEx_SetTamper_IT() |
dudmuck | 10:a3e2c558c003 | 35 | or HAL_RTCEx_SetTimeStamp_IT() functions. |
dudmuck | 10:a3e2c558c003 | 36 | (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to: |
dudmuck | 10:a3e2c558c003 | 37 | (+++) Configure the EXTI Line 20 to be sensitive to rising edges (Interrupt or Event modes) and |
dudmuck | 10:a3e2c558c003 | 38 | Enable the RTC WakeUp Interrupt using the HAL_RTCEx_SetWakeUpTimer_IT() function. |
dudmuck | 10:a3e2c558c003 | 39 | (+++) Configure the RTC to generate the RTC WakeUp event using the HAL_RTCEx_SetWakeUpTimer() |
dudmuck | 10:a3e2c558c003 | 40 | function. |
dudmuck | 10:a3e2c558c003 | 41 | */ |
dudmuck | 10:a3e2c558c003 | 42 | |
dudmuck | 10:a3e2c558c003 | 43 | extern RTC_HandleTypeDef RtcHandle; |
dudmuck | 10:a3e2c558c003 | 44 | |
dudmuck | 10:a3e2c558c003 | 45 | #if 0 |
dudmuck | 10:a3e2c558c003 | 46 | typedef struct |
dudmuck | 10:a3e2c558c003 | 47 | { |
dudmuck | 10:a3e2c558c003 | 48 | RTC_TypeDef *Instance; /*!< Register base address */ |
dudmuck | 10:a3e2c558c003 | 49 | RTC_InitTypeDef Init; /*!< RTC required parameters */ |
dudmuck | 10:a3e2c558c003 | 50 | HAL_LockTypeDef Lock; /*!< RTC locking object */ |
dudmuck | 10:a3e2c558c003 | 51 | __IO HAL_RTCStateTypeDef State; /*!< Time communication state */ |
dudmuck | 10:a3e2c558c003 | 52 | } RTC_HandleTypeDef; |
dudmuck | 10:a3e2c558c003 | 53 | |
dudmuck | 10:a3e2c558c003 | 54 | HAL_RTCEx_WakeUpTimerIRQHandler(): |
dudmuck | 10:a3e2c558c003 | 55 | __HAL_RTC_WAKEUPTIMER_GET_IT(): |
dudmuck | 10:a3e2c558c003 | 56 | ->Instance->ISR |
dudmuck | 10:a3e2c558c003 | 57 | ->Instance->CR |
dudmuck | 10:a3e2c558c003 | 58 | HAL_RTCEx_WakeUpTimerEventCallback(): |
dudmuck | 10:a3e2c558c003 | 59 | (none) |
dudmuck | 10:a3e2c558c003 | 60 | __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(): |
dudmuck | 10:a3e2c558c003 | 61 | ->Instance->ISR |
dudmuck | 10:a3e2c558c003 | 62 | ->State |
dudmuck | 10:a3e2c558c003 | 63 | |
dudmuck | 10:a3e2c558c003 | 64 | HAL_RTCEx_SetWakeUpTimer_IT(): |
dudmuck | 10:a3e2c558c003 | 65 | __HAL_LOCK(): |
dudmuck | 10:a3e2c558c003 | 66 | ->Lock |
dudmuck | 10:a3e2c558c003 | 67 | ->State |
dudmuck | 10:a3e2c558c003 | 68 | __HAL_RTC_WRITEPROTECTION_DISABLE(): |
dudmuck | 10:a3e2c558c003 | 69 | __HAL_RTC_WAKEUPTIMER_DISABLE(): |
dudmuck | 10:a3e2c558c003 | 70 | __HAL_RTC_WAKEUPTIMER_GET_FLAG(): |
dudmuck | 10:a3e2c558c003 | 71 | __HAL_RTC_WRITEPROTECTION_ENABLE(): |
dudmuck | 10:a3e2c558c003 | 72 | __HAL_UNLOCK(): |
dudmuck | 10:a3e2c558c003 | 73 | ->Instance->WUTR |
dudmuck | 10:a3e2c558c003 | 74 | ->Instance->CR |
dudmuck | 10:a3e2c558c003 | 75 | __HAL_RTC_WAKEUPTIMER_ENABLE(): |
dudmuck | 10:a3e2c558c003 | 76 | |
dudmuck | 10:a3e2c558c003 | 77 | HAL_RTCEx_DeactivateWakeUpTimer(): |
dudmuck | 10:a3e2c558c003 | 78 | |
dudmuck | 10:a3e2c558c003 | 79 | #endif /* #if 0 */ |
dudmuck | 10:a3e2c558c003 | 80 | |
dudmuck | 10:a3e2c558c003 | 81 | static void rtc_wkup_irq(void) |
dudmuck | 10:a3e2c558c003 | 82 | { |
dudmuck | 10:a3e2c558c003 | 83 | HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); |
dudmuck | 10:a3e2c558c003 | 84 | } |
dudmuck | 10:a3e2c558c003 | 85 | |
dudmuck | 10:a3e2c558c003 | 86 | void rtc_alarm_init() |
dudmuck | 10:a3e2c558c003 | 87 | { |
dudmuck | 10:a3e2c558c003 | 88 | /* Enable Ultra low power mode */ |
dudmuck | 10:a3e2c558c003 | 89 | HAL_PWREx_EnableUltraLowPower(); |
dudmuck | 10:a3e2c558c003 | 90 | |
dudmuck | 10:a3e2c558c003 | 91 | /* Enable the fast wake up from Ultra low power mode */ |
dudmuck | 10:a3e2c558c003 | 92 | HAL_PWREx_EnableFastWakeUp(); |
dudmuck | 10:a3e2c558c003 | 93 | |
dudmuck | 10:a3e2c558c003 | 94 | NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)rtc_wkup_irq); |
dudmuck | 10:a3e2c558c003 | 95 | NVIC_EnableIRQ(RTC_WKUP_IRQn); |
dudmuck | 10:a3e2c558c003 | 96 | } |
dudmuck | 10:a3e2c558c003 | 97 | |
dudmuck | 10:a3e2c558c003 | 98 | void rtc_alarm_start(uint32_t seconds) |
dudmuck | 10:a3e2c558c003 | 99 | { |
dudmuck | 10:a3e2c558c003 | 100 | /* Disable Wakeup Counter */ |
dudmuck | 10:a3e2c558c003 | 101 | HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); |
dudmuck | 10:a3e2c558c003 | 102 | |
dudmuck | 10:a3e2c558c003 | 103 | /* |
dudmuck | 10:a3e2c558c003 | 104 | RTC_WAKEUPCLOCK_RTCCLK_DIV16 488.3us steps, upto 32s |
dudmuck | 10:a3e2c558c003 | 105 | RTC_WAKEUPCLOCK_RTCCLK_DIV8 244us steps |
dudmuck | 10:a3e2c558c003 | 106 | RTC_WAKEUPCLOCK_RTCCLK_DIV4 122us steps |
dudmuck | 10:a3e2c558c003 | 107 | RTC_WAKEUPCLOCK_RTCCLK_DIV2: 61.035us steps |
dudmuck | 10:a3e2c558c003 | 108 | RTC_WAKEUPCLOCK_CK_SPRE_16BITS 1s steps |
dudmuck | 10:a3e2c558c003 | 109 | RTC_WAKEUPCLOCK_CK_SPRE_17BITS 1s steps |
dudmuck | 10:a3e2c558c003 | 110 | */ |
dudmuck | 10:a3e2c558c003 | 111 | |
dudmuck | 10:a3e2c558c003 | 112 | /* ## Setting the Wake up time ############################################*/ |
dudmuck | 10:a3e2c558c003 | 113 | /* see RM0038 section 20.3.4 */ |
dudmuck | 10:a3e2c558c003 | 114 | //HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, seconds, RTC_WAKEUPCLOCK_RTCCLK_DIV16); |
dudmuck | 10:a3e2c558c003 | 115 | HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, seconds, RTC_WAKEUPCLOCK_CK_SPRE_16BITS); |
dudmuck | 10:a3e2c558c003 | 116 | |
dudmuck | 10:a3e2c558c003 | 117 | } |
dudmuck | 10:a3e2c558c003 | 118 | |
dudmuck | 10:a3e2c558c003 | 119 | void rtc_print_subseconds() |
dudmuck | 10:a3e2c558c003 | 120 | { |
dudmuck | 10:a3e2c558c003 | 121 | printf("ssr:%04x ", RtcHandle.Instance->SSR); |
dudmuck | 10:a3e2c558c003 | 122 | } |
dudmuck | 10:a3e2c558c003 | 123 | |
dudmuck | 10:a3e2c558c003 | 124 | time_t rtc_read_with_subseconds(void) |
dudmuck | 10:a3e2c558c003 | 125 | { |
dudmuck | 10:a3e2c558c003 | 126 | RTC_DateTypeDef dateStruct; |
dudmuck | 10:a3e2c558c003 | 127 | RTC_TimeTypeDef timeStruct; |
dudmuck | 10:a3e2c558c003 | 128 | struct tm timeinfo; |
dudmuck | 10:a3e2c558c003 | 129 | |
dudmuck | 10:a3e2c558c003 | 130 | RtcHandle.Instance = RTC; |
dudmuck | 10:a3e2c558c003 | 131 | |
dudmuck | 10:a3e2c558c003 | 132 | // Read actual date and time |
dudmuck | 10:a3e2c558c003 | 133 | // Warning: the time must be read first! |
dudmuck | 10:a3e2c558c003 | 134 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
dudmuck | 10:a3e2c558c003 | 135 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
dudmuck | 10:a3e2c558c003 | 136 | |
dudmuck | 10:a3e2c558c003 | 137 | // Setup a tm structure based on the RTC |
dudmuck | 10:a3e2c558c003 | 138 | timeinfo.tm_wday = dateStruct.WeekDay; |
dudmuck | 10:a3e2c558c003 | 139 | timeinfo.tm_mon = dateStruct.Month - 1; |
dudmuck | 10:a3e2c558c003 | 140 | timeinfo.tm_mday = dateStruct.Date; |
dudmuck | 10:a3e2c558c003 | 141 | timeinfo.tm_year = dateStruct.Year + 100; |
dudmuck | 10:a3e2c558c003 | 142 | timeinfo.tm_hour = timeStruct.Hours; |
dudmuck | 10:a3e2c558c003 | 143 | timeinfo.tm_min = timeStruct.Minutes; |
dudmuck | 10:a3e2c558c003 | 144 | timeinfo.tm_sec = timeStruct.Seconds; |
dudmuck | 10:a3e2c558c003 | 145 | |
dudmuck | 10:a3e2c558c003 | 146 | // Convert to timestamp |
dudmuck | 10:a3e2c558c003 | 147 | time_t t = mktime(&timeinfo); |
dudmuck | 10:a3e2c558c003 | 148 | |
dudmuck | 10:a3e2c558c003 | 149 | t <<= 14; |
dudmuck | 10:a3e2c558c003 | 150 | printf("14:%08x ", t); |
dudmuck | 10:a3e2c558c003 | 151 | |
dudmuck | 10:a3e2c558c003 | 152 | //return (t << 14) | timeStruct.Seconds; |
dudmuck | 10:a3e2c558c003 | 153 | return t | timeStruct.SubSeconds; |
dudmuck | 10:a3e2c558c003 | 154 | } |
dudmuck | 10:a3e2c558c003 | 155 | |
dudmuck | 10:a3e2c558c003 | 156 | /** |
dudmuck | 10:a3e2c558c003 | 157 | * @brief RTC Wake Up callback |
dudmuck | 10:a3e2c558c003 | 158 | * @param None |
dudmuck | 10:a3e2c558c003 | 159 | * @retval None |
dudmuck | 10:a3e2c558c003 | 160 | */ |
dudmuck | 10:a3e2c558c003 | 161 | void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) |
dudmuck | 10:a3e2c558c003 | 162 | { |
dudmuck | 10:a3e2c558c003 | 163 | /* Clear Wake Up Flag */ |
dudmuck | 10:a3e2c558c003 | 164 | __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); |
dudmuck | 10:a3e2c558c003 | 165 | } |
dudmuck | 10:a3e2c558c003 | 166 |