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