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