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