Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LPCXpresso1769_blinky
Fork of mbed-dev by
targets/hal/TARGET_STM/TARGET_STM32F7/rtc_api.c@74:9322579e4309, 2016-02-26 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Feb 26 11:45:10 2016 +0000
- Revision:
- 74:9322579e4309
- Parent:
- 70:885f1c2f67b1
- Child:
- 78:fcc37a1ad8f5
Synchronized with git revision 121804a99d058122097dc72cb02236fa317a1cb4
Full URL: https://github.com/mbedmicro/mbed/commit/121804a99d058122097dc72cb02236fa317a1cb4/
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 | |
bogdanm | 0:9b334a45a8ff | 36 | static RTC_HandleTypeDef RtcHandle; |
bogdanm | 0:9b334a45a8ff | 37 | |
bogdanm | 0:9b334a45a8ff | 38 | void rtc_init(void) |
bogdanm | 0:9b334a45a8ff | 39 | { |
bogdanm | 0:9b334a45a8ff | 40 | RCC_OscInitTypeDef RCC_OscInitStruct; |
bogdanm | 0:9b334a45a8ff | 41 | uint32_t rtc_freq = 0; |
bogdanm | 0:9b334a45a8ff | 42 | |
mbed_official | 74:9322579e4309 | 43 | RtcHandle.Instance = RTC; |
mbed_official | 4:4a8e88c1ef9e | 44 | |
mbed_official | 74:9322579e4309 | 45 | // Enable Power clock |
mbed_official | 74:9322579e4309 | 46 | __PWR_CLK_ENABLE(); |
bogdanm | 0:9b334a45a8ff | 47 | |
mbed_official | 74:9322579e4309 | 48 | // Enable access to Backup domain |
mbed_official | 74:9322579e4309 | 49 | HAL_PWR_EnableBkUpAccess(); |
mbed_official | 74:9322579e4309 | 50 | |
mbed_official | 74:9322579e4309 | 51 | // Reset Backup domain |
mbed_official | 74:9322579e4309 | 52 | __HAL_RCC_BACKUPRESET_FORCE(); |
mbed_official | 74:9322579e4309 | 53 | __HAL_RCC_BACKUPRESET_RELEASE(); |
mbed_official | 74:9322579e4309 | 54 | |
mbed_official | 74:9322579e4309 | 55 | // Enable LSE Oscillator |
mbed_official | 74:9322579e4309 | 56 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
bogdanm | 0:9b334a45a8ff | 57 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
mbed_official | 74:9322579e4309 | 58 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT |
mbed_official | 74:9322579e4309 | 59 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { |
mbed_official | 74:9322579e4309 | 60 | // Connect LSE to RTC |
mbed_official | 74:9322579e4309 | 61 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); |
mbed_official | 74:9322579e4309 | 62 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
mbed_official | 74:9322579e4309 | 63 | rtc_freq = LSE_VALUE; |
mbed_official | 74:9322579e4309 | 64 | } else { |
mbed_official | 74:9322579e4309 | 65 | // Enable LSI clock |
mbed_official | 74:9322579e4309 | 66 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
mbed_official | 74:9322579e4309 | 67 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
mbed_official | 74:9322579e4309 | 68 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
mbed_official | 74:9322579e4309 | 69 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
mbed_official | 74:9322579e4309 | 70 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
mbed_official | 74:9322579e4309 | 71 | error("RTC error: LSI clock initialization failed."); |
mbed_official | 74:9322579e4309 | 72 | } |
mbed_official | 74:9322579e4309 | 73 | // Connect LSI to RTC |
mbed_official | 74:9322579e4309 | 74 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); |
mbed_official | 74:9322579e4309 | 75 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
mbed_official | 74:9322579e4309 | 76 | // [TODO] This value is LSI typical value. To be measured precisely using a timer input capture |
mbed_official | 74:9322579e4309 | 77 | rtc_freq = LSI_VALUE; |
bogdanm | 0:9b334a45a8ff | 78 | } |
mbed_official | 48:b854a29e778d | 79 | |
mbed_official | 74:9322579e4309 | 80 | if((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { // RTC initialization and status register (RTC_ISR), cold start (with no backup domain power) RTC reset value |
bogdanm | 0:9b334a45a8ff | 81 | |
mbed_official | 74:9322579e4309 | 82 | // Enable RTC |
mbed_official | 74:9322579e4309 | 83 | __HAL_RCC_RTC_ENABLE(); |
bogdanm | 0:9b334a45a8ff | 84 | |
mbed_official | 74:9322579e4309 | 85 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
mbed_official | 74:9322579e4309 | 86 | RtcHandle.Init.AsynchPrediv = 127; |
mbed_official | 74:9322579e4309 | 87 | RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1; |
mbed_official | 74:9322579e4309 | 88 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
mbed_official | 74:9322579e4309 | 89 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
mbed_official | 74:9322579e4309 | 90 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
mbed_official | 4:4a8e88c1ef9e | 91 | |
mbed_official | 74:9322579e4309 | 92 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
mbed_official | 74:9322579e4309 | 93 | error("RTC error: RTC initialization failed."); |
mbed_official | 74:9322579e4309 | 94 | } |
mbed_official | 74:9322579e4309 | 95 | |
bogdanm | 0:9b334a45a8ff | 96 | } |
bogdanm | 0:9b334a45a8ff | 97 | } |
bogdanm | 0:9b334a45a8ff | 98 | |
bogdanm | 0:9b334a45a8ff | 99 | void rtc_free(void) |
bogdanm | 0:9b334a45a8ff | 100 | { |
bogdanm | 0:9b334a45a8ff | 101 | // Enable Power clock |
bogdanm | 0:9b334a45a8ff | 102 | __PWR_CLK_ENABLE(); |
bogdanm | 0:9b334a45a8ff | 103 | |
bogdanm | 0:9b334a45a8ff | 104 | // Enable access to Backup domain |
bogdanm | 0:9b334a45a8ff | 105 | HAL_PWR_EnableBkUpAccess(); |
bogdanm | 0:9b334a45a8ff | 106 | |
bogdanm | 0:9b334a45a8ff | 107 | // Reset Backup domain |
bogdanm | 0:9b334a45a8ff | 108 | __HAL_RCC_BACKUPRESET_FORCE(); |
bogdanm | 0:9b334a45a8ff | 109 | __HAL_RCC_BACKUPRESET_RELEASE(); |
bogdanm | 0:9b334a45a8ff | 110 | |
bogdanm | 0:9b334a45a8ff | 111 | // Disable access to Backup domain |
bogdanm | 0:9b334a45a8ff | 112 | HAL_PWR_DisableBkUpAccess(); |
bogdanm | 0:9b334a45a8ff | 113 | |
bogdanm | 0:9b334a45a8ff | 114 | // Disable LSI and LSE clocks |
bogdanm | 0:9b334a45a8ff | 115 | RCC_OscInitTypeDef RCC_OscInitStruct; |
bogdanm | 0:9b334a45a8ff | 116 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
bogdanm | 0:9b334a45a8ff | 117 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
bogdanm | 0:9b334a45a8ff | 118 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
bogdanm | 0:9b334a45a8ff | 119 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
bogdanm | 0:9b334a45a8ff | 120 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
bogdanm | 0:9b334a45a8ff | 121 | } |
bogdanm | 0:9b334a45a8ff | 122 | |
bogdanm | 0:9b334a45a8ff | 123 | int rtc_isenabled(void) |
bogdanm | 0:9b334a45a8ff | 124 | { |
mbed_official | 4:4a8e88c1ef9e | 125 | if(RTC->ISR != 7) { |
mbed_official | 4:4a8e88c1ef9e | 126 | return 1; |
mbed_official | 4:4a8e88c1ef9e | 127 | } else { |
mbed_official | 4:4a8e88c1ef9e | 128 | return 0; |
mbed_official | 4:4a8e88c1ef9e | 129 | } |
bogdanm | 0:9b334a45a8ff | 130 | } |
bogdanm | 0:9b334a45a8ff | 131 | |
bogdanm | 0:9b334a45a8ff | 132 | /* |
bogdanm | 0:9b334a45a8ff | 133 | RTC Registers |
bogdanm | 0:9b334a45a8ff | 134 | RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
bogdanm | 0:9b334a45a8ff | 135 | RTC_Month 1=january, 2=february, ..., 12=december |
bogdanm | 0:9b334a45a8ff | 136 | RTC_Date day of the month 1-31 |
bogdanm | 0:9b334a45a8ff | 137 | RTC_Year year 0-99 |
bogdanm | 0:9b334a45a8ff | 138 | struct tm |
bogdanm | 0:9b334a45a8ff | 139 | tm_sec seconds after the minute 0-61 |
bogdanm | 0:9b334a45a8ff | 140 | tm_min minutes after the hour 0-59 |
bogdanm | 0:9b334a45a8ff | 141 | tm_hour hours since midnight 0-23 |
bogdanm | 0:9b334a45a8ff | 142 | tm_mday day of the month 1-31 |
bogdanm | 0:9b334a45a8ff | 143 | tm_mon months since January 0-11 |
bogdanm | 0:9b334a45a8ff | 144 | tm_year years since 1900 |
bogdanm | 0:9b334a45a8ff | 145 | tm_wday days since Sunday 0-6 |
bogdanm | 0:9b334a45a8ff | 146 | tm_yday days since January 1 0-365 |
bogdanm | 0:9b334a45a8ff | 147 | tm_isdst Daylight Saving Time flag |
bogdanm | 0:9b334a45a8ff | 148 | */ |
bogdanm | 0:9b334a45a8ff | 149 | time_t rtc_read(void) |
bogdanm | 0:9b334a45a8ff | 150 | { |
bogdanm | 0:9b334a45a8ff | 151 | RTC_DateTypeDef dateStruct; |
bogdanm | 0:9b334a45a8ff | 152 | RTC_TimeTypeDef timeStruct; |
bogdanm | 0:9b334a45a8ff | 153 | struct tm timeinfo; |
bogdanm | 0:9b334a45a8ff | 154 | |
bogdanm | 0:9b334a45a8ff | 155 | RtcHandle.Instance = RTC; |
bogdanm | 0:9b334a45a8ff | 156 | |
bogdanm | 0:9b334a45a8ff | 157 | // Read actual date and time |
bogdanm | 0:9b334a45a8ff | 158 | // Warning: the time must be read first! |
bogdanm | 0:9b334a45a8ff | 159 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
bogdanm | 0:9b334a45a8ff | 160 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
bogdanm | 0:9b334a45a8ff | 161 | |
bogdanm | 0:9b334a45a8ff | 162 | // Setup a tm structure based on the RTC |
bogdanm | 0:9b334a45a8ff | 163 | timeinfo.tm_wday = dateStruct.WeekDay; |
bogdanm | 0:9b334a45a8ff | 164 | timeinfo.tm_mon = dateStruct.Month - 1; |
bogdanm | 0:9b334a45a8ff | 165 | timeinfo.tm_mday = dateStruct.Date; |
bogdanm | 0:9b334a45a8ff | 166 | timeinfo.tm_year = dateStruct.Year + 100; |
bogdanm | 0:9b334a45a8ff | 167 | timeinfo.tm_hour = timeStruct.Hours; |
bogdanm | 0:9b334a45a8ff | 168 | timeinfo.tm_min = timeStruct.Minutes; |
bogdanm | 0:9b334a45a8ff | 169 | timeinfo.tm_sec = timeStruct.Seconds; |
bogdanm | 0:9b334a45a8ff | 170 | |
bogdanm | 0:9b334a45a8ff | 171 | // Convert to timestamp |
bogdanm | 0:9b334a45a8ff | 172 | time_t t = mktime(&timeinfo); |
bogdanm | 0:9b334a45a8ff | 173 | |
bogdanm | 0:9b334a45a8ff | 174 | return t; |
bogdanm | 0:9b334a45a8ff | 175 | } |
bogdanm | 0:9b334a45a8ff | 176 | |
bogdanm | 0:9b334a45a8ff | 177 | void rtc_write(time_t t) |
bogdanm | 0:9b334a45a8ff | 178 | { |
bogdanm | 0:9b334a45a8ff | 179 | RTC_DateTypeDef dateStruct; |
bogdanm | 0:9b334a45a8ff | 180 | RTC_TimeTypeDef timeStruct; |
bogdanm | 0:9b334a45a8ff | 181 | |
bogdanm | 0:9b334a45a8ff | 182 | RtcHandle.Instance = RTC; |
bogdanm | 0:9b334a45a8ff | 183 | |
mbed_official | 4:4a8e88c1ef9e | 184 | // Enable write access to Backup domain |
mbed_official | 4:4a8e88c1ef9e | 185 | HAL_PWR_EnableBkUpAccess(); |
mbed_official | 4:4a8e88c1ef9e | 186 | |
bogdanm | 0:9b334a45a8ff | 187 | // Convert the time into a tm |
bogdanm | 0:9b334a45a8ff | 188 | struct tm *timeinfo = localtime(&t); |
bogdanm | 0:9b334a45a8ff | 189 | |
bogdanm | 0:9b334a45a8ff | 190 | // Fill RTC structures |
bogdanm | 0:9b334a45a8ff | 191 | dateStruct.WeekDay = timeinfo->tm_wday; |
bogdanm | 0:9b334a45a8ff | 192 | dateStruct.Month = timeinfo->tm_mon + 1; |
bogdanm | 0:9b334a45a8ff | 193 | dateStruct.Date = timeinfo->tm_mday; |
bogdanm | 0:9b334a45a8ff | 194 | dateStruct.Year = timeinfo->tm_year - 100; |
bogdanm | 0:9b334a45a8ff | 195 | timeStruct.Hours = timeinfo->tm_hour; |
bogdanm | 0:9b334a45a8ff | 196 | timeStruct.Minutes = timeinfo->tm_min; |
bogdanm | 0:9b334a45a8ff | 197 | timeStruct.Seconds = timeinfo->tm_sec; |
bogdanm | 0:9b334a45a8ff | 198 | timeStruct.TimeFormat = RTC_HOURFORMAT12_PM; |
bogdanm | 0:9b334a45a8ff | 199 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
bogdanm | 0:9b334a45a8ff | 200 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
bogdanm | 0:9b334a45a8ff | 201 | |
bogdanm | 0:9b334a45a8ff | 202 | // Change the RTC current date/time |
bogdanm | 0:9b334a45a8ff | 203 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
bogdanm | 0:9b334a45a8ff | 204 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
bogdanm | 0:9b334a45a8ff | 205 | } |
bogdanm | 0:9b334a45a8ff | 206 | |
bogdanm | 0:9b334a45a8ff | 207 | #endif |