2 ******************************************************************************
3 * @file stm32f1xx_hal_wwdg.c
4 * @author MCD Application Team
7 * @brief WWDG HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Window Watchdog (WWDG) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral State functions
14 ==============================================================================
15 ##### WWDG specific features #####
16 ==============================================================================
18 Once enabled the WWDG generates a system reset on expiry of a programmed
19 time period, unless the program refreshes the counter (downcounter)
20 before reaching 0x3F value (i.e. a reset is generated when the counter
21 value rolls over from 0x40 to 0x3F).
23 (+) An MCU reset is also generated if the counter value is refreshed
24 before the counter has reached the refresh window value. This
25 implies that the counter must be refreshed in a limited window.
26 (+) Once enabled the WWDG cannot be disabled except by a system reset.
27 (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
29 (+) The WWDG counter input clock is derived from the APB clock divided
30 by a programmable prescaler.
31 (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
32 (+) WWDG timeout (mS) = 1000 * Counter / WWDG clock
33 (+) WWDG Counter refresh is allowed between the following limits :
34 (++) min time (mS) = 1000 * (Counter _ Window) / WWDG clock
35 (++) max time (mS) = 1000 * (Counter _ 0x40) / WWDG clock
37 (+) Min-max timeout value at 36 MHz(PCLK1): 910 us / 58.25 ms
39 (+) The Early Wakeup Interrupt (EWI) can be used if specific safety
40 operations or data logging must be performed before the actual reset is
41 generated. When the downcounter reaches the value 0x40, an EWI interrupt
42 is generated and the corresponding interrupt service routine (ISR) can
43 be used to trigger specific actions (such as communications or data
44 logging), before resetting the device.
45 In some applications, the EWI interrupt can be used to manage a software
46 system check and/or system recovery/graceful degradation, without
47 generating a WWDG reset. In this case, the corresponding interrupt
48 service routine (ISR) should reload the WWDG counter to avoid the WWDG
49 reset, then trigger the required actions.
50 Note:When the EWI interrupt cannot be served, e.g. due to a system lock
51 in a higher priority task, the WWDG reset will eventually be generated.
53 (+) Debug mode : When the microcontroller enters debug mode (core halted),
54 the WWDG counter either continues to work normally or stops, depending
55 on DBG_WWDG_STOP configuration bit in DBG module, accessible through
56 __HAL_DBGMCU_FREEZE_WWDG() and __HAL_DBGMCU_UNFREEZE_WWDG() macros
58 ##### How to use this driver #####
59 ==============================================================================
61 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
63 (+) Set the WWDG prescaler, refresh window, counter value and Early Wakeup
64 Interrupt mode using using HAL_WWDG_Init() function.
65 This enables WWDG peripheral and the downcounter starts downcounting
66 from given counter value.
67 Init function can be called again to modify all watchdog parameters,
68 however if EWI mode has been set once, it can't be clear until next
71 (+) The application program must refresh the WWDG counter at regular
72 intervals during normal operation to prevent an MCU reset using
73 HAL_WWDG_Refresh() function. This operation must occur only when
74 the counter is lower than the window value already programmed.
76 (+) if Early Wakeup Interrupt mode is enable an interrupt is generated when
77 the counter reaches 0x40. User can add his own code in weak function
78 HAL_WWDG_EarlyWakeupCallback().
80 *** WWDG HAL driver macros list ***
81 ==================================
83 Below the list of most used macros in WWDG HAL driver.
85 (+) __HAL_WWDG_GET_IT_SOURCE: Check the selected WWDG's interrupt source.
86 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status.
87 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags.
90 ******************************************************************************
93 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
95 * Redistribution and use in source and binary forms, with or without modification,
96 * are permitted provided that the following conditions are met:
97 * 1. Redistributions of source code must retain the above copyright notice,
98 * this list of conditions and the following disclaimer.
99 * 2. Redistributions in binary form must reproduce the above copyright notice,
100 * this list of conditions and the following disclaimer in the documentation
101 * and/or other materials provided with the distribution.
102 * 3. Neither the name of STMicroelectronics nor the names of its contributors
103 * may be used to endorse or promote products derived from this software
104 * without specific prior written permission.
106 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
107 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
108 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
109 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
110 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
111 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
112 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
113 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
114 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
115 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
117 ******************************************************************************
120 /* Includes ------------------------------------------------------------------*/
121 #include "stm32f1xx_hal.h"
123 /** @addtogroup STM32F1xx_HAL_Driver
127 #ifdef HAL_WWDG_MODULE_ENABLED
128 /** @defgroup WWDG WWDG
129 * @brief WWDG HAL module driver.
133 /* Private typedef -----------------------------------------------------------*/
134 /* Private define ------------------------------------------------------------*/
135 /* Private macro -------------------------------------------------------------*/
136 /* Private variables ---------------------------------------------------------*/
137 /* Private function prototypes -----------------------------------------------*/
138 /* Exported functions --------------------------------------------------------*/
140 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
144 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
145 * @brief Initialization and Configuration functions.
148 ==============================================================================
149 ##### Initialization and Configuration functions #####
150 ==============================================================================
152 This section provides functions allowing to:
153 (+) Initialize and start the WWDG according to the specified parameters
154 in the WWDG_InitTypeDef of associated handle.
155 (+) Initialize the WWDG MSP.
162 * @brief Initialize the WWDG according to the specified.
163 * parameters in the WWDG_InitTypeDef of associated handle.
164 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
165 * the configuration information for the specified WWDG module.
168 HAL_StatusTypeDef
HAL_WWDG_Init(WWDG_HandleTypeDef
*hwwdg
)
170 /* Check the WWDG handle allocation */
176 /* Check the parameters */
177 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg
->Instance
));
178 assert_param(IS_WWDG_PRESCALER(hwwdg
->Init
.Prescaler
));
179 assert_param(IS_WWDG_WINDOW(hwwdg
->Init
.Window
));
180 assert_param(IS_WWDG_COUNTER(hwwdg
->Init
.Counter
));
181 assert_param(IS_WWDG_EWI_MODE(hwwdg
->Init
.EWIMode
));
183 /* Init the low level hardware */
184 HAL_WWDG_MspInit(hwwdg
);
186 /* Set WWDG Counter */
187 WRITE_REG(hwwdg
->Instance
->CR
, (WWDG_CR_WDGA
| hwwdg
->Init
.Counter
));
189 /* Set WWDG Prescaler and Window */
190 WRITE_REG(hwwdg
->Instance
->CFR
, (hwwdg
->Init
.EWIMode
| hwwdg
->Init
.Prescaler
| hwwdg
->Init
.Window
));
192 /* Return function status */
197 * @brief Initialize the WWDG MSP.
198 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
199 * the configuration information for the specified WWDG module.
200 * @note When rewriting this function in user file, mechanism may be added
201 * to avoid multiple initialize when HAL_WWDG_Init function is called
202 * again to change parameters.
205 __weak
void HAL_WWDG_MspInit(WWDG_HandleTypeDef
*hwwdg
)
207 /* Prevent unused argument(s) compilation warning */
210 /* NOTE: This function should not be modified, when the callback is needed,
211 the HAL_WWDG_MspInit could be implemented in the user file
219 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
220 * @brief IO operation functions
223 ==============================================================================
224 ##### IO operation functions #####
225 ==============================================================================
227 This section provides functions allowing to:
228 (+) Refresh the WWDG.
229 (+) Handle WWDG interrupt request and associated function callback.
236 * @brief Refresh the WWDG.
237 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
238 * the configuration information for the specified WWDG module.
241 HAL_StatusTypeDef
HAL_WWDG_Refresh(WWDG_HandleTypeDef
*hwwdg
)
243 /* Write to WWDG CR the WWDG Counter value to refresh with */
244 WRITE_REG(hwwdg
->Instance
->CR
, (hwwdg
->Init
.Counter
));
246 /* Return function status */
251 * @brief Handle WWDG interrupt request.
252 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
253 * or data logging must be performed before the actual reset is generated.
254 * The EWI interrupt is enabled by calling HAL_WWDG_Init function with
255 * EWIMode set to WWDG_EWI_ENABLE.
256 * When the downcounter reaches the value 0x40, and EWI interrupt is
257 * generated and the corresponding Interrupt Service Routine (ISR) can
258 * be used to trigger specific actions (such as communications or data
259 * logging), before resetting the device.
260 * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
261 * the configuration information for the specified WWDG module.
264 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef
*hwwdg
)
266 /* Check if Early Wakeup Interrupt is enable */
267 if(__HAL_WWDG_GET_IT_SOURCE(hwwdg
, WWDG_IT_EWI
) != RESET
)
269 /* Check if WWDG Early Wakeup Interrupt occurred */
270 if(__HAL_WWDG_GET_FLAG(hwwdg
, WWDG_FLAG_EWIF
) != RESET
)
272 /* Clear the WWDG Early Wakeup flag */
273 __HAL_WWDG_CLEAR_FLAG(hwwdg
, WWDG_FLAG_EWIF
);
275 /* Early Wakeup callback */
276 HAL_WWDG_EarlyWakeupCallback(hwwdg
);
282 * @brief WWDG Early Wakeup callback.
283 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
284 * the configuration information for the specified WWDG module.
287 __weak
void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef
* hwwdg
)
289 /* Prevent unused argument(s) compilation warning */
292 /* NOTE: This function should not be modified, when the callback is needed,
293 the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
305 #endif /* HAL_WWDG_MODULE_ENABLED */
314 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/