]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_iwdg.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L1 / stm32l1xx_hal_iwdg.c
1 /**
2 ******************************************************************************
3 * @file stm32l1xx_hal_iwdg.c
4 * @author MCD Application Team
5 * @version V1.0.0
6 * @date 5-September-2014
7 * @brief IWDG HAL module driver.
8 *
9 * This file provides firmware functions to manage the following
10 * functionalities of the IWDG peripheral:
11 * + Initialization and Configuration functions
12 * + IO operation functions
13 * + Peripheral State functions
14 *
15 @verbatim
16
17 ================================================================================
18 ##### IWDG specific features #####
19 ================================================================================
20 [..]
21 (+) The IWDG can be started by either software or hardware (configurable
22 through option byte).
23 (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
24 thus stays active even if the main clock fails.
25 (+) Once the IWDG is started, the LSI is forced ON and cannot be disabled
26 (LSI cannot be disabled too), and the counter starts counting down from
27 the reset value of 0xFFF. When it reaches the end of count value (0x000)
28 a system reset is generated.
29 (+) The IWDG counter should be refreshed at regular intervals, otherwise the
30 watchdog generates an MCU reset when the counter reaches 0.
31 (+) The IWDG is implemented in the VDD voltage domain that is still functional
32 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
33 (+) IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
34 reset occurs.
35
36 (+) Min-max timeout value @37KHz (LSI): ~108us / ~28.3s
37 The IWDG timeout may vary due to LSI frequency dispersion. STM32L1xx
38 devices provide the capability to measure the LSI frequency (LSI clock
39 connected internally to TIM10 CH1 input capture). The measured value
40 can be used to have an IWDG timeout with an acceptable accuracy.
41 For more information, please refer to the STM32L1xx Reference manual.
42
43 ##### How to use this driver #####
44 ==============================================================================
45 [..]
46 (+) Set the IWDG prescaler and reload value
47 using HAL_IWDG_Init() function.
48 (+) Use IWDG using HAL_IWDG_Start() function to:
49 (++) Enable write access to IWDG_PR and IWDG_RLR registers.
50 (++) Configure the IWDG prescaler and counter reload values.
51 (++) Reload IWDG counter with value defined in the IWDG_RLR register.
52 (++) Start the IWDG, when the IWDG is used in software mode (no need
53 to enable the LSI, it will be enabled by hardware).
54 (+) Then the application program must refresh the IWDG counter at regular
55 intervals during normal operation to prevent an MCU reset, using
56 HAL_IWDG_Refresh() function.
57
58 *** IWDG HAL driver macros list ***
59 ====================================
60 [..]
61 Below the list of most used macros in IWDG HAL driver.
62
63 (+) __HAL_IWDG_START: Enable the IWDG peripheral
64 (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register
65 (+) __HAL_IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers
66 (+) __HAL_IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers
67 (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
68 (+) __HAL_IWDG_CLEAR_FLAG: Clear the IWDG's pending flags
69
70 @endverbatim
71 ******************************************************************************
72 * @attention
73 *
74 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
75 *
76 * Redistribution and use in source and binary forms, with or without modification,
77 * are permitted provided that the following conditions are met:
78 * 1. Redistributions of source code must retain the above copyright notice,
79 * this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright notice,
81 * this list of conditions and the following disclaimer in the documentation
82 * and/or other materials provided with the distribution.
83 * 3. Neither the name of STMicroelectronics nor the names of its contributors
84 * may be used to endorse or promote products derived from this software
85 * without specific prior written permission.
86 *
87 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
90 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
93 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
94 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
95 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
96 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97 *
98 ******************************************************************************
99 */
100
101 /* Includes ------------------------------------------------------------------*/
102 #include "stm32l1xx_hal.h"
103
104 /** @addtogroup STM32L1xx_HAL_Driver
105 * @{
106 */
107
108 /** @defgroup IWDG IWDG
109 * @brief IWDG HAL module driver.
110 * @{
111 */
112
113 #ifdef HAL_IWDG_MODULE_ENABLED
114
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
117
118 /** @defgroup IWDG_Private_Defines IWDG Private Defines
119 * @{
120 */
121
122 #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000
123
124 /**
125 * @}
126 */
127
128 /* Private macro -------------------------------------------------------------*/
129 /* Private variables ---------------------------------------------------------*/
130 /* Private function prototypes -----------------------------------------------*/
131 /* Private functions ---------------------------------------------------------*/
132
133 /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
134 * @{
135 */
136
137 /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
138 * @brief Initialization and Configuration functions.
139 *
140 @verbatim
141 ===============================================================================
142 ##### Initialization and de-initialization functions #####
143 ===============================================================================
144 [..] This section provides functions allowing to:
145 (+) Initialize the IWDG according to the specified parameters
146 in the IWDG_InitTypeDef and create the associated handle
147 (+) Initialize the IWDG MSP
148 (+) DeInitialize IWDG MSP
149
150 @endverbatim
151 * @{
152 */
153
154 /**
155 * @brief Initializes the IWDG according to the specified
156 * parameters in the IWDG_InitTypeDef and creates the associated handle.
157 * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
158 * the configuration information for the specified IWDG module.
159 * @retval HAL status
160 */
161 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
162 {
163 /* Check the IWDG handle allocation */
164 if(hiwdg == HAL_NULL)
165 {
166 return HAL_ERROR;
167 }
168
169 /* Check the parameters */
170 assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
171 assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
172 assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
173
174 /* Check pending flag, if previous update not done, return error */
175 if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
176 &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET))
177 {
178 return HAL_ERROR;
179 }
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 __HAL_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 ) > HAL_IWDG_DEFAULT_TIMEOUT)
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****/
360
Imprint / Impressum