1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, STMicroelectronics
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************
34 #include "mbed_error.h"
36 static int rtc_inited
= 0;
38 static RTC_HandleTypeDef RtcHandle
;
42 RCC_OscInitTypeDef RCC_OscInitStruct
;
43 uint32_t rtc_freq
= 0;
45 if (rtc_inited
) return;
48 RtcHandle
.Instance
= RTC
;
53 // Enable access to Backup domain
54 HAL_PWR_EnableBkUpAccess();
56 // Reset Backup domain
57 __HAL_RCC_BACKUPRESET_FORCE();
58 __HAL_RCC_BACKUPRESET_RELEASE();
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
) {
66 __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE
);
67 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE
);
71 RCC_OscInitStruct
.OscillatorType
= RCC_OSCILLATORTYPE_LSI
| RCC_OSCILLATORTYPE_LSE
;
72 RCC_OscInitStruct
.PLL
.PLLState
= RCC_PLL_NONE
; // Mandatory, otherwise the PLL is reconfigured!
73 RCC_OscInitStruct
.LSEState
= RCC_LSE_OFF
;
74 RCC_OscInitStruct
.LSIState
= RCC_LSI_ON
;
75 if (HAL_RCC_OscConfig(&RCC_OscInitStruct
) != HAL_OK
) {
76 error("RTC error: LSI clock initialization failed.");
79 __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI
);
80 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI
);
81 // This value is LSI typical value. To be measured precisely using a timer input capture for example.
86 __HAL_RCC_RTC_ENABLE();
88 RtcHandle
.Init
.HourFormat
= RTC_HOURFORMAT_24
;
89 #ifdef TARGET_MOTE_L152RC
90 /* SubSecond resolution of 16384Hz */
91 RtcHandle
.Init
.AsynchPrediv
= 1;
92 RtcHandle
.Init
.SynchPrediv
= (rtc_freq
/ 2) - 1;
94 RtcHandle
.Init
.AsynchPrediv
= 127;
95 RtcHandle
.Init
.SynchPrediv
= (rtc_freq
/ 128) - 1;
97 RtcHandle
.Init
.OutPut
= RTC_OUTPUT_DISABLE
;
98 RtcHandle
.Init
.OutPutPolarity
= RTC_OUTPUT_POLARITY_HIGH
;
99 RtcHandle
.Init
.OutPutType
= RTC_OUTPUT_TYPE_OPENDRAIN
;
101 if (HAL_RTC_Init(&RtcHandle
) != HAL_OK
) {
102 error("RTC error: RTC initialization failed.");
108 // Enable Power clock
111 // Enable access to Backup domain
112 HAL_PWR_EnableBkUpAccess();
114 // Reset Backup domain
115 __HAL_RCC_BACKUPRESET_FORCE();
116 __HAL_RCC_BACKUPRESET_RELEASE();
118 // Disable access to Backup domain
119 HAL_PWR_DisableBkUpAccess();
121 // Disable LSI and LSE clocks
122 RCC_OscInitTypeDef RCC_OscInitStruct
;
123 RCC_OscInitStruct
.OscillatorType
= RCC_OSCILLATORTYPE_LSI
| RCC_OSCILLATORTYPE_LSE
;
124 RCC_OscInitStruct
.PLL
.PLLState
= RCC_PLL_NONE
;
125 RCC_OscInitStruct
.LSIState
= RCC_LSI_OFF
;
126 RCC_OscInitStruct
.LSEState
= RCC_LSE_OFF
;
127 HAL_RCC_OscConfig(&RCC_OscInitStruct
);
132 int rtc_isenabled(void)
139 RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
140 RTC_Month 1=january, 2=february, ..., 12=december
141 RTC_Date day of the month 1-31
144 tm_sec seconds after the minute 0-61
145 tm_min minutes after the hour 0-59
146 tm_hour hours since midnight 0-23
147 tm_mday day of the month 1-31
148 tm_mon months since January 0-11
149 tm_year years since 1900
150 tm_wday days since Sunday 0-6
151 tm_yday days since January 1 0-365
152 tm_isdst Daylight Saving Time flag
154 time_t rtc_read(void)
156 RTC_DateTypeDef dateStruct
;
157 RTC_TimeTypeDef timeStruct
;
160 RtcHandle
.Instance
= RTC
;
162 // Read actual date and time
163 // Warning: the time must be read first!
164 HAL_RTC_GetTime(&RtcHandle
, &timeStruct
, FORMAT_BIN
);
165 HAL_RTC_GetDate(&RtcHandle
, &dateStruct
, FORMAT_BIN
);
167 // Setup a tm structure based on the RTC
168 timeinfo
.tm_wday
= dateStruct
.WeekDay
;
169 timeinfo
.tm_mon
= dateStruct
.Month
- 1;
170 timeinfo
.tm_mday
= dateStruct
.Date
;
171 timeinfo
.tm_year
= dateStruct
.Year
+ 100;
172 timeinfo
.tm_hour
= timeStruct
.Hours
;
173 timeinfo
.tm_min
= timeStruct
.Minutes
;
174 timeinfo
.tm_sec
= timeStruct
.Seconds
;
176 // Convert to timestamp
177 time_t t
= mktime(&timeinfo
);
182 void rtc_write(time_t t
)
184 RTC_DateTypeDef dateStruct
;
185 RTC_TimeTypeDef timeStruct
;
187 RtcHandle
.Instance
= RTC
;
189 // Convert the time into a tm
190 struct tm
*timeinfo
= localtime(&t
);
192 // Fill RTC structures
193 dateStruct
.WeekDay
= timeinfo
->tm_wday
;
194 dateStruct
.Month
= timeinfo
->tm_mon
+ 1;
195 dateStruct
.Date
= timeinfo
->tm_mday
;
196 dateStruct
.Year
= timeinfo
->tm_year
- 100;
197 timeStruct
.Hours
= timeinfo
->tm_hour
;
198 timeStruct
.Minutes
= timeinfo
->tm_min
;
199 timeStruct
.Seconds
= timeinfo
->tm_sec
;
200 timeStruct
.TimeFormat
= RTC_HOURFORMAT12_PM
;
201 timeStruct
.DayLightSaving
= RTC_DAYLIGHTSAVING_NONE
;
202 timeStruct
.StoreOperation
= RTC_STOREOPERATION_RESET
;
204 // Change the RTC current date/time
205 HAL_RTC_SetDate(&RtcHandle
, &dateStruct
, FORMAT_BIN
);
206 HAL_RTC_SetTime(&RtcHandle
, &timeStruct
, FORMAT_BIN
);