1 /**
2 ******************************************************************************
3 * @file stm32g0xx_hal_timebase_rtc_alarm_template.c
4 * @author MCD Application Team
5 * @brief HAL time base based on the hardware RTC_ALARM Template.
6 *
7 * This file override the native HAL time base functions (defined as weak)
8 * to use the RTC ALARM for time base generation:
9 * + Initializes the RTC peripheral to increment the seconds registers each 1ms
10 * + The alarm is configured to assert an interrupt when the RTC reaches 1ms
11 * + HAL_IncTick is called at each Alarm event and the time is reset to 00:00:00
12 * + HSE (default), LSE or LSI can be selected as RTC clock source
13 *
14 ******************************************************************************
15 * @attention
16 *
17 * Copyright (c) 2018 STMicroelectronics.
18 * All rights reserved.
19 *
20 * This software is licensed under terms that can be found in the LICENSE file
21 * in the root directory of this software component.
22 * If no LICENSE file comes with this software, it is provided AS-IS.
23 *
24 ******************************************************************************
25 @verbatim
26 ==============================================================================
27 ##### How to use this driver #####
28 ==============================================================================
29 [..]
30 This file must be copied to the application folder and modified as follows:
31 (#) Rename it to 'stm32g0xx_hal_timebase_rtc_alarm.c'
32 (#) Add this file and the RTC HAL drivers to your project and uncomment
33 HAL_RTC_MODULE_ENABLED define in stm32g0xx_hal_conf.h
34
35 [..]
36 (@) HAL RTC alarm and HAL RTC wakeup drivers can�t be used with low power modes:
37 The wake up capability of the RTC may be intrusive in case of prior low power mode
38 configuration requiring different wake up sources.
39 Application/Example behavior is no more guaranteed
40 (@) The stm32g0xx_hal_timebase_tim use is recommended for the Applications/Examples
41 requiring low power modes
42
43 @endverbatim
44 ******************************************************************************
45 */
46
47 /* Includes ------------------------------------------------------------------*/
48 #include "stm32g0xx_hal.h"
49 /** @addtogroup STM32G0xx_HAL_Driver
50 * @{
51 */
52
53 /** @defgroup HAL_TimeBase_RTC_Alarm_Template HAL TimeBase RTC Alarm Template
54 * @{
55 */
56
57 /* Private typedef -----------------------------------------------------------*/
58 /* Private define ------------------------------------------------------------*/
59
60 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
61 + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
62 + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
63 precision.
64 + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
65 precision.
66 */
67 /* #define RTC_CLOCK_SOURCE_HSE */
68 /* #define RTC_CLOCK_SOURCE_LSE */
69 #define RTC_CLOCK_SOURCE_LSI
70
71 /* Minimize Asynchronous prescaler for power consumption :
72 ck_apre = RTCCLK / (ASYNC prediv + 1)
73 ck_spre = ck_apre/(SYNC prediv + 1) = 1 Hz */
74 #if defined (RTC_CLOCK_SOURCE_LSE)
75 /* LSE Freq = 32.768 kHz RC */
76 #define RTC_ASYNCH_PREDIV 0u
77 #define RTC_SYNCH_PREDIV 0x3FFFu /* (16384 - 1) */
78 #elif defined (RTC_CLOCK_SOURCE_LSI)
79 /* LSI Freq = 32 kHz RC */
80 #define RTC_ASYNCH_PREDIV 0u
81 #define RTC_SYNCH_PREDIV 0x3E7Fu /* (16000 - 1) */
82 #elif defined (RTC_CLOCK_SOURCE_HSE)
83 /* HSE Freq as RTCCLK = 8 MHz / 32 = 250 kHz */
84 #define RTC_ASYNCH_PREDIV 0x07u /* (8 - 1) */
85 #define RTC_SYNCH_PREDIV 0x7A11u /* (31250 -1) */
86 #endif /* RTC_CLOCK_SOURCE_LSE */
87
88
89 /* Private macro -------------------------------------------------------------*/
90 /* Private variables ---------------------------------------------------------*/
91 RTC_HandleTypeDef hRTC_Handle = {.Init = {0}};
92
93 /* Private function prototypes -----------------------------------------------*/
94 void RTC_TAMP_IRQHandler(void);
95 /* Private functions ---------------------------------------------------------*/
96
97 /**
98 * @brief This function configures the RTC ALARM A as a time base source.
99 * The time source is configured to have 1ms time base with a dedicated
100 * Tick interrupt priority.
101 * Calendar time base is = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
102 * = 1s
103 * Alarm interrupt timebase is = (RTC_SYNCH_PREDIV / (1000 / uwTickFreq))
104 * = 1 ms when uwTickFreq is set to 1 kHz
105 * @note This function is called automatically at the beginning of program after
106 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
107 * @param TickPriority Tick interrupt priority.
108 * @retval HAL status
109 */
HAL_InitTick(uint32_t TickPriority)110 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
111 {
112 HAL_StatusTypeDef status = HAL_OK;
113 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
114 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
115 RTC_TimeTypeDef time;
116 RTC_DateTypeDef date;
117 RTC_AlarmTypeDef alarm;
118
119
120 /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero)*/
121 if ((uint32_t)uwTickFreq != 0U)
122 {
123 /* Disable backup domeain protection */
124 HAL_PWR_EnableBkUpAccess();
125
126 /* Enable RTC APB clock gating */
127 __HAL_RCC_RTCAPB_CLK_ENABLE();
128
129 /* Disable the Alarm A */
130 __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
131 /* In case of interrupt mode is used, the interrupt source must disabled */
132 __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
133 __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
134
135 /* Get RTC clock configuration */
136 HAL_RCCEx_GetPeriphCLKConfig(&PeriphClkInitStruct);
137
138 /*In case of RTC clock already enable, make sure it's the good one */
139 #if defined (RTC_CLOCK_SOURCE_LSE)
140 if ((PeriphClkInitStruct.RTCClockSelection == RCC_RTCCLKSOURCE_LSE) && (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != 0x00u))
141 #elif defined (RTC_CLOCK_SOURCE_LSI)
142 if ((PeriphClkInitStruct.RTCClockSelection == RCC_RTCCLKSOURCE_LSI) && (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != 0x00u))
143 #elif defined (RTC_CLOCK_SOURCE_HSE)
144 if ((PeriphClkInitStruct.RTCClockSelection == RCC_RTCCLKSOURCE_HSE_DIV32) && (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0x00u))
145 #else
146 #error Please select the RTC Clock source
147 #endif /* RTC_CLOCK_SOURCE_LSE */
148 {
149 /* Do nothing */
150 }
151 else
152 {
153 #ifdef RTC_CLOCK_SOURCE_LSE
154 /* Configure LSE as RTC clock source */
155 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
156 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
157 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
158 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
159 #elif defined (RTC_CLOCK_SOURCE_LSI)
160 /* Configure LSI as RTC clock source */
161 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
162 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
163 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
164 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
165 #elif defined (RTC_CLOCK_SOURCE_HSE)
166 /* Configure HSE as RTC clock source */
167 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
168 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
169 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
170 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
171 #endif /* RTC_CLOCK_SOURCE_LSE */
172
173 /* Configure oscillator */
174 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
175 if (status == HAL_OK)
176 {
177 /* Configure RTC clock source */
178 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
179 status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
180
181 /* Enable RTC Clock */
182 if(status == HAL_OK)
183 {
184 __HAL_RCC_RTC_ENABLE();
185 }
186 }
187 }
188
189 /* If RTC Clock configuration is ok */
190 if (status == HAL_OK)
191 {
192 /* The time base is defined to have highest synchronous prescaler but keeping
193 a 1Hz RTC frequency. */
194 hRTC_Handle.Instance = RTC;
195 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
196 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
197 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
198 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
199 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
200 hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
201 status = HAL_RTC_Init(&hRTC_Handle);
202 }
203
204 /* HAL RTC Init is ok & calendar has never been initialized */
205 if((status == HAL_OK) && (__HAL_RTC_GET_FLAG(&hRTC_Handle, RTC_FLAG_INITS) == 0x00u))
206 {
207 time.Hours = 0x00u;
208 time.Minutes = 0x00u;
209 time.Seconds = 0x00u;
210 time.TimeFormat = RTC_HOURFORMAT12_PM;
211 time.SubSeconds = 0x00u;
212 time.SecondFraction = 0x00u;
213 time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
214 time.StoreOperation = RTC_STOREOPERATION_RESET;
215 status = HAL_RTC_SetTime(&hRTC_Handle, &time, RTC_FORMAT_BCD);
216 if(status == HAL_OK)
217 {
218 date.WeekDay = RTC_WEEKDAY_MONDAY;
219 date.Date = 0x01u;
220 date.Month = RTC_MONTH_JANUARY;
221 date.Year = 0x01u;
222 status = HAL_RTC_SetDate(&hRTC_Handle, &date, RTC_FORMAT_BCD);
223 }
224 }
225
226 /* If RTC calendar is initialized */
227 if (status == HAL_OK)
228 {
229 alarm.AlarmTime.Hours = 0x00u;
230 alarm.AlarmTime.Minutes = 0x00u;
231 alarm.AlarmTime.Seconds = 0x00u;
232 alarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_PM;
233 alarm.AlarmTime.SubSeconds = (RTC_SYNCH_PREDIV / (1000 / (uint32_t)uwTickFreq));
234 alarm.AlarmTime.SecondFraction = 0x00u;
235 alarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
236 alarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
237 alarm.AlarmMask = RTC_ALARMMASK_ALL;
238
239 /* Depending on input frequency select Subsecond mask */
240 if (uwTickFreq == HAL_TICK_FREQ_1KHZ)
241 {
242 alarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_SS14_5;
243 }
244 else if (uwTickFreq == HAL_TICK_FREQ_100HZ)
245 {
246 alarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_SS14_9;
247 }
248 else
249 {
250 #if defined (RTC_CLOCK_SOURCE_HSE)
251 /* When RTCCLK = 1 MHz, need to mask Subsecond register bit 12 to 14
252 to have 10 Hhz interrupt */
253 alarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_SS14_12;
254 #else
255 /* When RTCCLK is around 32 kHz, need to mask Subsecond register bit 12 to 11
256 to have 10 Hhz interrupt */
257 alarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_SS14_11;
258 #endif /* RTC_CLOCK_SOURCE_HSE */
259 }
260 alarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
261 alarm.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
262 alarm.Alarm = RTC_ALARM_A;
263 status = HAL_RTC_SetAlarm_IT(&hRTC_Handle, &alarm, RTC_FORMAT_BCD);
264 if(status == HAL_OK)
265 {
266 /* Enable the RTC global Interrupt */
267 HAL_NVIC_EnableIRQ(RTC_TAMP_IRQn);
268
269 /* Configure the SysTick IRQ priority */
270 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
271 {
272 HAL_NVIC_SetPriority(RTC_TAMP_IRQn, TickPriority, 0U);
273 uwTickPrio = TickPriority;
274 }
275 else
276 {
277 status = HAL_ERROR;
278 }
279 }
280 }
281 }
282 else
283 {
284 status = HAL_ERROR;
285 }
286 return status;
287 }
288
289 /**
290 * @brief Suspend Tick increment.
291 * @note Disable the tick increment by disabling RTC_ALRA interrupt.
292 * @retval None
293 */
HAL_SuspendTick(void)294 void HAL_SuspendTick(void)
295 {
296 /* Disable the write protection for RTC registers */
297 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
298 /* Disable ALARM A Interrupt */
299 __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
300 /* Enable the write protection for RTC registers */
301 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
302 }
303
304 /**
305 * @brief Resume Tick increment.
306 * @note Enable the tick increment by Enabling RTC ALARM interrupt.
307 * @retval None
308 */
HAL_ResumeTick(void)309 void HAL_ResumeTick(void)
310 {
311 /* Disable the write protection for RTC registers */
312 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
313 /* Enable ALARM A interrupt */
314 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
315 /* Enable the write protection for RTC registers */
316 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
317 }
318
319 /**
320 * @brief Alarm Timer Event Callback in non blocking mode
321 * @note This function is called when RTC Alarm takes place, inside
322 * HAL_RTC_AlarmIRQHandler(). It makes a direct call to HAL_IncTick() to increment
323 * a global variable "uwTick" used as application time base.
324 * @param hrtc RTC handle
325 * @retval None
326 */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)327 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
328 {
329 HAL_IncTick();
330 }
331
332 /**
333 * @brief This function handles RTC ALARM interrupt request.
334 * @retval None
335 */
RTC_TAMP_IRQHandler(void)336 void RTC_TAMP_IRQHandler(void)
337 {
338 HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
339 }
340
341 /**
342 * @}
343 */
344
345 /**
346 * @}
347 */
348
349