1 /**
2   ******************************************************************************
3   * @file    stm32mp1xx_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   ******************************************************************************
11   * @attention
12   *
13   * Copyright (c) 2019 STMicroelectronics.
14   * All rights reserved.
15   *
16   * This software is licensed under terms that can be found in the LICENSE file
17   * in the root directory of this software component.
18   * If no LICENSE file comes with this software, it is provided AS-IS.
19   *
20   ******************************************************************************
21   @verbatim
22   ==============================================================================
23                       ##### WWDG Specific features #####
24   ==============================================================================
25   [..]
26     Once enabled the WWDG generates a system reset on expiry of a programmed
27     time period, unless the program refreshes the counter (T[6;0] downcounter)
28     before reaching 0x3F value (i.e. a reset is generated when the counter
29     value rolls down from 0x40 to 0x3F).
30 
31     (+) An MCU reset is also generated if the counter value is refreshed
32         before the counter has reached the refresh window value. This
33         implies that the counter must be refreshed in a limited window.
34     (+) Once enabled the WWDG cannot be disabled except by a system reset.
35     (+) WWDGRST flag in RCC CSR register can be used to inform when a WWDG
36         reset occurs.
37     (+) The WWDG counter input clock is derived from the APB clock divided
38         by a programmable prescaler.
39     (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
40     (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock (Hz)
41         where T[5;0] are the lowest 6 bits of Counter.
42     (+) WWDG Counter refresh is allowed between the following limits :
43         (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
44         (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
45     (+) Typical values:
46         (++) Counter min (T[5;0] = 0x00) @56MHz (PCLK1) with zero prescaler:
47              max timeout before reset: ~73.14�s
48         (++) Counter max (T[5;0] = 0x3F) @56MHz (PCLK1) with prescaler dividing by 128:
49              max timeout before reset: ~599.18ms
50 
51   ==============================================================================
52                      ##### How to use this driver #####
53   ==============================================================================
54   [..]
55     *** Common driver usage ***
56     ===========================
57     (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
58     (+) Set the WWDG prescaler, refresh window and counter value
59         using HAL_WWDG_Init() function.
60     (+) Start the WWDG using HAL_WWDG_Start() function.
61         When the WWDG is enabled the counter value should be configured to
62         a value greater than 0x40 to prevent generating an immediate reset.
63     (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
64         generated when the counter reaches 0x40, and then start the WWDG using
65         HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
66         add his own code by customization of callback HAL_WWDG_WakeupCallback.
67         Once enabled, EWI interrupt cannot be disabled except by a system reset.
68     (+) Then the application program must refresh the WWDG counter at regular
69         intervals during normal operation to prevent an MCU reset, using
70         HAL_WWDG_Refresh() function. This operation must occur only when
71         the counter is lower than the refresh window value already programmed.
72 
73   [..]
74     *** Callback registration ***
75     =============================
76     The compilation define  USE_HAL_WWDG_REGISTER_CALLBACKS when set to 1 allows
77     the user to configure dynamically the driver callbacks. Use Functions
78     @ref HAL_WWDG_RegisterCallback() to register a user callback.
79 
80     (+) Function @ref HAL_WWDG_RegisterCallback() allows to register following
81         callbacks:
82         (++) EwiCallback : callback for Early WakeUp Interrupt.
83         (++) MspInitCallback : WWDG MspInit.
84     This function takes as parameters the HAL peripheral handle, the Callback ID
85     and a pointer to the user callback function.
86 
87     (+) Use function @ref HAL_WWDG_UnRegisterCallback() to reset a callback to
88     the default weak (surcharged) function. @ref HAL_WWDG_UnRegisterCallback()
89     takes as parameters the HAL peripheral handle and the Callback ID.
90     This function allows to reset following callbacks:
91         (++) EwiCallback : callback for  Early WakeUp Interrupt.
92         (++) MspInitCallback : WWDG MspInit.
93 
94     When calling @ref HAL_WWDG_Init function, callbacks are reset to the
95     corresponding legacy weak (surcharged) functions:
96     @ref HAL_WWDG_EarlyWakeupCallback() and HAL_WWDG_MspInit() only if they have
97     not been registered before.
98 
99     When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or
100     not defined, the callback registering feature is not available
101     and weak (surcharged) callbacks are used.
102 
103     *** WWDG HAL driver macros list ***
104     ===================================
105     [..]
106       Below the list of most used macros in WWDG HAL driver.
107       (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
108       (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
109       (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
110       (+) __HAL_WWDG_ENABLE_IT: Enable the WWDG early wakeup interrupt
111 
112   @endverbatim
113   ******************************************************************************
114   */
115 
116 /* Includes ------------------------------------------------------------------*/
117 #include "stm32mp1xx_hal.h"
118 
119 /** @addtogroup STM32MP1xx_HAL_Driver
120   * @{
121   */
122 
123 #ifdef HAL_WWDG_MODULE_ENABLED
124 /** @defgroup WWDG WWDG
125   * @brief WWDG HAL module driver.
126   * @{
127   */
128 
129 /* Private typedef -----------------------------------------------------------*/
130 /* Private define ------------------------------------------------------------*/
131 /* Private macro -------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 /* Exported functions --------------------------------------------------------*/
135 
136 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
137   * @{
138   */
139 
140 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
141   *  @brief    Initialization and Configuration functions.
142   *
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.
152 
153 @endverbatim
154   * @{
155   */
156 
157 /**
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
163   */
HAL_WWDG_Init(WWDG_HandleTypeDef * hwwdg)164 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
165 {
166   /* Check the WWDG handle allocation */
167   if (hwwdg == NULL)
168   {
169     return HAL_ERROR;
170   }
171 
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));
178 
179 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
180   /* Reset Callback pointers */
181   if (hwwdg->EwiCallback == NULL)
182   {
183     hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
184   }
185 
186   if (hwwdg->MspInitCallback == NULL)
187   {
188     hwwdg->MspInitCallback = HAL_WWDG_MspInit;
189   }
190 
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 /* USE_HAL_WWDG_REGISTER_CALLBACKS */
197 
198   /* Set WWDG Counter */
199   WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
200 
201   /* Set WWDG Prescaler and Window */
202   WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
203 
204   /* Return function status */
205   return HAL_OK;
206 }
207 
208 
209 /**
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
217   */
HAL_WWDG_MspInit(WWDG_HandleTypeDef * hwwdg)218 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
219 {
220   /* Prevent unused argument(s) compilation warning */
221   UNUSED(hwwdg);
222 
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
225    */
226 }
227 
228 
229 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
230 /**
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
240   */
HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef * hwwdg,HAL_WWDG_CallbackIDTypeDef CallbackID,pWWDG_CallbackTypeDef pCallback)241 HAL_StatusTypeDef HAL_WWDG_RegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID,
242                                             pWWDG_CallbackTypeDef pCallback)
243 {
244   HAL_StatusTypeDef status = HAL_OK;
245 
246   if (pCallback == NULL)
247   {
248     status = HAL_ERROR;
249   }
250   else
251   {
252     switch (CallbackID)
253     {
254       case HAL_WWDG_EWI_CB_ID:
255         hwwdg->EwiCallback = pCallback;
256         break;
257 
258       case HAL_WWDG_MSPINIT_CB_ID:
259         hwwdg->MspInitCallback = pCallback;
260         break;
261 
262       default:
263         status = HAL_ERROR;
264         break;
265     }
266   }
267 
268   return status;
269 }
270 
271 
272 /**
273   * @brief  Unregister a WWDG Callback
274   *         WWDG Callback is redirected to the weak (surcharged) predefined callback
275   * @param  hwwdg WWDG handle
276   * @param  CallbackID ID of the callback to be registered
277   *         This parameter can be one of the following values:
278   *           @arg @ref HAL_WWDG_EWI_CB_ID Early WakeUp Interrupt Callback ID
279   *           @arg @ref HAL_WWDG_MSPINIT_CB_ID MspInit callback ID
280   * @retval status
281   */
HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef * hwwdg,HAL_WWDG_CallbackIDTypeDef CallbackID)282 HAL_StatusTypeDef HAL_WWDG_UnRegisterCallback(WWDG_HandleTypeDef *hwwdg, HAL_WWDG_CallbackIDTypeDef CallbackID)
283 {
284   HAL_StatusTypeDef status = HAL_OK;
285 
286   switch (CallbackID)
287   {
288     case HAL_WWDG_EWI_CB_ID:
289       hwwdg->EwiCallback = HAL_WWDG_EarlyWakeupCallback;
290       break;
291 
292     case HAL_WWDG_MSPINIT_CB_ID:
293       hwwdg->MspInitCallback = HAL_WWDG_MspInit;
294       break;
295 
296     default:
297       status = HAL_ERROR;
298       break;
299   }
300 
301   return status;
302 }
303 #endif /* USE_HAL_WWDG_REGISTER_CALLBACKS */
304 
305 /**
306   * @}
307   */
308 
309 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
310   *  @brief    IO operation functions
311   *
312 @verbatim
313   ==============================================================================
314                       ##### IO operation functions #####
315   ==============================================================================
316   [..]
317     This section provides functions allowing to:
318     (+) Refresh the WWDG.
319     (+) Handle WWDG interrupt request and associated function callback.
320 
321 @endverbatim
322   * @{
323   */
324 
325 /**
326   * @brief  Refresh the WWDG.
327   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
328   *                the configuration information for the specified WWDG module.
329   * @retval HAL status
330   */
HAL_WWDG_Refresh(WWDG_HandleTypeDef * hwwdg)331 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
332 {
333   /* Write to WWDG CR the WWDG Counter value to refresh with */
334   WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
335 
336   /* Return function status */
337   return HAL_OK;
338 }
339 
340 /**
341   * @brief  Handle WWDG interrupt request.
342   * @note   The Early Wakeup Interrupt (EWI) can be used if specific safety operations
343   *         or data logging must be performed before the actual reset is generated.
344   *         The EWI interrupt is enabled by calling HAL_WWDG_Init function with
345   *         EWIMode set to WWDG_EWI_ENABLE.
346   *         When the downcounter reaches the value 0x40, and EWI interrupt is
347   *         generated and the corresponding Interrupt Service Routine (ISR) can
348   *         be used to trigger specific actions (such as communications or data
349   *         logging), before resetting the device.
350   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
351   *                the configuration information for the specified WWDG module.
352   * @retval None
353   */
HAL_WWDG_IRQHandler(WWDG_HandleTypeDef * hwwdg)354 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
355 {
356   /* Check if Early Wakeup Interrupt is enable */
357   if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
358   {
359     /* Check if WWDG Early Wakeup Interrupt occurred */
360     if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
361     {
362       /* Clear the WWDG Early Wakeup flag */
363       __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
364 
365 #if (USE_HAL_WWDG_REGISTER_CALLBACKS == 1)
366       /* Early Wakeup registered callback */
367       hwwdg->EwiCallback(hwwdg);
368 #else
369       /* Early Wakeup callback */
370       HAL_WWDG_EarlyWakeupCallback(hwwdg);
371 #endif /*USE_HAL_WWDG_REGISTER_CALLBACKS */
372     }
373   }
374 }
375 
376 
377 /**
378   * @brief  WWDG Early Wakeup callback.
379   * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
380   *                the configuration information for the specified WWDG module.
381   * @retval None
382   */
HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef * hwwdg)383 __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
384 {
385   /* Prevent unused argument(s) compilation warning */
386   UNUSED(hwwdg);
387 
388   /* NOTE: This function should not be modified, when the callback is needed,
389            the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
390    */
391 }
392 
393 /**
394   * @}
395   */
396 
397 /**
398   * @}
399   */
400 
401 #endif /* HAL_WWDG_MODULE_ENABLED */
402 /**
403   * @}
404   */
405 
406 /**
407   * @}
408   */
409