The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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