]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F1/stm32f1xx_hal_wwdg.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F1 / stm32f1xx_hal_wwdg.c
1 /**
2   ******************************************************************************
3   * @file    stm32f1xx_hal_wwdg.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    15-December-2014
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   *         
14   @verbatim
15   ==============================================================================
16                       ##### WWDG specific features #####
17   ==============================================================================
18   [..] 
19     Once enabled the WWDG generates a system reset on expiry of a programmed
20     time period, unless the program refreshes the Counter (T[6;0] 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 clock (Hz) = PCLK / (4096 * Prescaler)
33     (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock
34         where T[5;0] are the lowest 6 bits of Counter.
35     (+) WWDG Counter refresh is allowed between the following limits :
36         (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
37         (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
38     
39     (+) Min-max timeout value @48 MHz(PCLK): ~85,3us / ~5,46 ms
40
41                      ##### How to use this driver #####
42   ==============================================================================
43   [..]
44     (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
45     (+) Set the WWDG prescaler, refresh window and counter value 
46         using HAL_WWDG_Init() function.
47     (+) Start the WWDG using HAL_WWDG_Start() function.
48         When the WWDG is enabled the counter value should be configured to 
49         a value greater than 0x40 to prevent generating an immediate reset.
50     (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is 
51         generated when the counter reaches 0x40, and then start the WWDG using
52         HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can 
53         add his own code by customization of function pointer HAL_WWDG_WakeupCallback
54         Once enabled, EWI interrupt cannot be disabled except by a system reset.          
55     (+) Then the application program must refresh the WWDG counter at regular
56         intervals during normal operation to prevent an MCU reset, using
57         HAL_WWDG_Refresh() function. This operation must occur only when
58         the counter is lower than the refresh window value already programmed.
59         
60      *** WWDG HAL driver macros list ***
61      ==================================
62      [..]
63        Below the list of most used macros in WWDG HAL driver.
64        
65       (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral 
66       (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
67       (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags 
68       (+) __HAL_WWDG_ENABLE_IT:  Enables the WWDG early wakeup interrupt 
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 "stm32f1xx_hal.h"
103
104 /** @addtogroup STM32F1xx_HAL_Driver
105   * @{
106   */
107
108 /** @defgroup WWDG WWDG
109   * @brief WWDG HAL module driver.
110   * @{
111   */
112
113 #ifdef HAL_WWDG_MODULE_ENABLED
114
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
117 /* Private macro -------------------------------------------------------------*/
118 /* Private variables ---------------------------------------------------------*/
119 /* Private function prototypes -----------------------------------------------*/
120 /* Exported functions --------------------------------------------------------*/
121
122 /** @defgroup WWDG_Exported_Functions WWDG Exported Functions
123   * @{
124   */
125
126 /** @defgroup WWDG_Exported_Functions_Group1 Initialization and de-initialization functions 
127  *  @brief    Initialization and Configuration functions. 
128  *
129 @verbatim      
130   ==============================================================================
131           ##### Initialization and de-initialization functions #####
132   ==============================================================================
133   [..]  
134     This section provides functions allowing to:
135     (+) Initialize the WWDG according to the specified parameters 
136         in the WWDG_InitTypeDef and create the associated handle
137     (+) DeInitialize the WWDG peripheral
138     (+) Initialize the WWDG MSP
139     (+) DeInitialize the WWDG MSP 
140  
141 @endverbatim
142   * @{
143   */
144
145 /**
146   * @brief  Initializes the WWDG according to the specified
147   *         parameters in the WWDG_InitTypeDef and creates the associated handle.
148   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
149   *              the configuration information for the specified WWDG module.
150   * @retval HAL status
151   */
152 HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
153 {
154   /* Check the WWDG handle allocation */
155   if(hwwdg == NULL)
156   {
157     return HAL_ERROR;
158   }
159
160   /* Check the parameters */
161   assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
162   assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
163   assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window)); 
164   assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter)); 
165   
166   if(hwwdg->State == HAL_WWDG_STATE_RESET)
167   {
168     /* Allocate lock resource and initialize it */
169     hwwdg-> Lock = HAL_UNLOCKED;
170     
171     /* Init the low level hardware */
172     HAL_WWDG_MspInit(hwwdg);
173   }
174   
175   /* Change WWDG peripheral state */
176   hwwdg->State = HAL_WWDG_STATE_BUSY;
177
178   /* Set WWDG Prescaler and Window */
179   MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
180  
181   /* Set WWDG Counter */
182   MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
183
184   /* Change WWDG peripheral state */
185   hwwdg->State = HAL_WWDG_STATE_READY;
186   
187   /* Return function status */
188   return HAL_OK;
189 }
190
191 /**
192   * @brief  DeInitializes the WWDG peripheral. 
193   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
194   *              the configuration information for the specified WWDG module.
195   * @retval HAL status
196   */
197 HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
198
199   /* Check the WWDG handle allocation */
200   if(hwwdg == NULL)
201   {
202     return HAL_ERROR;
203   }
204
205   /* Check the parameters */
206   assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
207
208   /* Change WWDG peripheral state */  
209   hwwdg->State = HAL_WWDG_STATE_BUSY;
210   
211   /* DeInit the low level hardware */
212   HAL_WWDG_MspDeInit(hwwdg);
213   
214   /* Reset WWDG Control register */
215   hwwdg->Instance->CR  = (uint32_t)0x0000007F;
216   
217   /* Reset WWDG Configuration register */
218   hwwdg->Instance->CFR = (uint32_t)0x0000007F;
219   
220   /* Reset WWDG Status register */
221   hwwdg->Instance->SR  = 0; 
222   
223   /* Change WWDG peripheral state */    
224   hwwdg->State = HAL_WWDG_STATE_RESET; 
225
226   /* Release Lock */
227   __HAL_UNLOCK(hwwdg);
228
229   /* Return function status */
230   return HAL_OK;
231 }
232
233 /**
234   * @brief  Initializes the WWDG MSP.
235   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
236   *              the configuration information for the specified WWDG module.
237   * @retval None
238   */
239 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
240 {
241   /* NOTE: This function Should not be modified, when the callback is needed,
242            the HAL_WWDG_MspInit could be implemented in the user file
243    */
244 }
245
246 /**
247   * @brief  DeInitializes the WWDG MSP.
248   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
249   *              the configuration information for the specified WWDG module.
250   * @retval None
251   */
252 __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
253 {
254   /* NOTE: This function Should not be modified, when the callback is needed,
255            the HAL_WWDG_MspDeInit could be implemented in the user file
256    */
257 }
258
259 /**
260   * @}
261   */
262
263 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions  
264  *  @brief   IO operation functions  
265  *
266 @verbatim   
267   ==============================================================================
268                        ##### IO operation functions #####
269   ==============================================================================  
270   [..]  
271     This section provides functions allowing to:
272     (+) Start the WWDG.
273     (+) Refresh the WWDG.
274     (+) Handle WWDG interrupt request. 
275
276 @endverbatim
277   * @{
278   */
279
280 /**
281   * @brief  Starts the WWDG.
282   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
283   *              the configuration information for the specified WWDG module.
284   * @retval HAL status
285   */
286 HAL_StatusTypeDef HAL_WWDG_Start(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 peripheral */
295   __HAL_WWDG_ENABLE(hwwdg);  
296   
297   /* Change WWDG peripheral state */    
298   hwwdg->State = HAL_WWDG_STATE_READY; 
299   
300   /* Process Unlocked */
301   __HAL_UNLOCK(hwwdg);
302   
303   /* Return function status */
304   return HAL_OK;
305 }
306
307 /**
308   * @brief  Starts the WWDG with interrupt enabled.
309   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
310   *              the configuration information for the specified WWDG module.
311   * @retval HAL status
312   */
313 HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
314 {
315   /* Process Locked */
316   __HAL_LOCK(hwwdg); 
317
318   /* Change WWDG peripheral state */  
319   hwwdg->State = HAL_WWDG_STATE_BUSY;
320
321   /* Enable the Early Wakeup Interrupt */ 
322   __HAL_WWDG_ENABLE_IT(hwwdg, WWDG_IT_EWI);
323
324   /* Enable the peripheral */
325   __HAL_WWDG_ENABLE(hwwdg);  
326
327   /* Return function status */
328   return HAL_OK;
329 }
330
331 /**
332   * @brief  Refreshes the WWDG.
333   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
334   *              the configuration information for the specified WWDG module.
335   * @param  Counter: value of counter to put in WWDG counter
336   * @retval HAL status
337   */
338 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
339 {
340   /* Process Locked */
341   __HAL_LOCK(hwwdg); 
342   
343   /* Change WWDG peripheral state */  
344   hwwdg->State = HAL_WWDG_STATE_BUSY;
345   
346   /* Check the parameters */
347   assert_param(IS_WWDG_COUNTER(Counter));
348   
349   /* Write to WWDG CR the WWDG Counter value to refresh with */
350   MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
351   
352   /* Change WWDG peripheral state */    
353   hwwdg->State = HAL_WWDG_STATE_READY; 
354   
355   /* Process Unlocked */
356   __HAL_UNLOCK(hwwdg);
357   
358   /* Return function status */
359   return HAL_OK;
360 }
361
362 /**
363   * @brief  Handles WWDG interrupt request.
364   * @note   The Early Wakeup Interrupt (EWI) can be used if specific safety operations 
365   *         or data logging must be performed before the actual reset is generated. 
366   *         The EWI interrupt is enabled when calling HAL_WWDG_Start_IT function.
367   *         When the downcounter reaches the value 0x40, and EWI interrupt is 
368   *         generated and the corresponding Interrupt Service Routine (ISR) can 
369   *         be used to trigger specific actions (such as communications or data 
370   *         logging), before resetting the device. 
371   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
372   *              the configuration information for the specified WWDG module.
373   * @retval None
374   */
375 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
376
377   /* Check if Early Wakeup Interrupt is enable */
378   if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
379   {
380     /* Wheck if WWDG Early Wakeup Interrupt occurred */
381   if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
382   {
383     /* Early Wakeup callback */ 
384     HAL_WWDG_WakeupCallback(hwwdg);
385     
386     /* Change WWDG peripheral state */
387     hwwdg->State = HAL_WWDG_STATE_READY; 
388     
389       /* Clear the WWDG Early Wakeup flag */
390     __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
391     
392     /* Process Unlocked */
393     __HAL_UNLOCK(hwwdg);
394   }
395
396 }
397
398 /**
399   * @brief  Early Wakeup WWDG callback.
400   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
401   *              the configuration information for the specified WWDG module.
402   * @retval None
403   */
404 __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
405 {
406   /* NOTE: This function Should not be modified, when the callback is needed,
407            the HAL_WWDG_WakeupCallback could be implemented in the user file
408    */
409 }
410
411 /**
412   * @}
413   */
414
415 /** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions 
416  *  @brief    Peripheral State functions. 
417  *
418 @verbatim   
419   ==============================================================================
420                         ##### Peripheral State functions #####
421   ==============================================================================  
422   [..]
423     This subsection permits to get in run-time the status of the peripheral 
424     and the data flow.
425
426 @endverbatim
427   * @{
428   */
429
430 /**
431   * @brief  Returns the WWDG state.
432   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
433   *              the configuration information for the specified WWDG module.
434   * @retval HAL state
435   */
436 HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
437 {
438   return hwwdg->State;
439 }
440
441 /**
442   * @}
443   */
444
445 /**
446   * @}
447   */
448
449 #endif /* HAL_WWDG_MODULE_ENABLED */
450 /**
451   * @}
452   */
453
454 /**
455   * @}
456   */
457
458 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/