Solution how to fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..

Dependencies:   mbed-dev

Fork of Nucleo_RTC_battery_bkup_pwr_off_okay by Kenji Arai

Experimental fork https://os.mbed.com/users/kenjiArai/code/Nucleo_RTC_battery_bkup_pwr_off_okay/ to fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..

At this moment (7/11/17) use forked mbed-dev https://os.mbed.com/users/maxxir/code/mbed-dev/.

Or require patch for ./mbed-dev/targets/TARGET_STM/rtc_api.c.

You can manual add to your project fresh mbed-dev and change file mbed-dev\targets\TARGET_STM\rtc_api.c from root project patched rtc_api.c.stm32f10x.txt.

Exploring origin errors, I saw that something wrong with HAL API on STM32F1xx with this functions:

 HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);

 HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);


 HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);

 HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);

Look here (as I understand it possible broken on STM32CUBE HAL level now):

https://community.st.com/thread/43218-stm32f103-loss-rtc-date-when-reset

So I use direct RTC register manipulation for STM32F1xx:

rtc_read(), rtc_write() (native rtc_init() - works good).

Also added stub for non-working on STM32F1xx rtc_read_subseconds().

Now the stm32F103 can survive power off, and allows you to get and set the time.

Tested OK on boards:

NUCLEO STM32F103RB, DIY STM32F100CB (forked from DISCO_F100RB)

Happy coding!

maxxir

10/11/17

Committer:
maxxir
Date:
Fri Nov 10 05:42:31 2017 +0000
Revision:
12:dfd6bc5c6f2f
Addded patched and renamed rtc_api.c to root of prj

Who changed what in which revision?

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