]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_iwdg.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_iwdg.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_iwdg.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   IWDG HAL module driver.\r
8   *    \r
9   *          This file provides firmware functions to manage the following \r
10   *          functionalities of the Independent Watchdog (IWDG) peripheral:\r
11   *           + Initialization and de-initialization functions\r
12   *           + IO operation functions\r
13   *           + Peripheral State functions\r
14   *         \r
15   @verbatim\r
16   ==============================================================================\r
17                     ##### IWDG Generic features #####\r
18   ==============================================================================\r
19     [..] \r
20     (+) The IWDG can be started by either software or hardware (configurable\r
21          through option byte).\r
22 \r
23     (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and\r
24          thus stays active even if the main clock fails.\r
25          Once the IWDG is started, the LSI is forced ON and cannot be disabled\r
26          (LSI cannot be disabled too), and the counter starts counting down from\r
27          the reset value of 0xFFF. When it reaches the end of count value (0x000)\r
28          a system reset is generated.\r
29 \r
30     (+) The IWDG counter should be refreshed at regular intervals, otherwise the\r
31          watchdog generates an MCU reset when the counter reaches 0.\r
32 \r
33     (+) The IWDG is implemented in the VDD voltage domain that is still functional\r
34          in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).\r
35          IWDGRST flag in RCC_CSR register can be used to inform when an IWDG\r
36          reset occurs.\r
37 \r
38     [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s\r
39          The IWDG timeout may vary due to LSI frequency dispersion. STM32L4xx\r
40          devices provide the capability to measure the LSI frequency (LSI clock\r
41          connected internally to TIM16 CH1 input capture). The measured value\r
42          can be used to have an IWDG timeout with an acceptable accuracy.\r
43 \r
44 \r
45                      ##### How to use this driver #####\r
46   ==============================================================================\r
47     [..]\r
48     If Window option is disabled\r
49     \r
50       (+) Use IWDG using HAL_IWDG_Init() function to :\r
51          (++) Enable write access to IWDG_PR, IWDG_RLR.\r
52          (++) Configure the IWDG prescaler, counter reload value.\r
53               This reload value will be loaded in the IWDG counter each time the counter\r
54               is reloaded, then the IWDG will start counting down from this value.\r
55       (+) Use IWDG using HAL_IWDG_Start() function to :\r
56          (++) Reload IWDG counter with value defined in the IWDG_RLR register.\r
57          (++) Start the IWDG, when the IWDG is used in software mode (no need \r
58               to enable the LSI, it will be enabled by hardware).\r
59       (+) Then the application program must refresh the IWDG counter at regular\r
60           intervals during normal operation to prevent an MCU reset, using\r
61           HAL_IWDG_Refresh() function.\r
62     [..] \r
63     if Window option is enabled:\r
64       \r
65       (+) Use IWDG using HAL_IWDG_Start() function to enable IWDG downcounter\r
66       (+) Use IWDG using HAL_IWDG_Init() function to :\r
67          (++) Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.\r
68          (++) Configure the IWDG prescaler, reload value and window value.\r
69       (+) Then the application program must refresh the IWDG counter at regular\r
70           intervals during normal operation to prevent an MCU reset, using\r
71           HAL_IWDG_Refresh() function.\r
72 \r
73      *** IWDG HAL driver macros list ***\r
74      ====================================\r
75      [..]\r
76        Below the list of most used macros in IWDG HAL driver.\r
77        \r
78       (+) __HAL_IWDG_START: Enable the IWDG peripheral\r
79       (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register    \r
80       (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status\r
81       (+) IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers\r
82       (+) IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers\r
83             \r
84   @endverbatim\r
85   ******************************************************************************\r
86   * @attention\r
87   *\r
88   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
89   *\r
90   * Redistribution and use in source and binary forms, with or without modification,\r
91   * are permitted provided that the following conditions are met:\r
92   *   1. Redistributions of source code must retain the above copyright notice,\r
93   *      this list of conditions and the following disclaimer.\r
94   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
95   *      this list of conditions and the following disclaimer in the documentation\r
96   *      and/or other materials provided with the distribution.\r
97   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
98   *      may be used to endorse or promote products derived from this software\r
99   *      without specific prior written permission.\r
100   *\r
101   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
102   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
103   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
104   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
105   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
106   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
107   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
108   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
109   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
110   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
111   *\r
112   ******************************************************************************\r
113   */\r
114 \r
115 /* Includes ------------------------------------------------------------------*/\r
116 #include "stm32f7xx_hal.h"\r
117 \r
118 /** @addtogroup STM32F7xx_HAL_Driver\r
119   * @{\r
120   */\r
121 \r
122 /** @defgroup IWDG IWDG HAL module driver\r
123   * @brief IWDG HAL module driver.\r
124   * @{\r
125   */\r
126 \r
127 #ifdef HAL_IWDG_MODULE_ENABLED\r
128 \r
129 /* Private typedef -----------------------------------------------------------*/\r
130 /* Private define ------------------------------------------------------------*/\r
131 /** @defgroup IWDG_Private_Defines IWDG Private Defines\r
132   * @{\r
133   */\r
134 \r
135 #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000\r
136 \r
137 /**\r
138   * @}\r
139   */\r
140 /* Private macro -------------------------------------------------------------*/\r
141 /* Private variables ---------------------------------------------------------*/\r
142 /* Private function prototypes -----------------------------------------------*/\r
143 /* Exported functions --------------------------------------------------------*/\r
144 \r
145 /** @defgroup IWDG_Exported_Functions IWDG Exported Functions\r
146   * @{\r
147   */\r
148 \r
149 /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions \r
150  *  @brief    Initialization and Configuration functions.\r
151  *\r
152 @verbatim\r
153  ===============================================================================\r
154           ##### Initialization and de-initialization functions #####\r
155  ===============================================================================\r
156     [..]  This section provides functions allowing to:\r
157       (+) Initialize the IWDG according to the specified parameters\r
158           in the IWDG_InitTypeDef and create the associated handle\r
159       (+) Manage Window option\r
160       (+) Initialize the IWDG MSP\r
161       (+) DeInitialize IWDG MSP \r
162 \r
163 @endverbatim\r
164   * @{\r
165   */\r
166 \r
167 /**\r
168   * @brief  Initializes the IWDG according to the specified\r
169   *         parameters in the IWDG_InitTypeDef and creates the associated handle.\r
170   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains\r
171   *                the configuration information for the specified IWDG module.\r
172   * @retval HAL status\r
173   */\r
174 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)\r
175 {\r
176   uint32_t tickstart = 0;\r
177 \r
178   /* Check the IWDG handle allocation */\r
179   if(hiwdg == NULL)\r
180   {\r
181     return HAL_ERROR;\r
182   }\r
183 \r
184   /* Check the parameters */\r
185   assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));\r
186   assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));\r
187   assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));\r
188 \r
189   /* Check pending flag, if previous update not done, return error */\r
190   if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)\r
191      &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)\r
192      &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET))\r
193   {\r
194     return HAL_ERROR;\r
195   }\r
196 \r
197   if(hiwdg->State == HAL_IWDG_STATE_RESET)\r
198   {\r
199     /* Allocate lock resource and initialize it */\r
200     hiwdg->Lock = HAL_UNLOCKED;\r
201 \r
202     /* Init the low level hardware */\r
203     HAL_IWDG_MspInit(hiwdg);\r
204   }\r
205 \r
206   /* Change IWDG peripheral state */\r
207   hiwdg->State = HAL_IWDG_STATE_BUSY;\r
208 \r
209   /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers */\r
210   /* by writing 0x5555 in KR */\r
211   IWDG_ENABLE_WRITE_ACCESS(hiwdg);\r
212 \r
213   /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */\r
214   MODIFY_REG(hiwdg->Instance->PR, (uint32_t)IWDG_PR_PR, hiwdg->Init.Prescaler);\r
215   MODIFY_REG(hiwdg->Instance->RLR, (uint32_t)IWDG_RLR_RL, hiwdg->Init.Reload);\r
216 \r
217   /* check if window option is enabled */\r
218   if (((hiwdg->Init.Window) != IWDG_WINDOW_DISABLE) || ((hiwdg->Instance->WINR) != IWDG_WINDOW_DISABLE))\r
219   {\r
220     tickstart = HAL_GetTick();\r
221 \r
222      /* Wait for register to be updated */\r
223     while((uint32_t)(hiwdg->Instance->SR) != RESET)\r
224     {\r
225       if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)\r
226       {\r
227         /* Set IWDG state */\r
228         hiwdg->State = HAL_IWDG_STATE_TIMEOUT;\r
229         return HAL_TIMEOUT;\r
230       }\r
231     }\r
232 \r
233     /* Write to IWDG WINR the IWDG_Window value to compare with */\r
234     MODIFY_REG(hiwdg->Instance->WINR, (uint32_t)IWDG_WINR_WIN, hiwdg->Init.Window);\r
235   }\r
236 \r
237   /* Change IWDG peripheral state */\r
238   hiwdg->State = HAL_IWDG_STATE_READY;\r
239 \r
240   /* Return function status */\r
241   return HAL_OK;\r
242 }\r
243 \r
244 /**\r
245   * @brief  Initializes the IWDG MSP.\r
246   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains\r
247   *                the configuration information for the specified IWDG module.\r
248   * @retval None\r
249   */\r
250 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)\r
251 {\r
252   /* NOTE : This function should not be modified, when the callback is needed,\r
253             the HAL_IWDG_MspInit could be implemented in the user file\r
254    */\r
255 }\r
256 \r
257 /**\r
258   * @}\r
259   */\r
260 \r
261 /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions  \r
262  *  @brief   IO operation functions  \r
263  *\r
264 @verbatim\r
265  ===============================================================================\r
266                       ##### IO operation functions #####\r
267  ===============================================================================\r
268     [..]  This section provides functions allowing to:\r
269       (+) Start the IWDG.\r
270       (+) Refresh the IWDG.\r
271 \r
272 @endverbatim\r
273   * @{\r
274   */\r
275 \r
276 /**\r
277   * @brief  Starts the IWDG.\r
278   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains\r
279   *                the configuration information for the specified IWDG module.\r
280   * @retval HAL status\r
281   */\r
282 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)\r
283 {\r
284   uint32_t tickstart = 0;\r
285 \r
286   /* Process locked */\r
287   __HAL_LOCK(hiwdg); \r
288 \r
289     /* Change IWDG peripheral state */\r
290   hiwdg->State = HAL_IWDG_STATE_BUSY;\r
291 \r
292   /* Reload IWDG counter with value defined in the RLR register */\r
293   if ((hiwdg->Init.Window) == IWDG_WINDOW_DISABLE)\r
294   {\r
295     __HAL_IWDG_RELOAD_COUNTER(hiwdg);\r
296   }\r
297 \r
298   /* Start the IWDG peripheral */\r
299   __HAL_IWDG_START(hiwdg);\r
300 \r
301   tickstart = HAL_GetTick();\r
302 \r
303   /* Wait until PVU, RVU, WVU flag are RESET */\r
304   while( (__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)\r
305          &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)\r
306          &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET) )\r
307   {\r
308     \r
309     if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)\r
310     {\r
311       /* Set IWDG state */\r
312       hiwdg->State = HAL_IWDG_STATE_TIMEOUT;\r
313       \r
314       /* Process unlocked */\r
315       __HAL_UNLOCK(hiwdg);\r
316       \r
317       return HAL_TIMEOUT;\r
318     }\r
319   }\r
320 \r
321   /* Change IWDG peripheral state */\r
322   hiwdg->State = HAL_IWDG_STATE_READY;\r
323 \r
324   /* Process Unlocked */\r
325   __HAL_UNLOCK(hiwdg);\r
326 \r
327   /* Return function status */\r
328   return HAL_OK;\r
329 }\r
330 \r
331 /**\r
332   * @brief  Refreshes the IWDG.\r
333   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains\r
334   *                the configuration information for the specified IWDG module.\r
335   * @retval HAL status\r
336   */\r
337 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)\r
338 {\r
339   uint32_t tickstart = 0;\r
340 \r
341   /* Process Locked */\r
342   __HAL_LOCK(hiwdg);\r
343 \r
344     /* Change IWDG peripheral state */\r
345   hiwdg->State = HAL_IWDG_STATE_BUSY;\r
346 \r
347   tickstart = HAL_GetTick();\r
348 \r
349   /* Wait until RVU flag is RESET */\r
350   while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)\r
351   {\r
352     if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)\r
353     {\r
354       /* Set IWDG state */\r
355       hiwdg->State = HAL_IWDG_STATE_TIMEOUT;\r
356 \r
357        /* Process unlocked */\r
358       __HAL_UNLOCK(hiwdg);\r
359 \r
360       return HAL_TIMEOUT;\r
361     }\r
362   }\r
363 \r
364   /* Reload IWDG counter with value defined in the reload register */\r
365   __HAL_IWDG_RELOAD_COUNTER(hiwdg);\r
366 \r
367   /* Change IWDG peripheral state */\r
368   hiwdg->State = HAL_IWDG_STATE_READY;\r
369 \r
370   /* Process Unlocked */\r
371   __HAL_UNLOCK(hiwdg);\r
372 \r
373   /* Return function status */\r
374   return HAL_OK;\r
375 }\r
376 \r
377 /**\r
378   * @}\r
379   */\r
380 \r
381 /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions \r
382  *  @brief    Peripheral State functions.\r
383  *\r
384 @verbatim\r
385  ===============================================================================\r
386                       ##### Peripheral State functions #####\r
387  ===============================================================================\r
388     [..]\r
389     This subsection permits to get in run-time the status of the peripheral.\r
390 \r
391 @endverbatim\r
392   * @{\r
393   */\r
394 \r
395 /**\r
396   * @brief  Returns the IWDG state.\r
397   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains\r
398   *                the configuration information for the specified IWDG module.\r
399   * @retval HAL state\r
400   */\r
401 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)\r
402 {\r
403   return hiwdg->State;\r
404 }\r
405 \r
406 /**\r
407   * @}\r
408   */\r
409 \r
410 /**\r
411   * @}\r
412   */\r
413 \r
414 #endif /* HAL_IWDG_MODULE_ENABLED */\r
415 /**\r
416   * @}\r
417   */\r
418 \r
419 /**\r
420   * @}\r
421   */\r
422 \r
423 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r