source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_wwdg.c@ 303

Last change on this file since 303 was 303, checked in by ertl-honda, 7 years ago

nucleo_f401re依存部の追加

File size: 15.9 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32f4xx_hal_wwdg.c
4 * @author MCD Application Team
5 * @version V1.4.1
6 * @date 09-October-2015
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
13 @verbatim
14 ==============================================================================
15 ##### WWDG specific features #####
16 ==============================================================================
17 [..]
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).
22
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
28 reset occurs.
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
36
37 (+) Min-max timeout value at 50 MHz(PCLK1): 81.9 us / 41.9 ms
38
39
40 ##### How to use this driver #####
41 ==============================================================================
42 [..]
43 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
44 (+) Set the WWDG prescaler, refresh window and counter value
45 using HAL_WWDG_Init() function.
46 (+) Start the WWDG using HAL_WWDG_Start() function.
47 When the WWDG is enabled the counter value should be configured to
48 a value greater than 0x40 to prevent generating an immediate reset.
49 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
50 generated when the counter reaches 0x40, and then start the WWDG using
51 HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
52 add his own code by customization of function pointer HAL_WWDG_WakeupCallback
53 Once enabled, EWI interrupt cannot be disabled except by a system reset.
54 (+) Then the application program must refresh the WWDG counter at regular
55 intervals during normal operation to prevent an MCU reset, using
56 HAL_WWDG_Refresh() function. This operation must occur only when
57 the counter is lower than the refresh window value already programmed.
58
59 *** WWDG HAL driver macros list ***
60 ==================================
61 [..]
62 Below the list of most used macros in WWDG HAL driver.
63
64 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
65 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
66 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
67 (+) __HAL_WWDG_ENABLE_IT: Enables the WWDG early wake-up interrupt
68
69 @endverbatim
70 ******************************************************************************
71 * @attention
72 *
73 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
74 *
75 * Redistribution and use in source and binary forms, with or without modification,
76 * are permitted provided that the following conditions are met:
77 * 1. Redistributions of source code must retain the above copyright notice,
78 * this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright notice,
80 * this list of conditions and the following disclaimer in the documentation
81 * and/or other materials provided with the distribution.
82 * 3. Neither the name of STMicroelectronics nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
85 *
86 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
92 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
93 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
94 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
95 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96 *
97 ******************************************************************************
98 */
99
100/* Includes ------------------------------------------------------------------*/
101#include "stm32f4xx_hal.h"
102
103/** @addtogroup STM32F4xx_HAL_Driver
104 * @{
105 */
106
107/** @defgroup WWDG WWDG
108 * @brief WWDG HAL module driver.
109 * @{
110 */
111
112#ifdef HAL_WWDG_MODULE_ENABLED
113
114/* Private typedef -----------------------------------------------------------*/
115/* Private define ------------------------------------------------------------*/
116/* Private macro -------------------------------------------------------------*/
117/* Private variables ---------------------------------------------------------*/
118/* Private function prototypes -----------------------------------------------*/
119/* Exported functions --------------------------------------------------------*/
120/** @defgroup WWDG_Exported_Functions WWDG Exported Functions
121 * @{
122 */
123
124/** @defgroup WWDG_Exported_Functions_Group1 Initialization and de-initialization functions
125 * @brief Initialization and Configuration functions.
126 *
127@verbatim
128 ==============================================================================
129 ##### Initialization and de-initialization functions #####
130 ==============================================================================
131 [..]
132 This section provides functions allowing to:
133 (+) Initialize the WWDG according to the specified parameters
134 in the WWDG_InitTypeDef and create the associated handle
135 (+) DeInitialize the WWDG peripheral
136 (+) Initialize the WWDG MSP
137 (+) DeInitialize the WWDG MSP
138
139@endverbatim
140 * @{
141 */
142
143/**
144 * @brief Initializes the WWDG according to the specified
145 * parameters in the WWDG_InitTypeDef and creates the associated handle.
146 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
147 * the configuration information for the specified WWDG module.
148 * @retval HAL status
149 */
150HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
151{
152 /* Check the WWDG handle allocation */
153 if(hwwdg == NULL)
154 {
155 return HAL_ERROR;
156 }
157
158 /* Check the parameters */
159 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
160 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
161 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
162 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
163
164 if(hwwdg->State == HAL_WWDG_STATE_RESET)
165 {
166 /* Allocate lock resource and initialize it */
167 hwwdg->Lock = HAL_UNLOCKED;
168 /* Init the low level hardware */
169 HAL_WWDG_MspInit(hwwdg);
170 }
171
172 /* Change WWDG peripheral state */
173 hwwdg->State = HAL_WWDG_STATE_BUSY;
174
175 /* Set WWDG Prescaler and Window */
176 MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
177 /* Set WWDG Counter */
178 MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
179
180 /* Change WWDG peripheral state */
181 hwwdg->State = HAL_WWDG_STATE_READY;
182
183 /* Return function status */
184 return HAL_OK;
185}
186
187/**
188 * @brief DeInitializes the WWDG peripheral.
189 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
190 * the configuration information for the specified WWDG module.
191 * @retval HAL status
192 */
193HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
194{
195 /* Check the WWDG handle allocation */
196 if(hwwdg == NULL)
197 {
198 return HAL_ERROR;
199 }
200
201 /* Check the parameters */
202 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
203
204 /* Change WWDG peripheral state */
205 hwwdg->State = HAL_WWDG_STATE_BUSY;
206
207 /* DeInit the low level hardware */
208 HAL_WWDG_MspDeInit(hwwdg);
209
210 /* Reset WWDG Control register */
211 hwwdg->Instance->CR = (uint32_t)0x0000007F;
212
213 /* Reset WWDG Configuration register */
214 hwwdg->Instance->CFR = (uint32_t)0x0000007F;
215
216 /* Reset WWDG Status register */
217 hwwdg->Instance->SR = 0;
218
219 /* Change WWDG peripheral state */
220 hwwdg->State = HAL_WWDG_STATE_RESET;
221
222 /* Release Lock */
223 __HAL_UNLOCK(hwwdg);
224
225 /* Return function status */
226 return HAL_OK;
227}
228
229/**
230 * @brief Initializes the WWDG MSP.
231 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
232 * the configuration information for the specified WWDG module.
233 * @retval None
234 */
235__weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
236{
237 /* NOTE: This function Should not be modified, when the callback is needed,
238 the HAL_WWDG_MspInit could be implemented in the user file
239 */
240}
241
242/**
243 * @brief DeInitializes the WWDG MSP.
244 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
245 * the configuration information for the specified WWDG module.
246 * @retval None
247 */
248__weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
249{
250 /* NOTE: This function Should not be modified, when the callback is needed,
251 the HAL_WWDG_MspDeInit could be implemented in the user file
252 */
253}
254
255/**
256 * @}
257 */
258
259/** @defgroup WWDG_Exported_Functions_Group2 IO operation functions
260 * @brief IO operation functions
261 *
262@verbatim
263 ==============================================================================
264 ##### IO operation functions #####
265 ==============================================================================
266 [..]
267 This section provides functions allowing to:
268 (+) Start the WWDG.
269 (+) Refresh the WWDG.
270 (+) Handle WWDG interrupt request.
271
272@endverbatim
273 * @{
274 */
275
276/**
277 * @brief Starts the WWDG.
278 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
279 * the configuration information for the specified WWDG module.
280 * @retval HAL status
281 */
282HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
283{
284 /* Process Locked */
285 __HAL_LOCK(hwwdg);
286
287 /* Change WWDG peripheral state */
288 hwwdg->State = HAL_WWDG_STATE_BUSY;
289
290 /* Enable the peripheral */
291 __HAL_WWDG_ENABLE(hwwdg);
292
293 /* Change WWDG peripheral state */
294 hwwdg->State = HAL_WWDG_STATE_READY;
295
296 /* Process Unlocked */
297 __HAL_UNLOCK(hwwdg);
298
299 /* Return function status */
300 return HAL_OK;
301}
302
303/**
304 * @brief Starts the WWDG with interrupt enabled.
305 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
306 * the configuration information for the specified WWDG module.
307 * @retval HAL status
308 */
309HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
310{
311 /* Process Locked */
312 __HAL_LOCK(hwwdg);
313
314 /* Change WWDG peripheral state */
315 hwwdg->State = HAL_WWDG_STATE_BUSY;
316
317 /* Enable the Early Wakeup Interrupt */
318 __HAL_WWDG_ENABLE_IT(hwwdg, WWDG_IT_EWI);
319
320 /* Enable the peripheral */
321 __HAL_WWDG_ENABLE(hwwdg);
322
323 /* Return function status */
324 return HAL_OK;
325}
326
327/**
328 * @brief Refreshes the WWDG.
329 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
330 * the configuration information for the specified WWDG module.
331 * @param Counter: value of counter to put in WWDG counter
332 * @retval HAL status
333 */
334HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
335{
336 /* Process Locked */
337 __HAL_LOCK(hwwdg);
338
339 /* Change WWDG peripheral state */
340 hwwdg->State = HAL_WWDG_STATE_BUSY;
341
342 /* Check the parameters */
343 assert_param(IS_WWDG_COUNTER(Counter));
344
345 /* Write to WWDG CR the WWDG Counter value to refresh with */
346 MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
347
348 /* Change WWDG peripheral state */
349 hwwdg->State = HAL_WWDG_STATE_READY;
350
351 /* Process Unlocked */
352 __HAL_UNLOCK(hwwdg);
353
354 /* Return function status */
355 return HAL_OK;
356}
357
358/**
359 * @brief Handles WWDG interrupt request.
360 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
361 * or data logging must be performed before the actual reset is generated.
362 * The EWI interrupt is enabled using __HAL_WWDG_ENABLE_IT() macro.
363 * When the downcounter reaches the value 0x40, and EWI interrupt is
364 * generated and the corresponding Interrupt Service Routine (ISR) can
365 * be used to trigger specific actions (such as communications or data
366 * logging), before resetting the device.
367 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
368 * the configuration information for the specified WWDG module.
369 * @retval None
370 */
371void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
372{
373 /* Check if Early Wakeup Interrupt is enable */
374 if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
375 {
376 /* Check if WWDG Early Wakeup Interrupt occurred */
377 if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
378 {
379 /* Early Wakeup callback */
380 HAL_WWDG_WakeupCallback(hwwdg);
381
382 /* Change WWDG peripheral state */
383 hwwdg->State = HAL_WWDG_STATE_READY;
384
385 /* Clear the WWDG Early Wakeup flag */
386 __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
387
388 /* Process Unlocked */
389 __HAL_UNLOCK(hwwdg);
390 }
391 }
392}
393
394/**
395 * @brief Early Wakeup WWDG callback.
396 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
397 * the configuration information for the specified WWDG module.
398 * @retval None
399 */
400__weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
401{
402 /* NOTE: This function Should not be modified, when the callback is needed,
403 the HAL_WWDG_WakeupCallback could be implemented in the user file
404 */
405}
406
407/**
408 * @}
409 */
410
411/** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions
412 * @brief Peripheral State functions.
413 *
414@verbatim
415 ==============================================================================
416 ##### Peripheral State functions #####
417 ==============================================================================
418 [..]
419 This subsection permits to get in run-time the status of the peripheral
420 and the data flow.
421
422@endverbatim
423 * @{
424 */
425
426/**
427 * @brief Returns the WWDG state.
428 * @param hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
429 * the configuration information for the specified WWDG module.
430 * @retval HAL state
431 */
432HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
433{
434 return hwwdg->State;
435}
436
437/**
438 * @}
439 */
440
441/**
442 * @}
443 */
444
445#endif /* HAL_WWDG_MODULE_ENABLED */
446/**
447 * @}
448 */
449
450/**
451 * @}
452 */
453
454/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.