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