001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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