Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_hal_wwdg.c
blob7807ff05728e4b2bfb1c543a455b282c2861599c
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_hal_wwdg.c
4 * @author MCD Application Team
5 * @brief WWDG HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Window Watchdog (WWDG) peripheral:
8 * + Initialization and Configuration functions
9 * + IO operation functions
10 @verbatim
11 ==============================================================================
12 ##### WWDG Specific features #####
13 ==============================================================================
14 [..]
15 Once enabled the WWDG generates a system reset on expiry of a programmed
16 time period, unless the program refreshes the counter (T[6;0] downcounter)
17 before reaching 0x3F value (i.e. a reset is generated when the counter
18 value rolls down from 0x40 to 0x3F).
20 (+) An MCU reset is also generated if the counter value is refreshed
21 before the counter has reached the refresh window value. This
22 implies that the counter must be refreshed in a limited window.
23 (+) Once enabled the WWDG cannot be disabled except by a system reset.
24 (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG
25 reset occurs.
26 (+) The WWDG counter input clock is derived from the APB clock divided
27 by a programmable prescaler.
28 (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
29 (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz)
30 where T[5;0] are the lowest 6 bits of Counter.
31 (+) WWDG Counter refresh is allowed between the following limits :
32 (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
33 (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
34 (+) Typical values:
35 (++) Counter min (T[5;0] = 0x00) @56 MHz(PCLK1) with zero prescaler:
36 max timeout before reset: ~73.14 µs
37 (++) Counter max (T[5;0] = 0x3F) @56 MHz(PCLK1) with prescaler dividing by 128:
38 max timeout before reset: ~599.18 ms
40 ==============================================================================
41 ##### How to use this driver #####
42 ==============================================================================
43 [..]
44 *** Common driver usage ***
45 ===========================
46 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
47 (+) Set the WWDG prescaler, refresh window and counter value
48 using HAL_WWDG_Init() function.
49 (+) Start the WWDG using HAL_WWDG_Start() function.
50 When the WWDG is enabled the counter value should be configured to
51 a value greater than 0x40 to prevent generating an immediate reset.
52 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
53 generated when the counter reaches 0x40, and then start the WWDG using
54 HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
55 add his own code by customization of callback HAL_WWDG_WakeupCallback.
56 Once enabled, EWI interrupt cannot be disabled except by a system reset.
57 (+) Then the application program must refresh the WWDG counter at regular
58 intervals during normal operation to prevent an MCU reset, using
59 HAL_WWDG_Refresh() function. This operation must occur only when
60 the counter is lower than the refresh window value already programmed.
62 [..]
63 *** Callback registration ***
64 =============================
65 The compilation define USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
66 the user to configure dynamically the driver callbacks. Use Functions
67 @ref HAL_WWDG_RegisterCallback() to register a user callback.
69 (+) Function @ref HAL_WWDG_RegisterCallback() allows to register following
70 callbacks:
71 (++) EwiCallback : callback for Early WakeUp Interrupt.
72 (++) MspInitCallback : WWDG MspInit.
73 This function takes as parameters the HAL peripheral handle, the Callback ID
74 and a pointer to the user callback function.
76 (+) Use function @ref HAL_WWDG_UnRegisterCallback() to reset a callback to
77 the default weak (surcharged) function. @ref HAL_WWDG_UnRegisterCallback()
78 takes as parameters the HAL peripheral handle and the Callback ID.
79 This function allows to reset following callbacks:
80 (++) EwiCallback : callback for Early WakeUp Interrupt.
81 (++) MspInitCallback : WWDG MspInit.
83 When calling @ref HAL_WWDG_Init function, callbacks are reset to the
84 corresponding legacy weak (surcharged) functions:
85 @ref HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
86 not been registered before.
88 When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
89 not defined, the callback registering feature is not available
90 and weak (surcharged) callbacks are used.
92 *** WWDG HAL driver macros list ***
93 ===================================
94 [..]
95 Below the list of most used macros in WWDG HAL driver.
96 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
97 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
98 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
99 (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
101 @endverbatim
102 ******************************************************************************
103 * @attention
105 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
106 * All rights reserved.</center></h2>
108 * This software component is licensed by ST under BSD 3-Clause license,
109 * the "License"; You may not use this file except in compliance with the
110 * License. You may obtain a copy of the License at:
111 * opensource.org/licenses/BSD-3-Clause
113 ******************************************************************************
116 /* Includes ------------------------------------------------------------------*/
117 #include "stm32g4xx_hal.h"
119 /** @addtogroup STM32G4xx_HAL_Driver
120 * @{
123 #ifdef HAL_WWDG_MODULE_ENABLED
124 /** @defgroup WWDG WWDG
125 * @brief WWDG HAL module driver.
126 * @{
129 /* Private typedef -----------------------------------------------------------*/
130 /* Private define ------------------------------------------------------------*/
131 /* Private macro -------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 /* Exported functions --------------------------------------------------------*/
136 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
137 * @{
140 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
141 * @brief Initialization and Configuration functions.
143 @verbatim
144 ==============================================================================
145 ##### Initialization and Configuration functions #####
146 ==============================================================================
147 [..]
148 This section provides functions allowing to:
149 (+) Initialize and start the WWDG according to the specified parameters
150 in the WWDG_InitTypeDef of associated handle.
151 (+) Initialize the WWDG MSP.
153 @endverbatim
154 * @{
158 * @brief Initialize the WWDG according to the specified.
159 * parameters in the WWDG_InitTypeDef of associated handle.
160 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
161 * the configuration information for the specified WWDG module.
162 * @retval HAL status
164 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
166 /* Check the WWDG handle allocation */
167 if (hwwdg == NULL)
169 return HAL_ERROR;
172 /* Check the parameters */
173 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
174 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
175 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
176 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
177 assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
179 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
180 /* Reset Callback pointers */
181 if(hwwdg->EwiCallback == NULL)
183 hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
186 if(hwwdg->MspInitCallback == NULL)
188 hwwdg->MspInitCallback = HAL_WWDG_MspInit;
191 /* Init the low level hardware */
192 hwwdg->MspInitCallback(hwwdg);
193 #else
194 /* Init the low level hardware */
195 HAL_WWDG_MspInit(hwwdg);
196 #endif
198 /* Set WWDG Counter */
199 WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
201 /* Set WWDG Prescaler and Window */
202 WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
204 /* Return function status */
205 return HAL_OK;
210 * @brief Initialize the WWDG MSP.
211 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
212 * the configuration information for the specified WWDG module.
213 * @note When rewriting this function in user file, mechanism may be added
214 * to avoid multiple initialize when HAL_WWDG_Init function is called
215 * again to change parameters.
216 * @retval None
218 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
220 /* Prevent unused argument(s) compilation warning */
221 UNUSED(hwwdg);
223 /* NOTE: This function should not be modified, when the callback is needed,
224 the HAL_WWDG_MspInit could be implemented in the user file
229 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
231 * @brief Register a User WWDG Callback
232 * To be used instead of the weak (surcharged) predefined callback
233 * @param hwwdg WWDG handle
234 * @param CallbackID ID of the callback to be registered
235 * This parameter can be one of the following values:
236 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
237 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
238 * @param pCallback pointer to the Callback function
239 * @retval status
241 HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID, pWWDG_CallbackTypeDef pCallback)
243 HAL_StatusTypeDef status = HAL_OK;
245 if(pCallback == NULL)
247 status = HAL_ERROR;
249 else
251 switch(CallbackID)
253 case HAL_WWDG_EWI_CB_ID:
254 hwwdg->EwiCallback = pCallback;
255 break;
257 case HAL_WWDG_MSPINIT_CB_ID:
258 hwwdg->MspInitCallback = pCallback;
259 break;
261 default:
262 status = HAL_ERROR;
263 break;
267 return status;
272 * @brief Unregister a WWDG Callback
273 * WWDG Callback is redirected to the weak (surcharged) predefined callback
274 * @param hwwdg WWDG handle
275 * @param CallbackID ID of the callback to be registered
276 * This parameter can be one of the following values:
277 * @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
278 * @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
279 * @retval status
281 HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
283 HAL_StatusTypeDef status = HAL_OK;
285 switch(CallbackID)
287 case HAL_WWDG_EWI_CB_ID:
288 hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
289 break;
291 case HAL_WWDG_MSPINIT_CB_ID:
292 hwwdg->MspInitCallback = HAL_WWDG_MspInit;
293 break;
295 default:
296 status = HAL_ERROR;
297 break;
300 return status;
302 #endif
305 * @}
308 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
309 * @brief IO operation functions
311 @verbatim
312 ==============================================================================
313 ##### IO operation functions #####
314 ==============================================================================
315 [..]
316 This section provides functions allowing to:
317 (+) Refresh the WWDG.
318 (+) Handle WWDG interrupt request and associated function callback.
320 @endverbatim
321 * @{
325 * @brief Refresh the WWDG.
326 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
327 * the configuration information for the specified WWDG module.
328 * @retval HAL status
330 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
332 /* Write to WWDG CR the WWDG Counter value to refresh with */
333 WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
335 /* Return function status */
336 return HAL_OK;
340 * @brief Handle WWDG interrupt request.
341 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
342 * or data logging must be performed before the actual reset is generated.
343 * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
344 * EWIMode set to WWDG_EWI_ENABLE.
345 * When the downcounter reaches the value 0x40, and EWI interrupt is
346 * generated and the corresponding Interrupt Service Routine (ISR) can
347 * be used to trigger specific actions (such as communications or data
348 * logging), before resetting the device.
349 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
350 * the configuration information for the specified WWDG module.
351 * @retval None
353 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
355 /* Check if Early Wakeup Interrupt is enable */
356 if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
358 /* Check if WWDG Early Wakeup Interrupt occurred */
359 if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
361 /* Clear the WWDG Early Wakeup flag */
362 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
364 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
365 /* Early Wakeup registered callback */
366 hwwdg->EwiCallback(hwwdg);
367 #else
368 /* Early Wakeup callback */
369 HAL_WWDG_EarlyWakeupCallback(hwwdg);
370 #endif
377 * @brief WWDG Early Wakeup callback.
378 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
379 * the configuration information for the specified WWDG module.
380 * @retval None
382 __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
384 /* Prevent unused argument(s) compilation warning */
385 UNUSED(hwwdg);
387 /* NOTE: This function should not be modified, when the callback is needed,
388 the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
393 * @}
397 * @}
400 #endif /* HAL_WWDG_MODULE_ENABLED */
402 * @}
406 * @}
409 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/