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