mbed library sources. Supersedes mbed-src.
Fork of mbed by
targets/hal/TARGET_STM/TARGET_STM32L4/rtc_api.c@111:86e691153a57, 2016-04-15 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Apr 15 11:00:11 2016 +0100
- Revision:
- 111:86e691153a57
- Parent:
- 103:173fc4c67e73
- Child:
- 144:ef7eb2e8f9f7
Synchronized with git revision 9b277ae859dd9b09ceec43e4c89b18eb6a840cd6
Full URL: https://github.com/mbedmicro/mbed/commit/9b277ae859dd9b09ceec43e4c89b18eb6a840cd6/
* Add DEVICE_RTC_LSI=0
* Change LSI value to 32 kHz
This is the value written in the device datasheet.
* Replace tabulations with spaces
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 3 | * Copyright (c) 2015, STMicroelectronics |
bogdanm | 0:9b334a45a8ff | 4 | * All rights reserved. |
bogdanm | 0:9b334a45a8ff | 5 | * |
bogdanm | 0:9b334a45a8ff | 6 | * Redistribution and use in source and binary forms, with or without |
bogdanm | 0:9b334a45a8ff | 7 | * modification, are permitted provided that the following conditions are met: |
bogdanm | 0:9b334a45a8ff | 8 | * |
bogdanm | 0:9b334a45a8ff | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
bogdanm | 0:9b334a45a8ff | 10 | * this list of conditions and the following disclaimer. |
bogdanm | 0:9b334a45a8ff | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
bogdanm | 0:9b334a45a8ff | 12 | * this list of conditions and the following disclaimer in the documentation |
bogdanm | 0:9b334a45a8ff | 13 | * and/or other materials provided with the distribution. |
bogdanm | 0:9b334a45a8ff | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
bogdanm | 0:9b334a45a8ff | 15 | * may be used to endorse or promote products derived from this software |
bogdanm | 0:9b334a45a8ff | 16 | * without specific prior written permission. |
bogdanm | 0:9b334a45a8ff | 17 | * |
bogdanm | 0:9b334a45a8ff | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
bogdanm | 0:9b334a45a8ff | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
bogdanm | 0:9b334a45a8ff | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
bogdanm | 0:9b334a45a8ff | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
bogdanm | 0:9b334a45a8ff | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
bogdanm | 0:9b334a45a8ff | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
bogdanm | 0:9b334a45a8ff | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
bogdanm | 0:9b334a45a8ff | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
bogdanm | 0:9b334a45a8ff | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
bogdanm | 0:9b334a45a8ff | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
bogdanm | 0:9b334a45a8ff | 28 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 29 | */ |
bogdanm | 0:9b334a45a8ff | 30 | #include "rtc_api.h" |
bogdanm | 0:9b334a45a8ff | 31 | |
bogdanm | 0:9b334a45a8ff | 32 | #if DEVICE_RTC |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | #include "mbed_error.h" |
bogdanm | 0:9b334a45a8ff | 35 | |
mbed_official | 71:a5b1c83f05dc | 36 | #if DEVICE_RTC_LSI |
bogdanm | 0:9b334a45a8ff | 37 | static int rtc_inited = 0; |
mbed_official | 71:a5b1c83f05dc | 38 | #endif |
bogdanm | 0:9b334a45a8ff | 39 | |
bogdanm | 0:9b334a45a8ff | 40 | static RTC_HandleTypeDef RtcHandle; |
bogdanm | 0:9b334a45a8ff | 41 | |
bogdanm | 0:9b334a45a8ff | 42 | void rtc_init(void) |
bogdanm | 0:9b334a45a8ff | 43 | { |
bogdanm | 0:9b334a45a8ff | 44 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
bogdanm | 0:9b334a45a8ff | 45 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
bogdanm | 0:9b334a45a8ff | 46 | uint32_t rtc_freq = 0; |
bogdanm | 0:9b334a45a8ff | 47 | |
mbed_official | 71:a5b1c83f05dc | 48 | #if DEVICE_RTC_LSI |
bogdanm | 0:9b334a45a8ff | 49 | if (rtc_inited) return; |
bogdanm | 0:9b334a45a8ff | 50 | rtc_inited = 1; |
mbed_official | 71:a5b1c83f05dc | 51 | #endif |
bogdanm | 0:9b334a45a8ff | 52 | |
bogdanm | 0:9b334a45a8ff | 53 | RtcHandle.Instance = RTC; |
bogdanm | 0:9b334a45a8ff | 54 | |
mbed_official | 71:a5b1c83f05dc | 55 | #if !DEVICE_RTC_LSI |
bogdanm | 0:9b334a45a8ff | 56 | // Enable LSE Oscillator |
bogdanm | 0:9b334a45a8ff | 57 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
bogdanm | 0:9b334a45a8ff | 58 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
bogdanm | 0:9b334a45a8ff | 59 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT |
bogdanm | 0:9b334a45a8ff | 60 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
bogdanm | 0:9b334a45a8ff | 61 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly |
bogdanm | 0:9b334a45a8ff | 62 | // Connect LSE to RTC |
bogdanm | 0:9b334a45a8ff | 63 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
bogdanm | 0:9b334a45a8ff | 64 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
bogdanm | 0:9b334a45a8ff | 65 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
bogdanm | 0:9b334a45a8ff | 66 | rtc_freq = LSE_VALUE; |
mbed_official | 71:a5b1c83f05dc | 67 | } else { |
mbed_official | 111:86e691153a57 | 68 | error("Cannot initialize RTC with LSE\n"); |
bogdanm | 0:9b334a45a8ff | 69 | } |
mbed_official | 111:86e691153a57 | 70 | #else |
mbed_official | 103:173fc4c67e73 | 71 | // Enable Power clock |
mbed_official | 103:173fc4c67e73 | 72 | __HAL_RCC_PWR_CLK_ENABLE(); |
mbed_official | 103:173fc4c67e73 | 73 | |
mbed_official | 103:173fc4c67e73 | 74 | // Enable access to Backup domain |
mbed_official | 103:173fc4c67e73 | 75 | HAL_PWR_EnableBkUpAccess(); |
mbed_official | 103:173fc4c67e73 | 76 | |
mbed_official | 103:173fc4c67e73 | 77 | // Reset Backup domain |
mbed_official | 103:173fc4c67e73 | 78 | __HAL_RCC_BACKUPRESET_FORCE(); |
mbed_official | 103:173fc4c67e73 | 79 | __HAL_RCC_BACKUPRESET_RELEASE(); |
mbed_official | 103:173fc4c67e73 | 80 | |
mbed_official | 111:86e691153a57 | 81 | // Enable LSI clock |
mbed_official | 111:86e691153a57 | 82 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
mbed_official | 111:86e691153a57 | 83 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
mbed_official | 111:86e691153a57 | 84 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
mbed_official | 111:86e691153a57 | 85 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
mbed_official | 111:86e691153a57 | 86 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
mbed_official | 111:86e691153a57 | 87 | error("Cannot initialize RTC with LSI\n"); |
mbed_official | 111:86e691153a57 | 88 | } |
mbed_official | 111:86e691153a57 | 89 | // Connect LSI to RTC |
mbed_official | 111:86e691153a57 | 90 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
mbed_official | 111:86e691153a57 | 91 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
mbed_official | 111:86e691153a57 | 92 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
mbed_official | 111:86e691153a57 | 93 | error("Cannot initialize RTC with LSI\n"); |
mbed_official | 111:86e691153a57 | 94 | } |
mbed_official | 111:86e691153a57 | 95 | // This value is LSI typical value (see device datasheet) |
mbed_official | 111:86e691153a57 | 96 | rtc_freq = 32000; |
mbed_official | 71:a5b1c83f05dc | 97 | #endif |
bogdanm | 0:9b334a45a8ff | 98 | |
mbed_official | 48:b854a29e778d | 99 | // Check if RTC is already initialized |
mbed_official | 48:b854a29e778d | 100 | if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return; |
mbed_official | 48:b854a29e778d | 101 | |
bogdanm | 0:9b334a45a8ff | 102 | // Enable RTC |
bogdanm | 0:9b334a45a8ff | 103 | __HAL_RCC_RTC_ENABLE(); |
bogdanm | 0:9b334a45a8ff | 104 | |
bogdanm | 0:9b334a45a8ff | 105 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
bogdanm | 0:9b334a45a8ff | 106 | RtcHandle.Init.AsynchPrediv = 127; |
bogdanm | 0:9b334a45a8ff | 107 | RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1; |
bogdanm | 0:9b334a45a8ff | 108 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
bogdanm | 0:9b334a45a8ff | 109 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
bogdanm | 0:9b334a45a8ff | 110 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
bogdanm | 0:9b334a45a8ff | 111 | |
bogdanm | 0:9b334a45a8ff | 112 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
bogdanm | 0:9b334a45a8ff | 113 | error("Cannot initialize RTC\n"); |
bogdanm | 0:9b334a45a8ff | 114 | } |
bogdanm | 0:9b334a45a8ff | 115 | } |
bogdanm | 0:9b334a45a8ff | 116 | |
bogdanm | 0:9b334a45a8ff | 117 | void rtc_free(void) |
bogdanm | 0:9b334a45a8ff | 118 | { |
mbed_official | 103:173fc4c67e73 | 119 | #if DEVICE_RTC_LSI |
bogdanm | 0:9b334a45a8ff | 120 | // Enable Power clock |
bogdanm | 0:9b334a45a8ff | 121 | __HAL_RCC_PWR_CLK_ENABLE(); |
bogdanm | 0:9b334a45a8ff | 122 | |
bogdanm | 0:9b334a45a8ff | 123 | // Enable access to Backup domain |
bogdanm | 0:9b334a45a8ff | 124 | HAL_PWR_EnableBkUpAccess(); |
bogdanm | 0:9b334a45a8ff | 125 | |
bogdanm | 0:9b334a45a8ff | 126 | // Reset Backup domain |
bogdanm | 0:9b334a45a8ff | 127 | __HAL_RCC_BACKUPRESET_FORCE(); |
bogdanm | 0:9b334a45a8ff | 128 | __HAL_RCC_BACKUPRESET_RELEASE(); |
bogdanm | 0:9b334a45a8ff | 129 | |
bogdanm | 0:9b334a45a8ff | 130 | // Disable access to Backup domain |
bogdanm | 0:9b334a45a8ff | 131 | HAL_PWR_DisableBkUpAccess(); |
mbed_official | 103:173fc4c67e73 | 132 | #endif |
bogdanm | 0:9b334a45a8ff | 133 | |
bogdanm | 0:9b334a45a8ff | 134 | // Disable LSI and LSE clocks |
bogdanm | 0:9b334a45a8ff | 135 | RCC_OscInitTypeDef RCC_OscInitStruct; |
bogdanm | 0:9b334a45a8ff | 136 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
bogdanm | 0:9b334a45a8ff | 137 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
bogdanm | 0:9b334a45a8ff | 138 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
bogdanm | 0:9b334a45a8ff | 139 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
bogdanm | 0:9b334a45a8ff | 140 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
bogdanm | 0:9b334a45a8ff | 141 | |
mbed_official | 71:a5b1c83f05dc | 142 | #if DEVICE_RTC_LSI |
bogdanm | 0:9b334a45a8ff | 143 | rtc_inited = 0; |
mbed_official | 111:86e691153a57 | 144 | #endif |
bogdanm | 0:9b334a45a8ff | 145 | } |
bogdanm | 0:9b334a45a8ff | 146 | |
bogdanm | 0:9b334a45a8ff | 147 | int rtc_isenabled(void) |
bogdanm | 0:9b334a45a8ff | 148 | { |
mbed_official | 71:a5b1c83f05dc | 149 | #if DEVICE_RTC_LSI |
mbed_official | 71:a5b1c83f05dc | 150 | return rtc_inited; |
mbed_official | 71:a5b1c83f05dc | 151 | #else |
mbed_official | 71:a5b1c83f05dc | 152 | if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { |
mbed_official | 71:a5b1c83f05dc | 153 | return 1; |
mbed_official | 71:a5b1c83f05dc | 154 | } else { |
mbed_official | 71:a5b1c83f05dc | 155 | return 0; |
mbed_official | 71:a5b1c83f05dc | 156 | } |
mbed_official | 71:a5b1c83f05dc | 157 | #endif |
bogdanm | 0:9b334a45a8ff | 158 | } |
bogdanm | 0:9b334a45a8ff | 159 | |
bogdanm | 0:9b334a45a8ff | 160 | /* |
bogdanm | 0:9b334a45a8ff | 161 | RTC Registers |
bogdanm | 0:9b334a45a8ff | 162 | RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
bogdanm | 0:9b334a45a8ff | 163 | RTC_Month 1=january, 2=february, ..., 12=december |
bogdanm | 0:9b334a45a8ff | 164 | RTC_Date day of the month 1-31 |
bogdanm | 0:9b334a45a8ff | 165 | RTC_Year year 0-99 |
bogdanm | 0:9b334a45a8ff | 166 | struct tm |
bogdanm | 0:9b334a45a8ff | 167 | tm_sec seconds after the minute 0-61 |
bogdanm | 0:9b334a45a8ff | 168 | tm_min minutes after the hour 0-59 |
bogdanm | 0:9b334a45a8ff | 169 | tm_hour hours since midnight 0-23 |
bogdanm | 0:9b334a45a8ff | 170 | tm_mday day of the month 1-31 |
bogdanm | 0:9b334a45a8ff | 171 | tm_mon months since January 0-11 |
bogdanm | 0:9b334a45a8ff | 172 | tm_year years since 1900 |
bogdanm | 0:9b334a45a8ff | 173 | tm_wday days since Sunday 0-6 |
bogdanm | 0:9b334a45a8ff | 174 | tm_yday days since January 1 0-365 |
bogdanm | 0:9b334a45a8ff | 175 | tm_isdst Daylight Saving Time flag |
bogdanm | 0:9b334a45a8ff | 176 | */ |
bogdanm | 0:9b334a45a8ff | 177 | time_t rtc_read(void) |
bogdanm | 0:9b334a45a8ff | 178 | { |
bogdanm | 0:9b334a45a8ff | 179 | RTC_DateTypeDef dateStruct; |
bogdanm | 0:9b334a45a8ff | 180 | RTC_TimeTypeDef timeStruct; |
bogdanm | 0:9b334a45a8ff | 181 | struct tm timeinfo; |
bogdanm | 0:9b334a45a8ff | 182 | |
bogdanm | 0:9b334a45a8ff | 183 | RtcHandle.Instance = RTC; |
bogdanm | 0:9b334a45a8ff | 184 | |
bogdanm | 0:9b334a45a8ff | 185 | // Read actual date and time |
bogdanm | 0:9b334a45a8ff | 186 | // Warning: the time must be read first! |
bogdanm | 0:9b334a45a8ff | 187 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
bogdanm | 0:9b334a45a8ff | 188 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
bogdanm | 0:9b334a45a8ff | 189 | |
bogdanm | 0:9b334a45a8ff | 190 | // Setup a tm structure based on the RTC |
bogdanm | 0:9b334a45a8ff | 191 | timeinfo.tm_wday = dateStruct.WeekDay; |
bogdanm | 0:9b334a45a8ff | 192 | timeinfo.tm_mon = dateStruct.Month - 1; |
bogdanm | 0:9b334a45a8ff | 193 | timeinfo.tm_mday = dateStruct.Date; |
bogdanm | 0:9b334a45a8ff | 194 | timeinfo.tm_year = dateStruct.Year + 100; |
bogdanm | 0:9b334a45a8ff | 195 | timeinfo.tm_hour = timeStruct.Hours; |
bogdanm | 0:9b334a45a8ff | 196 | timeinfo.tm_min = timeStruct.Minutes; |
bogdanm | 0:9b334a45a8ff | 197 | timeinfo.tm_sec = timeStruct.Seconds; |
bogdanm | 0:9b334a45a8ff | 198 | |
bogdanm | 0:9b334a45a8ff | 199 | // Convert to timestamp |
bogdanm | 0:9b334a45a8ff | 200 | time_t t = mktime(&timeinfo); |
bogdanm | 0:9b334a45a8ff | 201 | |
bogdanm | 0:9b334a45a8ff | 202 | return t; |
bogdanm | 0:9b334a45a8ff | 203 | } |
bogdanm | 0:9b334a45a8ff | 204 | |
bogdanm | 0:9b334a45a8ff | 205 | void rtc_write(time_t t) |
bogdanm | 0:9b334a45a8ff | 206 | { |
bogdanm | 0:9b334a45a8ff | 207 | RTC_DateTypeDef dateStruct; |
bogdanm | 0:9b334a45a8ff | 208 | RTC_TimeTypeDef timeStruct; |
bogdanm | 0:9b334a45a8ff | 209 | |
bogdanm | 0:9b334a45a8ff | 210 | RtcHandle.Instance = RTC; |
bogdanm | 0:9b334a45a8ff | 211 | |
bogdanm | 0:9b334a45a8ff | 212 | // Convert the time into a tm |
bogdanm | 0:9b334a45a8ff | 213 | struct tm *timeinfo = localtime(&t); |
bogdanm | 0:9b334a45a8ff | 214 | |
bogdanm | 0:9b334a45a8ff | 215 | // Fill RTC structures |
bogdanm | 0:9b334a45a8ff | 216 | dateStruct.WeekDay = timeinfo->tm_wday; |
bogdanm | 0:9b334a45a8ff | 217 | dateStruct.Month = timeinfo->tm_mon + 1; |
bogdanm | 0:9b334a45a8ff | 218 | dateStruct.Date = timeinfo->tm_mday; |
bogdanm | 0:9b334a45a8ff | 219 | dateStruct.Year = timeinfo->tm_year - 100; |
bogdanm | 0:9b334a45a8ff | 220 | timeStruct.Hours = timeinfo->tm_hour; |
bogdanm | 0:9b334a45a8ff | 221 | timeStruct.Minutes = timeinfo->tm_min; |
bogdanm | 0:9b334a45a8ff | 222 | timeStruct.Seconds = timeinfo->tm_sec; |
bogdanm | 0:9b334a45a8ff | 223 | timeStruct.TimeFormat = RTC_HOURFORMAT12_PM; |
bogdanm | 0:9b334a45a8ff | 224 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
bogdanm | 0:9b334a45a8ff | 225 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
bogdanm | 0:9b334a45a8ff | 226 | |
bogdanm | 0:9b334a45a8ff | 227 | // Change the RTC current date/time |
bogdanm | 0:9b334a45a8ff | 228 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
bogdanm | 0:9b334a45a8ff | 229 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
bogdanm | 0:9b334a45a8ff | 230 | } |
bogdanm | 0:9b334a45a8ff | 231 | |
bogdanm | 0:9b334a45a8ff | 232 | #endif |