t
Fork of mbed-dev by
targets/TARGET_STM/rtc_api.c@176:447f873cad2f, 2017-10-25 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Oct 25 14:53:38 2017 +0100
- Revision:
- 176:447f873cad2f
- Parent:
- 167:e84263d55307
This updates the lib to the mbed lib v 154
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 154:37f96f9d4de2 | 1 | /* mbed Microcontroller Library |
<> | 154:37f96f9d4de2 | 2 | ******************************************************************************* |
<> | 154:37f96f9d4de2 | 3 | * Copyright (c) 2016, STMicroelectronics |
<> | 154:37f96f9d4de2 | 4 | * All rights reserved. |
<> | 154:37f96f9d4de2 | 5 | * |
<> | 154:37f96f9d4de2 | 6 | * Redistribution and use in source and binary forms, with or without |
<> | 154:37f96f9d4de2 | 7 | * modification, are permitted provided that the following conditions are met: |
<> | 154:37f96f9d4de2 | 8 | * |
<> | 154:37f96f9d4de2 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
<> | 154:37f96f9d4de2 | 10 | * this list of conditions and the following disclaimer. |
<> | 154:37f96f9d4de2 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
<> | 154:37f96f9d4de2 | 12 | * this list of conditions and the following disclaimer in the documentation |
<> | 154:37f96f9d4de2 | 13 | * and/or other materials provided with the distribution. |
<> | 154:37f96f9d4de2 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
<> | 154:37f96f9d4de2 | 15 | * may be used to endorse or promote products derived from this software |
<> | 154:37f96f9d4de2 | 16 | * without specific prior written permission. |
<> | 154:37f96f9d4de2 | 17 | * |
<> | 154:37f96f9d4de2 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
<> | 154:37f96f9d4de2 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
<> | 154:37f96f9d4de2 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
<> | 154:37f96f9d4de2 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
<> | 154:37f96f9d4de2 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
<> | 154:37f96f9d4de2 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
<> | 154:37f96f9d4de2 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
<> | 154:37f96f9d4de2 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
<> | 154:37f96f9d4de2 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
<> | 154:37f96f9d4de2 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
<> | 154:37f96f9d4de2 | 28 | ******************************************************************************* |
<> | 154:37f96f9d4de2 | 29 | */ |
<> | 154:37f96f9d4de2 | 30 | #if DEVICE_RTC |
<> | 154:37f96f9d4de2 | 31 | |
<> | 154:37f96f9d4de2 | 32 | #include "rtc_api.h" |
<> | 154:37f96f9d4de2 | 33 | #include "rtc_api_hal.h" |
<> | 154:37f96f9d4de2 | 34 | #include "mbed_error.h" |
AnnaBridge | 167:e84263d55307 | 35 | #include "mbed_mktime.h" |
<> | 154:37f96f9d4de2 | 36 | |
<> | 154:37f96f9d4de2 | 37 | static RTC_HandleTypeDef RtcHandle; |
<> | 154:37f96f9d4de2 | 38 | |
<> | 154:37f96f9d4de2 | 39 | #if RTC_LSI |
<> | 154:37f96f9d4de2 | 40 | #define RTC_CLOCK LSI_VALUE |
<> | 154:37f96f9d4de2 | 41 | #else |
<> | 154:37f96f9d4de2 | 42 | #define RTC_CLOCK LSE_VALUE |
<> | 154:37f96f9d4de2 | 43 | #endif |
<> | 154:37f96f9d4de2 | 44 | |
<> | 154:37f96f9d4de2 | 45 | #if DEVICE_LOWPOWERTIMER |
<> | 154:37f96f9d4de2 | 46 | #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) |
<> | 154:37f96f9d4de2 | 47 | #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) |
<> | 154:37f96f9d4de2 | 48 | #else |
<> | 154:37f96f9d4de2 | 49 | #define RTC_ASYNCH_PREDIV (0x007F) |
<> | 154:37f96f9d4de2 | 50 | #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) |
<> | 154:37f96f9d4de2 | 51 | #endif |
<> | 154:37f96f9d4de2 | 52 | |
<> | 154:37f96f9d4de2 | 53 | #if DEVICE_LOWPOWERTIMER |
<> | 154:37f96f9d4de2 | 54 | static void (*irq_handler)(void); |
<> | 154:37f96f9d4de2 | 55 | static void RTC_IRQHandler(void); |
<> | 154:37f96f9d4de2 | 56 | #endif |
<> | 154:37f96f9d4de2 | 57 | |
<> | 154:37f96f9d4de2 | 58 | void rtc_init(void) |
<> | 154:37f96f9d4de2 | 59 | { |
AnnaBridge | 176:447f873cad2f | 60 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
AnnaBridge | 176:447f873cad2f | 61 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
<> | 154:37f96f9d4de2 | 62 | |
<> | 154:37f96f9d4de2 | 63 | // Enable access to Backup domain |
<> | 154:37f96f9d4de2 | 64 | HAL_PWR_EnableBkUpAccess(); |
<> | 154:37f96f9d4de2 | 65 | |
<> | 154:37f96f9d4de2 | 66 | RtcHandle.Instance = RTC; |
AnnaBridge | 176:447f873cad2f | 67 | RtcHandle.State = HAL_RTC_STATE_RESET; |
<> | 154:37f96f9d4de2 | 68 | |
<> | 154:37f96f9d4de2 | 69 | #if !RTC_LSI |
<> | 154:37f96f9d4de2 | 70 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
<> | 154:37f96f9d4de2 | 71 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
<> | 154:37f96f9d4de2 | 72 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
<> | 154:37f96f9d4de2 | 73 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
<> | 154:37f96f9d4de2 | 74 | |
<> | 154:37f96f9d4de2 | 75 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { |
<> | 154:37f96f9d4de2 | 76 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); |
<> | 154:37f96f9d4de2 | 77 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
<> | 154:37f96f9d4de2 | 78 | } else { |
<> | 154:37f96f9d4de2 | 79 | error("Cannot initialize RTC with LSE\n"); |
<> | 154:37f96f9d4de2 | 80 | } |
<> | 154:37f96f9d4de2 | 81 | |
<> | 154:37f96f9d4de2 | 82 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
<> | 154:37f96f9d4de2 | 83 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
<> | 154:37f96f9d4de2 | 84 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 85 | error("PeriphClkInitStruct RTC failed with LSE\n"); |
<> | 154:37f96f9d4de2 | 86 | } |
<> | 154:37f96f9d4de2 | 87 | #else /* !RTC_LSI */ |
AnnaBridge | 165:e614a9f1c9e2 | 88 | __HAL_RCC_PWR_CLK_ENABLE(); |
<> | 154:37f96f9d4de2 | 89 | |
<> | 154:37f96f9d4de2 | 90 | // Reset Backup domain |
<> | 154:37f96f9d4de2 | 91 | __HAL_RCC_BACKUPRESET_FORCE(); |
<> | 154:37f96f9d4de2 | 92 | __HAL_RCC_BACKUPRESET_RELEASE(); |
<> | 154:37f96f9d4de2 | 93 | |
<> | 154:37f96f9d4de2 | 94 | // Enable LSI clock |
<> | 154:37f96f9d4de2 | 95 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
<> | 154:37f96f9d4de2 | 96 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
<> | 154:37f96f9d4de2 | 97 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
<> | 154:37f96f9d4de2 | 98 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
<> | 154:37f96f9d4de2 | 99 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 100 | error("Cannot initialize RTC with LSI\n"); |
<> | 154:37f96f9d4de2 | 101 | } |
<> | 154:37f96f9d4de2 | 102 | |
<> | 154:37f96f9d4de2 | 103 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); |
<> | 154:37f96f9d4de2 | 104 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
<> | 154:37f96f9d4de2 | 105 | |
<> | 154:37f96f9d4de2 | 106 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
<> | 154:37f96f9d4de2 | 107 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
<> | 154:37f96f9d4de2 | 108 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 109 | error("PeriphClkInitStruct RTC failed with LSI\n"); |
<> | 154:37f96f9d4de2 | 110 | } |
<> | 154:37f96f9d4de2 | 111 | #endif /* !RTC_LSI */ |
<> | 154:37f96f9d4de2 | 112 | |
<> | 154:37f96f9d4de2 | 113 | // Enable RTC |
<> | 154:37f96f9d4de2 | 114 | __HAL_RCC_RTC_ENABLE(); |
<> | 154:37f96f9d4de2 | 115 | |
<> | 154:37f96f9d4de2 | 116 | #if TARGET_STM32F1 |
<> | 154:37f96f9d4de2 | 117 | RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; |
<> | 154:37f96f9d4de2 | 118 | #else /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 119 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
<> | 154:37f96f9d4de2 | 120 | RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; |
<> | 154:37f96f9d4de2 | 121 | RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; |
<> | 154:37f96f9d4de2 | 122 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
<> | 154:37f96f9d4de2 | 123 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
<> | 154:37f96f9d4de2 | 124 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
<> | 154:37f96f9d4de2 | 125 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 126 | |
<> | 154:37f96f9d4de2 | 127 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 128 | error("RTC error: RTC initialization failed."); |
<> | 154:37f96f9d4de2 | 129 | } |
<> | 154:37f96f9d4de2 | 130 | |
<> | 154:37f96f9d4de2 | 131 | #if DEVICE_LOWPOWERTIMER |
<> | 154:37f96f9d4de2 | 132 | |
<> | 154:37f96f9d4de2 | 133 | #if !RTC_LSI |
<> | 154:37f96f9d4de2 | 134 | if (!rtc_isenabled()) |
<> | 154:37f96f9d4de2 | 135 | #endif /* !RTC_LSI */ |
<> | 154:37f96f9d4de2 | 136 | { |
<> | 154:37f96f9d4de2 | 137 | rtc_write(0); |
<> | 154:37f96f9d4de2 | 138 | } |
<> | 154:37f96f9d4de2 | 139 | |
<> | 154:37f96f9d4de2 | 140 | NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 141 | NVIC_DisableIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 142 | NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); |
<> | 154:37f96f9d4de2 | 143 | NVIC_EnableIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 144 | |
<> | 154:37f96f9d4de2 | 145 | #endif /* DEVICE_LOWPOWERTIMER */ |
<> | 154:37f96f9d4de2 | 146 | } |
<> | 154:37f96f9d4de2 | 147 | |
<> | 154:37f96f9d4de2 | 148 | void rtc_free(void) |
<> | 154:37f96f9d4de2 | 149 | { |
<> | 154:37f96f9d4de2 | 150 | #if RTC_LSI |
<> | 154:37f96f9d4de2 | 151 | // Enable Power clock |
AnnaBridge | 165:e614a9f1c9e2 | 152 | __HAL_RCC_PWR_CLK_ENABLE(); |
<> | 154:37f96f9d4de2 | 153 | |
<> | 154:37f96f9d4de2 | 154 | // Enable access to Backup domain |
<> | 154:37f96f9d4de2 | 155 | HAL_PWR_EnableBkUpAccess(); |
<> | 154:37f96f9d4de2 | 156 | |
<> | 154:37f96f9d4de2 | 157 | // Reset Backup domain |
<> | 154:37f96f9d4de2 | 158 | __HAL_RCC_BACKUPRESET_FORCE(); |
<> | 154:37f96f9d4de2 | 159 | __HAL_RCC_BACKUPRESET_RELEASE(); |
<> | 154:37f96f9d4de2 | 160 | |
<> | 154:37f96f9d4de2 | 161 | // Disable access to Backup domain |
<> | 154:37f96f9d4de2 | 162 | HAL_PWR_DisableBkUpAccess(); |
<> | 154:37f96f9d4de2 | 163 | #endif |
<> | 154:37f96f9d4de2 | 164 | |
<> | 154:37f96f9d4de2 | 165 | // Disable LSI and LSE clocks |
AnnaBridge | 176:447f873cad2f | 166 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
<> | 154:37f96f9d4de2 | 167 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
<> | 154:37f96f9d4de2 | 168 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
<> | 154:37f96f9d4de2 | 169 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
<> | 154:37f96f9d4de2 | 170 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
<> | 154:37f96f9d4de2 | 171 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
<> | 154:37f96f9d4de2 | 172 | } |
<> | 154:37f96f9d4de2 | 173 | |
<> | 154:37f96f9d4de2 | 174 | /* |
<> | 160:d5399cc887bb | 175 | ST RTC_DateTypeDef structure |
<> | 160:d5399cc887bb | 176 | WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
<> | 160:d5399cc887bb | 177 | Month 0x1=january, 0x2=february, ..., 0x12=december |
<> | 160:d5399cc887bb | 178 | Date day of the month 1-31 |
<> | 160:d5399cc887bb | 179 | Year year 0-99 |
<> | 160:d5399cc887bb | 180 | |
<> | 160:d5399cc887bb | 181 | ST RTC_TimeTypeDef structure |
<> | 160:d5399cc887bb | 182 | Hours 0-12 if the RTC_HourFormat_12 is selected during init |
<> | 160:d5399cc887bb | 183 | 0-23 if the RTC_HourFormat_24 is selected during init |
<> | 160:d5399cc887bb | 184 | Minutes 0-59 |
<> | 160:d5399cc887bb | 185 | Seconds 0-59 |
<> | 160:d5399cc887bb | 186 | TimeFormat RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM |
<> | 160:d5399cc887bb | 187 | SubSeconds time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity |
<> | 160:d5399cc887bb | 188 | SecondFraction range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S) |
<> | 160:d5399cc887bb | 189 | DayLightSaving RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE |
<> | 160:d5399cc887bb | 190 | StoreOperation RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET |
<> | 160:d5399cc887bb | 191 | |
<> | 154:37f96f9d4de2 | 192 | struct tm |
<> | 154:37f96f9d4de2 | 193 | tm_sec seconds after the minute 0-61 |
<> | 154:37f96f9d4de2 | 194 | tm_min minutes after the hour 0-59 |
<> | 154:37f96f9d4de2 | 195 | tm_hour hours since midnight 0-23 |
<> | 154:37f96f9d4de2 | 196 | tm_mday day of the month 1-31 |
<> | 154:37f96f9d4de2 | 197 | tm_mon months since January 0-11 |
<> | 154:37f96f9d4de2 | 198 | tm_year years since 1900 |
<> | 154:37f96f9d4de2 | 199 | tm_wday days since Sunday 0-6 |
<> | 154:37f96f9d4de2 | 200 | tm_yday days since January 1 0-365 |
<> | 154:37f96f9d4de2 | 201 | tm_isdst Daylight Saving Time flag |
<> | 154:37f96f9d4de2 | 202 | */ |
<> | 160:d5399cc887bb | 203 | |
<> | 160:d5399cc887bb | 204 | /* |
<> | 160:d5399cc887bb | 205 | Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4: |
<> | 160:d5399cc887bb | 206 | BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits. |
<> | 160:d5399cc887bb | 207 | Because the first year is reserved to see if the RTC is init, the supposed range is 01-99. |
<> | 160:d5399cc887bb | 208 | 1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t). |
<> | 160:d5399cc887bb | 209 | 2nd point is to keep the year 1970 and the leap years synchronized. |
<> | 160:d5399cc887bb | 210 | |
<> | 160:d5399cc887bb | 211 | So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038. |
<> | 160:d5399cc887bb | 212 | 68 is also a multiple of 4 so it let the leap year synchronized. |
<> | 160:d5399cc887bb | 213 | |
<> | 160:d5399cc887bb | 214 | Information about STM32F1: |
<> | 160:d5399cc887bb | 215 | 32bit register is used (no BCD format) for the seconds and a software structure to store dates. |
<> | 160:d5399cc887bb | 216 | It is then not a problem to not use shifts. |
<> | 160:d5399cc887bb | 217 | */ |
<> | 160:d5399cc887bb | 218 | |
<> | 154:37f96f9d4de2 | 219 | time_t rtc_read(void) |
<> | 154:37f96f9d4de2 | 220 | { |
AnnaBridge | 176:447f873cad2f | 221 | RTC_DateTypeDef dateStruct = {0}; |
AnnaBridge | 176:447f873cad2f | 222 | RTC_TimeTypeDef timeStruct = {0}; |
<> | 154:37f96f9d4de2 | 223 | struct tm timeinfo; |
<> | 154:37f96f9d4de2 | 224 | |
<> | 154:37f96f9d4de2 | 225 | RtcHandle.Instance = RTC; |
<> | 154:37f96f9d4de2 | 226 | |
<> | 154:37f96f9d4de2 | 227 | // Read actual date and time |
<> | 154:37f96f9d4de2 | 228 | // Warning: the time must be read first! |
<> | 160:d5399cc887bb | 229 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
<> | 160:d5399cc887bb | 230 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
<> | 154:37f96f9d4de2 | 231 | |
<> | 154:37f96f9d4de2 | 232 | // Setup a tm structure based on the RTC |
<> | 160:d5399cc887bb | 233 | /* tm_wday information is ignored by mktime */ |
<> | 154:37f96f9d4de2 | 234 | timeinfo.tm_mon = dateStruct.Month - 1; |
<> | 154:37f96f9d4de2 | 235 | timeinfo.tm_mday = dateStruct.Date; |
<> | 154:37f96f9d4de2 | 236 | timeinfo.tm_year = dateStruct.Year + 68; |
<> | 154:37f96f9d4de2 | 237 | timeinfo.tm_hour = timeStruct.Hours; |
<> | 154:37f96f9d4de2 | 238 | timeinfo.tm_min = timeStruct.Minutes; |
<> | 154:37f96f9d4de2 | 239 | timeinfo.tm_sec = timeStruct.Seconds; |
<> | 154:37f96f9d4de2 | 240 | // Daylight Saving Time information is not available |
<> | 154:37f96f9d4de2 | 241 | timeinfo.tm_isdst = -1; |
<> | 154:37f96f9d4de2 | 242 | |
<> | 154:37f96f9d4de2 | 243 | // Convert to timestamp |
AnnaBridge | 167:e84263d55307 | 244 | time_t t = _rtc_mktime(&timeinfo); |
<> | 154:37f96f9d4de2 | 245 | |
<> | 154:37f96f9d4de2 | 246 | return t; |
<> | 154:37f96f9d4de2 | 247 | } |
<> | 154:37f96f9d4de2 | 248 | |
<> | 154:37f96f9d4de2 | 249 | void rtc_write(time_t t) |
<> | 154:37f96f9d4de2 | 250 | { |
AnnaBridge | 176:447f873cad2f | 251 | RTC_DateTypeDef dateStruct = {0}; |
AnnaBridge | 176:447f873cad2f | 252 | RTC_TimeTypeDef timeStruct = {0}; |
<> | 154:37f96f9d4de2 | 253 | |
<> | 154:37f96f9d4de2 | 254 | RtcHandle.Instance = RTC; |
<> | 154:37f96f9d4de2 | 255 | |
<> | 154:37f96f9d4de2 | 256 | // Convert the time into a tm |
AnnaBridge | 167:e84263d55307 | 257 | struct tm timeinfo; |
AnnaBridge | 167:e84263d55307 | 258 | if (_rtc_localtime(t, &timeinfo) == false) { |
AnnaBridge | 167:e84263d55307 | 259 | return; |
AnnaBridge | 167:e84263d55307 | 260 | } |
<> | 154:37f96f9d4de2 | 261 | |
<> | 154:37f96f9d4de2 | 262 | // Fill RTC structures |
AnnaBridge | 167:e84263d55307 | 263 | if (timeinfo.tm_wday == 0) { |
<> | 160:d5399cc887bb | 264 | dateStruct.WeekDay = 7; |
<> | 160:d5399cc887bb | 265 | } else { |
AnnaBridge | 167:e84263d55307 | 266 | dateStruct.WeekDay = timeinfo.tm_wday; |
<> | 160:d5399cc887bb | 267 | } |
AnnaBridge | 167:e84263d55307 | 268 | dateStruct.Month = timeinfo.tm_mon + 1; |
AnnaBridge | 167:e84263d55307 | 269 | dateStruct.Date = timeinfo.tm_mday; |
AnnaBridge | 167:e84263d55307 | 270 | dateStruct.Year = timeinfo.tm_year - 68; |
AnnaBridge | 167:e84263d55307 | 271 | timeStruct.Hours = timeinfo.tm_hour; |
AnnaBridge | 167:e84263d55307 | 272 | timeStruct.Minutes = timeinfo.tm_min; |
AnnaBridge | 167:e84263d55307 | 273 | timeStruct.Seconds = timeinfo.tm_sec; |
<> | 154:37f96f9d4de2 | 274 | |
<> | 154:37f96f9d4de2 | 275 | #if !(TARGET_STM32F1) |
<> | 154:37f96f9d4de2 | 276 | timeStruct.TimeFormat = RTC_HOURFORMAT_24; |
<> | 154:37f96f9d4de2 | 277 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
<> | 154:37f96f9d4de2 | 278 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
<> | 154:37f96f9d4de2 | 279 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 280 | |
<> | 154:37f96f9d4de2 | 281 | // Change the RTC current date/time |
<> | 160:d5399cc887bb | 282 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
<> | 160:d5399cc887bb | 283 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
<> | 154:37f96f9d4de2 | 284 | } |
<> | 154:37f96f9d4de2 | 285 | |
<> | 154:37f96f9d4de2 | 286 | int rtc_isenabled(void) |
<> | 154:37f96f9d4de2 | 287 | { |
<> | 160:d5399cc887bb | 288 | #if !(TARGET_STM32F1) |
<> | 160:d5399cc887bb | 289 | return ( ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) && ((RTC->ISR & RTC_ISR_RSF) == RTC_ISR_RSF) ); |
<> | 160:d5399cc887bb | 290 | #else /* TARGET_STM32F1 */ |
<> | 160:d5399cc887bb | 291 | return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF); |
<> | 160:d5399cc887bb | 292 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 293 | } |
<> | 154:37f96f9d4de2 | 294 | |
<> | 154:37f96f9d4de2 | 295 | #if DEVICE_LOWPOWERTIMER |
<> | 154:37f96f9d4de2 | 296 | |
<> | 154:37f96f9d4de2 | 297 | static void RTC_IRQHandler(void) |
<> | 154:37f96f9d4de2 | 298 | { |
AnnaBridge | 176:447f873cad2f | 299 | /* Update HAL state */ |
<> | 154:37f96f9d4de2 | 300 | HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); |
AnnaBridge | 176:447f873cad2f | 301 | /* In case of registered handler, call it. */ |
<> | 154:37f96f9d4de2 | 302 | if (irq_handler) { |
<> | 154:37f96f9d4de2 | 303 | irq_handler(); |
<> | 154:37f96f9d4de2 | 304 | } |
<> | 154:37f96f9d4de2 | 305 | } |
<> | 154:37f96f9d4de2 | 306 | |
<> | 154:37f96f9d4de2 | 307 | void rtc_set_irq_handler(uint32_t handler) |
<> | 154:37f96f9d4de2 | 308 | { |
<> | 154:37f96f9d4de2 | 309 | irq_handler = (void (*)(void))handler; |
<> | 154:37f96f9d4de2 | 310 | } |
<> | 154:37f96f9d4de2 | 311 | |
<> | 154:37f96f9d4de2 | 312 | uint32_t rtc_read_subseconds(void) |
<> | 154:37f96f9d4de2 | 313 | { |
<> | 154:37f96f9d4de2 | 314 | return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); |
<> | 154:37f96f9d4de2 | 315 | } |
<> | 154:37f96f9d4de2 | 316 | |
<> | 154:37f96f9d4de2 | 317 | void rtc_set_wake_up_timer(uint32_t delta) |
<> | 154:37f96f9d4de2 | 318 | { |
<> | 154:37f96f9d4de2 | 319 | uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); |
<> | 154:37f96f9d4de2 | 320 | |
<> | 154:37f96f9d4de2 | 321 | if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, |
<> | 154:37f96f9d4de2 | 322 | RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 323 | error("Set wake up timer failed\n"); |
<> | 154:37f96f9d4de2 | 324 | } |
<> | 154:37f96f9d4de2 | 325 | } |
<> | 154:37f96f9d4de2 | 326 | |
<> | 154:37f96f9d4de2 | 327 | void rtc_deactivate_wake_up_timer(void) |
<> | 154:37f96f9d4de2 | 328 | { |
<> | 154:37f96f9d4de2 | 329 | HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); |
<> | 154:37f96f9d4de2 | 330 | } |
<> | 154:37f96f9d4de2 | 331 | |
<> | 154:37f96f9d4de2 | 332 | void rtc_synchronize(void) |
<> | 154:37f96f9d4de2 | 333 | { |
<> | 154:37f96f9d4de2 | 334 | HAL_RTC_WaitForSynchro(&RtcHandle); |
<> | 154:37f96f9d4de2 | 335 | } |
<> | 154:37f96f9d4de2 | 336 | #endif /* DEVICE_LOWPOWERTIMER */ |
<> | 154:37f96f9d4de2 | 337 | |
<> | 154:37f96f9d4de2 | 338 | #endif /* DEVICE_RTC */ |