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