forked
targets/TARGET_STM/rtc_api.c@167:e84263d55307, 2017-06-21 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Jun 21 17:46:44 2017 +0100
- Revision:
- 167:e84263d55307
- Parent:
- 165:e614a9f1c9e2
This updates the lib to the mbed lib v 145
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 | { |
<> | 154:37f96f9d4de2 | 60 | RCC_OscInitTypeDef RCC_OscInitStruct; |
<> | 154:37f96f9d4de2 | 61 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
<> | 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; |
<> | 154:37f96f9d4de2 | 67 | |
<> | 154:37f96f9d4de2 | 68 | #if !RTC_LSI |
<> | 154:37f96f9d4de2 | 69 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
<> | 154:37f96f9d4de2 | 70 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
<> | 154:37f96f9d4de2 | 71 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
<> | 154:37f96f9d4de2 | 72 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
<> | 154:37f96f9d4de2 | 73 | |
<> | 154:37f96f9d4de2 | 74 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { |
<> | 154:37f96f9d4de2 | 75 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); |
<> | 154:37f96f9d4de2 | 76 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
<> | 154:37f96f9d4de2 | 77 | } else { |
<> | 154:37f96f9d4de2 | 78 | error("Cannot initialize RTC with LSE\n"); |
<> | 154:37f96f9d4de2 | 79 | } |
<> | 154:37f96f9d4de2 | 80 | |
<> | 154:37f96f9d4de2 | 81 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
<> | 154:37f96f9d4de2 | 82 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
<> | 154:37f96f9d4de2 | 83 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 84 | error("PeriphClkInitStruct RTC failed with LSE\n"); |
<> | 154:37f96f9d4de2 | 85 | } |
<> | 154:37f96f9d4de2 | 86 | #else /* !RTC_LSI */ |
AnnaBridge | 165:e614a9f1c9e2 | 87 | __HAL_RCC_PWR_CLK_ENABLE(); |
<> | 154:37f96f9d4de2 | 88 | |
<> | 154:37f96f9d4de2 | 89 | // Reset Backup domain |
<> | 154:37f96f9d4de2 | 90 | __HAL_RCC_BACKUPRESET_FORCE(); |
<> | 154:37f96f9d4de2 | 91 | __HAL_RCC_BACKUPRESET_RELEASE(); |
<> | 154:37f96f9d4de2 | 92 | |
<> | 154:37f96f9d4de2 | 93 | // Enable LSI clock |
<> | 154:37f96f9d4de2 | 94 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
<> | 154:37f96f9d4de2 | 95 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
<> | 154:37f96f9d4de2 | 96 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
<> | 154:37f96f9d4de2 | 97 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
<> | 154:37f96f9d4de2 | 98 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 99 | error("Cannot initialize RTC with LSI\n"); |
<> | 154:37f96f9d4de2 | 100 | } |
<> | 154:37f96f9d4de2 | 101 | |
<> | 154:37f96f9d4de2 | 102 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); |
<> | 154:37f96f9d4de2 | 103 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
<> | 154:37f96f9d4de2 | 104 | |
<> | 154:37f96f9d4de2 | 105 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
<> | 154:37f96f9d4de2 | 106 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
<> | 154:37f96f9d4de2 | 107 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 108 | error("PeriphClkInitStruct RTC failed with LSI\n"); |
<> | 154:37f96f9d4de2 | 109 | } |
<> | 154:37f96f9d4de2 | 110 | #endif /* !RTC_LSI */ |
<> | 154:37f96f9d4de2 | 111 | |
<> | 154:37f96f9d4de2 | 112 | // Enable RTC |
<> | 154:37f96f9d4de2 | 113 | __HAL_RCC_RTC_ENABLE(); |
<> | 154:37f96f9d4de2 | 114 | |
<> | 154:37f96f9d4de2 | 115 | #if TARGET_STM32F1 |
<> | 154:37f96f9d4de2 | 116 | RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; |
<> | 154:37f96f9d4de2 | 117 | #else /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 118 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
<> | 154:37f96f9d4de2 | 119 | RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; |
<> | 154:37f96f9d4de2 | 120 | RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; |
<> | 154:37f96f9d4de2 | 121 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
<> | 154:37f96f9d4de2 | 122 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
<> | 154:37f96f9d4de2 | 123 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
<> | 154:37f96f9d4de2 | 124 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 125 | |
<> | 154:37f96f9d4de2 | 126 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 127 | error("RTC error: RTC initialization failed."); |
<> | 154:37f96f9d4de2 | 128 | } |
<> | 154:37f96f9d4de2 | 129 | |
<> | 154:37f96f9d4de2 | 130 | #if DEVICE_LOWPOWERTIMER |
<> | 154:37f96f9d4de2 | 131 | |
<> | 154:37f96f9d4de2 | 132 | #if !RTC_LSI |
<> | 154:37f96f9d4de2 | 133 | if (!rtc_isenabled()) |
<> | 154:37f96f9d4de2 | 134 | #endif /* !RTC_LSI */ |
<> | 154:37f96f9d4de2 | 135 | { |
<> | 154:37f96f9d4de2 | 136 | rtc_write(0); |
<> | 154:37f96f9d4de2 | 137 | } |
<> | 154:37f96f9d4de2 | 138 | |
<> | 154:37f96f9d4de2 | 139 | NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 140 | NVIC_DisableIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 141 | NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); |
<> | 154:37f96f9d4de2 | 142 | NVIC_EnableIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 143 | |
<> | 154:37f96f9d4de2 | 144 | #endif /* DEVICE_LOWPOWERTIMER */ |
<> | 154:37f96f9d4de2 | 145 | } |
<> | 154:37f96f9d4de2 | 146 | |
<> | 154:37f96f9d4de2 | 147 | void rtc_free(void) |
<> | 154:37f96f9d4de2 | 148 | { |
<> | 154:37f96f9d4de2 | 149 | #if RTC_LSI |
<> | 154:37f96f9d4de2 | 150 | // Enable Power clock |
AnnaBridge | 165:e614a9f1c9e2 | 151 | __HAL_RCC_PWR_CLK_ENABLE(); |
<> | 154:37f96f9d4de2 | 152 | |
<> | 154:37f96f9d4de2 | 153 | // Enable access to Backup domain |
<> | 154:37f96f9d4de2 | 154 | HAL_PWR_EnableBkUpAccess(); |
<> | 154:37f96f9d4de2 | 155 | |
<> | 154:37f96f9d4de2 | 156 | // Reset Backup domain |
<> | 154:37f96f9d4de2 | 157 | __HAL_RCC_BACKUPRESET_FORCE(); |
<> | 154:37f96f9d4de2 | 158 | __HAL_RCC_BACKUPRESET_RELEASE(); |
<> | 154:37f96f9d4de2 | 159 | |
<> | 154:37f96f9d4de2 | 160 | // Disable access to Backup domain |
<> | 154:37f96f9d4de2 | 161 | HAL_PWR_DisableBkUpAccess(); |
<> | 154:37f96f9d4de2 | 162 | #endif |
<> | 154:37f96f9d4de2 | 163 | |
<> | 154:37f96f9d4de2 | 164 | // Disable LSI and LSE clocks |
<> | 154:37f96f9d4de2 | 165 | RCC_OscInitTypeDef RCC_OscInitStruct; |
<> | 154:37f96f9d4de2 | 166 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
<> | 154:37f96f9d4de2 | 167 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
<> | 154:37f96f9d4de2 | 168 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
<> | 154:37f96f9d4de2 | 169 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
<> | 154:37f96f9d4de2 | 170 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
<> | 154:37f96f9d4de2 | 171 | } |
<> | 154:37f96f9d4de2 | 172 | |
<> | 154:37f96f9d4de2 | 173 | /* |
<> | 160:d5399cc887bb | 174 | ST RTC_DateTypeDef structure |
<> | 160:d5399cc887bb | 175 | WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
<> | 160:d5399cc887bb | 176 | Month 0x1=january, 0x2=february, ..., 0x12=december |
<> | 160:d5399cc887bb | 177 | Date day of the month 1-31 |
<> | 160:d5399cc887bb | 178 | Year year 0-99 |
<> | 160:d5399cc887bb | 179 | |
<> | 160:d5399cc887bb | 180 | ST RTC_TimeTypeDef structure |
<> | 160:d5399cc887bb | 181 | Hours 0-12 if the RTC_HourFormat_12 is selected during init |
<> | 160:d5399cc887bb | 182 | 0-23 if the RTC_HourFormat_24 is selected during init |
<> | 160:d5399cc887bb | 183 | Minutes 0-59 |
<> | 160:d5399cc887bb | 184 | Seconds 0-59 |
<> | 160:d5399cc887bb | 185 | TimeFormat RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM |
<> | 160:d5399cc887bb | 186 | SubSeconds time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity |
<> | 160:d5399cc887bb | 187 | SecondFraction range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S) |
<> | 160:d5399cc887bb | 188 | DayLightSaving RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE |
<> | 160:d5399cc887bb | 189 | StoreOperation RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET |
<> | 160:d5399cc887bb | 190 | |
<> | 154:37f96f9d4de2 | 191 | struct tm |
<> | 154:37f96f9d4de2 | 192 | tm_sec seconds after the minute 0-61 |
<> | 154:37f96f9d4de2 | 193 | tm_min minutes after the hour 0-59 |
<> | 154:37f96f9d4de2 | 194 | tm_hour hours since midnight 0-23 |
<> | 154:37f96f9d4de2 | 195 | tm_mday day of the month 1-31 |
<> | 154:37f96f9d4de2 | 196 | tm_mon months since January 0-11 |
<> | 154:37f96f9d4de2 | 197 | tm_year years since 1900 |
<> | 154:37f96f9d4de2 | 198 | tm_wday days since Sunday 0-6 |
<> | 154:37f96f9d4de2 | 199 | tm_yday days since January 1 0-365 |
<> | 154:37f96f9d4de2 | 200 | tm_isdst Daylight Saving Time flag |
<> | 154:37f96f9d4de2 | 201 | */ |
<> | 160:d5399cc887bb | 202 | |
<> | 160:d5399cc887bb | 203 | /* |
<> | 160:d5399cc887bb | 204 | Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4: |
<> | 160:d5399cc887bb | 205 | BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits. |
<> | 160:d5399cc887bb | 206 | Because the first year is reserved to see if the RTC is init, the supposed range is 01-99. |
<> | 160:d5399cc887bb | 207 | 1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t). |
<> | 160:d5399cc887bb | 208 | 2nd point is to keep the year 1970 and the leap years synchronized. |
<> | 160:d5399cc887bb | 209 | |
<> | 160:d5399cc887bb | 210 | So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038. |
<> | 160:d5399cc887bb | 211 | 68 is also a multiple of 4 so it let the leap year synchronized. |
<> | 160:d5399cc887bb | 212 | |
<> | 160:d5399cc887bb | 213 | Information about STM32F1: |
<> | 160:d5399cc887bb | 214 | 32bit register is used (no BCD format) for the seconds and a software structure to store dates. |
<> | 160:d5399cc887bb | 215 | It is then not a problem to not use shifts. |
<> | 160:d5399cc887bb | 216 | */ |
<> | 160:d5399cc887bb | 217 | |
<> | 154:37f96f9d4de2 | 218 | time_t rtc_read(void) |
<> | 154:37f96f9d4de2 | 219 | { |
<> | 154:37f96f9d4de2 | 220 | RTC_DateTypeDef dateStruct; |
<> | 154:37f96f9d4de2 | 221 | RTC_TimeTypeDef timeStruct; |
<> | 154:37f96f9d4de2 | 222 | struct tm timeinfo; |
<> | 154:37f96f9d4de2 | 223 | |
<> | 154:37f96f9d4de2 | 224 | RtcHandle.Instance = RTC; |
<> | 154:37f96f9d4de2 | 225 | |
<> | 154:37f96f9d4de2 | 226 | // Read actual date and time |
<> | 154:37f96f9d4de2 | 227 | // Warning: the time must be read first! |
<> | 160:d5399cc887bb | 228 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
<> | 160:d5399cc887bb | 229 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
<> | 154:37f96f9d4de2 | 230 | |
<> | 154:37f96f9d4de2 | 231 | // Setup a tm structure based on the RTC |
<> | 160:d5399cc887bb | 232 | /* tm_wday information is ignored by mktime */ |
<> | 154:37f96f9d4de2 | 233 | timeinfo.tm_mon = dateStruct.Month - 1; |
<> | 154:37f96f9d4de2 | 234 | timeinfo.tm_mday = dateStruct.Date; |
<> | 154:37f96f9d4de2 | 235 | timeinfo.tm_year = dateStruct.Year + 68; |
<> | 154:37f96f9d4de2 | 236 | timeinfo.tm_hour = timeStruct.Hours; |
<> | 154:37f96f9d4de2 | 237 | timeinfo.tm_min = timeStruct.Minutes; |
<> | 154:37f96f9d4de2 | 238 | timeinfo.tm_sec = timeStruct.Seconds; |
<> | 154:37f96f9d4de2 | 239 | // Daylight Saving Time information is not available |
<> | 154:37f96f9d4de2 | 240 | timeinfo.tm_isdst = -1; |
<> | 154:37f96f9d4de2 | 241 | |
<> | 154:37f96f9d4de2 | 242 | // Convert to timestamp |
AnnaBridge | 167:e84263d55307 | 243 | time_t t = _rtc_mktime(&timeinfo); |
<> | 154:37f96f9d4de2 | 244 | |
<> | 154:37f96f9d4de2 | 245 | return t; |
<> | 154:37f96f9d4de2 | 246 | } |
<> | 154:37f96f9d4de2 | 247 | |
<> | 154:37f96f9d4de2 | 248 | void rtc_write(time_t t) |
<> | 154:37f96f9d4de2 | 249 | { |
<> | 154:37f96f9d4de2 | 250 | RTC_DateTypeDef dateStruct; |
<> | 154:37f96f9d4de2 | 251 | RTC_TimeTypeDef timeStruct; |
<> | 154:37f96f9d4de2 | 252 | |
<> | 154:37f96f9d4de2 | 253 | RtcHandle.Instance = RTC; |
<> | 154:37f96f9d4de2 | 254 | |
<> | 154:37f96f9d4de2 | 255 | // Convert the time into a tm |
AnnaBridge | 167:e84263d55307 | 256 | struct tm timeinfo; |
AnnaBridge | 167:e84263d55307 | 257 | if (_rtc_localtime(t, &timeinfo) == false) { |
AnnaBridge | 167:e84263d55307 | 258 | return; |
AnnaBridge | 167:e84263d55307 | 259 | } |
<> | 154:37f96f9d4de2 | 260 | |
<> | 154:37f96f9d4de2 | 261 | // Fill RTC structures |
AnnaBridge | 167:e84263d55307 | 262 | if (timeinfo.tm_wday == 0) { |
<> | 160:d5399cc887bb | 263 | dateStruct.WeekDay = 7; |
<> | 160:d5399cc887bb | 264 | } else { |
AnnaBridge | 167:e84263d55307 | 265 | dateStruct.WeekDay = timeinfo.tm_wday; |
<> | 160:d5399cc887bb | 266 | } |
AnnaBridge | 167:e84263d55307 | 267 | dateStruct.Month = timeinfo.tm_mon + 1; |
AnnaBridge | 167:e84263d55307 | 268 | dateStruct.Date = timeinfo.tm_mday; |
AnnaBridge | 167:e84263d55307 | 269 | dateStruct.Year = timeinfo.tm_year - 68; |
AnnaBridge | 167:e84263d55307 | 270 | timeStruct.Hours = timeinfo.tm_hour; |
AnnaBridge | 167:e84263d55307 | 271 | timeStruct.Minutes = timeinfo.tm_min; |
AnnaBridge | 167:e84263d55307 | 272 | timeStruct.Seconds = timeinfo.tm_sec; |
<> | 154:37f96f9d4de2 | 273 | |
<> | 154:37f96f9d4de2 | 274 | #if !(TARGET_STM32F1) |
<> | 154:37f96f9d4de2 | 275 | timeStruct.TimeFormat = RTC_HOURFORMAT_24; |
<> | 154:37f96f9d4de2 | 276 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
<> | 154:37f96f9d4de2 | 277 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
<> | 154:37f96f9d4de2 | 278 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 279 | |
<> | 154:37f96f9d4de2 | 280 | // Change the RTC current date/time |
<> | 160:d5399cc887bb | 281 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
<> | 160:d5399cc887bb | 282 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
<> | 154:37f96f9d4de2 | 283 | } |
<> | 154:37f96f9d4de2 | 284 | |
<> | 154:37f96f9d4de2 | 285 | int rtc_isenabled(void) |
<> | 154:37f96f9d4de2 | 286 | { |
<> | 160:d5399cc887bb | 287 | #if !(TARGET_STM32F1) |
<> | 160:d5399cc887bb | 288 | return ( ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) && ((RTC->ISR & RTC_ISR_RSF) == RTC_ISR_RSF) ); |
<> | 160:d5399cc887bb | 289 | #else /* TARGET_STM32F1 */ |
<> | 160:d5399cc887bb | 290 | return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF); |
<> | 160:d5399cc887bb | 291 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 292 | } |
<> | 154:37f96f9d4de2 | 293 | |
<> | 154:37f96f9d4de2 | 294 | #if DEVICE_LOWPOWERTIMER |
<> | 154:37f96f9d4de2 | 295 | |
<> | 154:37f96f9d4de2 | 296 | static void RTC_IRQHandler(void) |
<> | 154:37f96f9d4de2 | 297 | { |
<> | 154:37f96f9d4de2 | 298 | HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); |
<> | 154:37f96f9d4de2 | 299 | } |
<> | 154:37f96f9d4de2 | 300 | |
<> | 154:37f96f9d4de2 | 301 | void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) |
<> | 154:37f96f9d4de2 | 302 | { |
<> | 154:37f96f9d4de2 | 303 | if (irq_handler) { |
<> | 154:37f96f9d4de2 | 304 | // Fire the user callback |
<> | 154:37f96f9d4de2 | 305 | irq_handler(); |
<> | 154:37f96f9d4de2 | 306 | } |
<> | 154:37f96f9d4de2 | 307 | } |
<> | 154:37f96f9d4de2 | 308 | |
<> | 154:37f96f9d4de2 | 309 | void rtc_set_irq_handler(uint32_t handler) |
<> | 154:37f96f9d4de2 | 310 | { |
<> | 154:37f96f9d4de2 | 311 | irq_handler = (void (*)(void))handler; |
<> | 154:37f96f9d4de2 | 312 | } |
<> | 154:37f96f9d4de2 | 313 | |
<> | 154:37f96f9d4de2 | 314 | uint32_t rtc_read_subseconds(void) |
<> | 154:37f96f9d4de2 | 315 | { |
<> | 154:37f96f9d4de2 | 316 | return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); |
<> | 154:37f96f9d4de2 | 317 | } |
<> | 154:37f96f9d4de2 | 318 | |
<> | 154:37f96f9d4de2 | 319 | void rtc_set_wake_up_timer(uint32_t delta) |
<> | 154:37f96f9d4de2 | 320 | { |
<> | 154:37f96f9d4de2 | 321 | uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); |
<> | 154:37f96f9d4de2 | 322 | |
<> | 154:37f96f9d4de2 | 323 | if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, |
<> | 154:37f96f9d4de2 | 324 | RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 325 | error("Set wake up timer failed\n"); |
<> | 154:37f96f9d4de2 | 326 | } |
<> | 154:37f96f9d4de2 | 327 | } |
<> | 154:37f96f9d4de2 | 328 | |
<> | 154:37f96f9d4de2 | 329 | void rtc_deactivate_wake_up_timer(void) |
<> | 154:37f96f9d4de2 | 330 | { |
<> | 154:37f96f9d4de2 | 331 | HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); |
<> | 154:37f96f9d4de2 | 332 | } |
<> | 154:37f96f9d4de2 | 333 | |
<> | 154:37f96f9d4de2 | 334 | void rtc_synchronize(void) |
<> | 154:37f96f9d4de2 | 335 | { |
<> | 154:37f96f9d4de2 | 336 | HAL_RTC_WaitForSynchro(&RtcHandle); |
<> | 154:37f96f9d4de2 | 337 | } |
<> | 154:37f96f9d4de2 | 338 | #endif /* DEVICE_LOWPOWERTIMER */ |
<> | 154:37f96f9d4de2 | 339 | |
<> | 154:37f96f9d4de2 | 340 | #endif /* DEVICE_RTC */ |