1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_iwdg.c
4   * @author  MCD Application Team
5   * @brief   IWDG HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Independent Watchdog (IWDG) peripheral:
8   *           + Initialization and Start functions
9   *           + IO operation functions
10   *
11   ******************************************************************************
12   * @attention
13   *
14   * Copyright (c) 2016 STMicroelectronics.
15   * All rights reserved.
16   *
17   * This software is licensed under terms that can be found in the LICENSE file
18   * in the root directory of this software component.
19   * If no LICENSE file comes with this software, it is provided AS-IS.
20   *
21   ******************************************************************************
22   @verbatim
23   ==============================================================================
24                     ##### IWDG Generic features #####
25   ==============================================================================
26   [..]
27     (+) The IWDG can be started by either software or hardware (configurable
28         through option byte).
29 
30     (+) The IWDG is clocked by the Low-Speed Internal clock (LSI) and thus stays
31         active even if the main clock fails.
32 
33     (+) Once the IWDG is started, the LSI is forced ON and both cannot be
34         disabled. The counter starts counting down from the reset value (0xFFF).
35         When it reaches the end of count value (0x000) a reset signal is
36         generated (IWDG reset).
37 
38     (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
39         the IWDG_RLR value is reloaded into the counter and the watchdog reset
40         is prevented.
41 
42     (+) The IWDG is implemented in the VDD voltage domain that is still functional
43         in STOP and STANDBY mode (IWDG reset can wake up the CPU from STANDBY).
44         IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
45         reset occurs.
46 
47     (+) Debug mode: When the microcontroller enters debug mode (core halted),
48         the IWDG counter either continues to work normally or stops, depending
49         on DBG_IWDG_STOP configuration bit in DBG module, accessible through
50         __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros.
51 
52     [..] Min-max timeout value @40KHz (LSI): ~100us / ~26.2s
53          The IWDG timeout may vary due to LSI clock frequency dispersion.
54          STM32F3xx devices provide the capability to measure the LSI clock
55          frequency (LSI clock is internally connected to TIM16 CH1 input capture).
56          The measured value can be used to have an IWDG timeout with an
57          acceptable accuracy.
58 
59     [..] Default timeout value (necessary for IWDG_SR status register update):
60          Constant LSI_VALUE is defined based on the nominal LSI clock frequency.
61          This frequency being subject to variations as mentioned above, the
62          default timeout value (defined through constant HAL_IWDG_DEFAULT_TIMEOUT
63          below) may become too short or too long.
64          In such cases, this default timeout value can be tuned by redefining
65          the constant LSI_VALUE at user-application level (based, for instance,
66          on the measured LSI clock frequency as explained above).
67 
68                      ##### How to use this driver #####
69   ==============================================================================
70   [..]
71     (#) Use IWDG using HAL_IWDG_Init() function to :
72       (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
73            clock is forced ON and IWDG counter starts counting down.
74       (++) Enable write access to configuration registers:
75           IWDG_PR, IWDG_RLR and IWDG_WINR.
76       (++) Configure the IWDG prescaler and counter reload value. This reload
77            value will be loaded in the IWDG counter each time the watchdog is
78            reloaded, then the IWDG will start counting down from this value.
79       (++) Depending on window parameter:
80         (+++) If Window Init parameter is same as Window register value,
81              nothing more is done but reload counter value in order to exit
82              function with exact time base.
83         (+++) Else modify Window register. This will automatically reload
84              watchdog counter.
85       (++) Wait for status flags to be reset.
86 
87     (#) Then the application program must refresh the IWDG counter at regular
88         intervals during normal operation to prevent an MCU reset, using
89         HAL_IWDG_Refresh() function.
90 
91      *** IWDG HAL driver macros list ***
92      ====================================
93      [..]
94        Below the list of most used macros in IWDG HAL driver:
95       (+) __HAL_IWDG_START: Enable the IWDG peripheral
96       (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
97           the reload register
98 
99   @endverbatim
100   */
101 
102 /* Includes ------------------------------------------------------------------*/
103 #include "stm32f3xx_hal.h"
104 
105 /** @addtogroup STM32F3xx_HAL_Driver
106   * @{
107   */
108 
109 #ifdef HAL_IWDG_MODULE_ENABLED
110 /** @addtogroup IWDG
111   * @brief IWDG HAL module driver.
112   * @{
113   */
114 
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
117 /** @defgroup IWDG_Private_Defines IWDG Private Defines
118   * @{
119   */
120 /* Status register needs up to 5 LSI clock periods divided by the clock
121    prescaler to be updated. The number of LSI clock periods is upper-rounded to
122    6 for the timeout value calculation.
123    The timeout value is calculated using the highest prescaler (256) and
124    the LSI_VALUE constant. The value of this constant can be changed by the user
125    to take into account possible LSI clock period variations.
126    The timeout value is multiplied by 1000 to be converted in milliseconds.
127    LSI startup time is also considered here by adding LSI_STARTUP_TIME
128    converted in milliseconds. */
129 #define HAL_IWDG_DEFAULT_TIMEOUT        (((6UL * 256UL * 1000UL) / LSI_VALUE) + ((LSI_STARTUP_TIME / 1000UL) + 1UL))
130 #define IWDG_KERNEL_UPDATE_FLAGS        (IWDG_SR_WVU | IWDG_SR_RVU | IWDG_SR_PVU)
131 /**
132   * @}
133   */
134 
135 /* Private macro -------------------------------------------------------------*/
136 /* Private variables ---------------------------------------------------------*/
137 /* Private function prototypes -----------------------------------------------*/
138 /* Exported functions --------------------------------------------------------*/
139 
140 /** @addtogroup IWDG_Exported_Functions
141   * @{
142   */
143 
144 /** @addtogroup IWDG_Exported_Functions_Group1
145   *  @brief    Initialization and Start functions.
146   *
147 @verbatim
148  ===============================================================================
149           ##### Initialization and Start functions #####
150  ===============================================================================
151  [..]  This section provides functions allowing to:
152       (+) Initialize the IWDG according to the specified parameters in the
153           IWDG_InitTypeDef of associated handle.
154       (+) Manage Window option.
155       (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
156           is reloaded in order to exit function with correct time base.
157 
158 @endverbatim
159   * @{
160   */
161 
162 /**
163   * @brief  Initialize the IWDG according to the specified parameters in the
164   *         IWDG_InitTypeDef and start watchdog. Before exiting function,
165   *         watchdog is refreshed in order to have correct time base.
166   * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
167   *                the configuration information for the specified IWDG module.
168   * @retval HAL status
169   */
HAL_IWDG_Init(IWDG_HandleTypeDef * hiwdg)170 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
171 {
172   uint32_t tickstart;
173 
174   /* Check the IWDG handle allocation */
175   if (hiwdg == NULL)
176   {
177     return HAL_ERROR;
178   }
179 
180   /* Check the parameters */
181   assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
182   assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
183   assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
184   assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
185 
186   /* Enable IWDG. LSI is turned on automatically */
187   __HAL_IWDG_START(hiwdg);
188 
189   /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
190   0x5555 in KR */
191   IWDG_ENABLE_WRITE_ACCESS(hiwdg);
192 
193   /* Write to IWDG registers the Prescaler & Reload values to work with */
194   hiwdg->Instance->PR = hiwdg->Init.Prescaler;
195   hiwdg->Instance->RLR = hiwdg->Init.Reload;
196 
197   /* Check pending flag, if previous update not done, return timeout */
198   tickstart = HAL_GetTick();
199 
200   /* Wait for register to be updated */
201   while ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
202   {
203     if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
204     {
205       if ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
206       {
207         return HAL_TIMEOUT;
208       }
209     }
210   }
211 
212   /* If window parameter is different than current value, modify window
213   register */
214   if (hiwdg->Instance->WINR != hiwdg->Init.Window)
215   {
216     /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
217     even if window feature is disabled, Watchdog will be reloaded by writing
218     windows register */
219     hiwdg->Instance->WINR = hiwdg->Init.Window;
220   }
221   else
222   {
223     /* Reload IWDG counter with value defined in the reload register */
224     __HAL_IWDG_RELOAD_COUNTER(hiwdg);
225   }
226 
227   /* Return function status */
228   return HAL_OK;
229 }
230 
231 
232 /**
233   * @}
234   */
235 
236 
237 /** @addtogroup IWDG_Exported_Functions_Group2
238   *  @brief   IO operation functions
239   *
240 @verbatim
241  ===============================================================================
242                       ##### IO operation functions #####
243  ===============================================================================
244  [..]  This section provides functions allowing to:
245       (+) Refresh the IWDG.
246 
247 @endverbatim
248   * @{
249   */
250 
251 /**
252   * @brief  Refresh the IWDG.
253   * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
254   *                the configuration information for the specified IWDG module.
255   * @retval HAL status
256   */
HAL_IWDG_Refresh(IWDG_HandleTypeDef * hiwdg)257 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
258 {
259   /* Reload IWDG counter with value defined in the reload register */
260   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
261 
262   /* Return function status */
263   return HAL_OK;
264 }
265 
266 
267 /**
268   * @}
269   */
270 
271 /**
272   * @}
273   */
274 
275 #endif /* HAL_IWDG_MODULE_ENABLED */
276 /**
277   * @}
278   */
279 
280 /**
281   * @}
282   */
283