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