c0a83c6c420aa8ce85305618acc23413f819591a
[mTask.git] / int / com / lib / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_iwdg.c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_iwdg.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 22-April-2016
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 Start functions
11 * + IO operation functions
12 *
13 @verbatim
14 ==============================================================================
15 ##### IWDG Generic features #####
16 ==============================================================================
17 [..]
18 (+) The IWDG can be started by either software or hardware (configurable
19 through option byte).
20
21 (+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even
22 if the main clock fails.
23
24 (+) Once the IWDG is started, the LSI is forced ON and both can not be
25 disabled. The counter starts counting down from the reset value (0xFFF).
26 When it reaches the end of count value (0x000) a reset signal is
27 generated (IWDG reset).
28
29 (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
30 the IWDG_RLR value is reloaded in the counter and the watchdog reset is
31 prevented.
32
33 (+) The IWDG is implemented in the VDD voltage domain that is still functional
34 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
35 IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
36 reset occurs.
37
38 (+) Debug mode : When the microcontroller enters debug mode (core halted),
39 the IWDG counter either continues to work normally or stops, depending
40 on DBG_IWDG_STOP configuration bit in DBG module, accessible through
41 __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros
42
43 [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
44 The IWDG timeout may vary due to LSI frequency dispersion. STM32F7xx
45 devices provide the capability to measure the LSI frequency (LSI clock
46 connected internally to TIM16 CH1 input capture). The measured value
47 can be used to have an IWDG timeout with an acceptable accuracy.
48
49 ##### How to use this driver #####
50 ==============================================================================
51 [..]
52 (#) Use IWDG using HAL_IWDG_Init() function to :
53 (+) Enable instance by writing Start keyword in IWDG_KEY register. LSI
54 clock is forced ON and IWDG counter starts downcounting.
55 (+) Enable write access to configuration register: IWDG_PR, IWDG_RLR &
56 IWDG_WINR.
57 (+) Configure the IWDG prescaler and counter reload value. This reload
58 value will be loaded in the IWDG counter each time the watchdog is
59 reloaded, then the IWDG will start counting down from this value.
60 (+) wait for status flags to be reset"
61 (+) Depending on window parameter:
62 (++) If Window Init parameter is same as Window register value,
63 nothing more is done but reload counter value in order to exit
64 function withy exact time base.
65 (++) Else modify Window register. This will automatically reload
66 watchdog counter.
67
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 (+) __HAL_IWDG_START: Enable the IWDG peripheral
77 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
78 the reload register
79
80 @endverbatim
81 ******************************************************************************
82 * @attention
83 *
84 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
85 *
86 * Redistribution and use in source and binary forms, with or without modification,
87 * are permitted provided that the following conditions are met:
88 * 1. Redistributions of source code must retain the above copyright notice,
89 * this list of conditions and the following disclaimer.
90 * 2. Redistributions in binary form must reproduce the above copyright notice,
91 * this list of conditions and the following disclaimer in the documentation
92 * and/or other materials provided with the distribution.
93 * 3. Neither the name of STMicroelectronics nor the names of its contributors
94 * may be used to endorse or promote products derived from this software
95 * without specific prior written permission.
96 *
97 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
98 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
100 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
103 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
104 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
105 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
106 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
107 *
108 ******************************************************************************
109 */
110
111 /* Includes ------------------------------------------------------------------*/
112 #include "stm32f7xx_hal.h"
113
114 /** @addtogroup STM32F7xx_HAL_Driver
115 * @{
116 */
117
118 #ifdef HAL_IWDG_MODULE_ENABLED
119 /** @addtogroup IWDG
120 * @brief IWDG HAL module driver.
121 * @{
122 */
123
124 /* Private typedef -----------------------------------------------------------*/
125 /* Private define ------------------------------------------------------------*/
126 /** @defgroup IWDG_Private_Defines IWDG Private Defines
127 * @{
128 */
129 /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
130 higher prescaler (256), and according to LSI variation, we need to wait at
131 least 6 cycles so 48 ms. */
132 #define HAL_IWDG_DEFAULT_TIMEOUT 48u
133 /**
134 * @}
135 */
136
137 /* Private macro -------------------------------------------------------------*/
138 /* Private variables ---------------------------------------------------------*/
139 /* Private function prototypes -----------------------------------------------*/
140 /* Exported functions --------------------------------------------------------*/
141
142 /** @addtogroup IWDG_Exported_Functions
143 * @{
144 */
145
146 /** @addtogroup IWDG_Exported_Functions_Group1
147 * @brief Initialization and Start functions.
148 *
149 @verbatim
150 ===============================================================================
151 ##### Initialization and Start functions #####
152 ===============================================================================
153 [..] This section provides functions allowing to:
154 (+) Initialize the IWDG according to the specified parameters in the
155 IWDG_InitTypeDef of associated handle.
156 (+) Manage Window option.
157 (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
158 is reloaded in order to exit function with correct time base.
159
160 @endverbatim
161 * @{
162 */
163
164 /**
165 * @brief Initialize the IWDG according to the specified parameters in the
166 * IWDG_InitTypeDef and start watchdog. Before exiting function,
167 * watchdog is refreshed in order to have correct time base.
168 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
169 * the configuration information for the specified IWDG module.
170 * @retval HAL status
171 */
172 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
173 {
174 uint32_t tickstart;
175
176 /* Check the IWDG handle allocation */
177 if(hiwdg == NULL)
178 {
179 return HAL_ERROR;
180 }
181
182 /* Check the parameters */
183 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
184 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
185 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
186 assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
187
188 /* Enable IWDG. LSI is turned on automaticaly */
189 __HAL_IWDG_START(hiwdg);
190
191 /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
192 0x5555 in KR */
193 IWDG_ENABLE_WRITE_ACCESS(hiwdg);
194
195 /* Write to IWDG registers the Prescaler & Reload values to work with */
196 hiwdg->Instance->PR = hiwdg->Init.Prescaler;
197 hiwdg->Instance->RLR = hiwdg->Init.Reload;
198
199 /* Check pending flag, if previous update not done, return timeout */
200 tickstart = HAL_GetTick();
201
202 /* Wait for register to be updated */
203 while(hiwdg->Instance->SR != RESET)
204 {
205 if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
206 {
207 return HAL_TIMEOUT;
208 }
209 }
210
211 /* If window parameter is different than current value, modify window
212 register */
213 if(hiwdg->Instance->WINR != hiwdg->Init.Window)
214 {
215 /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
216 even if window feature is disabled, Watchdog will be reloaded by writing
217 windows register */
218 hiwdg->Instance->WINR = hiwdg->Init.Window;
219 }
220 else
221 {
222 /* Reload IWDG counter with value defined in the reload register */
223 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
224 }
225
226 /* Return function status */
227 return HAL_OK;
228 }
229
230 /**
231 * @}
232 */
233
234
235 /** @addtogroup IWDG_Exported_Functions_Group2
236 * @brief IO operation functions
237 *
238 @verbatim
239 ===============================================================================
240 ##### IO operation functions #####
241 ===============================================================================
242 [..] This section provides functions allowing to:
243 (+) Refresh the IWDG.
244
245 @endverbatim
246 * @{
247 */
248
249
250 /**
251 * @brief Refresh the IWDG.
252 * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
253 * the configuration information for the specified IWDG module.
254 * @retval HAL status
255 */
256 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
257 {
258 /* Reload IWDG counter with value defined in the reload register */
259 __HAL_IWDG_RELOAD_COUNTER(hiwdg);
260
261 /* Return function status */
262 return HAL_OK;
263 }
264
265 /**
266 * @}
267 */
268
269 /**
270 * @}
271 */
272
273 #endif /* HAL_IWDG_MODULE_ENABLED */
274 /**
275 * @}
276 */
277
278 /**
279 * @}
280 */
281
282 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/