]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_iwdg.c
2 ******************************************************************************
3 * @file stm32f0xx_hal_iwdg.c
4 * @author MCD Application Team
6 * @date 11-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
15 ===============================================================================
16 ##### IWDG Specific features #####
17 ===============================================================================
19 (+) The IWDG can be started by either software or hardware (configurable
21 (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
22 thus stays active even if the main clock fails.
23 (+) Once the IWDG is started, the LSI is forced ON and cannot be disabled
24 (LSI cannot be disabled too), and the counter starts counting down from
25 the reset value of 0xFFF. When it reaches the end of count value (0x000)
26 a system reset is generated.
27 (+) The IWDG counter should be refreshed at regular intervals, otherwise the
28 watchdog generates an MCU reset when the counter reaches 0.
29 (+) The IWDG is implemented in the VDD voltage domain that is still functional
30 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
31 (+) IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
33 (+) Min-max timeout value @41KHz (LSI): ~0.1ms / ~25.5s
34 The IWDG timeout may vary due to LSI frequency dispersion. STM32F0x
35 devices provide the capability to measure the LSI frequency (LSI clock
36 connected internally to TIM16 CH1 input capture). The measured value
37 can be used to have an IWDG timeout with an acceptable accuracy.
38 For more information, please refer to the STM32F0x Reference manual.
40 ##### How to use this driver #####
41 ===============================================================================
43 (#) if Window option is disabled
44 (++) Use IWDG using HAL_IWDG_Init() function to :
45 (+++) Enable write access to IWDG_PR, IWDG_RLR.
46 (+++) Configure the IWDG prescaler, counter reload value.
47 This reload value will be loaded in the IWDG counter each time the counter
48 is reloaded, then the IWDG will start counting down from this value.
49 (++) Use IWDG using HAL_IWDG_Start() function to :
50 (+++) Reload IWDG counter with value defined in the IWDG_RLR register.
51 (+++) Start the IWDG, when the IWDG is used in software mode (no need
52 to enable the LSI, it will be enabled by hardware).
53 (++) Then the application program must refresh the IWDG counter at regular
54 intervals during normal operation to prevent an MCU reset, using
55 HAL_IWDG_Refresh() function.
56 (#) if Window option is enabled:
57 (++) Use IWDG using HAL_IWDG_Start() function to enable IWDG downcounter
58 (++) Use IWDG using HAL_IWDG_Init() function to :
59 (+++) Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.
60 (+++) Configure the IWDG prescaler, reload value and window value.
61 (++) Then the application program must refresh the IWDG counter at regular
62 intervals during normal operation to prevent an MCU reset, using
63 HAL_IWDG_Refresh() function.
65 *** IWDG HAL driver macros list ***
66 ====================================
68 Below the list of most used macros in IWDG HAL driver.
70 (+) __HAL_IWDG_START: Enable the IWDG peripheral
71 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register
72 (+) __HAL_IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers
73 (+) __HAL_IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers
74 (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
77 ******************************************************************************
80 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
82 * Redistribution and use in source and binary forms, with or without modification,
83 * are permitted provided that the following conditions are met:
84 * 1. Redistributions of source code must retain the above copyright notice,
85 * this list of conditions and the following disclaimer.
86 * 2. Redistributions in binary form must reproduce the above copyright notice,
87 * this list of conditions and the following disclaimer in the documentation
88 * and/or other materials provided with the distribution.
89 * 3. Neither the name of STMicroelectronics nor the names of its contributors
90 * may be used to endorse or promote products derived from this software
91 * without specific prior written permission.
93 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
94 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
96 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
97 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
99 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
100 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
101 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
102 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
104 ******************************************************************************
107 /* Includes ------------------------------------------------------------------*/
108 #include "stm32f0xx_hal.h"
110 /** @addtogroup STM32F0xx_HAL_Driver
114 /** @defgroup IWDG IWDG HAL module driver
115 * @brief IWDG HAL module driver.
119 #ifdef HAL_IWDG_MODULE_ENABLED
121 /* Private typedef -----------------------------------------------------------*/
122 /* Private define ------------------------------------------------------------*/
124 /** @defgroup IWDG_Private_Defines IWDG Private Defines
128 #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000
134 /* Private macro -------------------------------------------------------------*/
135 /* Private variables ---------------------------------------------------------*/
136 /* Private function prototypes -----------------------------------------------*/
137 /* Private functions ---------------------------------------------------------*/
139 /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
143 /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
144 * @brief Initialization and Configuration functions.
147 ===============================================================================
148 ##### Initialization functions #####
149 ===============================================================================
150 [..] This section provides functions allowing to:
151 (+) Initialize the IWDG according to the specified parameters
152 in the IWDG_InitTypeDef and create the associated handle
153 (+) Manage Window option
154 (+) Initialize the IWDG MSP
155 (+) DeInitialize IWDG MSP
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.
168 HAL_StatusTypeDef
HAL_IWDG_Init(IWDG_HandleTypeDef
*hiwdg
)
170 uint32_t tickstart
= 0;
172 /* Check the IWDG handle allocation */
178 /* Check the parameters */
179 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg
->Instance
));
180 assert_param(IS_IWDG_PRESCALER(hiwdg
->Init
.Prescaler
));
181 assert_param(IS_IWDG_RELOAD(hiwdg
->Init
.Reload
));
182 assert_param(IS_IWDG_WINDOW(hiwdg
->Init
.Window
));
184 /* Check pending flag, if previous update not done, return error */
185 if((__HAL_IWDG_GET_FLAG(hiwdg
, IWDG_FLAG_PVU
) != RESET
)
186 &&(__HAL_IWDG_GET_FLAG(hiwdg
, IWDG_FLAG_RVU
) != RESET
)
187 &&(__HAL_IWDG_GET_FLAG(hiwdg
, IWDG_FLAG_WVU
) != RESET
))
192 if(hiwdg
->State
== HAL_IWDG_STATE_RESET
)
194 /* Init the low level hardware */
195 HAL_IWDG_MspInit(hiwdg
);
198 /* Change IWDG peripheral state */
199 hiwdg
->State
= HAL_IWDG_STATE_BUSY
;
201 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers */
202 /* by writing 0x5555 in KR */
203 __HAL_IWDG_ENABLE_WRITE_ACCESS(hiwdg
);
205 /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
206 MODIFY_REG(hiwdg
->Instance
->PR
, IWDG_PR_PR
, hiwdg
->Init
.Prescaler
);
207 MODIFY_REG(hiwdg
->Instance
->RLR
, IWDG_RLR_RL
, hiwdg
->Init
.Reload
);
209 /* check if window option is enabled */
210 if (((hiwdg
->Init
.Window
) != IWDG_WINDOW_DISABLE
) || ((hiwdg
->Instance
->WINR
) != IWDG_WINDOW_DISABLE
))
212 tickstart
= HAL_GetTick();
214 /* Wait for register to be updated */
215 while((uint32_t)(hiwdg
->Instance
->SR
) != RESET
)
217 if((HAL_GetTick() - tickstart
) > HAL_IWDG_DEFAULT_TIMEOUT
)
220 hiwdg
->State
= HAL_IWDG_STATE_TIMEOUT
;
225 /* Write to IWDG WINR the IWDG_Window value to compare with */
226 MODIFY_REG(hiwdg
->Instance
->WINR
, IWDG_WINR_WIN
, hiwdg
->Init
.Window
);
229 /* Change IWDG peripheral state */
230 hiwdg
->State
= HAL_IWDG_STATE_READY
;
232 /* Return function status */
237 * @brief Initializes the IWDG MSP.
238 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
239 * the configuration information for the specified IWDG module.
242 __weak
void HAL_IWDG_MspInit(IWDG_HandleTypeDef
*hiwdg
)
244 /* NOTE : This function Should not be modified, when the callback is needed,
245 the HAL_IWDG_MspInit could be implemented in the user file
253 /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
254 * @brief IO operation functions
257 ===============================================================================
258 ##### IO operation functions #####
259 ===============================================================================
260 [..] This section provides functions allowing to:
262 (+) Refresh the IWDG.
269 * @brief Starts the IWDG.
270 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
271 * the configuration information for the specified IWDG module.
274 HAL_StatusTypeDef
HAL_IWDG_Start(IWDG_HandleTypeDef
*hiwdg
)
276 uint32_t tickstart
= 0;
281 /* Change IWDG peripheral state */
282 hiwdg
->State
= HAL_IWDG_STATE_BUSY
;
284 /* Reload IWDG counter with value defined in the RLR register */
285 if ((hiwdg
->Init
.Window
) == IWDG_WINDOW_DISABLE
)
287 __HAL_IWDG_RELOAD_COUNTER(hiwdg
);
290 /* Enable the IWDG peripheral */
291 __HAL_IWDG_START(hiwdg
);
293 tickstart
= HAL_GetTick();
295 /* Wait until PVU, RVU, WVU flag are RESET */
296 while( (__HAL_IWDG_GET_FLAG(hiwdg
, IWDG_FLAG_PVU
) != RESET
)
297 &&(__HAL_IWDG_GET_FLAG(hiwdg
, IWDG_FLAG_RVU
) != RESET
)
298 &&(__HAL_IWDG_GET_FLAG(hiwdg
, IWDG_FLAG_WVU
) != RESET
) )
300 if((HAL_GetTick() - tickstart
) > HAL_IWDG_DEFAULT_TIMEOUT
)
303 hiwdg
->State
= HAL_IWDG_STATE_TIMEOUT
;
305 /* Process unlocked */
312 /* Change IWDG peripheral state */
313 hiwdg
->State
= HAL_IWDG_STATE_READY
;
315 /* Process Unlocked */
318 /* Return function status */
323 * @brief Refreshes the IWDG.
324 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
325 * the configuration information for the specified IWDG module.
328 HAL_StatusTypeDef
HAL_IWDG_Refresh(IWDG_HandleTypeDef
*hiwdg
)
330 uint32_t tickstart
= 0;
335 /* Change IWDG peripheral state */
336 hiwdg
->State
= HAL_IWDG_STATE_BUSY
;
338 tickstart
= HAL_GetTick();
340 /* Wait until RVU flag is RESET */
341 while(__HAL_IWDG_GET_FLAG(hiwdg
, IWDG_FLAG_RVU
) != RESET
)
343 if((HAL_GetTick() - tickstart
) > HAL_IWDG_DEFAULT_TIMEOUT
)
346 hiwdg
->State
= HAL_IWDG_STATE_TIMEOUT
;
348 /* Process unlocked */
355 /* Reload IWDG counter with value defined in the reload register */
356 __HAL_IWDG_RELOAD_COUNTER(hiwdg
);
358 /* Change IWDG peripheral state */
359 hiwdg
->State
= HAL_IWDG_STATE_READY
;
361 /* Process Unlocked */
364 /* Return function status */
372 /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
373 * @brief Peripheral State functions.
376 ===============================================================================
377 ##### Peripheral State functions #####
378 ===============================================================================
380 This subsection permits to get in run-time the status of the peripheral
388 * @brief Returns the IWDG state.
389 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
390 * the configuration information for the specified IWDG module.
393 HAL_IWDG_StateTypeDef
HAL_IWDG_GetState(IWDG_HandleTypeDef
*hiwdg
)
406 #endif /* HAL_IWDG_MODULE_ENABLED */
415 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/