]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/rtc_api.c
Merge commit '28203e909e83b1ac6becb45a3eadae23b190df32' into master-core-pull
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F0 / 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 #if DEVICE_RTC
33
34 #include "mbed_error.h"
35
36 static int rtc_inited = 0;
37
38 static RTC_HandleTypeDef RtcHandle;
39
40 void rtc_init(void)
41 {
42 RCC_OscInitTypeDef RCC_OscInitStruct;
43 uint32_t rtc_freq = 0;
44
45 if (rtc_inited) return;
46 rtc_inited = 1;
47
48 RtcHandle.Instance = RTC;
49
50 // Enable Power clock
51 __PWR_CLK_ENABLE();
52
53 // Enable access to Backup domain
54 HAL_PWR_EnableBkUpAccess();
55
56 // Reset Backup domain
57 __HAL_RCC_BACKUPRESET_FORCE();
58 __HAL_RCC_BACKUPRESET_RELEASE();
59
60 // Enable LSE Oscillator
61 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
62 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
63 RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT
64 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
65 // Connect LSE to RTC
66 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
67 rtc_freq = LSE_VALUE;
68 } else {
69 // Enable LSI clock
70 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
71 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
72 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
73 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
74 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
75 error("RTC error: LSI clock initialization failed.");
76 }
77 // Connect LSI to RTC
78 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
79 // This value is LSI typical value. To be measured precisely using a timer input capture for example.
80 rtc_freq = LSI_VALUE;
81 }
82
83 // Enable RTC
84 __HAL_RCC_RTC_ENABLE();
85
86 RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
87 RtcHandle.Init.AsynchPrediv = 127;
88 RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1;
89 RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
90 RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
91 RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
92
93 if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
94 error("RTC error: RTC initialization failed.");
95 }
96 }
97
98 void rtc_free(void)
99 {
100 // Enable Power clock
101 __PWR_CLK_ENABLE();
102
103 // Enable access to Backup domain
104 HAL_PWR_EnableBkUpAccess();
105
106 // Reset Backup domain
107 __HAL_RCC_BACKUPRESET_FORCE();
108 __HAL_RCC_BACKUPRESET_RELEASE();
109
110 // Disable access to Backup domain
111 HAL_PWR_DisableBkUpAccess();
112
113 // Disable LSI and LSE clocks
114 RCC_OscInitTypeDef RCC_OscInitStruct;
115 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
116 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
117 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
118 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
119 HAL_RCC_OscConfig(&RCC_OscInitStruct);
120
121 rtc_inited = 0;
122 }
123
124 int rtc_isenabled(void)
125 {
126 return rtc_inited;
127 }
128
129 /*
130 RTC Registers
131 RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
132 RTC_Month 1=january, 2=february, ..., 12=december
133 RTC_Date day of the month 1-31
134 RTC_Year year 0-99
135 struct tm
136 tm_sec seconds after the minute 0-61
137 tm_min minutes after the hour 0-59
138 tm_hour hours since midnight 0-23
139 tm_mday day of the month 1-31
140 tm_mon months since January 0-11
141 tm_year years since 1900
142 tm_wday days since Sunday 0-6
143 tm_yday days since January 1 0-365
144 tm_isdst Daylight Saving Time flag
145 */
146 time_t rtc_read(void)
147 {
148 RTC_DateTypeDef dateStruct;
149 RTC_TimeTypeDef timeStruct;
150 struct tm timeinfo;
151
152 RtcHandle.Instance = RTC;
153
154 // Read actual date and time
155 // Warning: the time must be read first!
156 HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
157 HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
158
159 // Setup a tm structure based on the RTC
160 timeinfo.tm_wday = dateStruct.WeekDay;
161 timeinfo.tm_mon = dateStruct.Month - 1;
162 timeinfo.tm_mday = dateStruct.Date;
163 timeinfo.tm_year = dateStruct.Year + 100;
164 timeinfo.tm_hour = timeStruct.Hours;
165 timeinfo.tm_min = timeStruct.Minutes;
166 timeinfo.tm_sec = timeStruct.Seconds;
167
168 // Convert to timestamp
169 time_t t = mktime(&timeinfo);
170
171 return t;
172 }
173
174 void rtc_write(time_t t)
175 {
176 RTC_DateTypeDef dateStruct;
177 RTC_TimeTypeDef timeStruct;
178
179 RtcHandle.Instance = RTC;
180
181 // Convert the time into a tm
182 struct tm *timeinfo = localtime(&t);
183
184 // Fill RTC structures
185 dateStruct.WeekDay = timeinfo->tm_wday;
186 dateStruct.Month = timeinfo->tm_mon + 1;
187 dateStruct.Date = timeinfo->tm_mday;
188 dateStruct.Year = timeinfo->tm_year - 100;
189 timeStruct.Hours = timeinfo->tm_hour;
190 timeStruct.Minutes = timeinfo->tm_min;
191 timeStruct.Seconds = timeinfo->tm_sec;
192 timeStruct.TimeFormat = RTC_HOURFORMAT12_PM;
193 timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
194 timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
195
196 // Change the RTC current date/time
197 HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
198 HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
199 }
200
201 #endif
Imprint / Impressum