提交 | 用户 | age
|
bfc108
|
1 |
/**
|
Q |
2 |
******************************************************************************
|
|
3 |
* @file stm32f0xx_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 function
|
|
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 over from 0x40 to 0x3F).
|
|
19 |
|
|
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 |
|
|
24 |
(+) Once enabled the WWDG cannot be disabled except by a system reset.
|
|
25 |
|
|
26 |
(+) WWDGRST flag in RCC_CSR register informs when a WWDG reset has
|
|
27 |
occurred (check available with __HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)).
|
|
28 |
|
|
29 |
(+) The WWDG downcounter input clock is derived from the APB clock divided
|
|
30 |
by a programmable prescaler.
|
|
31 |
|
|
32 |
(+) WWDG downcounter clock (Hz) = PCLK / (4096 * Prescaler)
|
|
33 |
|
|
34 |
(+) WWDG timeout (ms) = (1000 * (T[5;0] + 1)) / (WWDG downcounter clock)
|
|
35 |
where T[5;0] are the lowest 6 bits of downcounter.
|
|
36 |
|
|
37 |
(+) WWDG Counter refresh is allowed between the following limits :
|
|
38 |
(++) min time (ms) = (1000 * (T[5;0] - Window)) / (WWDG downcounter clock)
|
|
39 |
(++) max time (ms) = (1000 * (T[5;0] - 0x40)) / (WWDG downcounter clock)
|
|
40 |
|
|
41 |
(+) Min-max timeout value @48 MHz(PCLK): ~85,3us / ~5,46 ms
|
|
42 |
|
|
43 |
(+) The Early Wakeup Interrupt (EWI) can be used if specific safety
|
|
44 |
operations or data logging must be performed before the actual reset is
|
|
45 |
generated. When the downcounter reaches the value 0x40, an EWI interrupt
|
|
46 |
is generated and the corresponding interrupt service routine (ISR) can
|
|
47 |
be used to trigger specific actions (such as communications or data
|
|
48 |
logging), before resetting the device.
|
|
49 |
In some applications, the EWI interrupt can be used to manage a software
|
|
50 |
system check and/or system recovery/graceful degradation, without
|
|
51 |
generating a WWDG reset. In this case, the corresponding interrupt
|
|
52 |
service routine (ISR) should reload the WWDG counter to avoid the WWDG
|
|
53 |
reset, then trigger the required actions.
|
|
54 |
Note:When the EWI interrupt cannot be served, e.g. due to a system lock
|
|
55 |
in a higher priority task, the WWDG reset will eventually be generated.
|
|
56 |
|
|
57 |
(+) Debug mode : When the microcontroller enters debug mode (core halted),
|
|
58 |
the WWDG counter either continues to work normally or stops, depending
|
|
59 |
on DBG_WWDG_STOP configuration bit in DBG module, accessible through
|
|
60 |
__HAL_DBGMCU_FREEZE_WWDG() and __HAL_DBGMCU_UNFREEZE_WWDG() macros
|
|
61 |
|
|
62 |
##### How to use this driver #####
|
|
63 |
==============================================================================
|
|
64 |
[..]
|
|
65 |
(+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
|
|
66 |
|
|
67 |
(+) Set the WWDG prescaler, refresh window, counter value and Early Wakeup
|
|
68 |
Interrupt mode using using HAL_WWDG_Init() function.
|
|
69 |
This enables WWDG peripheral and the downcounter starts downcounting
|
|
70 |
from given counter value.
|
|
71 |
Init function can be called again to modify all watchdog parameters,
|
|
72 |
however if EWI mode has been set once, it can't be clear until next
|
|
73 |
reset.
|
|
74 |
|
|
75 |
(+) The application program must refresh the WWDG counter at regular
|
|
76 |
intervals during normal operation to prevent an MCU reset using
|
|
77 |
HAL_WWDG_Refresh() function. This operation must occur only when
|
|
78 |
the counter is lower than the window value already programmed.
|
|
79 |
|
|
80 |
(+) if Early Wakeup Interrupt mode is enable an interrupt is generated when
|
|
81 |
the counter reaches 0x40. User can add his own code in weak function
|
|
82 |
HAL_WWDG_EarlyWakeupCallback().
|
|
83 |
|
|
84 |
*** WWDG HAL driver macros list ***
|
|
85 |
==================================
|
|
86 |
[..]
|
|
87 |
Below the list of most used macros in WWDG HAL driver.
|
|
88 |
|
|
89 |
(+) __HAL_WWDG_GET_IT_SOURCE: Check the selected WWDG's interrupt source.
|
|
90 |
(+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status.
|
|
91 |
(+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags.
|
|
92 |
|
|
93 |
@endverbatim
|
|
94 |
******************************************************************************
|
|
95 |
* @attention
|
|
96 |
*
|
|
97 |
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
|
|
98 |
*
|
|
99 |
* Redistribution and use in source and binary forms, with or without modification,
|
|
100 |
* are permitted provided that the following conditions are met:
|
|
101 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
102 |
* this list of conditions and the following disclaimer.
|
|
103 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
104 |
* this list of conditions and the following disclaimer in the documentation
|
|
105 |
* and/or other materials provided with the distribution.
|
|
106 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
107 |
* may be used to endorse or promote products derived from this software
|
|
108 |
* without specific prior written permission.
|
|
109 |
*
|
|
110 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
111 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
112 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
113 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
114 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
115 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
116 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
117 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
118 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
119 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
120 |
*
|
|
121 |
******************************************************************************
|
|
122 |
*/
|
|
123 |
|
|
124 |
/* Includes ------------------------------------------------------------------*/
|
|
125 |
#include "stm32f0xx_hal.h"
|
|
126 |
|
|
127 |
/** @addtogroup STM32F0xx_HAL_Driver
|
|
128 |
* @{
|
|
129 |
*/
|
|
130 |
|
|
131 |
#ifdef HAL_WWDG_MODULE_ENABLED
|
|
132 |
/** @defgroup WWDG WWDG
|
|
133 |
* @brief WWDG HAL module driver.
|
|
134 |
* @{
|
|
135 |
*/
|
|
136 |
|
|
137 |
/* Private typedef -----------------------------------------------------------*/
|
|
138 |
/* Private define ------------------------------------------------------------*/
|
|
139 |
/* Private macro -------------------------------------------------------------*/
|
|
140 |
/* Private variables ---------------------------------------------------------*/
|
|
141 |
/* Private function prototypes -----------------------------------------------*/
|
|
142 |
/* Exported functions --------------------------------------------------------*/
|
|
143 |
|
|
144 |
/** @defgroup WWDG_Exported_Functions WWDG Exported Functions
|
|
145 |
* @{
|
|
146 |
*/
|
|
147 |
|
|
148 |
/** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions
|
|
149 |
* @brief Initialization and Configuration functions.
|
|
150 |
*
|
|
151 |
@verbatim
|
|
152 |
==============================================================================
|
|
153 |
##### Initialization and Configuration functions #####
|
|
154 |
==============================================================================
|
|
155 |
[..]
|
|
156 |
This section provides functions allowing to:
|
|
157 |
(+) Initialize and start the WWDG according to the specified parameters
|
|
158 |
in the WWDG_InitTypeDef of associated handle.
|
|
159 |
(+) Initialize the WWDG MSP.
|
|
160 |
|
|
161 |
@endverbatim
|
|
162 |
* @{
|
|
163 |
*/
|
|
164 |
|
|
165 |
/**
|
|
166 |
* @brief Initialize the WWDG according to the specified.
|
|
167 |
* parameters in the WWDG_InitTypeDef of associated handle.
|
|
168 |
* @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
|
|
169 |
* the configuration information for the specified WWDG module.
|
|
170 |
* @retval HAL status
|
|
171 |
*/
|
|
172 |
HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
|
|
173 |
{
|
|
174 |
/* Check the WWDG handle allocation */
|
|
175 |
if(hwwdg == NULL)
|
|
176 |
{
|
|
177 |
return HAL_ERROR;
|
|
178 |
}
|
|
179 |
|
|
180 |
/* Check the parameters */
|
|
181 |
assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
|
|
182 |
assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
|
|
183 |
assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
|
|
184 |
assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
|
|
185 |
assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode));
|
|
186 |
|
|
187 |
/* Init the low level hardware */
|
|
188 |
HAL_WWDG_MspInit(hwwdg);
|
|
189 |
|
|
190 |
/* Set WWDG Counter */
|
|
191 |
WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter));
|
|
192 |
|
|
193 |
/* Set WWDG Prescaler and Window */
|
|
194 |
WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window));
|
|
195 |
|
|
196 |
/* Return function status */
|
|
197 |
return HAL_OK;
|
|
198 |
}
|
|
199 |
|
|
200 |
|
|
201 |
/**
|
|
202 |
* @brief Initialize the WWDG MSP.
|
|
203 |
* @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
|
|
204 |
* the configuration information for the specified WWDG module.
|
|
205 |
* @note When rewriting this function in user file, mechanism may be added
|
|
206 |
* to avoid multiple initialize when HAL_WWDG_Init function is called
|
|
207 |
* again to change parameters.
|
|
208 |
* @retval None
|
|
209 |
*/
|
|
210 |
__weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
|
|
211 |
{
|
|
212 |
/* Prevent unused argument(s) compilation warning */
|
|
213 |
UNUSED(hwwdg);
|
|
214 |
|
|
215 |
/* NOTE: This function should not be modified, when the callback is needed,
|
|
216 |
the HAL_WWDG_MspInit could be implemented in the user file
|
|
217 |
*/
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* @}
|
|
222 |
*/
|
|
223 |
|
|
224 |
/** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
|
|
225 |
* @brief IO operation functions
|
|
226 |
*
|
|
227 |
@verbatim
|
|
228 |
==============================================================================
|
|
229 |
##### IO operation functions #####
|
|
230 |
==============================================================================
|
|
231 |
[..]
|
|
232 |
This section provides functions allowing to:
|
|
233 |
(+) Refresh the WWDG.
|
|
234 |
(+) Handle WWDG interrupt request and associated function callback.
|
|
235 |
|
|
236 |
@endverbatim
|
|
237 |
* @{
|
|
238 |
*/
|
|
239 |
|
|
240 |
/**
|
|
241 |
* @brief Refresh the WWDG.
|
|
242 |
* @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
|
|
243 |
* the configuration information for the specified WWDG module.
|
|
244 |
* @retval HAL status
|
|
245 |
*/
|
|
246 |
HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
|
|
247 |
{
|
|
248 |
/* Write to WWDG CR the WWDG Counter value to refresh with */
|
|
249 |
WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter));
|
|
250 |
|
|
251 |
/* Return function status */
|
|
252 |
return HAL_OK;
|
|
253 |
}
|
|
254 |
|
|
255 |
/**
|
|
256 |
* @brief Handle WWDG interrupt request.
|
|
257 |
* @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
|
|
258 |
* or data logging must be performed before the actual reset is generated.
|
|
259 |
* The EWI interrupt is enabled by calling HAL_WWDG_Init function with
|
|
260 |
* EWIMode set to WWDG_EWI_ENABLE.
|
|
261 |
* When the downcounter reaches the value 0x40, and EWI interrupt is
|
|
262 |
* generated and the corresponding Interrupt Service Routine (ISR) can
|
|
263 |
* be used to trigger specific actions (such as communications or data
|
|
264 |
* logging), before resetting the device.
|
|
265 |
* @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
|
|
266 |
* the configuration information for the specified WWDG module.
|
|
267 |
* @retval None
|
|
268 |
*/
|
|
269 |
void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
|
|
270 |
{
|
|
271 |
/* Check if Early Wakeup Interrupt is enable */
|
|
272 |
if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
|
|
273 |
{
|
|
274 |
/* Check if WWDG Early Wakeup Interrupt occurred */
|
|
275 |
if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
|
|
276 |
{
|
|
277 |
/* Clear the WWDG Early Wakeup flag */
|
|
278 |
__HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
|
|
279 |
|
|
280 |
/* Early Wakeup callback */
|
|
281 |
HAL_WWDG_EarlyWakeupCallback(hwwdg);
|
|
282 |
}
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
|
|
287 |
/**
|
|
288 |
* @brief WWDG Early Wakeup callback.
|
|
289 |
* @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains
|
|
290 |
* the configuration information for the specified WWDG module.
|
|
291 |
* @retval None
|
|
292 |
*/
|
|
293 |
__weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef* hwwdg)
|
|
294 |
{
|
|
295 |
/* Prevent unused argument(s) compilation warning */
|
|
296 |
UNUSED(hwwdg);
|
|
297 |
|
|
298 |
/* NOTE: This function should not be modified, when the callback is needed,
|
|
299 |
the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
|
|
300 |
*/
|
|
301 |
}
|
|
302 |
|
|
303 |
/**
|
|
304 |
* @}
|
|
305 |
*/
|
|
306 |
|
|
307 |
/**
|
|
308 |
* @}
|
|
309 |
*/
|
|
310 |
|
|
311 |
#endif /* HAL_WWDG_MODULE_ENABLED */
|
|
312 |
/**
|
|
313 |
* @}
|
|
314 |
*/
|
|
315 |
|
|
316 |
/**
|
|
317 |
* @}
|
|
318 |
*/
|
|
319 |
|
|
320 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|