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