mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/rtc_api.c@87:085cde657901, 2014-02-08 (annotated)
- Committer:
- mbed_official
- Date:
- Sat Feb 08 19:45:06 2014 +0000
- Revision:
- 87:085cde657901
- Child:
- 125:23cc3068a9e4
Synchronized with git revision 9272cdeb45ec7e6077641536509413da8fd2ebc2
Full URL: https://github.com/mbedmicro/mbed/commit/9272cdeb45ec7e6077641536509413da8fd2ebc2/
Add NUCLEO_F401RE, improvements
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 | 87:085cde657901 | 31 | #include "stm32f4xx_hal.h" |
mbed_official | 87:085cde657901 | 32 | #include "error.h" |
mbed_official | 87:085cde657901 | 33 | |
mbed_official | 87:085cde657901 | 34 | static int rtc_inited = 0; |
mbed_official | 87:085cde657901 | 35 | |
mbed_official | 87:085cde657901 | 36 | static RTC_HandleTypeDef RtcHandle; |
mbed_official | 87:085cde657901 | 37 | |
mbed_official | 87:085cde657901 | 38 | void rtc_init(void) { |
mbed_official | 87:085cde657901 | 39 | if (rtc_inited) return; |
mbed_official | 87:085cde657901 | 40 | rtc_inited = 1; |
mbed_official | 87:085cde657901 | 41 | |
mbed_official | 87:085cde657901 | 42 | RtcHandle.Instance = RTC; |
mbed_official | 87:085cde657901 | 43 | |
mbed_official | 87:085cde657901 | 44 | // Enable Power clock |
mbed_official | 87:085cde657901 | 45 | __PWR_CLK_ENABLE(); |
mbed_official | 87:085cde657901 | 46 | |
mbed_official | 87:085cde657901 | 47 | // Allow access to RTC |
mbed_official | 87:085cde657901 | 48 | HAL_PWR_EnableBkUpAccess(); |
mbed_official | 87:085cde657901 | 49 | |
mbed_official | 87:085cde657901 | 50 | // Reset Backup domain |
mbed_official | 87:085cde657901 | 51 | __HAL_RCC_BACKUPRESET_FORCE(); |
mbed_official | 87:085cde657901 | 52 | __HAL_RCC_BACKUPRESET_RELEASE(); |
mbed_official | 87:085cde657901 | 53 | |
mbed_official | 87:085cde657901 | 54 | // Enable LSI clock |
mbed_official | 87:085cde657901 | 55 | RCC_OscInitTypeDef RCC_OscInitStruct; |
mbed_official | 87:085cde657901 | 56 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
mbed_official | 87:085cde657901 | 57 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
mbed_official | 87:085cde657901 | 58 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
mbed_official | 87:085cde657901 | 59 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
mbed_official | 87:085cde657901 | 60 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
mbed_official | 87:085cde657901 | 61 | error("RTC error: LSI clock initialization failed."); |
mbed_official | 87:085cde657901 | 62 | } |
mbed_official | 87:085cde657901 | 63 | |
mbed_official | 87:085cde657901 | 64 | // Connect LSI to RTC |
mbed_official | 87:085cde657901 | 65 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); |
mbed_official | 87:085cde657901 | 66 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
mbed_official | 87:085cde657901 | 67 | |
mbed_official | 87:085cde657901 | 68 | // Enable RTC clock |
mbed_official | 87:085cde657901 | 69 | __HAL_RCC_RTC_ENABLE(); |
mbed_official | 87:085cde657901 | 70 | |
mbed_official | 87:085cde657901 | 71 | // This is LSI typical value |
mbed_official | 87:085cde657901 | 72 | // To be measured precisely using a timer input capture [TODO] |
mbed_official | 87:085cde657901 | 73 | uint32_t lsi_freq = 32000; |
mbed_official | 87:085cde657901 | 74 | |
mbed_official | 87:085cde657901 | 75 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
mbed_official | 87:085cde657901 | 76 | RtcHandle.Init.AsynchPrediv = 127; |
mbed_official | 87:085cde657901 | 77 | RtcHandle.Init.SynchPrediv = (lsi_freq / 128) - 1; |
mbed_official | 87:085cde657901 | 78 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
mbed_official | 87:085cde657901 | 79 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
mbed_official | 87:085cde657901 | 80 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
mbed_official | 87:085cde657901 | 81 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
mbed_official | 87:085cde657901 | 82 | error("RTC error: RTC initialization failed."); |
mbed_official | 87:085cde657901 | 83 | } |
mbed_official | 87:085cde657901 | 84 | } |
mbed_official | 87:085cde657901 | 85 | |
mbed_official | 87:085cde657901 | 86 | void rtc_free(void) { |
mbed_official | 87:085cde657901 | 87 | // Enable Power clock |
mbed_official | 87:085cde657901 | 88 | __PWR_CLK_ENABLE(); |
mbed_official | 87:085cde657901 | 89 | |
mbed_official | 87:085cde657901 | 90 | // Allow access to RTC |
mbed_official | 87:085cde657901 | 91 | HAL_PWR_EnableBkUpAccess(); |
mbed_official | 87:085cde657901 | 92 | |
mbed_official | 87:085cde657901 | 93 | // Reset Backup domain |
mbed_official | 87:085cde657901 | 94 | __HAL_RCC_BACKUPRESET_FORCE(); |
mbed_official | 87:085cde657901 | 95 | __HAL_RCC_BACKUPRESET_RELEASE(); |
mbed_official | 87:085cde657901 | 96 | |
mbed_official | 87:085cde657901 | 97 | // Disable LSI clock |
mbed_official | 87:085cde657901 | 98 | RCC_OscInitTypeDef RCC_OscInitStruct; |
mbed_official | 87:085cde657901 | 99 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
mbed_official | 87:085cde657901 | 100 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
mbed_official | 87:085cde657901 | 101 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
mbed_official | 87:085cde657901 | 102 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
mbed_official | 87:085cde657901 | 103 | |
mbed_official | 87:085cde657901 | 104 | rtc_inited = 0; |
mbed_official | 87:085cde657901 | 105 | } |
mbed_official | 87:085cde657901 | 106 | |
mbed_official | 87:085cde657901 | 107 | int rtc_isenabled(void) { |
mbed_official | 87:085cde657901 | 108 | return rtc_inited; |
mbed_official | 87:085cde657901 | 109 | } |
mbed_official | 87:085cde657901 | 110 | |
mbed_official | 87:085cde657901 | 111 | /* |
mbed_official | 87:085cde657901 | 112 | RTC Registers |
mbed_official | 87:085cde657901 | 113 | RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
mbed_official | 87:085cde657901 | 114 | RTC_Month 1=january, 2=february, ..., 12=december |
mbed_official | 87:085cde657901 | 115 | RTC_Date day of the month 1-31 |
mbed_official | 87:085cde657901 | 116 | RTC_Year year 0-99 |
mbed_official | 87:085cde657901 | 117 | struct tm |
mbed_official | 87:085cde657901 | 118 | tm_sec seconds after the minute 0-61 |
mbed_official | 87:085cde657901 | 119 | tm_min minutes after the hour 0-59 |
mbed_official | 87:085cde657901 | 120 | tm_hour hours since midnight 0-23 |
mbed_official | 87:085cde657901 | 121 | tm_mday day of the month 1-31 |
mbed_official | 87:085cde657901 | 122 | tm_mon months since January 0-11 |
mbed_official | 87:085cde657901 | 123 | tm_year years since 1900 |
mbed_official | 87:085cde657901 | 124 | tm_wday days since Sunday 0-6 |
mbed_official | 87:085cde657901 | 125 | tm_yday days since January 1 0-365 |
mbed_official | 87:085cde657901 | 126 | tm_isdst Daylight Saving Time flag |
mbed_official | 87:085cde657901 | 127 | */ |
mbed_official | 87:085cde657901 | 128 | time_t rtc_read(void) { |
mbed_official | 87:085cde657901 | 129 | RTC_DateTypeDef dateStruct; |
mbed_official | 87:085cde657901 | 130 | RTC_TimeTypeDef timeStruct; |
mbed_official | 87:085cde657901 | 131 | struct tm timeinfo; |
mbed_official | 87:085cde657901 | 132 | |
mbed_official | 87:085cde657901 | 133 | RtcHandle.Instance = RTC; |
mbed_official | 87:085cde657901 | 134 | |
mbed_official | 87:085cde657901 | 135 | // Read actual date and time |
mbed_official | 87:085cde657901 | 136 | // Warning: the time must be read first! |
mbed_official | 87:085cde657901 | 137 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
mbed_official | 87:085cde657901 | 138 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
mbed_official | 87:085cde657901 | 139 | |
mbed_official | 87:085cde657901 | 140 | // Setup a tm structure based on the RTC |
mbed_official | 87:085cde657901 | 141 | timeinfo.tm_wday = dateStruct.WeekDay; |
mbed_official | 87:085cde657901 | 142 | timeinfo.tm_mon = dateStruct.Month - 1; |
mbed_official | 87:085cde657901 | 143 | timeinfo.tm_mday = dateStruct.Date; |
mbed_official | 87:085cde657901 | 144 | timeinfo.tm_year = dateStruct.Year + 100; |
mbed_official | 87:085cde657901 | 145 | timeinfo.tm_hour = timeStruct.Hours; |
mbed_official | 87:085cde657901 | 146 | timeinfo.tm_min = timeStruct.Minutes; |
mbed_official | 87:085cde657901 | 147 | timeinfo.tm_sec = timeStruct.Seconds; |
mbed_official | 87:085cde657901 | 148 | |
mbed_official | 87:085cde657901 | 149 | // Convert to timestamp |
mbed_official | 87:085cde657901 | 150 | time_t t = mktime(&timeinfo); |
mbed_official | 87:085cde657901 | 151 | |
mbed_official | 87:085cde657901 | 152 | return t; |
mbed_official | 87:085cde657901 | 153 | } |
mbed_official | 87:085cde657901 | 154 | |
mbed_official | 87:085cde657901 | 155 | void rtc_write(time_t t) { |
mbed_official | 87:085cde657901 | 156 | RTC_DateTypeDef dateStruct; |
mbed_official | 87:085cde657901 | 157 | RTC_TimeTypeDef timeStruct; |
mbed_official | 87:085cde657901 | 158 | |
mbed_official | 87:085cde657901 | 159 | RtcHandle.Instance = RTC; |
mbed_official | 87:085cde657901 | 160 | |
mbed_official | 87:085cde657901 | 161 | // Convert the time into a tm |
mbed_official | 87:085cde657901 | 162 | struct tm *timeinfo = localtime(&t); |
mbed_official | 87:085cde657901 | 163 | |
mbed_official | 87:085cde657901 | 164 | // Fill RTC structures |
mbed_official | 87:085cde657901 | 165 | dateStruct.WeekDay = timeinfo->tm_wday; |
mbed_official | 87:085cde657901 | 166 | dateStruct.Month = timeinfo->tm_mon + 1; |
mbed_official | 87:085cde657901 | 167 | dateStruct.Date = timeinfo->tm_mday; |
mbed_official | 87:085cde657901 | 168 | dateStruct.Year = timeinfo->tm_year - 100; |
mbed_official | 87:085cde657901 | 169 | timeStruct.Hours = timeinfo->tm_hour; |
mbed_official | 87:085cde657901 | 170 | timeStruct.Minutes = timeinfo->tm_min; |
mbed_official | 87:085cde657901 | 171 | timeStruct.Seconds = timeinfo->tm_sec; |
mbed_official | 87:085cde657901 | 172 | timeStruct.TimeFormat = RTC_HOURFORMAT12_PM; |
mbed_official | 87:085cde657901 | 173 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
mbed_official | 87:085cde657901 | 174 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
mbed_official | 87:085cde657901 | 175 | |
mbed_official | 87:085cde657901 | 176 | // Change the RTC current date/time |
mbed_official | 87:085cde657901 | 177 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
mbed_official | 87:085cde657901 | 178 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
mbed_official | 87:085cde657901 | 179 | } |