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