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