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