1 /**
2 ******************************************************************************
3 * @file stm32wb0x_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) 2024 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 'stm32wb0x_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 boardname_hal_conf.h
33
34 [..]
35 (@) HAL RTC alarm and HAL RTC wakeup drivers can't 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 stm32wb0x_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 "stm32wb0x_hal.h"
48
49 /** @addtogroup STM32WB0x_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_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_LSE
67 /* #define RTC_CLOCK_SOURCE_LSI */
68
69 #define RTC_ASYNCH_PREDIV 0U
70 #define RTC_SYNCH_PREDIV 31U
71
72 /* Private macro -------------------------------------------------------------*/
73 /* Private variables ---------------------------------------------------------*/
74 extern RTC_HandleTypeDef hRTC_Handle;
75 RTC_HandleTypeDef hRTC_Handle;
76
77 /* Private function prototypes -----------------------------------------------*/
78 void RTC_IRQHandler(void);
79 /* Private functions ---------------------------------------------------------*/
80
81 /**
82 * @brief This function configures the RTC_ALARMA as a time base source.
83 * The time source is configured to have 1ms time base with a dedicated
84 * Tick interrupt priority.
85 * @note This function is called automatically at the beginning of program after
86 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
87 * @param TickPriority: Tick interrupt priority.
88 * @retval HAL status
89 */
HAL_InitTick(uint32_t TickPriority)90 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
91 {
92 __IO uint32_t counter = 0U;
93
94 RCC_OscInitTypeDef RCC_OscInitStruct;
95
96 #ifdef RTC_CLOCK_SOURCE_LSE
97 /* Configure LSE as RTC clock source */
98 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
99 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
100 #elif defined (RTC_CLOCK_SOURCE_LSI)
101 /* Configure LSI as RTC clock source */
102 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
103 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
104 #else
105 #error Please select the RTC Clock source
106 #endif /* RTC_CLOCK_SOURCE_LSE */
107
108 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
109 {
110 /* Enable RTC Clock */
111 __HAL_RCC_RTC_CLK_ENABLE();
112 /* The time base should be 1ms
113 Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
114 HSE as RTC clock
115 Time base = ((99 + 1) * (9 + 1)) / 1MHz
116 = 1ms
117 LSE as RTC clock
118 Time base = ((31 + 1) * (0 + 1)) / 32.768KHz
119 = ~1ms
120 LSI as RTC clock
121 Time base = ((31 + 1) * (0 + 1)) / 32KHz
122 = 1ms
123 */
124 hRTC_Handle.Instance = RTC;
125 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
126 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
127 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
128 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
129 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
130 if (HAL_RTC_Init(&hRTC_Handle) != HAL_OK)
131 {
132 return HAL_ERROR;
133 }
134
135 /* Disable the write protection for RTC registers */
136 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
137
138 /* Disable the Alarm A interrupt */
139 __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
140
141 /* Clear flag alarm A */
142 __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
143
144 counter = 0U;
145 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
146 while (__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == 0U)
147 {
148 if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
149 {
150 return HAL_ERROR;
151 }
152 }
153
154 hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
155
156 /* Configure the Alarm state: Enable Alarm */
157 __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
158 /* Configure the Alarm interrupt */
159 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
160
161 /* Check if the Initialization mode is set */
162 if ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
163 {
164 /* Set the Initialization mode */
165 hRTC_Handle.Instance->ISR = (uint32_t)RTC_INIT_MASK;
166 counter = 0U;
167 while ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
168 {
169 if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
170 {
171 return HAL_ERROR;
172 }
173 }
174 }
175 hRTC_Handle.Instance->DR = 0U;
176 hRTC_Handle.Instance->TR = 0U;
177
178 hRTC_Handle.Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
179
180 /* Enable the write protection for RTC registers */
181 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
182
183 HAL_NVIC_SetPriority(RTC_IRQn, TickPriority, 0);
184 HAL_NVIC_EnableIRQ(RTC_IRQn);
185 return HAL_OK;
186 }
187 return HAL_ERROR;
188 }
189
190 /**
191 * @brief Suspend Tick increment.
192 * @note Disable the tick increment by disabling RTC ALARM interrupt.
193 * @retval None
194 */
HAL_SuspendTick(void)195 void HAL_SuspendTick(void)
196 {
197 /* Disable the write protection for RTC registers */
198 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
199 /* Disable RTC ALARM update Interrupt */
200 __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
201 /* Enable the write protection for RTC registers */
202 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
203 }
204
205 /**
206 * @brief Resume Tick increment.
207 * @note Enable the tick increment by Enabling RTC ALARM interrupt.
208 * @retval None
209 */
HAL_ResumeTick(void)210 void HAL_ResumeTick(void)
211 {
212 /* Disable the write protection for RTC registers */
213 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
214 /* Enable RTC ALARM Update interrupt */
215 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
216 /* Enable the write protection for RTC registers */
217 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
218 }
219
220 /**
221 * @brief ALARM A Event Callback in non blocking mode
222 * @note This function is called when RTC_ALARM interrupt took place, inside
223 * RTC_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
224 * a global variable "uwTick" used as application time base.
225 * @param hrtc : RTC handle
226 * @retval None
227 */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)228 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
229 {
230 __IO uint32_t counter = 0U;
231
232 HAL_IncTick();
233
234 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
235
236 /* Set the Initialization mode */
237 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
238
239 while ((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
240 {
241 if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
242 {
243 break;
244 }
245 }
246
247 hrtc->Instance->DR = 0U;
248 hrtc->Instance->TR = 0U;
249
250 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
251
252 /* Enable the write protection for RTC registers */
253 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
254 }
255
256 /**
257 * @brief This function handles RTC ALARM interrupt request.
258 * @retval None
259 */
RTC_IRQHandler(void)260 void RTC_IRQHandler(void)
261 {
262 HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
263 }
264
265 /**
266 * @}
267 */
268
269 /**
270 * @}
271 */
272