1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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) 2017 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 'stm32h7xx_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 stm32h7xx_hal_conf.h
34
35 [..]
36 (@) HAL RTC alarm and HAL RTC wakeup drivers can not 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 stm32h7xx_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 "stm32h7xx_hal.h"
49 /** @addtogroup STM32H7xx_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 #ifdef RTC_CLOCK_SOURCE_HSE
72 #define RTC_ASYNCH_PREDIV 99U
73 #define RTC_SYNCH_PREDIV 9U
74 #define RCC_RTCCLKSOURCE_1MHZ ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 12U)))
75 #else /* RTC_CLOCK_SOURCE_LSE || RTC_CLOCK_SOURCE_LSI */
76 #define RTC_ASYNCH_PREDIV 0U
77 #define RTC_SYNCH_PREDIV 31U
78 #endif /* RTC_CLOCK_SOURCE_HSE */
79
80 /* Private macro -------------------------------------------------------------*/
81 /* Private variables ---------------------------------------------------------*/
82 static RTC_HandleTypeDef hRTC_Handle;
83 /* Private function prototypes -----------------------------------------------*/
84 void RTC_Alarm_IRQHandler(void);
85 /* Private functions ---------------------------------------------------------*/
86
87 /**
88 * @brief This function configures the RTC_ALARMA as a time base source.
89 * The time source is configured to have 1ms time base with a dedicated
90 * Tick interrupt priority.
91 * @note This function is called automatically at the beginning of program after
92 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
93 * @param TickPriority Tick interrupt priority.
94 * @retval HAL status
95 */
HAL_InitTick(uint32_t TickPriority)96 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
97 {
98 __IO uint32_t counter = 0U;
99
100 RCC_OscInitTypeDef RCC_OscInitStruct;
101 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
102 HAL_StatusTypeDef status;
103
104 #ifdef RTC_CLOCK_SOURCE_LSE
105 /* Configure LSE as RTC clock source */
106 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
107 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
108 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
109 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
110 #elif defined (RTC_CLOCK_SOURCE_LSI)
111 /* Configure LSI as RTC clock source */
112 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
113 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
114 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
115 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
116 #elif defined (RTC_CLOCK_SOURCE_HSE)
117 /* Configure HSE as RTC clock source */
118 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
119 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
120 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
121 /* Ensure that RTC is clocked by 1MHz */
122 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
123 #else
124 #error Please select the RTC Clock source
125 #endif /* RTC_CLOCK_SOURCE_LSE */
126
127 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
128 if (status == HAL_OK)
129 {
130 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
131 status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
132 }
133 if (status == HAL_OK)
134 {
135 /* Enable RTC Clock */
136 __HAL_RCC_RTC_ENABLE();
137 /* The time base should be 1ms
138 Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
139 HSE as RTC clock
140 Time base = ((99 + 1) * (9 + 1)) / 1MHz
141 = 1ms
142 LSE as RTC clock
143 Time base = ((31 + 1) * (0 + 1)) / 32.768KHz
144 = ~1ms
145 LSI as RTC clock
146 Time base = ((31 + 1) * (0 + 1)) / 32KHz
147 = 1ms
148 */
149 hRTC_Handle.Instance = RTC;
150 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
151 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
152 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
153 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
154 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
155 hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
156 status = HAL_RTC_Init(&hRTC_Handle);
157 }
158 if (status == HAL_OK)
159 {
160 /* Disable the write protection for RTC registers */
161 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
162
163 /* Disable the Alarm A interrupt */
164 __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
165
166 /* Clear flag alarm A */
167 __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
168
169 counter = 0U;
170 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
171 #if defined(RTC_ICSR_ALRAWF)
172 while ( READ_BIT(hRTC_Handle.Instance->ICSR, RTC_FLAG_ALRAWF) == (uint32_t)RESET)
173 #else
174 while (__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == (uint32_t)RESET)
175 #endif /* RTC_ICSR_ALRAWF */
176 {
177 if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
178 {
179 status = HAL_ERROR;
180 }
181 }
182 }
183 if (status == HAL_OK)
184 {
185 hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
186
187 /* Configure the Alarm state: Enable Alarm */
188 __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
189 /* Configure the Alarm interrupt */
190 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
191
192 /* RTC Alarm Interrupt Configuration: EXTI configuration */
193 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
194 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
195
196 /* Check if the Initialization mode is set */
197 #if defined(RTC_ISR_INITF)
198 if ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
199 #else
200 if ((hRTC_Handle.Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
201 #endif /* RTC_ISR_INITF */
202 {
203 /* Set the Initialization mode */
204 #if defined(RTC_ISR_INITF)
205 hRTC_Handle.Instance->ISR = (uint32_t)RTC_INIT_MASK;
206 #else
207 hRTC_Handle.Instance->ICSR = (uint32_t)RTC_INIT_MASK;
208 #endif /* RTC_ISR_INITF */
209 counter = 0U;
210 #if defined(RTC_ISR_INITF)
211 while ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
212 #else
213 while ((hRTC_Handle.Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
214 #endif /* RTC_ISR_INITF */
215 {
216 if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
217 {
218 status = HAL_ERROR;
219 }
220 }
221 }
222 }
223 if (status == HAL_OK)
224 {
225 hRTC_Handle.Instance->DR = 0U;
226 hRTC_Handle.Instance->TR = 0U;
227
228 #if defined(RTC_ISR_INIT)
229 hRTC_Handle.Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
230 #else
231 hRTC_Handle.Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
232 #endif /* RTC_ISR_INIT */
233
234 /* Enable the write protection for RTC registers */
235 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
236
237 /* Enable the RTC Alarm Interrupt */
238 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
239
240 /* Configure the SysTick IRQ priority */
241 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
242 {
243 HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority, 0U);
244 uwTickPrio = TickPriority;
245 }
246 else
247 {
248 status = HAL_ERROR;
249 }
250
251 }
252 return status;
253 }
254
255 /**
256 * @brief Suspend Tick increment.
257 * @note Disable the tick increment by disabling RTC ALARM interrupt.
258 * @retval None
259 */
HAL_SuspendTick(void)260 void HAL_SuspendTick(void)
261 {
262 /* Disable the write protection for RTC registers */
263 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
264 /* Disable RTC ALARM update Interrupt */
265 __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
266 /* Enable the write protection for RTC registers */
267 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
268 }
269
270 /**
271 * @brief Resume Tick increment.
272 * @note Enable the tick increment by Enabling RTC ALARM interrupt.
273 * @retval None
274 */
HAL_ResumeTick(void)275 void HAL_ResumeTick(void)
276 {
277 /* Disable the write protection for RTC registers */
278 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
279 /* Enable RTC ALARM Update interrupt */
280 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
281 /* Enable the write protection for RTC registers */
282 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
283 }
284
285 /**
286 * @brief ALARM A Event Callback in non blocking mode
287 * @note This function is called when RTC_ALARM interrupt took place, inside
288 * RTC_ALARM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
289 * a global variable "uwTick" used as application time base.
290 * @param hrtc RTC handle
291 * @retval None
292 */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)293 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
294 {
295 __IO uint32_t counter = 0U;
296
297 HAL_IncTick();
298
299 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
300
301 /* Set the Initialization mode */
302 #if defined(RTC_ISR_INIT)
303 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
304
305 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
306 #else
307 hrtc->Instance->ICSR = (uint32_t)RTC_INIT_MASK;
308
309 while((hrtc->Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
310 #endif /* RTC_ISR_INIT */
311 {
312 if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
313 {
314 break;
315 }
316 }
317
318 hrtc->Instance->DR = 0U;
319 hrtc->Instance->TR = 0U;
320 #if defined(RTC_ISR_INIT)
321 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
322 #else
323 hrtc->Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
324 #endif /* RTC_ISR_INIT */
325 /* Enable the write protection for RTC registers */
326 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
327 }
328
329 /**
330 * @brief This function handles RTC ALARM interrupt request.
331 * @retval None
332 */
RTC_Alarm_IRQHandler(void)333 void RTC_Alarm_IRQHandler(void)
334 {
335 HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
336 }
337
338 /**
339 * @}
340 */
341
342 /**
343 * @}
344 */
345
346