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