1 | /**
|
---|
2 | ******************************************************************************
|
---|
3 | * @file stm32f4xx_hal_iwdg.c
|
---|
4 | * @author MCD Application Team
|
---|
5 | * @version V1.4.1
|
---|
6 | * @date 09-October-2015
|
---|
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) 2015 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 | /* Allocate lock resource and initialize it */
|
---|
184 | hiwdg->Lock = HAL_UNLOCKED;
|
---|
185 | /* Init the low level hardware */
|
---|
186 | HAL_IWDG_MspInit(hiwdg);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /* Change IWDG peripheral state */
|
---|
190 | hiwdg->State = HAL_IWDG_STATE_BUSY;
|
---|
191 |
|
---|
192 | /* Enable write access to IWDG_PR and IWDG_RLR registers */
|
---|
193 | IWDG_ENABLE_WRITE_ACCESS(hiwdg);
|
---|
194 |
|
---|
195 | /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
|
---|
196 | MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
|
---|
197 | MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
|
---|
198 |
|
---|
199 | /* Change IWDG peripheral state */
|
---|
200 | hiwdg->State = HAL_IWDG_STATE_READY;
|
---|
201 |
|
---|
202 | /* Return function status */
|
---|
203 | return HAL_OK;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * @brief Initializes the IWDG MSP.
|
---|
208 | * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
|
---|
209 | * the configuration information for the specified IWDG module.
|
---|
210 | * @retval None
|
---|
211 | */
|
---|
212 | __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
|
---|
213 | {
|
---|
214 | /* NOTE : This function Should not be modified, when the callback is needed,
|
---|
215 | the HAL_IWDG_MspInit could be implemented in the user file
|
---|
216 | */
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * @}
|
---|
221 | */
|
---|
222 |
|
---|
223 | /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
|
---|
224 | * @brief IO operation functions
|
---|
225 | *
|
---|
226 | @verbatim
|
---|
227 | ===============================================================================
|
---|
228 | ##### IO operation functions #####
|
---|
229 | ===============================================================================
|
---|
230 | [..] This section provides functions allowing to:
|
---|
231 | (+) Start the IWDG.
|
---|
232 | (+) Refresh the IWDG.
|
---|
233 |
|
---|
234 | @endverbatim
|
---|
235 | * @{
|
---|
236 | */
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * @brief Starts the IWDG.
|
---|
240 | * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
|
---|
241 | * the configuration information for the specified IWDG module.
|
---|
242 | * @retval HAL status
|
---|
243 | */
|
---|
244 | HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
|
---|
245 | {
|
---|
246 | /* Process Locked */
|
---|
247 | __HAL_LOCK(hiwdg);
|
---|
248 |
|
---|
249 | /* Change IWDG peripheral state */
|
---|
250 | hiwdg->State = HAL_IWDG_STATE_BUSY;
|
---|
251 |
|
---|
252 | /* Start the IWDG peripheral */
|
---|
253 | __HAL_IWDG_START(hiwdg);
|
---|
254 |
|
---|
255 | /* Reload IWDG counter with value defined in the RLR register */
|
---|
256 | __HAL_IWDG_RELOAD_COUNTER(hiwdg);
|
---|
257 |
|
---|
258 | /* Change IWDG peripheral state */
|
---|
259 | hiwdg->State = HAL_IWDG_STATE_READY;
|
---|
260 |
|
---|
261 | /* Process Unlocked */
|
---|
262 | __HAL_UNLOCK(hiwdg);
|
---|
263 |
|
---|
264 | /* Return function status */
|
---|
265 | return HAL_OK;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * @brief Refreshes the IWDG.
|
---|
270 | * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
|
---|
271 | * the configuration information for the specified IWDG module.
|
---|
272 | * @retval HAL status
|
---|
273 | */
|
---|
274 | HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
|
---|
275 | {
|
---|
276 | uint32_t tickstart = 0;
|
---|
277 |
|
---|
278 | /* Process Locked */
|
---|
279 | __HAL_LOCK(hiwdg);
|
---|
280 |
|
---|
281 | /* Change IWDG peripheral state */
|
---|
282 | hiwdg->State = HAL_IWDG_STATE_BUSY;
|
---|
283 |
|
---|
284 | tickstart = HAL_GetTick();
|
---|
285 |
|
---|
286 | /* Wait until RVU flag is RESET */
|
---|
287 | while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
|
---|
288 | {
|
---|
289 | if((HAL_GetTick() - tickstart ) > IWDG_TIMEOUT_FLAG)
|
---|
290 | {
|
---|
291 | /* Set IWDG state */
|
---|
292 | hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
|
---|
293 |
|
---|
294 | /* Process unlocked */
|
---|
295 | __HAL_UNLOCK(hiwdg);
|
---|
296 |
|
---|
297 | return HAL_TIMEOUT;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | /* Reload IWDG counter with value defined in the reload register */
|
---|
302 | __HAL_IWDG_RELOAD_COUNTER(hiwdg);
|
---|
303 |
|
---|
304 | /* Change IWDG peripheral state */
|
---|
305 | hiwdg->State = HAL_IWDG_STATE_READY;
|
---|
306 |
|
---|
307 | /* Process Unlocked */
|
---|
308 | __HAL_UNLOCK(hiwdg);
|
---|
309 |
|
---|
310 | /* Return function status */
|
---|
311 | return HAL_OK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * @}
|
---|
316 | */
|
---|
317 |
|
---|
318 | /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
|
---|
319 | * @brief Peripheral State functions.
|
---|
320 | *
|
---|
321 | @verbatim
|
---|
322 | ===============================================================================
|
---|
323 | ##### Peripheral State functions #####
|
---|
324 | ===============================================================================
|
---|
325 | [..]
|
---|
326 | This subsection permits to get in run-time the status of the peripheral
|
---|
327 | and the data flow.
|
---|
328 |
|
---|
329 | @endverbatim
|
---|
330 | * @{
|
---|
331 | */
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * @brief Returns the IWDG state.
|
---|
335 | * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
|
---|
336 | * the configuration information for the specified IWDG module.
|
---|
337 | * @retval HAL state
|
---|
338 | */
|
---|
339 | HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
|
---|
340 | {
|
---|
341 | return hiwdg->State;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * @}
|
---|
346 | */
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * @}
|
---|
350 | */
|
---|
351 |
|
---|
352 | #endif /* HAL_IWDG_MODULE_ENABLED */
|
---|
353 | /**
|
---|
354 | * @}
|
---|
355 | */
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * @}
|
---|
359 | */
|
---|
360 |
|
---|
361 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
---|