1 /**
2 ******************************************************************************
3 * @file stm32u0xx_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 * @attention
15 *
16 * Copyright (c) 2023 STMicroelectronics.
17 * All rights reserved.
18 *
19 * This software is licensed under terms that can be found in the LICENSE file
20 * in the root directory of this software component.
21 * If no LICENSE file comes with this software, it is provided AS-IS.
22 *
23 ******************************************************************************
24 @verbatim
25 ==============================================================================
26 ##### How to use this driver #####
27 ==============================================================================
28 [..]
29 This file must be copied to the application folder and modified as follows:
30 (#) Rename it to 'stm32u0xx_hal_timebase_rtc_alarm.c'
31 (#) Add this file and the RTC HAL drivers to your project and uncomment
32 HAL_RTC_MODULE_ENABLED define in stm32u0xx_hal_conf.h
33
34 [..]
35 (@) HAL RTC alarm and HAL RTC wakeup drivers cannot be used with low power modes:
36 The wake up capability of the RTC may be intrusive in case of prior low power mode
37 configuration requiring different wake up sources.
38 Application/Example behavior is no more guaranteed
39 (@) The stm32u0xx_hal_timebase_tim use is recommended for the Applications/Examples
40 requiring low power modes
41
42 @endverbatim
43 ******************************************************************************
44 */
45
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32u0xx_hal.h"
48 /** @addtogroup STM32U0xx_HAL_Driver
49 * @{
50 */
51
52 /** @defgroup HAL_TimeBase_RTC_Alarm_Template HAL TimeBase RTC Alarm Template
53 * @{
54 */
55
56 /* Private typedef -----------------------------------------------------------*/
57 /* Private define ------------------------------------------------------------*/
58
59 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
60 + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
61 + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
62 precision.
63 + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
64 precision.
65 */
66 #define RTC_CLOCK_SOURCE_HSE
67 /* #define RTC_CLOCK_SOURCE_LSE */
68 /* #define RTC_CLOCK_SOURCE_LSI */
69
70 #ifdef RTC_CLOCK_SOURCE_HSE
71 #define RTC_ASYNCH_PREDIV 49U
72 #define RTC_SYNCH_PREDIV 4U
73 #else /* RTC_CLOCK_SOURCE_LSE || RTC_CLOCK_SOURCE_LSI */
74 #define RTC_ASYNCH_PREDIV 0U
75 #define RTC_SYNCH_PREDIV 31U
76 #endif /* RTC_CLOCK_SOURCE_HSE */
77
78 /* Private macro -------------------------------------------------------------*/
79 /* Private variables ---------------------------------------------------------*/
80 extern RTC_HandleTypeDef hRTC_Handle;
81 RTC_HandleTypeDef hRTC_Handle;
82
83 /* Private function prototypes -----------------------------------------------*/
84 void RTC_TAMP_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
103 #ifdef RTC_CLOCK_SOURCE_LSE
104 /* Configure LSE as RTC clock source */
105 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
106 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
107 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
108 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
109 #elif defined (RTC_CLOCK_SOURCE_LSI)
110 /* Configure LSI as RTC clock source */
111 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
112 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
113 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
114 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
115 #elif defined (RTC_CLOCK_SOURCE_HSE)
116 /* Configure HSE as RTC clock source */
117 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
118 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
119 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
120 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
121 #else
122 #error Please select the RTC Clock source
123 #endif /* RTC_CLOCK_SOURCE_LSE */
124
125 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
126 {
127 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
128 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK)
129 {
130 /* Enable RTC Clock */
131 __HAL_RCC_RTC_ENABLE();
132 __HAL_RCC_RTCAPB_CLK_ENABLE();
133
134 /* The time base should be 1ms
135 Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
136 HSE as RTC clock
137 Time base = ((49 + 1) * (4 + 1)) / 250kHz
138 = 1ms
139 LSE as RTC clock
140 Time base = ((31 + 1) * (0 + 1)) / 32.768KHz
141 = ~1ms
142 LSI as RTC clock
143 Time base = ((31 + 1) * (0 + 1)) / 32KHz
144 = 1ms
145 */
146 hRTC_Handle.Instance = RTC;
147 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
148 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
149 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
150 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
151 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
152 hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
153 if (HAL_RTC_Init(&hRTC_Handle) != HAL_OK)
154 {
155 return HAL_ERROR;
156 }
157
158 /* Disable the write protection for RTC registers */
159 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
160
161 /* Disable the Alarm A interrupt */
162 __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
163
164 /* Clear flag alarm A */
165 __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
166
167 counter = 0U;
168 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
169 while (__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF) == 0U)
170 {
171 if (counter++ == (SystemCoreClock / 56U)) /* Timeout = ~ 1s */
172 {
173 return HAL_ERROR;
174 }
175 }
176
177 hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
178
179 /* Configure the Alarm state: Enable Alarm */
180 __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
181 /* Configure the Alarm interrupt */
182 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
183
184 /* RTC Alarm Interrupt Configuration: EXTI configuration */
185 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
186
187 /* Check if the Initialization mode is set */
188 if ((hRTC_Handle.Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
189 {
190 /* Set the Initialization mode */
191 hRTC_Handle.Instance->ICSR = (uint32_t)RTC_INIT_MASK;
192 counter = 0U;
193 while ((hRTC_Handle.Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
194 {
195 if (counter++ == (SystemCoreClock / 56U)) /* Timeout = ~ 1s */
196 {
197 return HAL_ERROR;
198 }
199 }
200 }
201 hRTC_Handle.Instance->DR = 0U;
202 hRTC_Handle.Instance->TR = 0U;
203
204 hRTC_Handle.Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
205
206 /* Enable the write protection for RTC registers */
207 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
208
209 HAL_NVIC_SetPriority(RTC_TAMP_IRQn, TickPriority, 0U);
210 HAL_NVIC_EnableIRQ(RTC_TAMP_IRQn);
211 return HAL_OK;
212 }
213 }
214 return HAL_ERROR;
215 }
216
217 /**
218 * @brief Suspend Tick increment.
219 * @note Disable the tick increment by disabling RTC ALARM interrupt.
220 * @retval None
221 */
HAL_SuspendTick(void)222 void HAL_SuspendTick(void)
223 {
224 /* Disable the write protection for RTC registers */
225 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
226 /* Disable RTC ALARM update Interrupt */
227 __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
228 /* Enable the write protection for RTC registers */
229 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
230 }
231
232 /**
233 * @brief Resume Tick increment.
234 * @note Enable the tick increment by Enabling RTC ALARM interrupt.
235 * @retval None
236 */
HAL_ResumeTick(void)237 void HAL_ResumeTick(void)
238 {
239 /* Disable the write protection for RTC registers */
240 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
241 /* Enable RTC ALARM Update interrupt */
242 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
243 /* Enable the write protection for RTC registers */
244 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
245 }
246
247 /**
248 * @brief ALARM A Event Callback in non blocking mode
249 * @note This function is called when RTC_ALARM interrupt took place, inside
250 * RTC_ALARM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
251 * a global variable "uwTick" used as application time base.
252 * @param hrtc : RTC handle
253 * @retval None
254 */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)255 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
256 {
257 __IO uint32_t counter = 0U;
258
259 HAL_IncTick();
260
261 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
262
263 /* Set the Initialization mode */
264 hrtc->Instance->ICSR = (uint32_t)RTC_INIT_MASK;
265
266 while ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
267 {
268 if (counter++ == (SystemCoreClock / 56U)) /* Timeout = ~ 1s */
269 {
270 break;
271 }
272 }
273
274 hrtc->Instance->DR = 0U;
275 hrtc->Instance->TR = 0U;
276
277 hrtc->Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
278
279 /* Enable the write protection for RTC registers */
280 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
281 }
282
283 /**
284 * @brief This function handles RTC ALARM interrupt request.
285 * @retval None
286 */
RTC_TAMP_IRQHandler(void)287 void RTC_TAMP_IRQHandler(void)
288 {
289 HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
290 }
291
292 /**
293 * @}
294 */
295
296 /**
297 * @}
298 */
299