Modified for BG96

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Apr 19 17:12:19 2018 +0100
Revision:
184:08ed48f1de7f
Parent:
182:a56a73fd2a6f
Child:
186:707f6e361f3e
mbed-dev library. Release version 161

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