]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_wwdg.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_wwdg.c
1 /**
2 ******************************************************************************
3 * @file stm32l0xx_hal_wwdg.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 06-February-2015
7 * @brief WWDG HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Window Watchdog (WWDG) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral State functions
13 @verbatim
14 ==============================================================================
15 ##### WWDG specific features #####
16 ==============================================================================
17 [..]
18 Once enabled the WWDG generates a system reset on expiry of a programmed
19 time period, unless the program refreshes the counter (downcounter)
20 before reaching 0x3F value (i.e. a reset is generated when the counter
21 value rolls over from 0x40 to 0x3F).
22
23 (+) An MCU reset is also generated if the counter value is refreshed
24 before the counter has reached the refresh window value. This
25 implies that the counter must be refreshed in a limited window.
26 (+) Once enabled the WWDG cannot be disabled except by a system reset.
27 (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
28 reset occurs.
29 (+) The WWDG counter input clock is derived from the APB clock divided
30 by a programmable prescaler.
31 (+) WWDG counter clock = PCLK1 / Prescaler
32 WWDG timeout = (WWDG counter clock) * (counter value)
33 (+) Min-max timeout value @32 MHz(PCLK1): ~128.0 us / ~65.54 ms
34
35
36 ##### How to use this driver #####
37 ==============================================================================
38 [..]
39 (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
40 (+) Set the WWDG prescaler, refresh window and counter value
41 using HAL_WWDG_Init() function.
42 (+) Start the WWDG using HAL_WWDG_Start() function.
43 When the WWDG is enabled the counter value should be configured to
44 a value greater than 0x40 to prevent generating an immediate reset.
45 (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
46 generated when the counter reaches 0x40, and then start the WWDG using
47 HAL_WWDG_Start_IT().
48 Once enabled, EWI interrupt cannot be disabled except by a system reset.
49 (+) Then the application program must refresh the WWDG counter at regular
50 intervals during normal operation to prevent an MCU reset, using
51 HAL_WWDG_Refresh() function. This operation must occur only when
52 the counter is lower than the refresh window value already programmed.
53
54 *** WWDG HAL driver macros list ***
55 ==================================
56 [..]
57 Below the list of most used macros in WWDG HAL driver.
58
59 (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
60 (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
61 (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
62 (+) __HAL_WWDG_ENABLE_IT: Enables the WWDG early wakeup interrupt
63
64 @endverbatim
65 ******************************************************************************
66 * @attention
67 *
68 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
69 *
70 * Redistribution and use in source and binary forms, with or without modification,
71 * are permitted provided that the following conditions are met:
72 * 1. Redistributions of source code must retain the above copyright notice,
73 * this list of conditions and the following disclaimer.
74 * 2. Redistributions in binary form must reproduce the above copyright notice,
75 * this list of conditions and the following disclaimer in the documentation
76 * and/or other materials provided with the distribution.
77 * 3. Neither the name of STMicroelectronics nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
80 *
81 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
82 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
84 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
87 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
88 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
89 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
90 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91 *
92 ******************************************************************************
93 */
94
95 /* Includes ------------------------------------------------------------------*/
96 #include "stm32l0xx_hal.h"
97
98 /** @addtogroup STM32L0xx_HAL_Driver
99 * @{
100 */
101
102 /** @addtogroup WWDG
103 * @brief WWDG HAL module driver.
104 * @{
105 */
106
107 #ifdef HAL_WWDG_MODULE_ENABLED
108
109 /** @addtogroup WWDG_Exported_Functions WWDG Exported Functions
110 * @{
111 */
112
113 /** @addtogroup WWDG_Exported_Functions_Group1
114 * @brief Initialization and Configuration functions.
115 *
116 @verbatim
117 ==============================================================================
118 ##### Initialization and de-initialization functions #####
119 ==============================================================================
120 [..]
121 This section provides functions allowing to:
122 (+) Initialize the WWDG according to the specified parameters
123 in the WWDG_InitTypeDef and create the associated handle
124 (+) DeInitialize the WWDG peripheral
125 (+) Initialize the WWDG MSP
126 (+) DeInitialize the WWDG MSP
127
128 @endverbatim
129 * @{
130 */
131
132 /**
133 * @brief Initializes the WWDG according to the specified
134 * parameters in the WWDG_InitTypeDef and creates the associated handle.
135 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
136 * the configuration information for the specified WWDG module.
137 * @retval HAL status
138 */
139 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
140 {
141
142 /* Check the WWDG handle allocation */
143 if(hwwdg == NULL)
144 {
145 return HAL_ERROR;
146 }
147
148 /* Check the parameters */
149 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
150 assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
151 assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
152 assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
153
154 if(hwwdg->State == HAL_WWDG_STATE_RESET)
155 {
156 /* Init the low level hardware */
157 HAL_WWDG_MspInit(hwwdg);
158 }
159
160 /* Take lock and change peripheral state */
161 __HAL_LOCK(hwwdg);
162 hwwdg->State = HAL_WWDG_STATE_BUSY;
163
164 /* Set WWDG Prescaler and Window and Counter*/
165 MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
166 MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
167
168 /* Change peripheral state and release lock*/
169 hwwdg->State = HAL_WWDG_STATE_READY;
170 __HAL_UNLOCK(hwwdg);
171
172 /* Return function status */
173 return HAL_OK;
174 }
175
176 /**
177 * @brief DeInitializes the WWDG peripheral.
178 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
179 * the configuration information for the specified WWDG module.
180 * @retval HAL status
181 */
182 HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
183 {
184 /* Check the parameters */
185 assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
186
187 /* Take lock and change peripheral state */
188 __HAL_LOCK(hwwdg);
189 hwwdg->State = HAL_WWDG_STATE_BUSY;
190
191 /* DeInit the low level hardware */
192 HAL_WWDG_MspDeInit(hwwdg);
193
194 /* Reset WWDG Control, configuration and status register */
195 MODIFY_REG(hwwdg->Instance->CR, (WWDG_CR_T | WWDG_CR_WDGA),0x0000007F);
196 MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W | WWDG_CFR_EWI),0x0000007F);
197 MODIFY_REG(hwwdg->Instance->SR,WWDG_SR_EWIF,0x0);
198
199 /* Change peripheral state and release lock*/
200 hwwdg->State = HAL_WWDG_STATE_RESET;
201 __HAL_UNLOCK(hwwdg);
202
203 /* Return function status */
204 return HAL_OK;
205 }
206
207 /**
208 * @brief Initializes the WWDG MSP.
209 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
210 * the configuration information for the specified WWDG module.
211 * @retval None
212 */
213 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
214 {
215 /* NOTE: This function Should not be modified, when the callback is needed,
216 the HAL_WWDG_MspInit could be implemented in the user file
217 */
218 }
219
220 /**
221 * @brief DeInitializes the WWDG MSP.
222 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
223 * the configuration information for the specified WWDG module.
224 * @retval None
225 */
226 __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
227 {
228 /* NOTE: This function Should not be modified, when the callback is needed,
229 the HAL_WWDG_MspDeInit could be implemented in the user file
230 */
231 }
232
233 /**
234 * @}
235 */
236
237 /** @addtogroup WWDG_Exported_Functions_Group2
238 * @brief IO operation functions
239 *
240 @verbatim
241 ==============================================================================
242 ##### IO operation functions #####
243 ==============================================================================
244 [..]
245 This section provides functions allowing to:
246 (+) Start the WWDG.
247 (+) Refresh the WWDG.
248 (+) Handle WWDG interrupt request.
249
250 @endverbatim
251 * @{
252 */
253
254 /**
255 * @brief Starts the WWDG.
256 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
257 * the configuration information for the specified WWDG module.
258 * @retval HAL status
259 */
260 HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
261 {
262 /* Take lock and change peripheral state */
263 __HAL_LOCK(hwwdg);
264 hwwdg->State = HAL_WWDG_STATE_BUSY;
265
266 /* Enable the peripheral */
267 __HAL_WWDG_ENABLE(hwwdg);
268
269 /* Change peripheral state and release lock*/
270 hwwdg->State = HAL_WWDG_STATE_READY;
271 __HAL_UNLOCK(hwwdg);
272
273 /* Return function status */
274 return HAL_OK;
275 }
276
277 /**
278 * @brief Starts the WWDG with interrupt enabled.
279 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
280 * the configuration information for the specified WWDG module.
281 * @retval HAL status
282 */
283 HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
284 {
285 /* Take lock and change peripheral state */
286 __HAL_LOCK(hwwdg);
287 hwwdg->State = HAL_WWDG_STATE_BUSY;
288
289 /* Enable the Early Wakeup Interrupt */
290 __HAL_WWDG_ENABLE_IT(hwwdg,WWDG_IT_EWI);
291
292 /* Enable the peripheral */
293 __HAL_WWDG_ENABLE(hwwdg);
294
295 /* Change peripheral state and release lock*/
296 hwwdg->State = HAL_WWDG_STATE_READY;
297 __HAL_UNLOCK(hwwdg);
298
299 /* Return function status */
300 return HAL_OK;
301 }
302
303 /**
304 * @brief Refreshes the WWDG.
305 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
306 * the configuration information for the specified WWDG module.
307 * @param Counter: value of counter to put in WWDG counter
308 * @retval HAL status
309 */
310 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
311 {
312
313 /* Check the parameters */
314 assert_param(IS_WWDG_COUNTER(Counter));
315
316 /* Write to WWDG CR the WWDG Counter value to refresh with */
317 MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
318
319 /* Return function status */
320 return HAL_OK;
321 }
322
323 /**
324 * @brief Handles WWDG interrupt request.
325 * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations
326 * or data logging must be performed before the actual reset is generated.
327 * The EWI interrupt is enabled using __HAL_WWDG_ENABLE_IT() macro.
328 * When the downcounter reaches the value 0x40, and EWI interrupt is
329 * generated and the corresponding Interrupt Service Routine (ISR) can
330 * be used to trigger specific actions (such as communications or data
331 * logging), before resetting the device.
332 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
333 * the configuration information for the specified WWDG module.
334 * @retval None
335 */
336 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
337 {
338 /* Check if Early Wakeup Interrupt is enable */
339 if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
340 {
341 /* Check if WWDG Early Wakeup Interrupt occurred */
342 if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
343 {
344 /* Early Wakeup callback */
345 HAL_WWDG_WakeupCallback(hwwdg);
346
347 /* Clear the WWDG Data Ready flag */
348 __HAL_WWDG_CLEAR_IT(hwwdg, WWDG_FLAG_EWIF);
349 }
350 }
351 }
352
353 /**
354 * @brief Early Wakeup WWDG callback.
355 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
356 * the configuration information for the specified WWDG module.
357 * @retval None
358 */
359 __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
360 {
361 }
362
363 /**
364 * @}
365 */
366
367 /** @addtogroup WWDG_Exported_Functions_Group3
368 * @brief Peripheral State functions.
369 *
370 @verbatim
371 ==============================================================================
372 ##### Peripheral State functions #####
373 ==============================================================================
374 [..]
375 This subsection permits to get in run-time the status of the peripheral
376 and the data flow.
377
378 @endverbatim
379 * @{
380 */
381
382 /**
383 * @brief Returns the WWDG state.
384 * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains
385 * the configuration information for the specified WWDG module.
386 * @retval HAL state
387 */
388 HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
389 {
390 return hwwdg->State;
391 }
392
393 /**
394 * @}
395 */
396
397 /**
398 * @}
399 */
400
401 #endif /* HAL_WWDG_MODULE_ENABLED */
402 /**
403 * @}
404 */
405
406 /**
407 * @}
408 */
409
410 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
411
Imprint / Impressum