Modified for BG96

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Wed Jan 17 15:23:54 2018 +0000
Revision:
180:96ed750bd169
Parent:
176:447f873cad2f
Child:
181:57724642e740
mbed-dev libray. Release version 158

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 154:37f96f9d4de2 1 /* mbed Microcontroller Library
<> 154:37f96f9d4de2 2 *******************************************************************************
Anna Bridge 180:96ed750bd169 3 * Copyright (c) 2017, 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 */
Anna Bridge 180:96ed750bd169 30
<> 154:37f96f9d4de2 31 #if DEVICE_RTC
<> 154:37f96f9d4de2 32
<> 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 DEVICE_LOWPOWERTIMER
<> 154:37f96f9d4de2 40 static void (*irq_handler)(void);
<> 154:37f96f9d4de2 41 static void RTC_IRQHandler(void);
<> 154:37f96f9d4de2 42 #endif
<> 154:37f96f9d4de2 43
<> 154:37f96f9d4de2 44 void rtc_init(void)
<> 154:37f96f9d4de2 45 {
AnnaBridge 176:447f873cad2f 46 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
AnnaBridge 176:447f873cad2f 47 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
<> 154:37f96f9d4de2 48
<> 154:37f96f9d4de2 49 // Enable access to Backup domain
Anna Bridge 180:96ed750bd169 50 __HAL_RCC_PWR_CLK_ENABLE();
<> 154:37f96f9d4de2 51 HAL_PWR_EnableBkUpAccess();
<> 154:37f96f9d4de2 52
Anna Bridge 180:96ed750bd169 53 if (rtc_isenabled()) {
Anna Bridge 180:96ed750bd169 54 return;
Anna Bridge 180:96ed750bd169 55 }
<> 154:37f96f9d4de2 56
Anna Bridge 180:96ed750bd169 57 #if MBED_CONF_TARGET_LSE_AVAILABLE
Anna Bridge 180:96ed750bd169 58 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_LSE;
<> 154:37f96f9d4de2 59 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
<> 154:37f96f9d4de2 60 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
<> 154:37f96f9d4de2 61 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
<> 154:37f96f9d4de2 62
Anna Bridge 180:96ed750bd169 63 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
<> 154:37f96f9d4de2 64 error("Cannot initialize RTC with LSE\n");
<> 154:37f96f9d4de2 65 }
<> 154:37f96f9d4de2 66
Anna Bridge 180:96ed750bd169 67 __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
Anna Bridge 180:96ed750bd169 68 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
Anna Bridge 180:96ed750bd169 69
<> 154:37f96f9d4de2 70 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
<> 154:37f96f9d4de2 71 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
<> 154:37f96f9d4de2 72 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
<> 154:37f96f9d4de2 73 error("PeriphClkInitStruct RTC failed with LSE\n");
<> 154:37f96f9d4de2 74 }
Anna Bridge 180:96ed750bd169 75 #else /* MBED_CONF_TARGET_LSE_AVAILABLE */
<> 154:37f96f9d4de2 76 // Reset Backup domain
<> 154:37f96f9d4de2 77 __HAL_RCC_BACKUPRESET_FORCE();
<> 154:37f96f9d4de2 78 __HAL_RCC_BACKUPRESET_RELEASE();
<> 154:37f96f9d4de2 79
<> 154:37f96f9d4de2 80 // Enable LSI clock
Anna Bridge 180:96ed750bd169 81 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_LSE;
<> 154:37f96f9d4de2 82 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
<> 154:37f96f9d4de2 83 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
<> 154:37f96f9d4de2 84 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
<> 154:37f96f9d4de2 85 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
<> 154:37f96f9d4de2 86 error("Cannot initialize RTC with LSI\n");
<> 154:37f96f9d4de2 87 }
<> 154:37f96f9d4de2 88
<> 154:37f96f9d4de2 89 __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
<> 154:37f96f9d4de2 90 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
<> 154:37f96f9d4de2 91
<> 154:37f96f9d4de2 92 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
<> 154:37f96f9d4de2 93 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
<> 154:37f96f9d4de2 94 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
<> 154:37f96f9d4de2 95 error("PeriphClkInitStruct RTC failed with LSI\n");
<> 154:37f96f9d4de2 96 }
Anna Bridge 180:96ed750bd169 97 #endif /* MBED_CONF_TARGET_LSE_AVAILABLE */
<> 154:37f96f9d4de2 98
<> 154:37f96f9d4de2 99 // Enable RTC
<> 154:37f96f9d4de2 100 __HAL_RCC_RTC_ENABLE();
<> 154:37f96f9d4de2 101
Anna Bridge 180:96ed750bd169 102 RtcHandle.Instance = RTC;
Anna Bridge 180:96ed750bd169 103 RtcHandle.State = HAL_RTC_STATE_RESET;
Anna Bridge 180:96ed750bd169 104
<> 154:37f96f9d4de2 105 #if TARGET_STM32F1
<> 154:37f96f9d4de2 106 RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
<> 154:37f96f9d4de2 107 #else /* TARGET_STM32F1 */
<> 154:37f96f9d4de2 108 RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
Anna Bridge 180:96ed750bd169 109
Anna Bridge 180:96ed750bd169 110 /* PREDIV_A : 7-bit asynchronous prescaler */
Anna Bridge 180:96ed750bd169 111 #if DEVICE_LOWPOWERTIMER
Anna Bridge 180:96ed750bd169 112 /* PREDIV_A is set to a small value to improve the SubSeconds resolution */
Anna Bridge 180:96ed750bd169 113 /* with a 32768Hz clock, PREDIV_A=7 gives a precision of 244us */
Anna Bridge 180:96ed750bd169 114 RtcHandle.Init.AsynchPrediv = 7;
Anna Bridge 180:96ed750bd169 115 #else
Anna Bridge 180:96ed750bd169 116 /* PREDIV_A is set to the maximum value to improve the consumption */
Anna Bridge 180:96ed750bd169 117 RtcHandle.Init.AsynchPrediv = 0x007F;
Anna Bridge 180:96ed750bd169 118 #endif
Anna Bridge 180:96ed750bd169 119 /* PREDIV_S : 15-bit synchronous prescaler */
Anna Bridge 180:96ed750bd169 120 /* PREDIV_S is set in order to get a 1 Hz clock */
Anna Bridge 180:96ed750bd169 121 RtcHandle.Init.SynchPrediv = RTC_CLOCK / (RtcHandle.Init.AsynchPrediv + 1) - 1;
<> 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) {
Anna Bridge 180:96ed750bd169 128 error("RTC initialization failed");
<> 154:37f96f9d4de2 129 }
<> 154:37f96f9d4de2 130
Anna Bridge 180:96ed750bd169 131 rtc_synchronize(); // Wait for RSF
<> 154:37f96f9d4de2 132
Anna Bridge 180:96ed750bd169 133 if (!rtc_isenabled()) {
<> 154:37f96f9d4de2 134 rtc_write(0);
<> 154:37f96f9d4de2 135 }
<> 154:37f96f9d4de2 136 }
<> 154:37f96f9d4de2 137
<> 154:37f96f9d4de2 138 void rtc_free(void)
<> 154:37f96f9d4de2 139 {
Anna Bridge 180:96ed750bd169 140 #if !MBED_CONF_TARGET_LSE_AVAILABLE
<> 154:37f96f9d4de2 141 // Enable Power clock
AnnaBridge 165:e614a9f1c9e2 142 __HAL_RCC_PWR_CLK_ENABLE();
<> 154:37f96f9d4de2 143
<> 154:37f96f9d4de2 144 // Enable access to Backup domain
<> 154:37f96f9d4de2 145 HAL_PWR_EnableBkUpAccess();
<> 154:37f96f9d4de2 146
<> 154:37f96f9d4de2 147 // Reset Backup domain
<> 154:37f96f9d4de2 148 __HAL_RCC_BACKUPRESET_FORCE();
<> 154:37f96f9d4de2 149 __HAL_RCC_BACKUPRESET_RELEASE();
<> 154:37f96f9d4de2 150
<> 154:37f96f9d4de2 151 // Disable access to Backup domain
<> 154:37f96f9d4de2 152 HAL_PWR_DisableBkUpAccess();
<> 154:37f96f9d4de2 153 #endif
<> 154:37f96f9d4de2 154
<> 154:37f96f9d4de2 155 // Disable LSI and LSE clocks
AnnaBridge 176:447f873cad2f 156 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
<> 154:37f96f9d4de2 157 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
<> 154:37f96f9d4de2 158 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
<> 154:37f96f9d4de2 159 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
<> 154:37f96f9d4de2 160 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
<> 154:37f96f9d4de2 161 HAL_RCC_OscConfig(&RCC_OscInitStruct);
<> 154:37f96f9d4de2 162 }
<> 154:37f96f9d4de2 163
<> 154:37f96f9d4de2 164 /*
<> 160:d5399cc887bb 165 ST RTC_DateTypeDef structure
<> 160:d5399cc887bb 166 WeekDay 1=monday, 2=tuesday, ..., 7=sunday
<> 160:d5399cc887bb 167 Month 0x1=january, 0x2=february, ..., 0x12=december
<> 160:d5399cc887bb 168 Date day of the month 1-31
<> 160:d5399cc887bb 169 Year year 0-99
<> 160:d5399cc887bb 170
<> 160:d5399cc887bb 171 ST RTC_TimeTypeDef structure
<> 160:d5399cc887bb 172 Hours 0-12 if the RTC_HourFormat_12 is selected during init
<> 160:d5399cc887bb 173 0-23 if the RTC_HourFormat_24 is selected during init
<> 160:d5399cc887bb 174 Minutes 0-59
<> 160:d5399cc887bb 175 Seconds 0-59
<> 160:d5399cc887bb 176 TimeFormat RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM
<> 160:d5399cc887bb 177 SubSeconds time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity
<> 160:d5399cc887bb 178 SecondFraction range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S)
<> 160:d5399cc887bb 179 DayLightSaving RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE
<> 160:d5399cc887bb 180 StoreOperation RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET
<> 160:d5399cc887bb 181
<> 154:37f96f9d4de2 182 struct tm
<> 154:37f96f9d4de2 183 tm_sec seconds after the minute 0-61
<> 154:37f96f9d4de2 184 tm_min minutes after the hour 0-59
<> 154:37f96f9d4de2 185 tm_hour hours since midnight 0-23
<> 154:37f96f9d4de2 186 tm_mday day of the month 1-31
<> 154:37f96f9d4de2 187 tm_mon months since January 0-11
<> 154:37f96f9d4de2 188 tm_year years since 1900
<> 154:37f96f9d4de2 189 tm_wday days since Sunday 0-6
<> 154:37f96f9d4de2 190 tm_yday days since January 1 0-365
<> 154:37f96f9d4de2 191 tm_isdst Daylight Saving Time flag
<> 154:37f96f9d4de2 192 */
<> 160:d5399cc887bb 193
<> 160:d5399cc887bb 194 /*
<> 160:d5399cc887bb 195 Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4:
<> 160:d5399cc887bb 196 BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits.
<> 160:d5399cc887bb 197 Because the first year is reserved to see if the RTC is init, the supposed range is 01-99.
<> 160:d5399cc887bb 198 1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t).
<> 160:d5399cc887bb 199 2nd point is to keep the year 1970 and the leap years synchronized.
<> 160:d5399cc887bb 200
<> 160:d5399cc887bb 201 So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038.
<> 160:d5399cc887bb 202 68 is also a multiple of 4 so it let the leap year synchronized.
<> 160:d5399cc887bb 203
<> 160:d5399cc887bb 204 Information about STM32F1:
<> 160:d5399cc887bb 205 32bit register is used (no BCD format) for the seconds and a software structure to store dates.
<> 160:d5399cc887bb 206 It is then not a problem to not use shifts.
<> 160:d5399cc887bb 207 */
<> 160:d5399cc887bb 208
<> 154:37f96f9d4de2 209 time_t rtc_read(void)
<> 154:37f96f9d4de2 210 {
AnnaBridge 176:447f873cad2f 211 RTC_DateTypeDef dateStruct = {0};
AnnaBridge 176:447f873cad2f 212 RTC_TimeTypeDef timeStruct = {0};
<> 154:37f96f9d4de2 213 struct tm timeinfo;
<> 154:37f96f9d4de2 214
<> 154:37f96f9d4de2 215 RtcHandle.Instance = RTC;
<> 154:37f96f9d4de2 216
<> 154:37f96f9d4de2 217 // Read actual date and time
<> 154:37f96f9d4de2 218 // Warning: the time must be read first!
<> 160:d5399cc887bb 219 HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
<> 160:d5399cc887bb 220 HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
<> 154:37f96f9d4de2 221
<> 154:37f96f9d4de2 222 // Setup a tm structure based on the RTC
<> 160:d5399cc887bb 223 /* tm_wday information is ignored by mktime */
<> 154:37f96f9d4de2 224 timeinfo.tm_mon = dateStruct.Month - 1;
<> 154:37f96f9d4de2 225 timeinfo.tm_mday = dateStruct.Date;
<> 154:37f96f9d4de2 226 timeinfo.tm_year = dateStruct.Year + 68;
<> 154:37f96f9d4de2 227 timeinfo.tm_hour = timeStruct.Hours;
<> 154:37f96f9d4de2 228 timeinfo.tm_min = timeStruct.Minutes;
<> 154:37f96f9d4de2 229 timeinfo.tm_sec = timeStruct.Seconds;
<> 154:37f96f9d4de2 230 // Daylight Saving Time information is not available
<> 154:37f96f9d4de2 231 timeinfo.tm_isdst = -1;
<> 154:37f96f9d4de2 232
<> 154:37f96f9d4de2 233 // Convert to timestamp
AnnaBridge 167:e84263d55307 234 time_t t = _rtc_mktime(&timeinfo);
<> 154:37f96f9d4de2 235
<> 154:37f96f9d4de2 236 return t;
<> 154:37f96f9d4de2 237 }
<> 154:37f96f9d4de2 238
<> 154:37f96f9d4de2 239 void rtc_write(time_t t)
<> 154:37f96f9d4de2 240 {
AnnaBridge 176:447f873cad2f 241 RTC_DateTypeDef dateStruct = {0};
AnnaBridge 176:447f873cad2f 242 RTC_TimeTypeDef timeStruct = {0};
<> 154:37f96f9d4de2 243
<> 154:37f96f9d4de2 244 RtcHandle.Instance = RTC;
<> 154:37f96f9d4de2 245
<> 154:37f96f9d4de2 246 // Convert the time into a tm
AnnaBridge 167:e84263d55307 247 struct tm timeinfo;
AnnaBridge 167:e84263d55307 248 if (_rtc_localtime(t, &timeinfo) == false) {
AnnaBridge 167:e84263d55307 249 return;
AnnaBridge 167:e84263d55307 250 }
<> 154:37f96f9d4de2 251
<> 154:37f96f9d4de2 252 // Fill RTC structures
AnnaBridge 167:e84263d55307 253 if (timeinfo.tm_wday == 0) {
<> 160:d5399cc887bb 254 dateStruct.WeekDay = 7;
<> 160:d5399cc887bb 255 } else {
AnnaBridge 167:e84263d55307 256 dateStruct.WeekDay = timeinfo.tm_wday;
<> 160:d5399cc887bb 257 }
AnnaBridge 167:e84263d55307 258 dateStruct.Month = timeinfo.tm_mon + 1;
AnnaBridge 167:e84263d55307 259 dateStruct.Date = timeinfo.tm_mday;
AnnaBridge 167:e84263d55307 260 dateStruct.Year = timeinfo.tm_year - 68;
AnnaBridge 167:e84263d55307 261 timeStruct.Hours = timeinfo.tm_hour;
AnnaBridge 167:e84263d55307 262 timeStruct.Minutes = timeinfo.tm_min;
AnnaBridge 167:e84263d55307 263 timeStruct.Seconds = timeinfo.tm_sec;
<> 154:37f96f9d4de2 264
<> 154:37f96f9d4de2 265 #if !(TARGET_STM32F1)
<> 154:37f96f9d4de2 266 timeStruct.TimeFormat = RTC_HOURFORMAT_24;
<> 154:37f96f9d4de2 267 timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
<> 154:37f96f9d4de2 268 timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
<> 154:37f96f9d4de2 269 #endif /* TARGET_STM32F1 */
<> 154:37f96f9d4de2 270
<> 154:37f96f9d4de2 271 // Change the RTC current date/time
Anna Bridge 180:96ed750bd169 272 if (HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN) != HAL_OK) {
Anna Bridge 180:96ed750bd169 273 error("HAL_RTC_SetDate error\n");
Anna Bridge 180:96ed750bd169 274 }
Anna Bridge 180:96ed750bd169 275 if (HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN) != HAL_OK) {
Anna Bridge 180:96ed750bd169 276 error("HAL_RTC_SetTime error\n");
Anna Bridge 180:96ed750bd169 277 }
<> 154:37f96f9d4de2 278 }
<> 154:37f96f9d4de2 279
<> 154:37f96f9d4de2 280 int rtc_isenabled(void)
<> 154:37f96f9d4de2 281 {
<> 160:d5399cc887bb 282 #if !(TARGET_STM32F1)
<> 160:d5399cc887bb 283 return ( ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) && ((RTC->ISR & RTC_ISR_RSF) == RTC_ISR_RSF) );
<> 160:d5399cc887bb 284 #else /* TARGET_STM32F1 */
<> 160:d5399cc887bb 285 return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF);
<> 160:d5399cc887bb 286 #endif /* TARGET_STM32F1 */
<> 154:37f96f9d4de2 287 }
<> 154:37f96f9d4de2 288
Anna Bridge 180:96ed750bd169 289 void rtc_synchronize(void)
Anna Bridge 180:96ed750bd169 290 {
Anna Bridge 180:96ed750bd169 291 RtcHandle.Instance = RTC;
Anna Bridge 180:96ed750bd169 292 if (HAL_RTC_WaitForSynchro(&RtcHandle) != HAL_OK) {
Anna Bridge 180:96ed750bd169 293 error("rtc_synchronize error\n");
Anna Bridge 180:96ed750bd169 294 }
Anna Bridge 180:96ed750bd169 295 }
Anna Bridge 180:96ed750bd169 296
<> 154:37f96f9d4de2 297 #if DEVICE_LOWPOWERTIMER
<> 154:37f96f9d4de2 298
<> 154:37f96f9d4de2 299 static void RTC_IRQHandler(void)
<> 154:37f96f9d4de2 300 {
AnnaBridge 176:447f873cad2f 301 /* Update HAL state */
Anna Bridge 180:96ed750bd169 302 RtcHandle.Instance = RTC;
<> 154:37f96f9d4de2 303 HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
AnnaBridge 176:447f873cad2f 304 /* In case of registered handler, call it. */
<> 154:37f96f9d4de2 305 if (irq_handler) {
<> 154:37f96f9d4de2 306 irq_handler();
<> 154:37f96f9d4de2 307 }
<> 154:37f96f9d4de2 308 }
<> 154:37f96f9d4de2 309
<> 154:37f96f9d4de2 310 uint32_t rtc_read_subseconds(void)
<> 154:37f96f9d4de2 311 {
Anna Bridge 180:96ed750bd169 312 return 1000000.f * ((double)((RTC->PRER & RTC_PRER_PREDIV_S) - RTC->SSR) / ((RTC->PRER & RTC_PRER_PREDIV_S) + 1));
<> 154:37f96f9d4de2 313 }
<> 154:37f96f9d4de2 314
<> 154:37f96f9d4de2 315 void rtc_set_wake_up_timer(uint32_t delta)
<> 154:37f96f9d4de2 316 {
Anna Bridge 180:96ed750bd169 317 /* Ex for Wakeup period resolution with RTCCLK=32768 Hz :
Anna Bridge 180:96ed750bd169 318 * RTCCLK_DIV2: ~122us < wakeup period < ~4s
Anna Bridge 180:96ed750bd169 319 * RTCCLK_DIV4: ~244us < wakeup period < ~8s
Anna Bridge 180:96ed750bd169 320 * RTCCLK_DIV8: ~488us < wakeup period < ~16s
Anna Bridge 180:96ed750bd169 321 * RTCCLK_DIV16: ~976us < wakeup period < ~32s
Anna Bridge 180:96ed750bd169 322 * CK_SPRE_16BITS: 1s < wakeup period < (0xFFFF+ 1) x 1 s = 65536 s (18 hours)
Anna Bridge 180:96ed750bd169 323 * CK_SPRE_17BITS: 18h+1s < wakeup period < (0x1FFFF+ 1) x 1 s = 131072 s (36 hours)
Anna Bridge 180:96ed750bd169 324 */
Anna Bridge 180:96ed750bd169 325 uint32_t WakeUpClock[6] = {RTC_WAKEUPCLOCK_RTCCLK_DIV2, RTC_WAKEUPCLOCK_RTCCLK_DIV4, RTC_WAKEUPCLOCK_RTCCLK_DIV8, RTC_WAKEUPCLOCK_RTCCLK_DIV16, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, RTC_WAKEUPCLOCK_CK_SPRE_17BITS};
Anna Bridge 180:96ed750bd169 326 uint8_t ClockDiv[4] = {2, 4, 8, 16};
Anna Bridge 180:96ed750bd169 327 uint32_t WakeUpCounter;
Anna Bridge 180:96ed750bd169 328 uint8_t DivIndex = 0;
<> 154:37f96f9d4de2 329
Anna Bridge 180:96ed750bd169 330 do {
Anna Bridge 180:96ed750bd169 331 WakeUpCounter = delta / (ClockDiv[DivIndex] * 1000000 / RTC_CLOCK);
Anna Bridge 180:96ed750bd169 332 DivIndex++;
Anna Bridge 180:96ed750bd169 333 } while ( (WakeUpCounter > 0xFFFF) && (DivIndex < 4) );
Anna Bridge 180:96ed750bd169 334
Anna Bridge 180:96ed750bd169 335 if (WakeUpCounter > 0xFFFF) {
Anna Bridge 180:96ed750bd169 336 WakeUpCounter = delta / 1000000;
Anna Bridge 180:96ed750bd169 337 DivIndex++;
Anna Bridge 180:96ed750bd169 338 }
Anna Bridge 180:96ed750bd169 339
Anna Bridge 180:96ed750bd169 340 irq_handler = (void (*)(void))lp_ticker_irq_handler;
Anna Bridge 180:96ed750bd169 341 NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
Anna Bridge 180:96ed750bd169 342 NVIC_EnableIRQ(RTC_WKUP_IRQn);
Anna Bridge 180:96ed750bd169 343
Anna Bridge 180:96ed750bd169 344 RtcHandle.Instance = RTC;
Anna Bridge 180:96ed750bd169 345 if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0xFFFF & WakeUpCounter, WakeUpClock[DivIndex-1]) != HAL_OK) {
Anna Bridge 180:96ed750bd169 346 error("rtc_set_wake_up_timer init error (%d)\n", DivIndex);
<> 154:37f96f9d4de2 347 }
<> 154:37f96f9d4de2 348 }
<> 154:37f96f9d4de2 349
<> 154:37f96f9d4de2 350 void rtc_deactivate_wake_up_timer(void)
<> 154:37f96f9d4de2 351 {
Anna Bridge 180:96ed750bd169 352 RtcHandle.Instance = RTC;
<> 154:37f96f9d4de2 353 HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
<> 154:37f96f9d4de2 354 }
<> 154:37f96f9d4de2 355
<> 154:37f96f9d4de2 356 #endif /* DEVICE_LOWPOWERTIMER */
<> 154:37f96f9d4de2 357
<> 154:37f96f9d4de2 358 #endif /* DEVICE_RTC */