mbed-os
Fork of mbed-os by
targets/TARGET_STM/TARGET_STM32L4/rtc_api.c@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | ******************************************************************************* |
elessair | 0:f269e3021894 | 3 | * Copyright (c) 2015, STMicroelectronics |
elessair | 0:f269e3021894 | 4 | * All rights reserved. |
elessair | 0:f269e3021894 | 5 | * |
elessair | 0:f269e3021894 | 6 | * Redistribution and use in source and binary forms, with or without |
elessair | 0:f269e3021894 | 7 | * modification, are permitted provided that the following conditions are met: |
elessair | 0:f269e3021894 | 8 | * |
elessair | 0:f269e3021894 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
elessair | 0:f269e3021894 | 10 | * this list of conditions and the following disclaimer. |
elessair | 0:f269e3021894 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elessair | 0:f269e3021894 | 12 | * this list of conditions and the following disclaimer in the documentation |
elessair | 0:f269e3021894 | 13 | * and/or other materials provided with the distribution. |
elessair | 0:f269e3021894 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elessair | 0:f269e3021894 | 15 | * may be used to endorse or promote products derived from this software |
elessair | 0:f269e3021894 | 16 | * without specific prior written permission. |
elessair | 0:f269e3021894 | 17 | * |
elessair | 0:f269e3021894 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elessair | 0:f269e3021894 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elessair | 0:f269e3021894 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elessair | 0:f269e3021894 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elessair | 0:f269e3021894 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elessair | 0:f269e3021894 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elessair | 0:f269e3021894 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elessair | 0:f269e3021894 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elessair | 0:f269e3021894 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elessair | 0:f269e3021894 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elessair | 0:f269e3021894 | 28 | ******************************************************************************* |
elessair | 0:f269e3021894 | 29 | */ |
elessair | 0:f269e3021894 | 30 | #include "rtc_api.h" |
elessair | 0:f269e3021894 | 31 | #include "rtc_api_hal.h" |
elessair | 0:f269e3021894 | 32 | |
elessair | 0:f269e3021894 | 33 | #if DEVICE_RTC |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | #include "mbed_error.h" |
elessair | 0:f269e3021894 | 36 | |
elessair | 0:f269e3021894 | 37 | #if RTC_LSI |
elessair | 0:f269e3021894 | 38 | static int rtc_inited = 0; |
elessair | 0:f269e3021894 | 39 | #endif |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | static RTC_HandleTypeDef RtcHandle; |
elessair | 0:f269e3021894 | 42 | |
elessair | 0:f269e3021894 | 43 | #if RTC_LSI |
elessair | 0:f269e3021894 | 44 | #define RTC_CLOCK LSI_VALUE |
elessair | 0:f269e3021894 | 45 | #else |
elessair | 0:f269e3021894 | 46 | #define RTC_CLOCK LSE_VALUE |
elessair | 0:f269e3021894 | 47 | #endif |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | #if DEVICE_LOWPOWERTIMER |
elessair | 0:f269e3021894 | 50 | #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) |
elessair | 0:f269e3021894 | 51 | #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) |
elessair | 0:f269e3021894 | 52 | #else |
elessair | 0:f269e3021894 | 53 | #define RTC_ASYNCH_PREDIV (0x007F) |
elessair | 0:f269e3021894 | 54 | #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) |
elessair | 0:f269e3021894 | 55 | #endif |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | #if DEVICE_LOWPOWERTIMER |
elessair | 0:f269e3021894 | 58 | static void (*irq_handler)(void); |
elessair | 0:f269e3021894 | 59 | static void RTC_IRQHandler(void); |
elessair | 0:f269e3021894 | 60 | #endif |
elessair | 0:f269e3021894 | 61 | |
elessair | 0:f269e3021894 | 62 | void rtc_init(void) |
elessair | 0:f269e3021894 | 63 | { |
elessair | 0:f269e3021894 | 64 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
elessair | 0:f269e3021894 | 65 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
elessair | 0:f269e3021894 | 66 | |
elessair | 0:f269e3021894 | 67 | #if RTC_LSI |
elessair | 0:f269e3021894 | 68 | if (rtc_inited) return; |
elessair | 0:f269e3021894 | 69 | rtc_inited = 1; |
elessair | 0:f269e3021894 | 70 | #endif |
elessair | 0:f269e3021894 | 71 | |
elessair | 0:f269e3021894 | 72 | RtcHandle.Instance = RTC; |
elessair | 0:f269e3021894 | 73 | |
elessair | 0:f269e3021894 | 74 | #if !RTC_LSI |
elessair | 0:f269e3021894 | 75 | // Enable LSE Oscillator |
elessair | 0:f269e3021894 | 76 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
elessair | 0:f269e3021894 | 77 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
elessair | 0:f269e3021894 | 78 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT |
elessair | 0:f269e3021894 | 79 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
elessair | 0:f269e3021894 | 80 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly |
elessair | 0:f269e3021894 | 81 | // Connect LSE to RTC |
elessair | 0:f269e3021894 | 82 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
elessair | 0:f269e3021894 | 83 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
elessair | 0:f269e3021894 | 84 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
elessair | 0:f269e3021894 | 85 | } else { |
elessair | 0:f269e3021894 | 86 | error("Cannot initialize RTC with LSE\n"); |
elessair | 0:f269e3021894 | 87 | } |
elessair | 0:f269e3021894 | 88 | #else |
elessair | 0:f269e3021894 | 89 | // Enable Power clock |
elessair | 0:f269e3021894 | 90 | __HAL_RCC_PWR_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 91 | |
elessair | 0:f269e3021894 | 92 | // Enable access to Backup domain |
elessair | 0:f269e3021894 | 93 | HAL_PWR_EnableBkUpAccess(); |
elessair | 0:f269e3021894 | 94 | |
elessair | 0:f269e3021894 | 95 | // Reset Backup domain |
elessair | 0:f269e3021894 | 96 | __HAL_RCC_BACKUPRESET_FORCE(); |
elessair | 0:f269e3021894 | 97 | __HAL_RCC_BACKUPRESET_RELEASE(); |
elessair | 0:f269e3021894 | 98 | |
elessair | 0:f269e3021894 | 99 | // Enable LSI clock |
elessair | 0:f269e3021894 | 100 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
elessair | 0:f269e3021894 | 101 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
elessair | 0:f269e3021894 | 102 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
elessair | 0:f269e3021894 | 103 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
elessair | 0:f269e3021894 | 104 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
elessair | 0:f269e3021894 | 105 | error("Cannot initialize RTC with LSI\n"); |
elessair | 0:f269e3021894 | 106 | } |
elessair | 0:f269e3021894 | 107 | // Connect LSI to RTC |
elessair | 0:f269e3021894 | 108 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
elessair | 0:f269e3021894 | 109 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
elessair | 0:f269e3021894 | 110 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
elessair | 0:f269e3021894 | 111 | error("Cannot initialize RTC with LSI\n"); |
elessair | 0:f269e3021894 | 112 | } |
elessair | 0:f269e3021894 | 113 | #endif |
elessair | 0:f269e3021894 | 114 | |
elessair | 0:f269e3021894 | 115 | // Check if RTC is already initialized |
elessair | 0:f269e3021894 | 116 | if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return; |
elessair | 0:f269e3021894 | 117 | |
elessair | 0:f269e3021894 | 118 | // Enable RTC |
elessair | 0:f269e3021894 | 119 | __HAL_RCC_RTC_ENABLE(); |
elessair | 0:f269e3021894 | 120 | |
elessair | 0:f269e3021894 | 121 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
elessair | 0:f269e3021894 | 122 | RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; |
elessair | 0:f269e3021894 | 123 | RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; |
elessair | 0:f269e3021894 | 124 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
elessair | 0:f269e3021894 | 125 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
elessair | 0:f269e3021894 | 126 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
elessair | 0:f269e3021894 | 127 | |
elessair | 0:f269e3021894 | 128 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
elessair | 0:f269e3021894 | 129 | error("Cannot initialize RTC\n"); |
elessair | 0:f269e3021894 | 130 | } |
elessair | 0:f269e3021894 | 131 | |
elessair | 0:f269e3021894 | 132 | #if DEVICE_LOWPOWERTIMER |
elessair | 0:f269e3021894 | 133 | #if RTC_LSI |
elessair | 0:f269e3021894 | 134 | rtc_write(0); |
elessair | 0:f269e3021894 | 135 | #else |
elessair | 0:f269e3021894 | 136 | if (!rtc_isenabled()) { |
elessair | 0:f269e3021894 | 137 | rtc_write(0); |
elessair | 0:f269e3021894 | 138 | } |
elessair | 0:f269e3021894 | 139 | #endif |
elessair | 0:f269e3021894 | 140 | NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); |
elessair | 0:f269e3021894 | 141 | NVIC_DisableIRQ(RTC_WKUP_IRQn); |
elessair | 0:f269e3021894 | 142 | NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); |
elessair | 0:f269e3021894 | 143 | NVIC_EnableIRQ(RTC_WKUP_IRQn); |
elessair | 0:f269e3021894 | 144 | #endif |
elessair | 0:f269e3021894 | 145 | } |
elessair | 0:f269e3021894 | 146 | |
elessair | 0:f269e3021894 | 147 | void rtc_free(void) |
elessair | 0:f269e3021894 | 148 | { |
elessair | 0:f269e3021894 | 149 | #if RTC_LSI |
elessair | 0:f269e3021894 | 150 | // Enable Power clock |
elessair | 0:f269e3021894 | 151 | __HAL_RCC_PWR_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 152 | |
elessair | 0:f269e3021894 | 153 | // Enable access to Backup domain |
elessair | 0:f269e3021894 | 154 | HAL_PWR_EnableBkUpAccess(); |
elessair | 0:f269e3021894 | 155 | |
elessair | 0:f269e3021894 | 156 | // Reset Backup domain |
elessair | 0:f269e3021894 | 157 | __HAL_RCC_BACKUPRESET_FORCE(); |
elessair | 0:f269e3021894 | 158 | __HAL_RCC_BACKUPRESET_RELEASE(); |
elessair | 0:f269e3021894 | 159 | |
elessair | 0:f269e3021894 | 160 | // Disable access to Backup domain |
elessair | 0:f269e3021894 | 161 | HAL_PWR_DisableBkUpAccess(); |
elessair | 0:f269e3021894 | 162 | #endif |
elessair | 0:f269e3021894 | 163 | |
elessair | 0:f269e3021894 | 164 | // Disable LSI and LSE clocks |
elessair | 0:f269e3021894 | 165 | RCC_OscInitTypeDef RCC_OscInitStruct; |
elessair | 0:f269e3021894 | 166 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
elessair | 0:f269e3021894 | 167 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
elessair | 0:f269e3021894 | 168 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
elessair | 0:f269e3021894 | 169 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
elessair | 0:f269e3021894 | 170 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
elessair | 0:f269e3021894 | 171 | |
elessair | 0:f269e3021894 | 172 | #if RTC_LSI |
elessair | 0:f269e3021894 | 173 | rtc_inited = 0; |
elessair | 0:f269e3021894 | 174 | #endif |
elessair | 0:f269e3021894 | 175 | } |
elessair | 0:f269e3021894 | 176 | |
elessair | 0:f269e3021894 | 177 | int rtc_isenabled(void) |
elessair | 0:f269e3021894 | 178 | { |
elessair | 0:f269e3021894 | 179 | #if RTC_LSI |
elessair | 0:f269e3021894 | 180 | return rtc_inited; |
elessair | 0:f269e3021894 | 181 | #else |
elessair | 0:f269e3021894 | 182 | if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { |
elessair | 0:f269e3021894 | 183 | return 1; |
elessair | 0:f269e3021894 | 184 | } else { |
elessair | 0:f269e3021894 | 185 | return 0; |
elessair | 0:f269e3021894 | 186 | } |
elessair | 0:f269e3021894 | 187 | #endif |
elessair | 0:f269e3021894 | 188 | } |
elessair | 0:f269e3021894 | 189 | |
elessair | 0:f269e3021894 | 190 | /* |
elessair | 0:f269e3021894 | 191 | RTC Registers |
elessair | 0:f269e3021894 | 192 | RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
elessair | 0:f269e3021894 | 193 | RTC_Month 1=january, 2=february, ..., 12=december |
elessair | 0:f269e3021894 | 194 | RTC_Date day of the month 1-31 |
elessair | 0:f269e3021894 | 195 | RTC_Year year 0-99 |
elessair | 0:f269e3021894 | 196 | struct tm |
elessair | 0:f269e3021894 | 197 | tm_sec seconds after the minute 0-61 |
elessair | 0:f269e3021894 | 198 | tm_min minutes after the hour 0-59 |
elessair | 0:f269e3021894 | 199 | tm_hour hours since midnight 0-23 |
elessair | 0:f269e3021894 | 200 | tm_mday day of the month 1-31 |
elessair | 0:f269e3021894 | 201 | tm_mon months since January 0-11 |
elessair | 0:f269e3021894 | 202 | tm_year years since 1900 |
elessair | 0:f269e3021894 | 203 | tm_wday days since Sunday 0-6 |
elessair | 0:f269e3021894 | 204 | tm_yday days since January 1 0-365 |
elessair | 0:f269e3021894 | 205 | tm_isdst Daylight Saving Time flag |
elessair | 0:f269e3021894 | 206 | */ |
elessair | 0:f269e3021894 | 207 | time_t rtc_read(void) |
elessair | 0:f269e3021894 | 208 | { |
elessair | 0:f269e3021894 | 209 | RTC_DateTypeDef dateStruct; |
elessair | 0:f269e3021894 | 210 | RTC_TimeTypeDef timeStruct; |
elessair | 0:f269e3021894 | 211 | struct tm timeinfo; |
elessair | 0:f269e3021894 | 212 | |
elessair | 0:f269e3021894 | 213 | RtcHandle.Instance = RTC; |
elessair | 0:f269e3021894 | 214 | |
elessair | 0:f269e3021894 | 215 | // Read actual date and time |
elessair | 0:f269e3021894 | 216 | // Warning: the time must be read first! |
elessair | 0:f269e3021894 | 217 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
elessair | 0:f269e3021894 | 218 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
elessair | 0:f269e3021894 | 219 | |
elessair | 0:f269e3021894 | 220 | // Setup a tm structure based on the RTC |
elessair | 0:f269e3021894 | 221 | timeinfo.tm_wday = dateStruct.WeekDay; |
elessair | 0:f269e3021894 | 222 | timeinfo.tm_mon = dateStruct.Month - 1; |
elessair | 0:f269e3021894 | 223 | timeinfo.tm_mday = dateStruct.Date; |
elessair | 0:f269e3021894 | 224 | timeinfo.tm_year = dateStruct.Year + 68; |
elessair | 0:f269e3021894 | 225 | timeinfo.tm_hour = timeStruct.Hours; |
elessair | 0:f269e3021894 | 226 | timeinfo.tm_min = timeStruct.Minutes; |
elessair | 0:f269e3021894 | 227 | timeinfo.tm_sec = timeStruct.Seconds; |
elessair | 0:f269e3021894 | 228 | // Daylight Saving Time information is not available |
elessair | 0:f269e3021894 | 229 | timeinfo.tm_isdst = -1; |
elessair | 0:f269e3021894 | 230 | |
elessair | 0:f269e3021894 | 231 | // Convert to timestamp |
elessair | 0:f269e3021894 | 232 | time_t t = mktime(&timeinfo); |
elessair | 0:f269e3021894 | 233 | |
elessair | 0:f269e3021894 | 234 | return t; |
elessair | 0:f269e3021894 | 235 | } |
elessair | 0:f269e3021894 | 236 | |
elessair | 0:f269e3021894 | 237 | void rtc_write(time_t t) |
elessair | 0:f269e3021894 | 238 | { |
elessair | 0:f269e3021894 | 239 | RTC_DateTypeDef dateStruct; |
elessair | 0:f269e3021894 | 240 | RTC_TimeTypeDef timeStruct; |
elessair | 0:f269e3021894 | 241 | |
elessair | 0:f269e3021894 | 242 | RtcHandle.Instance = RTC; |
elessair | 0:f269e3021894 | 243 | |
elessair | 0:f269e3021894 | 244 | // Convert the time into a tm |
elessair | 0:f269e3021894 | 245 | struct tm *timeinfo = localtime(&t); |
elessair | 0:f269e3021894 | 246 | |
elessair | 0:f269e3021894 | 247 | // Fill RTC structures |
elessair | 0:f269e3021894 | 248 | dateStruct.WeekDay = timeinfo->tm_wday; |
elessair | 0:f269e3021894 | 249 | dateStruct.Month = timeinfo->tm_mon + 1; |
elessair | 0:f269e3021894 | 250 | dateStruct.Date = timeinfo->tm_mday; |
elessair | 0:f269e3021894 | 251 | dateStruct.Year = timeinfo->tm_year - 68; |
elessair | 0:f269e3021894 | 252 | timeStruct.Hours = timeinfo->tm_hour; |
elessair | 0:f269e3021894 | 253 | timeStruct.Minutes = timeinfo->tm_min; |
elessair | 0:f269e3021894 | 254 | timeStruct.Seconds = timeinfo->tm_sec; |
elessair | 0:f269e3021894 | 255 | timeStruct.TimeFormat = RTC_HOURFORMAT_24; |
elessair | 0:f269e3021894 | 256 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
elessair | 0:f269e3021894 | 257 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
elessair | 0:f269e3021894 | 258 | |
elessair | 0:f269e3021894 | 259 | // Change the RTC current date/time |
elessair | 0:f269e3021894 | 260 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
elessair | 0:f269e3021894 | 261 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
elessair | 0:f269e3021894 | 262 | } |
elessair | 0:f269e3021894 | 263 | |
elessair | 0:f269e3021894 | 264 | #if DEVICE_LOWPOWERTIMER |
elessair | 0:f269e3021894 | 265 | |
elessair | 0:f269e3021894 | 266 | static void RTC_IRQHandler(void) |
elessair | 0:f269e3021894 | 267 | { |
elessair | 0:f269e3021894 | 268 | HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); |
elessair | 0:f269e3021894 | 269 | } |
elessair | 0:f269e3021894 | 270 | |
elessair | 0:f269e3021894 | 271 | void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) |
elessair | 0:f269e3021894 | 272 | { |
elessair | 0:f269e3021894 | 273 | if (irq_handler) { |
elessair | 0:f269e3021894 | 274 | // Fire the user callback |
elessair | 0:f269e3021894 | 275 | irq_handler(); |
elessair | 0:f269e3021894 | 276 | } |
elessair | 0:f269e3021894 | 277 | } |
elessair | 0:f269e3021894 | 278 | |
elessair | 0:f269e3021894 | 279 | void rtc_set_irq_handler(uint32_t handler) |
elessair | 0:f269e3021894 | 280 | { |
elessair | 0:f269e3021894 | 281 | irq_handler = (void (*)(void))handler; |
elessair | 0:f269e3021894 | 282 | } |
elessair | 0:f269e3021894 | 283 | |
elessair | 0:f269e3021894 | 284 | uint32_t rtc_read_subseconds(void) |
elessair | 0:f269e3021894 | 285 | { |
elessair | 0:f269e3021894 | 286 | return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); |
elessair | 0:f269e3021894 | 287 | } |
elessair | 0:f269e3021894 | 288 | |
elessair | 0:f269e3021894 | 289 | void rtc_set_wake_up_timer(uint32_t delta) |
elessair | 0:f269e3021894 | 290 | { |
elessair | 0:f269e3021894 | 291 | uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); |
elessair | 0:f269e3021894 | 292 | |
elessair | 0:f269e3021894 | 293 | if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, |
elessair | 0:f269e3021894 | 294 | RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { |
elessair | 0:f269e3021894 | 295 | error("Set wake up timer failed\n"); |
elessair | 0:f269e3021894 | 296 | } |
elessair | 0:f269e3021894 | 297 | } |
elessair | 0:f269e3021894 | 298 | |
elessair | 0:f269e3021894 | 299 | void rtc_deactivate_wake_up_timer(void) |
elessair | 0:f269e3021894 | 300 | { |
elessair | 0:f269e3021894 | 301 | HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); |
elessair | 0:f269e3021894 | 302 | } |
elessair | 0:f269e3021894 | 303 | |
elessair | 0:f269e3021894 | 304 | void rtc_synchronize(void) |
elessair | 0:f269e3021894 | 305 | { |
elessair | 0:f269e3021894 | 306 | HAL_RTC_WaitForSynchro(&RtcHandle); |
elessair | 0:f269e3021894 | 307 | } |
elessair | 0:f269e3021894 | 308 | #endif // DEVICE_LOWPOWERTIMER |
elessair | 0:f269e3021894 | 309 | |
elessair | 0:f269e3021894 | 310 | #endif |