mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/rtc_api.c@170:bfbcaf535cca, 2014-04-24 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Apr 24 15:30:06 2014 +0100
- Revision:
- 170:bfbcaf535cca
- Parent:
- 162:937d965048d3
- Child:
- 216:577900467c9e
Synchronized with git revision 99a8b2179e9ca41d7254f71e1e46202718aa663d
Full URL: https://github.com/mbedmicro/mbed/commit/99a8b2179e9ca41d7254f71e1e46202718aa663d/
[NUCLEO_F302R8] Add LSE configuration for RTC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 125:23cc3068a9e4 | 1 | /* mbed Microcontroller Library |
mbed_official | 125:23cc3068a9e4 | 2 | ******************************************************************************* |
mbed_official | 125:23cc3068a9e4 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 125:23cc3068a9e4 | 4 | * All rights reserved. |
mbed_official | 125:23cc3068a9e4 | 5 | * |
mbed_official | 125:23cc3068a9e4 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 125:23cc3068a9e4 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 125:23cc3068a9e4 | 8 | * |
mbed_official | 125:23cc3068a9e4 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 125:23cc3068a9e4 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 125:23cc3068a9e4 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 125:23cc3068a9e4 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 125:23cc3068a9e4 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 125:23cc3068a9e4 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 125:23cc3068a9e4 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 125:23cc3068a9e4 | 16 | * without specific prior written permission. |
mbed_official | 125:23cc3068a9e4 | 17 | * |
mbed_official | 125:23cc3068a9e4 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 125:23cc3068a9e4 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 125:23cc3068a9e4 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 125:23cc3068a9e4 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 125:23cc3068a9e4 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 125:23cc3068a9e4 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 125:23cc3068a9e4 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 125:23cc3068a9e4 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 125:23cc3068a9e4 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 125:23cc3068a9e4 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 125:23cc3068a9e4 | 28 | ******************************************************************************* |
mbed_official | 125:23cc3068a9e4 | 29 | */ |
mbed_official | 125:23cc3068a9e4 | 30 | #include "rtc_api.h" |
mbed_official | 125:23cc3068a9e4 | 31 | |
mbed_official | 162:937d965048d3 | 32 | #if DEVICE_RTC |
mbed_official | 162:937d965048d3 | 33 | |
mbed_official | 170:bfbcaf535cca | 34 | #include "wait_api.h" |
mbed_official | 170:bfbcaf535cca | 35 | |
mbed_official | 170:bfbcaf535cca | 36 | #define LSE_STARTUP_TIMEOUT ((uint16_t)500) // delay in ms |
mbed_official | 170:bfbcaf535cca | 37 | |
mbed_official | 125:23cc3068a9e4 | 38 | static int rtc_inited = 0; |
mbed_official | 125:23cc3068a9e4 | 39 | |
mbed_official | 125:23cc3068a9e4 | 40 | void rtc_init(void) { |
mbed_official | 170:bfbcaf535cca | 41 | uint32_t StartUpCounter = 0; |
mbed_official | 170:bfbcaf535cca | 42 | uint32_t LSEStatus = 0; |
mbed_official | 170:bfbcaf535cca | 43 | uint32_t rtc_freq = 0; |
mbed_official | 170:bfbcaf535cca | 44 | |
mbed_official | 125:23cc3068a9e4 | 45 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock |
mbed_official | 125:23cc3068a9e4 | 46 | |
mbed_official | 170:bfbcaf535cca | 47 | PWR_BackupAccessCmd(ENABLE); // Enable access to Backup domain |
mbed_official | 125:23cc3068a9e4 | 48 | |
mbed_official | 170:bfbcaf535cca | 49 | // Reset back up registers |
mbed_official | 125:23cc3068a9e4 | 50 | RCC_BackupResetCmd(ENABLE); |
mbed_official | 125:23cc3068a9e4 | 51 | RCC_BackupResetCmd(DISABLE); |
mbed_official | 170:bfbcaf535cca | 52 | |
mbed_official | 170:bfbcaf535cca | 53 | // Enable LSE clock |
mbed_official | 170:bfbcaf535cca | 54 | RCC_LSEConfig(RCC_LSE_ON); |
mbed_official | 135:067cc8ba23da | 55 | |
mbed_official | 170:bfbcaf535cca | 56 | // Wait till LSE is ready |
mbed_official | 170:bfbcaf535cca | 57 | do { |
mbed_official | 170:bfbcaf535cca | 58 | LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY); |
mbed_official | 170:bfbcaf535cca | 59 | wait_ms(1); |
mbed_official | 170:bfbcaf535cca | 60 | StartUpCounter++; |
mbed_official | 170:bfbcaf535cca | 61 | } while((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT)); |
mbed_official | 135:067cc8ba23da | 62 | |
mbed_official | 170:bfbcaf535cca | 63 | if (StartUpCounter > LSE_STARTUP_TIMEOUT) { |
mbed_official | 170:bfbcaf535cca | 64 | // The LSE has not started, use LSI instead. |
mbed_official | 170:bfbcaf535cca | 65 | // The RTC Clock may vary due to LSI frequency dispersion. |
mbed_official | 170:bfbcaf535cca | 66 | RCC_LSEConfig(RCC_LSE_OFF); |
mbed_official | 170:bfbcaf535cca | 67 | RCC_LSICmd(ENABLE); // Enable LSI |
mbed_official | 170:bfbcaf535cca | 68 | while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready |
mbed_official | 170:bfbcaf535cca | 69 | RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select the RTC Clock Source |
mbed_official | 170:bfbcaf535cca | 70 | rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture |
mbed_official | 170:bfbcaf535cca | 71 | } |
mbed_official | 170:bfbcaf535cca | 72 | else { |
mbed_official | 170:bfbcaf535cca | 73 | // The LSE has correctly started |
mbed_official | 170:bfbcaf535cca | 74 | RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source |
mbed_official | 170:bfbcaf535cca | 75 | rtc_freq = LSE_VALUE; |
mbed_official | 170:bfbcaf535cca | 76 | } |
mbed_official | 170:bfbcaf535cca | 77 | |
mbed_official | 170:bfbcaf535cca | 78 | RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock |
mbed_official | 170:bfbcaf535cca | 79 | |
mbed_official | 125:23cc3068a9e4 | 80 | RTC_WaitForSynchro(); // Wait for RTC registers synchronization |
mbed_official | 125:23cc3068a9e4 | 81 | |
mbed_official | 125:23cc3068a9e4 | 82 | RTC_InitTypeDef RTC_InitStructure; |
mbed_official | 125:23cc3068a9e4 | 83 | RTC_InitStructure.RTC_AsynchPrediv = 127; |
mbed_official | 170:bfbcaf535cca | 84 | RTC_InitStructure.RTC_SynchPrediv = (rtc_freq / 128) - 1; |
mbed_official | 125:23cc3068a9e4 | 85 | RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; |
mbed_official | 125:23cc3068a9e4 | 86 | RTC_Init(&RTC_InitStructure); |
mbed_official | 170:bfbcaf535cca | 87 | |
mbed_official | 170:bfbcaf535cca | 88 | PWR_BackupAccessCmd(DISABLE); // Disable access to Backup domain |
mbed_official | 170:bfbcaf535cca | 89 | |
mbed_official | 125:23cc3068a9e4 | 90 | rtc_inited = 1; |
mbed_official | 125:23cc3068a9e4 | 91 | } |
mbed_official | 125:23cc3068a9e4 | 92 | |
mbed_official | 125:23cc3068a9e4 | 93 | void rtc_free(void) { |
mbed_official | 125:23cc3068a9e4 | 94 | RCC_DeInit(); // Resets the RCC clock configuration to the default reset state |
mbed_official | 125:23cc3068a9e4 | 95 | rtc_inited = 0; |
mbed_official | 125:23cc3068a9e4 | 96 | } |
mbed_official | 125:23cc3068a9e4 | 97 | |
mbed_official | 125:23cc3068a9e4 | 98 | int rtc_isenabled(void) { |
mbed_official | 125:23cc3068a9e4 | 99 | return rtc_inited; |
mbed_official | 125:23cc3068a9e4 | 100 | } |
mbed_official | 125:23cc3068a9e4 | 101 | |
mbed_official | 125:23cc3068a9e4 | 102 | /* |
mbed_official | 125:23cc3068a9e4 | 103 | RTC Registers |
mbed_official | 125:23cc3068a9e4 | 104 | RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
mbed_official | 125:23cc3068a9e4 | 105 | RTC_Month 1=january, 2=february, ..., 12=december |
mbed_official | 125:23cc3068a9e4 | 106 | RTC_Date day of the month 1-31 |
mbed_official | 125:23cc3068a9e4 | 107 | RTC_Year year 0-99 |
mbed_official | 125:23cc3068a9e4 | 108 | struct tm |
mbed_official | 125:23cc3068a9e4 | 109 | tm_sec seconds after the minute 0-61 |
mbed_official | 125:23cc3068a9e4 | 110 | tm_min minutes after the hour 0-59 |
mbed_official | 125:23cc3068a9e4 | 111 | tm_hour hours since midnight 0-23 |
mbed_official | 125:23cc3068a9e4 | 112 | tm_mday day of the month 1-31 |
mbed_official | 125:23cc3068a9e4 | 113 | tm_mon months since January 0-11 |
mbed_official | 125:23cc3068a9e4 | 114 | tm_year years since 1900 |
mbed_official | 125:23cc3068a9e4 | 115 | tm_wday days since Sunday 0-6 |
mbed_official | 125:23cc3068a9e4 | 116 | tm_yday days since January 1 0-365 |
mbed_official | 125:23cc3068a9e4 | 117 | tm_isdst Daylight Saving Time flag |
mbed_official | 125:23cc3068a9e4 | 118 | */ |
mbed_official | 125:23cc3068a9e4 | 119 | time_t rtc_read(void) { |
mbed_official | 125:23cc3068a9e4 | 120 | RTC_DateTypeDef dateStruct; |
mbed_official | 125:23cc3068a9e4 | 121 | RTC_TimeTypeDef timeStruct; |
mbed_official | 125:23cc3068a9e4 | 122 | struct tm timeinfo; |
mbed_official | 135:067cc8ba23da | 123 | |
mbed_official | 125:23cc3068a9e4 | 124 | // Read actual date and time |
mbed_official | 125:23cc3068a9e4 | 125 | RTC_GetTime(RTC_Format_BIN, &timeStruct); |
mbed_official | 125:23cc3068a9e4 | 126 | RTC_GetDate(RTC_Format_BIN, &dateStruct); |
mbed_official | 135:067cc8ba23da | 127 | |
mbed_official | 125:23cc3068a9e4 | 128 | // Setup a tm structure based on the RTC |
mbed_official | 125:23cc3068a9e4 | 129 | timeinfo.tm_wday = dateStruct.RTC_WeekDay; |
mbed_official | 125:23cc3068a9e4 | 130 | timeinfo.tm_mon = dateStruct.RTC_Month - 1; |
mbed_official | 125:23cc3068a9e4 | 131 | timeinfo.tm_mday = dateStruct.RTC_Date; |
mbed_official | 125:23cc3068a9e4 | 132 | timeinfo.tm_year = dateStruct.RTC_Year + 100; |
mbed_official | 125:23cc3068a9e4 | 133 | timeinfo.tm_hour = timeStruct.RTC_Hours; |
mbed_official | 125:23cc3068a9e4 | 134 | timeinfo.tm_min = timeStruct.RTC_Minutes; |
mbed_official | 125:23cc3068a9e4 | 135 | timeinfo.tm_sec = timeStruct.RTC_Seconds; |
mbed_official | 135:067cc8ba23da | 136 | |
mbed_official | 125:23cc3068a9e4 | 137 | // Convert to timestamp |
mbed_official | 125:23cc3068a9e4 | 138 | time_t t = mktime(&timeinfo); |
mbed_official | 135:067cc8ba23da | 139 | |
mbed_official | 135:067cc8ba23da | 140 | return t; |
mbed_official | 125:23cc3068a9e4 | 141 | } |
mbed_official | 125:23cc3068a9e4 | 142 | |
mbed_official | 125:23cc3068a9e4 | 143 | void rtc_write(time_t t) { |
mbed_official | 125:23cc3068a9e4 | 144 | RTC_DateTypeDef dateStruct; |
mbed_official | 125:23cc3068a9e4 | 145 | RTC_TimeTypeDef timeStruct; |
mbed_official | 125:23cc3068a9e4 | 146 | |
mbed_official | 125:23cc3068a9e4 | 147 | // Convert the time into a tm |
mbed_official | 125:23cc3068a9e4 | 148 | struct tm *timeinfo = localtime(&t); |
mbed_official | 135:067cc8ba23da | 149 | |
mbed_official | 125:23cc3068a9e4 | 150 | // Fill RTC structures |
mbed_official | 125:23cc3068a9e4 | 151 | dateStruct.RTC_WeekDay = timeinfo->tm_wday; |
mbed_official | 125:23cc3068a9e4 | 152 | dateStruct.RTC_Month = timeinfo->tm_mon + 1; |
mbed_official | 125:23cc3068a9e4 | 153 | dateStruct.RTC_Date = timeinfo->tm_mday; |
mbed_official | 125:23cc3068a9e4 | 154 | dateStruct.RTC_Year = timeinfo->tm_year - 100; |
mbed_official | 125:23cc3068a9e4 | 155 | timeStruct.RTC_Hours = timeinfo->tm_hour; |
mbed_official | 125:23cc3068a9e4 | 156 | timeStruct.RTC_Minutes = timeinfo->tm_min; |
mbed_official | 125:23cc3068a9e4 | 157 | timeStruct.RTC_Seconds = timeinfo->tm_sec; |
mbed_official | 125:23cc3068a9e4 | 158 | timeStruct.RTC_H12 = RTC_HourFormat_24; |
mbed_official | 135:067cc8ba23da | 159 | |
mbed_official | 125:23cc3068a9e4 | 160 | // Change the RTC current date/time |
mbed_official | 135:067cc8ba23da | 161 | PWR_BackupAccessCmd(ENABLE); // Enable access to RTC |
mbed_official | 125:23cc3068a9e4 | 162 | RTC_SetDate(RTC_Format_BIN, &dateStruct); |
mbed_official | 135:067cc8ba23da | 163 | RTC_SetTime(RTC_Format_BIN, &timeStruct); |
mbed_official | 125:23cc3068a9e4 | 164 | PWR_BackupAccessCmd(DISABLE); // Disable access to RTC |
mbed_official | 125:23cc3068a9e4 | 165 | } |
mbed_official | 162:937d965048d3 | 166 | |
mbed_official | 162:937d965048d3 | 167 | #endif |