Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 *******************************************************************************
lypinator 0:bb348c97df44 3 * Copyright (c) 2018, STMicroelectronics
lypinator 0:bb348c97df44 4 * All rights reserved.
lypinator 0:bb348c97df44 5 *
lypinator 0:bb348c97df44 6 * Redistribution and use in source and binary forms, with or without
lypinator 0:bb348c97df44 7 * modification, are permitted provided that the following conditions are met:
lypinator 0:bb348c97df44 8 *
lypinator 0:bb348c97df44 9 * 1. Redistributions of source code must retain the above copyright notice,
lypinator 0:bb348c97df44 10 * this list of conditions and the following disclaimer.
lypinator 0:bb348c97df44 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
lypinator 0:bb348c97df44 12 * this list of conditions and the following disclaimer in the documentation
lypinator 0:bb348c97df44 13 * and/or other materials provided with the distribution.
lypinator 0:bb348c97df44 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
lypinator 0:bb348c97df44 15 * may be used to endorse or promote products derived from this software
lypinator 0:bb348c97df44 16 * without specific prior written permission.
lypinator 0:bb348c97df44 17 *
lypinator 0:bb348c97df44 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
lypinator 0:bb348c97df44 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
lypinator 0:bb348c97df44 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
lypinator 0:bb348c97df44 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
lypinator 0:bb348c97df44 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
lypinator 0:bb348c97df44 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
lypinator 0:bb348c97df44 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
lypinator 0:bb348c97df44 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
lypinator 0:bb348c97df44 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
lypinator 0:bb348c97df44 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lypinator 0:bb348c97df44 28 *******************************************************************************
lypinator 0:bb348c97df44 29 */
lypinator 0:bb348c97df44 30
lypinator 0:bb348c97df44 31 #if DEVICE_RTC
lypinator 0:bb348c97df44 32
lypinator 0:bb348c97df44 33 #include "rtc_api_hal.h"
lypinator 0:bb348c97df44 34 #include "mbed_mktime.h"
lypinator 0:bb348c97df44 35 #include "mbed_error.h"
lypinator 0:bb348c97df44 36 #include "mbed_critical.h"
lypinator 0:bb348c97df44 37
lypinator 0:bb348c97df44 38 #if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
lypinator 0:bb348c97df44 39 volatile uint32_t LP_continuous_time = 0;
lypinator 0:bb348c97df44 40 volatile uint32_t LP_last_RTC_time = 0;
lypinator 0:bb348c97df44 41 #endif
lypinator 0:bb348c97df44 42
lypinator 0:bb348c97df44 43 static int RTC_inited = 0;
lypinator 0:bb348c97df44 44
lypinator 0:bb348c97df44 45 static RTC_HandleTypeDef RtcHandle;
lypinator 0:bb348c97df44 46
lypinator 0:bb348c97df44 47 void rtc_init(void)
lypinator 0:bb348c97df44 48 {
lypinator 0:bb348c97df44 49 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
lypinator 0:bb348c97df44 50 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
lypinator 0:bb348c97df44 51
lypinator 0:bb348c97df44 52 if (RTC_inited) {
lypinator 0:bb348c97df44 53 return;
lypinator 0:bb348c97df44 54 }
lypinator 0:bb348c97df44 55 RTC_inited = 1;
lypinator 0:bb348c97df44 56
lypinator 0:bb348c97df44 57 // Enable access to Backup domain
lypinator 0:bb348c97df44 58 __HAL_RCC_PWR_CLK_ENABLE();
lypinator 0:bb348c97df44 59 HAL_PWR_EnableBkUpAccess();
lypinator 0:bb348c97df44 60
lypinator 0:bb348c97df44 61 #if MBED_CONF_TARGET_LSE_AVAILABLE
lypinator 0:bb348c97df44 62 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
lypinator 0:bb348c97df44 63 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
lypinator 0:bb348c97df44 64 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
lypinator 0:bb348c97df44 65
lypinator 0:bb348c97df44 66 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
lypinator 0:bb348c97df44 67 error("Cannot initialize RTC with LSE\n");
lypinator 0:bb348c97df44 68 }
lypinator 0:bb348c97df44 69
lypinator 0:bb348c97df44 70 __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
lypinator 0:bb348c97df44 71 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
lypinator 0:bb348c97df44 72
lypinator 0:bb348c97df44 73 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
lypinator 0:bb348c97df44 74 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
lypinator 0:bb348c97df44 75 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
lypinator 0:bb348c97df44 76 error("PeriphClkInitStruct RTC failed with LSE\n");
lypinator 0:bb348c97df44 77 }
lypinator 0:bb348c97df44 78 #else /* MBED_CONF_TARGET_LSE_AVAILABLE */
lypinator 0:bb348c97df44 79 // Reset Backup domain
lypinator 0:bb348c97df44 80 __HAL_RCC_BACKUPRESET_FORCE();
lypinator 0:bb348c97df44 81 __HAL_RCC_BACKUPRESET_RELEASE();
lypinator 0:bb348c97df44 82
lypinator 0:bb348c97df44 83 // Enable LSI clock
lypinator 0:bb348c97df44 84 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
lypinator 0:bb348c97df44 85 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
lypinator 0:bb348c97df44 86 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
lypinator 0:bb348c97df44 87 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
lypinator 0:bb348c97df44 88 error("Cannot initialize RTC with LSI\n");
lypinator 0:bb348c97df44 89 }
lypinator 0:bb348c97df44 90
lypinator 0:bb348c97df44 91 __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
lypinator 0:bb348c97df44 92 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
lypinator 0:bb348c97df44 93
lypinator 0:bb348c97df44 94 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
lypinator 0:bb348c97df44 95 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
lypinator 0:bb348c97df44 96 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
lypinator 0:bb348c97df44 97 error("PeriphClkInitStruct RTC failed with LSI\n");
lypinator 0:bb348c97df44 98 }
lypinator 0:bb348c97df44 99 #endif /* MBED_CONF_TARGET_LSE_AVAILABLE */
lypinator 0:bb348c97df44 100
lypinator 0:bb348c97df44 101 // Enable RTC
lypinator 0:bb348c97df44 102 __HAL_RCC_RTC_ENABLE();
lypinator 0:bb348c97df44 103
lypinator 0:bb348c97df44 104 RtcHandle.Instance = RTC;
lypinator 0:bb348c97df44 105 RtcHandle.State = HAL_RTC_STATE_RESET;
lypinator 0:bb348c97df44 106
lypinator 0:bb348c97df44 107 #if TARGET_STM32F1
lypinator 0:bb348c97df44 108 RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
lypinator 0:bb348c97df44 109 #else /* TARGET_STM32F1 */
lypinator 0:bb348c97df44 110 RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
lypinator 0:bb348c97df44 111 RtcHandle.Init.AsynchPrediv = PREDIV_A_VALUE;
lypinator 0:bb348c97df44 112 RtcHandle.Init.SynchPrediv = PREDIV_S_VALUE;
lypinator 0:bb348c97df44 113 RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
lypinator 0:bb348c97df44 114 RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
lypinator 0:bb348c97df44 115 RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
lypinator 0:bb348c97df44 116 #endif /* TARGET_STM32F1 */
lypinator 0:bb348c97df44 117
lypinator 0:bb348c97df44 118 if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
lypinator 0:bb348c97df44 119 error("RTC initialization failed");
lypinator 0:bb348c97df44 120 }
lypinator 0:bb348c97df44 121
lypinator 0:bb348c97df44 122 #if !(TARGET_STM32F1) && !(TARGET_STM32F2)
lypinator 0:bb348c97df44 123 /* STM32F1 : there are no shadow registers */
lypinator 0:bb348c97df44 124 /* STM32F2 : shadow registers can not be bypassed */
lypinator 0:bb348c97df44 125 if (HAL_RTCEx_EnableBypassShadow(&RtcHandle) != HAL_OK) {
lypinator 0:bb348c97df44 126 error("EnableBypassShadow error");
lypinator 0:bb348c97df44 127 }
lypinator 0:bb348c97df44 128 #endif /* TARGET_STM32F1 || TARGET_STM32F2 */
lypinator 0:bb348c97df44 129 }
lypinator 0:bb348c97df44 130
lypinator 0:bb348c97df44 131 void rtc_free(void)
lypinator 0:bb348c97df44 132 {
lypinator 0:bb348c97df44 133 /* RTC clock can not be reset */
lypinator 0:bb348c97df44 134 }
lypinator 0:bb348c97df44 135
lypinator 0:bb348c97df44 136
lypinator 0:bb348c97df44 137 /*
lypinator 0:bb348c97df44 138 Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4:
lypinator 0:bb348c97df44 139 BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits.
lypinator 0:bb348c97df44 140 Because the first year is reserved to see if the RTC is init, the supposed range is 01-99.
lypinator 0:bb348c97df44 141 1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t).
lypinator 0:bb348c97df44 142 2nd point is to keep the year 1970 and the leap years synchronized.
lypinator 0:bb348c97df44 143
lypinator 0:bb348c97df44 144 So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038.
lypinator 0:bb348c97df44 145 68 is also a multiple of 4 so it let the leap year synchronized.
lypinator 0:bb348c97df44 146
lypinator 0:bb348c97df44 147 Information about STM32F1:
lypinator 0:bb348c97df44 148 32bit register is used (no BCD format) for the seconds.
lypinator 0:bb348c97df44 149 For date, there is no specific register, only a software structure.
lypinator 0:bb348c97df44 150 It is then not a problem to not use shifts.
lypinator 0:bb348c97df44 151 */
lypinator 0:bb348c97df44 152 #if TARGET_STM32F1
lypinator 0:bb348c97df44 153 time_t rtc_read(void)
lypinator 0:bb348c97df44 154 {
lypinator 0:bb348c97df44 155 RTC_DateTypeDef dateStruct = {0};
lypinator 0:bb348c97df44 156 RTC_TimeTypeDef timeStruct = {0};
lypinator 0:bb348c97df44 157 struct tm timeinfo;
lypinator 0:bb348c97df44 158
lypinator 0:bb348c97df44 159 RtcHandle.Instance = RTC;
lypinator 0:bb348c97df44 160
lypinator 0:bb348c97df44 161 // Read actual date and time
lypinator 0:bb348c97df44 162 // Warning: the time must be read first!
lypinator 0:bb348c97df44 163 HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
lypinator 0:bb348c97df44 164 HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
lypinator 0:bb348c97df44 165
lypinator 0:bb348c97df44 166 /* date information is null before first write procedure */
lypinator 0:bb348c97df44 167 /* set 01/01/1970 as default values */
lypinator 0:bb348c97df44 168 if (dateStruct.Year == 0) {
lypinator 0:bb348c97df44 169 dateStruct.Year = 2 ;
lypinator 0:bb348c97df44 170 dateStruct.Month = 1 ;
lypinator 0:bb348c97df44 171 dateStruct.Date = 1 ;
lypinator 0:bb348c97df44 172 }
lypinator 0:bb348c97df44 173
lypinator 0:bb348c97df44 174 // Setup a tm structure based on the RTC
lypinator 0:bb348c97df44 175 /* tm_wday information is ignored by _rtc_maketime */
lypinator 0:bb348c97df44 176 /* tm_isdst information is ignored by _rtc_maketime */
lypinator 0:bb348c97df44 177 timeinfo.tm_mon = dateStruct.Month - 1;
lypinator 0:bb348c97df44 178 timeinfo.tm_mday = dateStruct.Date;
lypinator 0:bb348c97df44 179 timeinfo.tm_year = dateStruct.Year + 68;
lypinator 0:bb348c97df44 180 timeinfo.tm_hour = timeStruct.Hours;
lypinator 0:bb348c97df44 181 timeinfo.tm_min = timeStruct.Minutes;
lypinator 0:bb348c97df44 182 timeinfo.tm_sec = timeStruct.Seconds;
lypinator 0:bb348c97df44 183
lypinator 0:bb348c97df44 184 // Convert to timestamp
lypinator 0:bb348c97df44 185 time_t t;
lypinator 0:bb348c97df44 186 if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
lypinator 0:bb348c97df44 187 return 0;
lypinator 0:bb348c97df44 188 }
lypinator 0:bb348c97df44 189
lypinator 0:bb348c97df44 190 return t;
lypinator 0:bb348c97df44 191 }
lypinator 0:bb348c97df44 192
lypinator 0:bb348c97df44 193 #else /* TARGET_STM32F1 */
lypinator 0:bb348c97df44 194
lypinator 0:bb348c97df44 195 time_t rtc_read(void)
lypinator 0:bb348c97df44 196 {
lypinator 0:bb348c97df44 197 struct tm timeinfo;
lypinator 0:bb348c97df44 198
lypinator 0:bb348c97df44 199 /* Since the shadow registers are bypassed we have to read the time twice and compare them until both times are the same */
lypinator 0:bb348c97df44 200 uint32_t Read_time = RTC->TR & RTC_TR_RESERVED_MASK;
lypinator 0:bb348c97df44 201 uint32_t Read_date = RTC->DR & RTC_DR_RESERVED_MASK;
lypinator 0:bb348c97df44 202
lypinator 0:bb348c97df44 203 while ((Read_time != (RTC->TR & RTC_TR_RESERVED_MASK)) || (Read_date != (RTC->DR & RTC_DR_RESERVED_MASK))) {
lypinator 0:bb348c97df44 204 Read_time = RTC->TR & RTC_TR_RESERVED_MASK;
lypinator 0:bb348c97df44 205 Read_date = RTC->DR & RTC_DR_RESERVED_MASK;
lypinator 0:bb348c97df44 206 }
lypinator 0:bb348c97df44 207
lypinator 0:bb348c97df44 208 /* Setup a tm structure based on the RTC
lypinator 0:bb348c97df44 209 struct tm :
lypinator 0:bb348c97df44 210 tm_sec seconds after the minute 0-61
lypinator 0:bb348c97df44 211 tm_min minutes after the hour 0-59
lypinator 0:bb348c97df44 212 tm_hour hours since midnight 0-23
lypinator 0:bb348c97df44 213 tm_mday day of the month 1-31
lypinator 0:bb348c97df44 214 tm_mon months since January 0-11
lypinator 0:bb348c97df44 215 tm_year years since 1900
lypinator 0:bb348c97df44 216 tm_yday information is ignored by _rtc_maketime
lypinator 0:bb348c97df44 217 tm_wday information is ignored by _rtc_maketime
lypinator 0:bb348c97df44 218 tm_isdst information is ignored by _rtc_maketime
lypinator 0:bb348c97df44 219 */
lypinator 0:bb348c97df44 220 timeinfo.tm_mday = RTC_Bcd2ToByte((uint8_t)(Read_date & (RTC_DR_DT | RTC_DR_DU)));
lypinator 0:bb348c97df44 221 timeinfo.tm_mon = RTC_Bcd2ToByte((uint8_t)((Read_date & (RTC_DR_MT | RTC_DR_MU)) >> 8)) - 1;
lypinator 0:bb348c97df44 222 timeinfo.tm_year = RTC_Bcd2ToByte((uint8_t)((Read_date & (RTC_DR_YT | RTC_DR_YU)) >> 16)) + 68;
lypinator 0:bb348c97df44 223 timeinfo.tm_hour = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_HT | RTC_TR_HU)) >> 16));
lypinator 0:bb348c97df44 224 timeinfo.tm_min = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_MNT | RTC_TR_MNU)) >> 8));
lypinator 0:bb348c97df44 225 timeinfo.tm_sec = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_ST | RTC_TR_SU)) >> 0));
lypinator 0:bb348c97df44 226
lypinator 0:bb348c97df44 227 // Convert to timestamp
lypinator 0:bb348c97df44 228 time_t t;
lypinator 0:bb348c97df44 229 if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
lypinator 0:bb348c97df44 230 return 0;
lypinator 0:bb348c97df44 231 }
lypinator 0:bb348c97df44 232
lypinator 0:bb348c97df44 233 return t;
lypinator 0:bb348c97df44 234 }
lypinator 0:bb348c97df44 235
lypinator 0:bb348c97df44 236 #endif /* TARGET_STM32F1 */
lypinator 0:bb348c97df44 237
lypinator 0:bb348c97df44 238 void rtc_write(time_t t)
lypinator 0:bb348c97df44 239 {
lypinator 0:bb348c97df44 240 RTC_DateTypeDef dateStruct = {0};
lypinator 0:bb348c97df44 241 RTC_TimeTypeDef timeStruct = {0};
lypinator 0:bb348c97df44 242
lypinator 0:bb348c97df44 243 core_util_critical_section_enter();
lypinator 0:bb348c97df44 244 RtcHandle.Instance = RTC;
lypinator 0:bb348c97df44 245
lypinator 0:bb348c97df44 246 // Convert the time into a tm
lypinator 0:bb348c97df44 247 struct tm timeinfo;
lypinator 0:bb348c97df44 248 if (_rtc_localtime(t, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
lypinator 0:bb348c97df44 249 return;
lypinator 0:bb348c97df44 250 }
lypinator 0:bb348c97df44 251
lypinator 0:bb348c97df44 252 // Fill RTC structures
lypinator 0:bb348c97df44 253 if (timeinfo.tm_wday == 0) { /* Sunday specific case */
lypinator 0:bb348c97df44 254 dateStruct.WeekDay = RTC_WEEKDAY_SUNDAY;
lypinator 0:bb348c97df44 255 } else {
lypinator 0:bb348c97df44 256 dateStruct.WeekDay = timeinfo.tm_wday;
lypinator 0:bb348c97df44 257 }
lypinator 0:bb348c97df44 258 dateStruct.Month = timeinfo.tm_mon + 1;
lypinator 0:bb348c97df44 259 dateStruct.Date = timeinfo.tm_mday;
lypinator 0:bb348c97df44 260 dateStruct.Year = timeinfo.tm_year - 68;
lypinator 0:bb348c97df44 261 timeStruct.Hours = timeinfo.tm_hour;
lypinator 0:bb348c97df44 262 timeStruct.Minutes = timeinfo.tm_min;
lypinator 0:bb348c97df44 263 timeStruct.Seconds = timeinfo.tm_sec;
lypinator 0:bb348c97df44 264
lypinator 0:bb348c97df44 265 #if !(TARGET_STM32F1)
lypinator 0:bb348c97df44 266 timeStruct.TimeFormat = RTC_HOURFORMAT_24;
lypinator 0:bb348c97df44 267 timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
lypinator 0:bb348c97df44 268 timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
lypinator 0:bb348c97df44 269 #endif /* TARGET_STM32F1 */
lypinator 0:bb348c97df44 270
lypinator 0:bb348c97df44 271 #if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
lypinator 0:bb348c97df44 272 /* Need to update LP_continuous_time value before new RTC time */
lypinator 0:bb348c97df44 273 rtc_read_lp();
lypinator 0:bb348c97df44 274
lypinator 0:bb348c97df44 275 /* LP_last_RTC_time value is updated with the new RTC time */
lypinator 0:bb348c97df44 276 LP_last_RTC_time = timeStruct.Seconds + timeStruct.Minutes * 60 + timeStruct.Hours * 60 * 60;
lypinator 0:bb348c97df44 277
lypinator 0:bb348c97df44 278 /* Save current SSR */
lypinator 0:bb348c97df44 279 uint32_t Read_SubSeconds = (uint32_t)(RTC->SSR);
lypinator 0:bb348c97df44 280 #endif /* DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM */
lypinator 0:bb348c97df44 281
lypinator 0:bb348c97df44 282 // Change the RTC current date/time
lypinator 0:bb348c97df44 283 if (HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN) != HAL_OK) {
lypinator 0:bb348c97df44 284 error("HAL_RTC_SetDate error\n");
lypinator 0:bb348c97df44 285 }
lypinator 0:bb348c97df44 286 if (HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN) != HAL_OK) {
lypinator 0:bb348c97df44 287 error("HAL_RTC_SetTime error\n");
lypinator 0:bb348c97df44 288 }
lypinator 0:bb348c97df44 289
lypinator 0:bb348c97df44 290 #if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
lypinator 0:bb348c97df44 291 while (Read_SubSeconds != (RTC->SSR)) {
lypinator 0:bb348c97df44 292 }
lypinator 0:bb348c97df44 293 #endif /* DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM */
lypinator 0:bb348c97df44 294
lypinator 0:bb348c97df44 295 core_util_critical_section_exit();
lypinator 0:bb348c97df44 296 }
lypinator 0:bb348c97df44 297
lypinator 0:bb348c97df44 298 int rtc_isenabled(void)
lypinator 0:bb348c97df44 299 {
lypinator 0:bb348c97df44 300 #if !(TARGET_STM32F1)
lypinator 0:bb348c97df44 301 return ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS);
lypinator 0:bb348c97df44 302 #else /* TARGET_STM32F1 */
lypinator 0:bb348c97df44 303 return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF);
lypinator 0:bb348c97df44 304 #endif /* TARGET_STM32F1 */
lypinator 0:bb348c97df44 305 }
lypinator 0:bb348c97df44 306
lypinator 0:bb348c97df44 307
lypinator 0:bb348c97df44 308 #if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
lypinator 0:bb348c97df44 309
lypinator 0:bb348c97df44 310 static void RTC_IRQHandler(void);
lypinator 0:bb348c97df44 311 static void (*irq_handler)(void);
lypinator 0:bb348c97df44 312
lypinator 0:bb348c97df44 313 volatile uint8_t lp_Fired = 0;
lypinator 0:bb348c97df44 314
lypinator 0:bb348c97df44 315 static void RTC_IRQHandler(void)
lypinator 0:bb348c97df44 316 {
lypinator 0:bb348c97df44 317 /* Update HAL state */
lypinator 0:bb348c97df44 318 RtcHandle.Instance = RTC;
lypinator 0:bb348c97df44 319 if (__HAL_RTC_WAKEUPTIMER_GET_IT(&RtcHandle, RTC_IT_WUT)) {
lypinator 0:bb348c97df44 320 /* Get the status of the Interrupt */
lypinator 0:bb348c97df44 321 if ((uint32_t)(RTC->CR & RTC_IT_WUT) != (uint32_t)RESET) {
lypinator 0:bb348c97df44 322 /* Clear the WAKEUPTIMER interrupt pending bit */
lypinator 0:bb348c97df44 323 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RtcHandle, RTC_FLAG_WUTF);
lypinator 0:bb348c97df44 324
lypinator 0:bb348c97df44 325 lp_Fired = 0;
lypinator 0:bb348c97df44 326 if (irq_handler) {
lypinator 0:bb348c97df44 327 irq_handler();
lypinator 0:bb348c97df44 328 }
lypinator 0:bb348c97df44 329 }
lypinator 0:bb348c97df44 330 }
lypinator 0:bb348c97df44 331
lypinator 0:bb348c97df44 332 if (lp_Fired) {
lypinator 0:bb348c97df44 333 lp_Fired = 0;
lypinator 0:bb348c97df44 334 if (irq_handler) {
lypinator 0:bb348c97df44 335 irq_handler();
lypinator 0:bb348c97df44 336 }
lypinator 0:bb348c97df44 337 }
lypinator 0:bb348c97df44 338
lypinator 0:bb348c97df44 339 __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
lypinator 0:bb348c97df44 340 }
lypinator 0:bb348c97df44 341
lypinator 0:bb348c97df44 342 uint32_t rtc_read_lp(void)
lypinator 0:bb348c97df44 343 {
lypinator 0:bb348c97df44 344 struct tm timeinfo;
lypinator 0:bb348c97df44 345
lypinator 0:bb348c97df44 346 /* Since the shadow registers are bypassed we have to read the time twice and compare them until both times are the same */
lypinator 0:bb348c97df44 347 /* We don't have to read date as we bypass shadow registers */
lypinator 0:bb348c97df44 348 uint32_t Read_SecondFraction = (uint32_t)(RTC->PRER & RTC_PRER_PREDIV_S);
lypinator 0:bb348c97df44 349 uint32_t Read_time = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
lypinator 0:bb348c97df44 350 uint32_t Read_SubSeconds = (uint32_t)(RTC->SSR);
lypinator 0:bb348c97df44 351
lypinator 0:bb348c97df44 352 while ((Read_time != (RTC->TR & RTC_TR_RESERVED_MASK)) || (Read_SubSeconds != (RTC->SSR))) {
lypinator 0:bb348c97df44 353 Read_time = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
lypinator 0:bb348c97df44 354 Read_SubSeconds = (uint32_t)(RTC->SSR);
lypinator 0:bb348c97df44 355 }
lypinator 0:bb348c97df44 356
lypinator 0:bb348c97df44 357 timeinfo.tm_hour = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_HT | RTC_TR_HU)) >> 16));
lypinator 0:bb348c97df44 358 timeinfo.tm_min = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_MNT | RTC_TR_MNU)) >> 8));
lypinator 0:bb348c97df44 359 timeinfo.tm_sec = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_ST | RTC_TR_SU)) >> 0));
lypinator 0:bb348c97df44 360
lypinator 0:bb348c97df44 361 uint32_t RTC_time_s = timeinfo.tm_sec + timeinfo.tm_min * 60 + timeinfo.tm_hour * 60 * 60; // Max 0x0001-517F => * 8191 + 8191 = 0x2A2E-AE80
lypinator 0:bb348c97df44 362
lypinator 0:bb348c97df44 363 if (LP_last_RTC_time <= RTC_time_s) {
lypinator 0:bb348c97df44 364 LP_continuous_time += (RTC_time_s - LP_last_RTC_time);
lypinator 0:bb348c97df44 365 } else {
lypinator 0:bb348c97df44 366 /* Add 24h */
lypinator 0:bb348c97df44 367 LP_continuous_time += (24 * 60 * 60 + RTC_time_s - LP_last_RTC_time);
lypinator 0:bb348c97df44 368 }
lypinator 0:bb348c97df44 369 LP_last_RTC_time = RTC_time_s;
lypinator 0:bb348c97df44 370
lypinator 0:bb348c97df44 371 return LP_continuous_time * PREDIV_S_VALUE + Read_SecondFraction - Read_SubSeconds;
lypinator 0:bb348c97df44 372 }
lypinator 0:bb348c97df44 373
lypinator 0:bb348c97df44 374 void rtc_set_wake_up_timer(timestamp_t timestamp)
lypinator 0:bb348c97df44 375 {
lypinator 0:bb348c97df44 376 uint32_t WakeUpCounter;
lypinator 0:bb348c97df44 377 uint32_t current_lp_time;
lypinator 0:bb348c97df44 378
lypinator 0:bb348c97df44 379 current_lp_time = rtc_read_lp();
lypinator 0:bb348c97df44 380
lypinator 0:bb348c97df44 381 if (timestamp < current_lp_time) {
lypinator 0:bb348c97df44 382 WakeUpCounter = 0xFFFFFFFF - current_lp_time + timestamp;
lypinator 0:bb348c97df44 383 } else {
lypinator 0:bb348c97df44 384 WakeUpCounter = timestamp - current_lp_time;
lypinator 0:bb348c97df44 385 }
lypinator 0:bb348c97df44 386
lypinator 0:bb348c97df44 387 if (WakeUpCounter > 0xFFFF) {
lypinator 0:bb348c97df44 388 WakeUpCounter = 0xFFFF;
lypinator 0:bb348c97df44 389 }
lypinator 0:bb348c97df44 390
lypinator 0:bb348c97df44 391 RtcHandle.Instance = RTC;
lypinator 0:bb348c97df44 392 if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, WakeUpCounter, RTC_WAKEUPCLOCK_RTCCLK_DIV4) != HAL_OK) {
lypinator 0:bb348c97df44 393 error("rtc_set_wake_up_timer init error\n");
lypinator 0:bb348c97df44 394 }
lypinator 0:bb348c97df44 395
lypinator 0:bb348c97df44 396 NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
lypinator 0:bb348c97df44 397 irq_handler = (void (*)(void))lp_ticker_irq_handler;
lypinator 0:bb348c97df44 398 NVIC_EnableIRQ(RTC_WKUP_IRQn);
lypinator 0:bb348c97df44 399 }
lypinator 0:bb348c97df44 400
lypinator 0:bb348c97df44 401 void rtc_fire_interrupt(void)
lypinator 0:bb348c97df44 402 {
lypinator 0:bb348c97df44 403 lp_Fired = 1;
lypinator 0:bb348c97df44 404 NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
lypinator 0:bb348c97df44 405 irq_handler = (void (*)(void))lp_ticker_irq_handler;
lypinator 0:bb348c97df44 406 NVIC_SetPendingIRQ(RTC_WKUP_IRQn);
lypinator 0:bb348c97df44 407 NVIC_EnableIRQ(RTC_WKUP_IRQn);
lypinator 0:bb348c97df44 408 }
lypinator 0:bb348c97df44 409
lypinator 0:bb348c97df44 410 void rtc_deactivate_wake_up_timer(void)
lypinator 0:bb348c97df44 411 {
lypinator 0:bb348c97df44 412 RtcHandle.Instance = RTC;
lypinator 0:bb348c97df44 413 __HAL_RTC_WRITEPROTECTION_DISABLE(&RtcHandle);
lypinator 0:bb348c97df44 414 __HAL_RTC_WAKEUPTIMER_DISABLE(&RtcHandle);
lypinator 0:bb348c97df44 415 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&RtcHandle, RTC_IT_WUT);
lypinator 0:bb348c97df44 416 __HAL_RTC_WRITEPROTECTION_ENABLE(&RtcHandle);
lypinator 0:bb348c97df44 417 NVIC_DisableIRQ(RTC_WKUP_IRQn);
lypinator 0:bb348c97df44 418 }
lypinator 0:bb348c97df44 419
lypinator 0:bb348c97df44 420 #endif /* DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM */
lypinator 0:bb348c97df44 421
lypinator 0:bb348c97df44 422 #endif /* DEVICE_RTC */