inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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