1 /**
2 ******************************************************************************
3 * @file stm32u5xx_hal_iwdg.c
4 * @author MCD Application Team
5 * @brief IWDG HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Independent Watchdog (IWDG) peripheral:
8 * + Initialization and Start functions
9 * + IO operation functions
10 *
11 ******************************************************************************
12 * @attention
13 *
14 * Copyright (c) 2021 STMicroelectronics.
15 * All rights reserved.
16 *
17 * This software is licensed under terms that can be found in the LICENSE file
18 * in the root directory of this software component.
19 * If no LICENSE file comes with this software, it is provided AS-IS.
20 *
21 ******************************************************************************
22 @verbatim
23 ==============================================================================
24 ##### IWDG Generic features #####
25 ==============================================================================
26 [..]
27 (+) The IWDG can be started by either software or hardware (configurable
28 through option byte).
29
30 (+) The IWDG is clocked by the Low-Speed Internal clock (LSI) and thus stays
31 active even if the main clock fails.
32
33 (+) Once the IWDG is started, the LSI is forced ON and both cannot be
34 disabled. The counter starts counting down from the reset value (0xFFF).
35 When it reaches the end of count value (0x000) a reset signal is
36 generated (IWDG reset).
37
38 (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
39 the IWDG_RLR value is reloaded into the counter and the watchdog reset
40 is prevented.
41
42 (+) The IWDG is implemented in the VDD voltage domain that is still functional
43 in STOP and STANDBY mode (IWDG reset can wake up the CPU from STANDBY).
44 IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
45 reset occurs.
46
47 (+) Debug mode: When the microcontroller enters debug mode (core halted),
48 the IWDG counter either continues to work normally or stops, depending
49 on DBG_IWDG_STOP configuration bit in DBG module, accessible through
50 __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros.
51
52 [..] Min-max timeout value @32KHz (LSI): ~125us / ~131.04s
53 The IWDG timeout may vary due to LSI clock frequency dispersion.
54 STM32U5xx devices provide the capability to measure the LSI clock
55 frequency (LSI clock is internally connected to TIM16 CH1 input capture).
56 The measured value can be used to have an IWDG timeout with an
57 acceptable accuracy.
58
59 [..] Default timeout value (necessary for IWDG_SR status register update):
60 Constant LSI_VALUE is defined based on the nominal LSI clock frequency.
61 This frequency being subject to variations as mentioned above, the
62 default timeout value (defined through constant HAL_IWDG_DEFAULT_TIMEOUT
63 below) may become too short or too long.
64 In such cases, this default timeout value can be tuned by redefining
65 the constant LSI_VALUE at user-application level (based, for instance,
66 on the measured LSI clock frequency as explained above).
67
68 ##### How to use this driver #####
69 ==============================================================================
70 [..]
71 (#) Register callback to treat Iwdg interrupt and MspInit using HAL_IWDG_RegisterCallback().
72 (++) Provide exiting handle as first parameter.
73 (++) Provide which callback will be registered using one value from
74 HAL_IWDG_CallbackIDTypeDef.
75 (++) Provide callback function pointer.
76
77 (#) Use IWDG using HAL_IWDG_Init() function to :
78 (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
79 clock is forced ON and IWDG counter starts counting down.
80 (++) Enable write access to configuration registers:
81 IWDG_PR, IWDG_RLR, IWDG_WINR, IWDG_EWCR.
82 (++) Configure the IWDG prescaler and counter reload value. This reload
83 value will be loaded in the IWDG counter each time the watchdog is
84 reloaded, then the IWDG will start counting down from this value.
85 (++) Depending on window parameter:
86 (+++) If Window Init parameter is same as Window register value,
87 nothing more is done but reload counter value in order to exit
88 function with exact time base.
89 (+++) Else modify Window register. This will automatically reload
90 watchdog counter.
91 (++) Depending on Early Wakeup Interrupt parameter:
92 (+++) If EWI is set to disable, comparator is set to 0, interrupt is
93 disable & flag is clear.
94 (+++) Else modify EWCR register, setting comparator value, enable
95 interrupt & clear flag.
96 (++) Wait for status flags to be reset.
97
98 (#) Then the application program must refresh the IWDG counter at regular
99 intervals during normal operation to prevent an MCU reset, using
100 HAL_IWDG_Refresh() function.
101
102 *** IWDG HAL driver macros list ***
103 ====================================
104 [..]
105 Below the list of most used macros in IWDG HAL driver:
106 (+) __HAL_IWDG_START: Enable the IWDG peripheral
107 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
108 the reload register
109
110 @endverbatim
111 */
112
113 /* Includes ------------------------------------------------------------------*/
114 #include "stm32u5xx_hal.h"
115
116 /** @addtogroup STM32U5xx_HAL_Driver
117 * @{
118 */
119
120 #ifdef HAL_IWDG_MODULE_ENABLED
121 /** @addtogroup IWDG
122 * @brief IWDG HAL module driver.
123 * @{
124 */
125
126 /* Private typedef -----------------------------------------------------------*/
127 /* Private define ------------------------------------------------------------*/
128 /** @defgroup IWDG_Private_Defines IWDG Private Defines
129 * @{
130 */
131 /* Status register needs up to 5 LSI clock periods to be updated. However a
132 synchronisation is added on prescaled LSI clock rising edge, so we only
133 consider a highest prescaler cycle.
134 The timeout value is calculated using the highest prescaler (1024) and
135 the LSI_VALUE constant. The value of this constant can be changed by the user
136 to take into account possible LSI clock period variations.
137 The timeout value is multiplied by 1000 to be converted in milliseconds.
138 LSI startup time is also considered here by adding LSI_STARTUP_TIME
139 converted in milliseconds. */
140 #define HAL_IWDG_DEFAULT_TIMEOUT (((1UL * 1024UL * 1000UL) / (LSI_VALUE / 128U)) + \
141 ((LSI_STARTUP_TIME / 1000UL) + 1UL))
142 #define IWDG_KERNEL_UPDATE_FLAGS (IWDG_SR_EWU | IWDG_SR_WVU | IWDG_SR_RVU | IWDG_SR_PVU)
143 /**
144 * @}
145 */
146
147 /* Private macro -------------------------------------------------------------*/
148 /* Private variables ---------------------------------------------------------*/
149 /* Private function prototypes -----------------------------------------------*/
150 /* Exported functions --------------------------------------------------------*/
151
152 /** @addtogroup IWDG_Exported_Functions
153 * @{
154 */
155
156 /** @addtogroup IWDG_Exported_Functions_Group1
157 * @brief Initialization and Start functions.
158 *
159 @verbatim
160 ===============================================================================
161 ##### Initialization and Start functions #####
162 ===============================================================================
163 [..] This section provides functions allowing to:
164 (+) Initialize the IWDG according to the specified parameters in the
165 IWDG_InitTypeDef of associated handle.
166 (+) Manage Window option.
167 (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
168 is reloaded in order to exit function with correct time base.
169
170 @endverbatim
171 * @{
172 */
173
174 /**
175 * @brief Initialize the IWDG according to the specified parameters in the
176 * IWDG_InitTypeDef and start watchdog. Before exiting function,
177 * watchdog is refreshed in order to have correct time base.
178 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
179 * the configuration information for the specified IWDG module.
180 * @retval HAL status
181 */
HAL_IWDG_Init(IWDG_HandleTypeDef * hiwdg)182 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
183 {
184 uint32_t tickstart;
185
186 /* Check the IWDG handle allocation */
187 if (hiwdg == NULL)
188 {
189 return HAL_ERROR;
190 }
191
192 /* Check the parameters */
193 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
194 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
195 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
196 assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
197 assert_param(IS_IWDG_EWI(hiwdg->Init.EWI));
198
199 #if (USE_HAL_IWDG_REGISTER_CALLBACKS == 1)
200 /* Reset Callback pointers */
201 if (hiwdg->EwiCallback == NULL)
202 {
203 hiwdg->EwiCallback = HAL_IWDG_EarlyWakeupCallback;
204 }
205 if (hiwdg->MspInitCallback == NULL)
206 {
207 hiwdg->MspInitCallback = HAL_IWDG_MspInit;
208 }
209
210 /* Init the low level hardware */
211 hiwdg->MspInitCallback(hiwdg);
212 #else
213 /* Init the low level hardware */
214 HAL_IWDG_MspInit(hiwdg);
215 #endif /* USE_HAL_IWDG_REGISTER_CALLBACKS */
216
217 /* Enable IWDG. LSI is turned on automatically */
218 __HAL_IWDG_START(hiwdg);
219
220 /* Enable write access to IWDG_PR, IWDG_RLR, IWDG_WINR, IWDG_EWCR registers by writing
221 0x5555 in KR */
222 IWDG_ENABLE_WRITE_ACCESS(hiwdg);
223
224 /* Write to IWDG registers the Prescaler & Reload values to work with */
225 hiwdg->Instance->PR = hiwdg->Init.Prescaler;
226 hiwdg->Instance->RLR = hiwdg->Init.Reload;
227
228 if (hiwdg->Init.EWI == IWDG_EWI_DISABLE)
229 {
230 /* EWI comparator value equal 0, disable the early wakeup interrupt
231 * acknowledge the early wakeup interrupt in any cases. it clears the EWIF flag in SR register
232 * Set Watchdog Early Wakeup Comparator to 0x00 */
233 hiwdg->Instance->EWCR = IWDG_EWCR_EWIC;
234 }
235 else
236 {
237 /* EWI comparator value different from 0, enable the early wakeup interrupt,
238 * acknowledge the early wakeup interrupt in any cases. it clears the EWIF flag in SR register
239 * Set Watchdog Early Wakeup Comparator value */
240 hiwdg->Instance->EWCR = IWDG_EWCR_EWIE | IWDG_EWCR_EWIC | hiwdg->Init.EWI;
241 }
242
243 /* Check pending flag, if previous update not done, return timeout */
244 tickstart = HAL_GetTick();
245
246 /* Wait for register to be updated */
247 while ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
248 {
249 if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
250 {
251 if ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
252 {
253 return HAL_TIMEOUT;
254 }
255 }
256 }
257
258 /* If window parameter is different than current value, modify window
259 register */
260 if (hiwdg->Instance->WINR != hiwdg->Init.Window)
261 {
262 /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
263 even if window feature is disabled, Watchdog will be reloaded by writing
264 windows register */
265 hiwdg->Instance->WINR = hiwdg->Init.Window;
266 }
267 else
268 {
269 /* Reload IWDG counter with value defined in the reload register */
270 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
271 }
272
273 /* Return function status */
274 return HAL_OK;
275 }
276
277
278 /**
279 * @brief Initialize the IWDG MSP.
280 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
281 * the configuration information for the specified IWDG module.
282 * @note When rewriting this function in user file, mechanism may be added
283 * to avoid multiple initialize when HAL_IWDG_Init function is called
284 * again to change parameters.
285 * @retval None
286 */
HAL_IWDG_MspInit(IWDG_HandleTypeDef * hiwdg)287 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
288 {
289 /* Prevent unused argument(s) compilation warning */
290 UNUSED(hiwdg);
291
292 /* NOTE: This function should not be modified, when the callback is needed,
293 the HAL_IWDG_MspInit could be implemented in the user file
294 */
295 }
296
297
298 #if (USE_HAL_IWDG_REGISTER_CALLBACKS == 1)
299 /**
300 * @brief Register a User IWDG Callback
301 * To be used instead of the weak (surcharged) predefined callback
302 * @param hiwdg IWDG handle
303 * @param CallbackID ID of the callback to be registered
304 * This parameter can be one of the following values:
305 * @arg @ref HAL_IWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
306 * @arg @ref HAL_IWDG_MSPINIT_CB_ID MspInit callback ID
307 * @param pCallback pointer to the Callback function
308 * @retval status
309 */
HAL_IWDG_RegisterCallback(IWDG_HandleTypeDef * hiwdg,HAL_IWDG_CallbackIDTypeDef CallbackID,pIWDG_CallbackTypeDef pCallback)310 HAL_StatusTypeDef HAL_IWDG_RegisterCallback(IWDG_HandleTypeDef *hiwdg, HAL_IWDG_CallbackIDTypeDef CallbackID,
311 pIWDG_CallbackTypeDef pCallback)
312 {
313 HAL_StatusTypeDef status = HAL_OK;
314
315 if (pCallback == NULL)
316 {
317 status = HAL_ERROR;
318 }
319 else
320 {
321 switch (CallbackID)
322 {
323 case HAL_IWDG_EWI_CB_ID:
324 hiwdg->EwiCallback = pCallback;
325 break;
326 case HAL_IWDG_MSPINIT_CB_ID:
327 hiwdg->MspInitCallback = pCallback;
328 break;
329
330 default:
331 status = HAL_ERROR;
332 break;
333 }
334 }
335
336 return status;
337 }
338
339
340 /**
341 * @brief Unregister a IWDG Callback
342 * IWDG Callback is redirected to the weak (surcharged) predefined callback
343 * @param hiwdg IWDG handle
344 * @param CallbackID ID of the callback to be registered
345 * This parameter can be one of the following values:
346 * @arg @ref HAL_IWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
347 * @arg @ref HAL_IWDG_MSPINIT_CB_ID MspInit callback ID
348 * @retval status
349 */
HAL_IWDG_UnRegisterCallback(IWDG_HandleTypeDef * hiwdg,HAL_IWDG_CallbackIDTypeDef CallbackID)350 HAL_StatusTypeDef HAL_IWDG_UnRegisterCallback(IWDG_HandleTypeDef *hiwdg, HAL_IWDG_CallbackIDTypeDef CallbackID)
351 {
352 HAL_StatusTypeDef status = HAL_OK;
353
354 switch (CallbackID)
355 {
356 case HAL_IWDG_EWI_CB_ID:
357 hiwdg->EwiCallback = HAL_IWDG_EarlyWakeupCallback;
358 break;
359 case HAL_IWDG_MSPINIT_CB_ID:
360 hiwdg->MspInitCallback = HAL_IWDG_MspInit;
361 break;
362
363 default:
364 status = HAL_ERROR;
365 break;
366 }
367
368 return status;
369 }
370 #endif /* USE_HAL_IWDG_REGISTER_CALLBACKS */
371
372
373 /**
374 * @}
375 */
376
377
378 /** @addtogroup IWDG_Exported_Functions_Group2
379 * @brief IO operation functions
380 *
381 @verbatim
382 ===============================================================================
383 ##### IO operation functions #####
384 ===============================================================================
385 [..] This section provides functions allowing to:
386 (+) Refresh the IWDG.
387
388 @endverbatim
389 * @{
390 */
391
392 /**
393 * @brief Refresh the IWDG.
394 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
395 * the configuration information for the specified IWDG module.
396 * @retval HAL status
397 */
HAL_IWDG_Refresh(IWDG_HandleTypeDef * hiwdg)398 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
399 {
400 /* Reload IWDG counter with value defined in the reload register */
401 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
402
403 /* Return function status */
404 return HAL_OK;
405 }
406
407
408 /**
409 * @brief Handle IWDG interrupt request.
410 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
411 * or data logging must be performed before the actual reset is generated.
412 * The EWI interrupt is enabled by calling HAL_IWDG_Init function with
413 * EWIMode set to IWDG_EWI_ENABLE.
414 * When the downcounter reaches the value 0x40, and EWI interrupt is
415 * generated and the corresponding Interrupt Service Routine (ISR) can
416 * be used to trigger specific actions (such as communications or data
417 * logging), before resetting the device.
418 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
419 * the configuration information for the specified IWDG module.
420 * @retval None
421 */
HAL_IWDG_IRQHandler(IWDG_HandleTypeDef * hiwdg)422 void HAL_IWDG_IRQHandler(IWDG_HandleTypeDef *hiwdg)
423 {
424 /* Check if IWDG Early Wakeup Interrupt occurred */
425 if ((hiwdg->Instance->SR & IWDG_SR_EWIF) != 0x00u)
426 {
427 /* Clear the IWDG Early Wakeup flag */
428 hiwdg->Instance->EWCR |= IWDG_EWCR_EWIC;
429
430 #if (USE_HAL_IWDG_REGISTER_CALLBACKS == 1)
431 /* Early Wakeup registered callback */
432 hiwdg->EwiCallback(hiwdg);
433 #else
434 /* Early Wakeup callback */
435 HAL_IWDG_EarlyWakeupCallback(hiwdg);
436 #endif /* USE_HAL_IWDG_REGISTER_CALLBACKS */
437 }
438 }
439
440
441 /**
442 * @brief IWDG Early Wakeup callback.
443 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
444 * the configuration information for the specified IWDG module.
445 * @retval None
446 */
HAL_IWDG_EarlyWakeupCallback(IWDG_HandleTypeDef * hiwdg)447 __weak void HAL_IWDG_EarlyWakeupCallback(IWDG_HandleTypeDef *hiwdg)
448 {
449 /* Prevent unused argument(s) compilation warning */
450 UNUSED(hiwdg);
451
452 /* NOTE: This function should not be modified, when the callback is needed,
453 the HAL_IWDG_EarlyWakeupCallback could be implemented in the user file
454 */
455 }
456
457
458 /**
459 * @}
460 */
461
462 /**
463 * @}
464 */
465
466 #endif /* HAL_IWDG_MODULE_ENABLED */
467 /**
468 * @}
469 */
470
471 /**
472 * @}
473 */
474