test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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