fixed drive strength
Fork of mbed-dev by
targets/TARGET_STM/rtc_api.c@163:1d4c9d0af1e9, 2017-04-11 (annotated)
- Committer:
- cpadua
- Date:
- Tue Apr 11 20:39:24 2017 +0000
- Revision:
- 163:1d4c9d0af1e9
- Parent:
- 160:d5399cc887bb
fixed i2c-api.c
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 | #else /* !RTC_LSI */ |
<> | 154:37f96f9d4de2 | 86 | __PWR_CLK_ENABLE(); |
<> | 154:37f96f9d4de2 | 87 | |
<> | 154:37f96f9d4de2 | 88 | // Reset Backup domain |
<> | 154:37f96f9d4de2 | 89 | __HAL_RCC_BACKUPRESET_FORCE(); |
<> | 154:37f96f9d4de2 | 90 | __HAL_RCC_BACKUPRESET_RELEASE(); |
<> | 154:37f96f9d4de2 | 91 | |
<> | 154:37f96f9d4de2 | 92 | // Enable LSI clock |
<> | 154:37f96f9d4de2 | 93 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
<> | 154:37f96f9d4de2 | 94 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
<> | 154:37f96f9d4de2 | 95 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
<> | 154:37f96f9d4de2 | 96 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
<> | 154:37f96f9d4de2 | 97 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 98 | error("Cannot initialize RTC with LSI\n"); |
<> | 154:37f96f9d4de2 | 99 | } |
<> | 154:37f96f9d4de2 | 100 | |
<> | 154:37f96f9d4de2 | 101 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); |
<> | 154:37f96f9d4de2 | 102 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
<> | 154:37f96f9d4de2 | 103 | |
<> | 154:37f96f9d4de2 | 104 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
<> | 154:37f96f9d4de2 | 105 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
<> | 154:37f96f9d4de2 | 106 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 107 | error("PeriphClkInitStruct RTC failed with LSI\n"); |
<> | 154:37f96f9d4de2 | 108 | } |
<> | 154:37f96f9d4de2 | 109 | #endif /* !RTC_LSI */ |
<> | 154:37f96f9d4de2 | 110 | |
<> | 154:37f96f9d4de2 | 111 | // Enable RTC |
<> | 154:37f96f9d4de2 | 112 | __HAL_RCC_RTC_ENABLE(); |
<> | 154:37f96f9d4de2 | 113 | |
<> | 154:37f96f9d4de2 | 114 | #if TARGET_STM32F1 |
<> | 154:37f96f9d4de2 | 115 | RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; |
<> | 154:37f96f9d4de2 | 116 | #else /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 117 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
<> | 154:37f96f9d4de2 | 118 | RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; |
<> | 154:37f96f9d4de2 | 119 | RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; |
<> | 154:37f96f9d4de2 | 120 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
<> | 154:37f96f9d4de2 | 121 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
<> | 154:37f96f9d4de2 | 122 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
<> | 154:37f96f9d4de2 | 123 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 124 | |
<> | 154:37f96f9d4de2 | 125 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 126 | error("RTC error: RTC initialization failed."); |
<> | 154:37f96f9d4de2 | 127 | } |
<> | 154:37f96f9d4de2 | 128 | |
<> | 154:37f96f9d4de2 | 129 | #if DEVICE_LOWPOWERTIMER |
<> | 154:37f96f9d4de2 | 130 | |
<> | 154:37f96f9d4de2 | 131 | #if !RTC_LSI |
<> | 154:37f96f9d4de2 | 132 | if (!rtc_isenabled()) |
<> | 154:37f96f9d4de2 | 133 | #endif /* !RTC_LSI */ |
<> | 154:37f96f9d4de2 | 134 | { |
<> | 154:37f96f9d4de2 | 135 | rtc_write(0); |
<> | 154:37f96f9d4de2 | 136 | } |
<> | 154:37f96f9d4de2 | 137 | |
<> | 154:37f96f9d4de2 | 138 | NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 139 | NVIC_DisableIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 140 | NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); |
<> | 154:37f96f9d4de2 | 141 | NVIC_EnableIRQ(RTC_WKUP_IRQn); |
<> | 154:37f96f9d4de2 | 142 | |
<> | 154:37f96f9d4de2 | 143 | #endif /* DEVICE_LOWPOWERTIMER */ |
<> | 154:37f96f9d4de2 | 144 | } |
<> | 154:37f96f9d4de2 | 145 | |
<> | 154:37f96f9d4de2 | 146 | void rtc_free(void) |
<> | 154:37f96f9d4de2 | 147 | { |
<> | 154:37f96f9d4de2 | 148 | #if RTC_LSI |
<> | 154:37f96f9d4de2 | 149 | // Enable Power clock |
<> | 154:37f96f9d4de2 | 150 | __PWR_CLK_ENABLE(); |
<> | 154:37f96f9d4de2 | 151 | |
<> | 154:37f96f9d4de2 | 152 | // Enable access to Backup domain |
<> | 154:37f96f9d4de2 | 153 | HAL_PWR_EnableBkUpAccess(); |
<> | 154:37f96f9d4de2 | 154 | |
<> | 154:37f96f9d4de2 | 155 | // Reset Backup domain |
<> | 154:37f96f9d4de2 | 156 | __HAL_RCC_BACKUPRESET_FORCE(); |
<> | 154:37f96f9d4de2 | 157 | __HAL_RCC_BACKUPRESET_RELEASE(); |
<> | 154:37f96f9d4de2 | 158 | |
<> | 154:37f96f9d4de2 | 159 | // Disable access to Backup domain |
<> | 154:37f96f9d4de2 | 160 | HAL_PWR_DisableBkUpAccess(); |
<> | 154:37f96f9d4de2 | 161 | #endif |
<> | 154:37f96f9d4de2 | 162 | |
<> | 154:37f96f9d4de2 | 163 | // Disable LSI and LSE clocks |
<> | 154:37f96f9d4de2 | 164 | RCC_OscInitTypeDef RCC_OscInitStruct; |
<> | 154:37f96f9d4de2 | 165 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
<> | 154:37f96f9d4de2 | 166 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
<> | 154:37f96f9d4de2 | 167 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
<> | 154:37f96f9d4de2 | 168 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
<> | 154:37f96f9d4de2 | 169 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
<> | 154:37f96f9d4de2 | 170 | } |
<> | 154:37f96f9d4de2 | 171 | |
<> | 154:37f96f9d4de2 | 172 | /* |
<> | 160:d5399cc887bb | 173 | ST RTC_DateTypeDef structure |
<> | 160:d5399cc887bb | 174 | WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
<> | 160:d5399cc887bb | 175 | Month 0x1=january, 0x2=february, ..., 0x12=december |
<> | 160:d5399cc887bb | 176 | Date day of the month 1-31 |
<> | 160:d5399cc887bb | 177 | Year year 0-99 |
<> | 160:d5399cc887bb | 178 | |
<> | 160:d5399cc887bb | 179 | ST RTC_TimeTypeDef structure |
<> | 160:d5399cc887bb | 180 | Hours 0-12 if the RTC_HourFormat_12 is selected during init |
<> | 160:d5399cc887bb | 181 | 0-23 if the RTC_HourFormat_24 is selected during init |
<> | 160:d5399cc887bb | 182 | Minutes 0-59 |
<> | 160:d5399cc887bb | 183 | Seconds 0-59 |
<> | 160:d5399cc887bb | 184 | TimeFormat RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM |
<> | 160:d5399cc887bb | 185 | SubSeconds time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity |
<> | 160:d5399cc887bb | 186 | SecondFraction range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S) |
<> | 160:d5399cc887bb | 187 | DayLightSaving RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE |
<> | 160:d5399cc887bb | 188 | StoreOperation RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET |
<> | 160:d5399cc887bb | 189 | |
<> | 154:37f96f9d4de2 | 190 | struct tm |
<> | 154:37f96f9d4de2 | 191 | tm_sec seconds after the minute 0-61 |
<> | 154:37f96f9d4de2 | 192 | tm_min minutes after the hour 0-59 |
<> | 154:37f96f9d4de2 | 193 | tm_hour hours since midnight 0-23 |
<> | 154:37f96f9d4de2 | 194 | tm_mday day of the month 1-31 |
<> | 154:37f96f9d4de2 | 195 | tm_mon months since January 0-11 |
<> | 154:37f96f9d4de2 | 196 | tm_year years since 1900 |
<> | 154:37f96f9d4de2 | 197 | tm_wday days since Sunday 0-6 |
<> | 154:37f96f9d4de2 | 198 | tm_yday days since January 1 0-365 |
<> | 154:37f96f9d4de2 | 199 | tm_isdst Daylight Saving Time flag |
<> | 154:37f96f9d4de2 | 200 | */ |
<> | 160:d5399cc887bb | 201 | |
<> | 160:d5399cc887bb | 202 | /* |
<> | 160:d5399cc887bb | 203 | Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4: |
<> | 160:d5399cc887bb | 204 | BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits. |
<> | 160:d5399cc887bb | 205 | Because the first year is reserved to see if the RTC is init, the supposed range is 01-99. |
<> | 160:d5399cc887bb | 206 | 1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t). |
<> | 160:d5399cc887bb | 207 | 2nd point is to keep the year 1970 and the leap years synchronized. |
<> | 160:d5399cc887bb | 208 | |
<> | 160:d5399cc887bb | 209 | So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038. |
<> | 160:d5399cc887bb | 210 | 68 is also a multiple of 4 so it let the leap year synchronized. |
<> | 160:d5399cc887bb | 211 | |
<> | 160:d5399cc887bb | 212 | Information about STM32F1: |
<> | 160:d5399cc887bb | 213 | 32bit register is used (no BCD format) for the seconds and a software structure to store dates. |
<> | 160:d5399cc887bb | 214 | It is then not a problem to not use shifts. |
<> | 160:d5399cc887bb | 215 | */ |
<> | 160:d5399cc887bb | 216 | |
<> | 154:37f96f9d4de2 | 217 | time_t rtc_read(void) |
<> | 154:37f96f9d4de2 | 218 | { |
<> | 154:37f96f9d4de2 | 219 | RTC_DateTypeDef dateStruct; |
<> | 154:37f96f9d4de2 | 220 | RTC_TimeTypeDef timeStruct; |
<> | 154:37f96f9d4de2 | 221 | struct tm timeinfo; |
<> | 154:37f96f9d4de2 | 222 | |
<> | 154:37f96f9d4de2 | 223 | RtcHandle.Instance = RTC; |
<> | 154:37f96f9d4de2 | 224 | |
<> | 154:37f96f9d4de2 | 225 | // Read actual date and time |
<> | 154:37f96f9d4de2 | 226 | // Warning: the time must be read first! |
<> | 160:d5399cc887bb | 227 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
<> | 160:d5399cc887bb | 228 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
<> | 154:37f96f9d4de2 | 229 | |
<> | 154:37f96f9d4de2 | 230 | // Setup a tm structure based on the RTC |
<> | 160:d5399cc887bb | 231 | /* tm_wday information is ignored by mktime */ |
<> | 154:37f96f9d4de2 | 232 | timeinfo.tm_mon = dateStruct.Month - 1; |
<> | 154:37f96f9d4de2 | 233 | timeinfo.tm_mday = dateStruct.Date; |
<> | 154:37f96f9d4de2 | 234 | timeinfo.tm_year = dateStruct.Year + 68; |
<> | 154:37f96f9d4de2 | 235 | timeinfo.tm_hour = timeStruct.Hours; |
<> | 154:37f96f9d4de2 | 236 | timeinfo.tm_min = timeStruct.Minutes; |
<> | 154:37f96f9d4de2 | 237 | timeinfo.tm_sec = timeStruct.Seconds; |
<> | 154:37f96f9d4de2 | 238 | // Daylight Saving Time information is not available |
<> | 154:37f96f9d4de2 | 239 | timeinfo.tm_isdst = -1; |
<> | 154:37f96f9d4de2 | 240 | |
<> | 154:37f96f9d4de2 | 241 | // Convert to timestamp |
<> | 154:37f96f9d4de2 | 242 | time_t t = mktime(&timeinfo); |
<> | 154:37f96f9d4de2 | 243 | |
<> | 154:37f96f9d4de2 | 244 | return t; |
<> | 154:37f96f9d4de2 | 245 | } |
<> | 154:37f96f9d4de2 | 246 | |
<> | 154:37f96f9d4de2 | 247 | void rtc_write(time_t t) |
<> | 154:37f96f9d4de2 | 248 | { |
<> | 154:37f96f9d4de2 | 249 | RTC_DateTypeDef dateStruct; |
<> | 154:37f96f9d4de2 | 250 | RTC_TimeTypeDef timeStruct; |
<> | 154:37f96f9d4de2 | 251 | |
<> | 154:37f96f9d4de2 | 252 | RtcHandle.Instance = RTC; |
<> | 154:37f96f9d4de2 | 253 | |
<> | 154:37f96f9d4de2 | 254 | // Convert the time into a tm |
<> | 154:37f96f9d4de2 | 255 | struct tm *timeinfo = localtime(&t); |
<> | 154:37f96f9d4de2 | 256 | |
<> | 154:37f96f9d4de2 | 257 | // Fill RTC structures |
<> | 160:d5399cc887bb | 258 | if (timeinfo->tm_wday == 0) { |
<> | 160:d5399cc887bb | 259 | dateStruct.WeekDay = 7; |
<> | 160:d5399cc887bb | 260 | } else { |
<> | 160:d5399cc887bb | 261 | dateStruct.WeekDay = timeinfo->tm_wday; |
<> | 160:d5399cc887bb | 262 | } |
<> | 154:37f96f9d4de2 | 263 | dateStruct.Month = timeinfo->tm_mon + 1; |
<> | 154:37f96f9d4de2 | 264 | dateStruct.Date = timeinfo->tm_mday; |
<> | 154:37f96f9d4de2 | 265 | dateStruct.Year = timeinfo->tm_year - 68; |
<> | 154:37f96f9d4de2 | 266 | timeStruct.Hours = timeinfo->tm_hour; |
<> | 154:37f96f9d4de2 | 267 | timeStruct.Minutes = timeinfo->tm_min; |
<> | 154:37f96f9d4de2 | 268 | timeStruct.Seconds = timeinfo->tm_sec; |
<> | 154:37f96f9d4de2 | 269 | |
<> | 154:37f96f9d4de2 | 270 | #if !(TARGET_STM32F1) |
<> | 154:37f96f9d4de2 | 271 | timeStruct.TimeFormat = RTC_HOURFORMAT_24; |
<> | 154:37f96f9d4de2 | 272 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
<> | 154:37f96f9d4de2 | 273 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
<> | 154:37f96f9d4de2 | 274 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 275 | |
<> | 154:37f96f9d4de2 | 276 | // Change the RTC current date/time |
<> | 160:d5399cc887bb | 277 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
<> | 160:d5399cc887bb | 278 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
<> | 154:37f96f9d4de2 | 279 | } |
<> | 154:37f96f9d4de2 | 280 | |
<> | 154:37f96f9d4de2 | 281 | int rtc_isenabled(void) |
<> | 154:37f96f9d4de2 | 282 | { |
<> | 160:d5399cc887bb | 283 | #if !(TARGET_STM32F1) |
<> | 160:d5399cc887bb | 284 | return ( ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) && ((RTC->ISR & RTC_ISR_RSF) == RTC_ISR_RSF) ); |
<> | 160:d5399cc887bb | 285 | #else /* TARGET_STM32F1 */ |
<> | 160:d5399cc887bb | 286 | return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF); |
<> | 160:d5399cc887bb | 287 | #endif /* TARGET_STM32F1 */ |
<> | 154:37f96f9d4de2 | 288 | } |
<> | 154:37f96f9d4de2 | 289 | |
<> | 154:37f96f9d4de2 | 290 | #if DEVICE_LOWPOWERTIMER |
<> | 154:37f96f9d4de2 | 291 | |
<> | 154:37f96f9d4de2 | 292 | static void RTC_IRQHandler(void) |
<> | 154:37f96f9d4de2 | 293 | { |
<> | 154:37f96f9d4de2 | 294 | HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); |
<> | 154:37f96f9d4de2 | 295 | } |
<> | 154:37f96f9d4de2 | 296 | |
<> | 154:37f96f9d4de2 | 297 | void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) |
<> | 154:37f96f9d4de2 | 298 | { |
<> | 154:37f96f9d4de2 | 299 | if (irq_handler) { |
<> | 154:37f96f9d4de2 | 300 | // Fire the user callback |
<> | 154:37f96f9d4de2 | 301 | irq_handler(); |
<> | 154:37f96f9d4de2 | 302 | } |
<> | 154:37f96f9d4de2 | 303 | } |
<> | 154:37f96f9d4de2 | 304 | |
<> | 154:37f96f9d4de2 | 305 | void rtc_set_irq_handler(uint32_t handler) |
<> | 154:37f96f9d4de2 | 306 | { |
<> | 154:37f96f9d4de2 | 307 | irq_handler = (void (*)(void))handler; |
<> | 154:37f96f9d4de2 | 308 | } |
<> | 154:37f96f9d4de2 | 309 | |
<> | 154:37f96f9d4de2 | 310 | uint32_t rtc_read_subseconds(void) |
<> | 154:37f96f9d4de2 | 311 | { |
<> | 154:37f96f9d4de2 | 312 | return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); |
<> | 154:37f96f9d4de2 | 313 | } |
<> | 154:37f96f9d4de2 | 314 | |
<> | 154:37f96f9d4de2 | 315 | void rtc_set_wake_up_timer(uint32_t delta) |
<> | 154:37f96f9d4de2 | 316 | { |
<> | 154:37f96f9d4de2 | 317 | uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); |
<> | 154:37f96f9d4de2 | 318 | |
<> | 154:37f96f9d4de2 | 319 | if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, |
<> | 154:37f96f9d4de2 | 320 | RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { |
<> | 154:37f96f9d4de2 | 321 | error("Set wake up timer failed\n"); |
<> | 154:37f96f9d4de2 | 322 | } |
<> | 154:37f96f9d4de2 | 323 | } |
<> | 154:37f96f9d4de2 | 324 | |
<> | 154:37f96f9d4de2 | 325 | void rtc_deactivate_wake_up_timer(void) |
<> | 154:37f96f9d4de2 | 326 | { |
<> | 154:37f96f9d4de2 | 327 | HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); |
<> | 154:37f96f9d4de2 | 328 | } |
<> | 154:37f96f9d4de2 | 329 | |
<> | 154:37f96f9d4de2 | 330 | void rtc_synchronize(void) |
<> | 154:37f96f9d4de2 | 331 | { |
<> | 154:37f96f9d4de2 | 332 | HAL_RTC_WaitForSynchro(&RtcHandle); |
<> | 154:37f96f9d4de2 | 333 | } |
<> | 154:37f96f9d4de2 | 334 | #endif /* DEVICE_LOWPOWERTIMER */ |
<> | 154:37f96f9d4de2 | 335 | |
<> | 154:37f96f9d4de2 | 336 | #endif /* DEVICE_RTC */ |