Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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