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