]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_wwdg.c
Merge pull request #24 from marknsikora/master
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F0 / stm32f0xx_hal_wwdg.c
1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_hal_wwdg.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    11-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 __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 "stm32f0xx_hal.h"
103
104 /** @addtogroup STM32F0xx_HAL_Driver
105   * @{
106   */
107
108 /** @defgroup WWDG WWDG HAL module driver
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     /* Init the low level hardware */
169     HAL_WWDG_MspInit(hwwdg);
170   }
171   
172   /* Change WWDG peripheral state */
173   hwwdg->State = HAL_WWDG_STATE_BUSY;
174   
175   /* Set WWDG Prescaler and Window */
176   MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
177  
178   /* Set WWDG Counter */
179   MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
180    
181   /* Change WWDG peripheral state */
182   hwwdg->State = HAL_WWDG_STATE_READY;
183   
184   /* Return function status */
185   return HAL_OK;
186 }
187
188 /**
189   * @brief  DeInitializes the WWDG peripheral. 
190   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
191   *              the configuration information for the specified WWDG module.
192   * @retval HAL status
193   */
194 HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
195 {
196   /* Check the parameters */
197   assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
198
199   /* Change WWDG peripheral state */  
200   hwwdg->State = HAL_WWDG_STATE_BUSY;
201
202   /* DeInit the low level hardware */
203   HAL_WWDG_MspDeInit(hwwdg);
204   
205   /* Reset WWDG Control register */
206   hwwdg->Instance->CR  = (uint32_t)0x0000007F;
207     
208   /* Reset WWDG Configuration register */
209   hwwdg->Instance->CFR = (uint32_t)0x0000007F;
210   
211   /* Reset WWDG Status register */
212   hwwdg->Instance->SR  = 0; 
213   
214   /* Change WWDG peripheral state */    
215   hwwdg->State = HAL_WWDG_STATE_RESET; 
216
217   /* Release Lock */
218   __HAL_UNLOCK(hwwdg);
219
220   /* Return function status */
221   return HAL_OK;
222 }
223
224 /**
225   * @brief  Initializes the WWDG MSP.
226   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
227   *              the configuration information for the specified WWDG module.
228   * @retval None
229   */
230 __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
231 {
232   /* NOTE : This function Should not be modified, when the callback is needed,
233             the HAL_WWDG_MspInit could be implemented in the user file
234    */
235 }
236
237 /**
238   * @brief  DeInitializes the WWDG MSP.
239   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
240   *              the configuration information for the specified WWDG module.
241   * @retval None
242   */
243 __weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
244 {
245   /* NOTE : This function Should not be modified, when the callback is needed,
246             the HAL_WWDG_MspDeInit could be implemented in the user file
247    */
248 }
249
250 /**
251   * @}
252   */
253
254 /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions 
255  *  @brief    IO operation functions 
256  *
257 @verbatim   
258   ==============================================================================
259                        ##### IO operation functions #####
260   ==============================================================================  
261   [..]  
262     This section provides functions allowing to:
263       (+) Start the WWDG.
264       (+) Refresh the WWDG.
265       (+) Handle WWDG interrupt request. 
266
267 @endverbatim
268   * @{
269   */
270
271 /**
272   * @brief  Starts the WWDG.
273   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
274   *              the configuration information for the specified WWDG module.
275   * @retval HAL status
276   */
277 HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
278 {
279   /* Process Locked */
280   __HAL_LOCK(hwwdg); 
281   
282   /* Change WWDG peripheral state */  
283   hwwdg->State = HAL_WWDG_STATE_BUSY;
284                   
285   /* Enable the peripheral */
286   __HAL_WWDG_ENABLE(hwwdg);  
287
288   /* Change WWDG peripheral state */    
289   hwwdg->State = HAL_WWDG_STATE_READY; 
290                   
291   /* Process Unlocked */
292   __HAL_UNLOCK(hwwdg);
293   
294   /* Return function status */
295   return HAL_OK;
296 }
297
298 /**
299   * @brief  Starts the WWDG with interrupt enabled.
300   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
301   *              the configuration information for the specified WWDG module.
302   * @retval HAL status
303   */
304 HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
305 {
306   /* Process Locked */
307   __HAL_LOCK(hwwdg); 
308   
309   /* Change WWDG peripheral state */  
310   hwwdg->State = HAL_WWDG_STATE_BUSY;
311
312   /* Enable the Early Wakeup Interrupt */ 
313   __HAL_WWDG_ENABLE_IT(hwwdg, WWDG_IT_EWI);
314                   
315   /* Enable the peripheral */
316   __HAL_WWDG_ENABLE(hwwdg);  
317   
318   /* Return function status */
319   return HAL_OK;
320 }
321
322 /**
323   * @brief  Refreshes the WWDG.
324   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
325   *              the configuration information for the specified WWDG module.
326   * @param  Counter: value of counter to put in WWDG counter
327   * @retval HAL status
328   */
329 HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
330 {
331   /* Process Locked */
332   __HAL_LOCK(hwwdg); 
333
334   /* Change WWDG peripheral state */  
335   hwwdg->State = HAL_WWDG_STATE_BUSY;
336
337   /* Check the parameters */
338   assert_param(IS_WWDG_COUNTER(Counter));
339
340   /* Write to WWDG CR the WWDG Counter value to refresh with */
341   MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
342   
343   /* Change WWDG peripheral state */    
344   hwwdg->State = HAL_WWDG_STATE_READY; 
345                   
346   /* Process Unlocked */
347   __HAL_UNLOCK(hwwdg);
348   
349   /* Return function status */
350   return HAL_OK;
351 }
352
353 /**
354   * @brief  Handles WWDG interrupt request.
355   * @note   The Early Wakeup Interrupt (EWI) can be used if specific safety operations 
356   *         or data logging must be performed before the actual reset is generated. 
357   *         The EWI interrupt is enabled when calling HAL_WWDG_Start_IT function.
358   *         When the downcounter reaches the value 0x40, and EWI interrupt is 
359   *         generated and the corresponding Interrupt Service Routine (ISR) can 
360   *         be used to trigger specific actions (such as communications or data 
361   *         logging), before resetting the device. 
362   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
363   *              the configuration information for the specified WWDG module.
364   * @retval None
365   */
366 void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
367 {
368   /* Check if Early Wakeup Interrupt is enable */
369   if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
370   {
371     /* Wheck if WWDG Early Wakeup Interrupt occurred */
372     if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
373     {
374       /* Early Wakeup callback */ 
375       HAL_WWDG_WakeupCallback(hwwdg);
376     
377       /* Change WWDG peripheral state */
378       hwwdg->State = HAL_WWDG_STATE_READY; 
379
380       /* Clear the WWDG Early Wakeup flag */
381       __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
382     
383       /* Process Unlocked */
384       __HAL_UNLOCK(hwwdg);
385     }
386   }
387 }
388
389 /**
390   * @brief  Early Wakeup WWDG callback.
391   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
392   *              the configuration information for the specified WWDG module.
393   * @retval None
394   */
395 __weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
396 {
397   /* NOTE : This function Should not be modified, when the callback is needed,
398             the HAL_WWDG_WakeupCallback could be implemented in the user file
399    */
400 }
401
402 /**
403   * @}
404   */
405
406 /** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions 
407  *  @brief    Peripheral State functions. 
408  *
409 @verbatim   
410   ==============================================================================
411                         ##### Peripheral State functions #####
412   ==============================================================================  
413     [..]
414     This subsection permits to get in run-time the status of the peripheral 
415     and the data flow.
416
417 @endverbatim
418   * @{
419   */
420
421 /**
422   * @brief  Returns the WWDG state.
423   * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
424   *              the configuration information for the specified WWDG module.
425   * @retval HAL state
426   */
427 HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
428 {
429   return hwwdg->State;
430 }
431
432 /**
433   * @}
434   */
435
436 /**
437   * @}
438   */
439
440 #endif /* HAL_WWDG_MODULE_ENABLED */
441 /**
442   * @}
443   */
444
445 /**
446   * @}
447   */
448
449 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/