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