]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_iwdg.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F0 / stm32f0xx_hal_iwdg.c
1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_hal_iwdg.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    11-December-2014
7   * @brief   IWDG HAL module driver.
8   *          This file provides firmware functions to manage the following 
9   *          functionalities of the Independent Watchdog (IWDG) peripheral:
10   *           + Initialization and Configuration functions
11   *           + IO operation functions
12   *           + Peripheral State functions
13   *         
14   @verbatim
15  ===============================================================================
16                         ##### IWDG Specific features #####
17  ===============================================================================
18     [..]
19       (+) The IWDG can be started by either software or hardware (configurable
20           through option byte).
21       (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
22           thus stays active even if the main clock fails.
23       (+) Once the IWDG is started, the LSI is forced ON and cannot be disabled
24           (LSI cannot be disabled too), and the counter starts counting down from 
25           the reset value of 0xFFF. When it reaches the end of count value (0x000)
26           a system reset is generated.
27       (+) The IWDG counter should be refreshed at regular intervals, otherwise the
28           watchdog generates an MCU reset when the counter reaches 0.          
29       (+) The IWDG is implemented in the VDD voltage domain that is still functional
30           in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
31       (+) IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
32           reset occurs.
33       (+) Min-max timeout value @41KHz (LSI): ~0.1ms / ~25.5s
34           The IWDG timeout may vary due to LSI frequency dispersion. STM32F0x
35           devices provide the capability to measure the LSI frequency (LSI clock
36           connected internally to TIM16 CH1 input capture). The measured value
37           can be used to have an IWDG timeout with an acceptable accuracy.
38           For more information, please refer to the STM32F0x Reference manual.
39
40                         ##### How to use this driver #####
41  ===============================================================================
42     [..]
43     (#) if Window option is disabled
44       (++) Use IWDG using HAL_IWDG_Init() function to :
45          (+++) Enable write access to IWDG_PR, IWDG_RLR.   
46          (+++) Configure the IWDG prescaler, counter reload value.
47               This reload value will be loaded in the IWDG counter each time the counter
48               is reloaded, then the IWDG will start counting down from this value.
49       (++) Use IWDG using HAL_IWDG_Start() function to :
50          (+++) Reload IWDG counter with value defined in the IWDG_RLR register.
51          (+++) Start the IWDG, when the IWDG is used in software mode (no need 
52               to enable the LSI, it will be enabled by hardware).
53       (++) Then the application program must refresh the IWDG counter at regular
54           intervals during normal operation to prevent an MCU reset, using
55           HAL_IWDG_Refresh() function.    
56     (#) if Window option is enabled:
57       (++) Use IWDG using HAL_IWDG_Start() function to enable IWDG downcounter 
58       (++) Use IWDG using HAL_IWDG_Init() function to :
59          (+++) Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.   
60          (+++) Configure the IWDG prescaler, reload value and window value.
61       (++) Then the application program must refresh the IWDG counter at regular
62           intervals during normal operation to prevent an MCU reset, using
63           HAL_IWDG_Refresh() function.          
64
65      *** IWDG HAL driver macros list ***
66      ====================================
67      [..]
68        Below the list of most used macros in IWDG HAL driver.
69        
70       (+) __HAL_IWDG_START: Enable the IWDG peripheral
71       (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register    
72       (+) __HAL_IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers
73       (+) __HAL_IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers
74       (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
75
76   @endverbatim
77   ******************************************************************************
78   * @attention
79   *
80   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
81   *
82   * Redistribution and use in source and binary forms, with or without modification,
83   * are permitted provided that the following conditions are met:
84   *   1. Redistributions of source code must retain the above copyright notice,
85   *      this list of conditions and the following disclaimer.
86   *   2. Redistributions in binary form must reproduce the above copyright notice,
87   *      this list of conditions and the following disclaimer in the documentation
88   *      and/or other materials provided with the distribution.
89   *   3. Neither the name of STMicroelectronics nor the names of its contributors
90   *      may be used to endorse or promote products derived from this software
91   *      without specific prior written permission.
92   *
93   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
94   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
96   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
97   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
99   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
100   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
101   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
102   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103   *
104   ******************************************************************************  
105   */ 
106
107 /* Includes ------------------------------------------------------------------*/
108 #include "stm32f0xx_hal.h"
109
110 /** @addtogroup STM32F0xx_HAL_Driver
111   * @{
112   */
113
114 /** @defgroup IWDG IWDG HAL module driver
115   * @brief IWDG HAL module driver.
116   * @{
117   */
118
119 #ifdef HAL_IWDG_MODULE_ENABLED
120
121 /* Private typedef -----------------------------------------------------------*/
122 /* Private define ------------------------------------------------------------*/
123
124 /** @defgroup IWDG_Private_Defines IWDG Private Defines
125   * @{
126   */
127
128 #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000
129
130 /**
131   * @}
132   */
133
134 /* Private macro -------------------------------------------------------------*/
135 /* Private variables ---------------------------------------------------------*/
136 /* Private function prototypes -----------------------------------------------*/
137 /* Private functions ---------------------------------------------------------*/
138
139 /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
140   * @{
141   */
142
143 /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions 
144  *  @brief    Initialization and Configuration functions. 
145  *
146 @verbatim    
147  ===============================================================================
148               ##### Initialization functions #####
149  ===============================================================================
150     [..]  This section provides functions allowing to:
151       (+) Initialize the IWDG according to the specified parameters 
152           in the IWDG_InitTypeDef and create the associated handle
153       (+) Manage Window option
154       (+) Initialize the IWDG MSP
155       (+) DeInitialize IWDG MSP 
156  
157 @endverbatim
158   * @{
159   */
160
161 /**
162   * @brief  Initializes the IWDG according to the specified
163   *         parameters in the IWDG_InitTypeDef and creates the associated handle.
164   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
165   *                the configuration information for the specified IWDG module.
166   * @retval HAL status
167   */
168 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
169
170   uint32_t tickstart = 0;
171
172   /* Check the IWDG handle allocation */
173   if(hiwdg == NULL)
174   {
175     return HAL_ERROR;
176   }
177   
178   /* Check the parameters */
179   assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
180   assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
181   assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
182   assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
183
184   /* Check pending flag, if previous update not done, return error */
185   if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
186      &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
187      &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET))
188   {
189     return HAL_ERROR;
190   }
191   
192   if(hiwdg->State == HAL_IWDG_STATE_RESET)
193   { 
194   /* Init the low level hardware */
195   HAL_IWDG_MspInit(hiwdg);
196   }
197
198   /* Change IWDG peripheral state */
199   hiwdg->State = HAL_IWDG_STATE_BUSY;
200
201   /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers */  
202   /* by writing 0x5555 in KR */  
203   __HAL_IWDG_ENABLE_WRITE_ACCESS(hiwdg);
204   
205   /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
206   MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
207   MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
208  
209   /* check if window option is enabled */
210   if (((hiwdg->Init.Window) != IWDG_WINDOW_DISABLE) || ((hiwdg->Instance->WINR) != IWDG_WINDOW_DISABLE))
211   {
212     tickstart = HAL_GetTick();
213
214      /* Wait for register to be updated */
215     while((uint32_t)(hiwdg->Instance->SR) != RESET)
216     {
217       if((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
218       { 
219         /* Set IWDG state */
220         hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
221         return HAL_TIMEOUT;
222       } 
223     }
224
225     /* Write to IWDG WINR the IWDG_Window value to compare with */
226     MODIFY_REG(hiwdg->Instance->WINR, IWDG_WINR_WIN, hiwdg->Init.Window);
227
228   } 
229   /* Change IWDG peripheral state */
230   hiwdg->State = HAL_IWDG_STATE_READY;
231  
232   /* Return function status */
233   return HAL_OK;
234 }
235
236 /**
237   * @brief  Initializes the IWDG MSP.
238   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
239   *                the configuration information for the specified IWDG module.
240   * @retval None
241   */
242 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
243 {
244   /* NOTE : This function Should not be modified, when the callback is needed,
245             the HAL_IWDG_MspInit could be implemented in the user file
246    */
247 }
248
249 /**
250   * @}
251   */
252
253 /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions  
254  *  @brief   IO operation functions  
255  *
256 @verbatim   
257  ===============================================================================
258                       ##### IO operation functions #####
259  ===============================================================================  
260     [..]  This section provides functions allowing to:
261       (+) Start the IWDG.
262       (+) Refresh the IWDG.
263
264 @endverbatim
265   * @{
266   */
267
268 /**
269   * @brief  Starts the IWDG.
270   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
271   *                the configuration information for the specified IWDG module.
272   * @retval HAL status
273   */
274 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
275
276   uint32_t tickstart = 0;
277
278   /* Process Locked */
279   __HAL_LOCK(hiwdg); 
280                   
281     /* Change IWDG peripheral state */  
282   hiwdg->State = HAL_IWDG_STATE_BUSY;
283                    
284   /* Reload IWDG counter with value defined in the RLR register */
285   if ((hiwdg->Init.Window) == IWDG_WINDOW_DISABLE)
286   {
287   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
288   }
289
290   /* Enable the IWDG peripheral */
291   __HAL_IWDG_START(hiwdg);
292
293   tickstart = HAL_GetTick();
294
295   /* Wait until PVU, RVU, WVU flag are RESET */
296   while( (__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
297         &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
298         &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET) )
299   {
300     if((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
301     { 
302       /* Set IWDG state */
303       hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
304  
305        /* Process unlocked */
306       __HAL_UNLOCK(hiwdg);
307
308       return HAL_TIMEOUT;
309     } 
310   }
311
312   /* Change IWDG peripheral state */    
313   hiwdg->State = HAL_IWDG_STATE_READY; 
314                   
315   /* Process Unlocked */
316   __HAL_UNLOCK(hiwdg);
317   
318   /* Return function status */
319   return HAL_OK;
320 }
321
322 /**
323   * @brief  Refreshes the IWDG.
324   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
325   *                the configuration information for the specified IWDG module.
326   * @retval HAL status
327   */
328 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
329   {
330   uint32_t tickstart = 0;
331
332   /* Process Locked */
333   __HAL_LOCK(hiwdg); 
334                   
335     /* Change IWDG peripheral state */  
336   hiwdg->State = HAL_IWDG_STATE_BUSY;
337   
338   tickstart = HAL_GetTick();
339
340   /* Wait until RVU flag is RESET */
341   while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
342   {
343     if((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
344     { 
345       /* Set IWDG state */
346       hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
347
348        /* Process unlocked */
349       __HAL_UNLOCK(hiwdg);
350
351       return HAL_TIMEOUT;
352     } 
353   }
354
355   /* Reload IWDG counter with value defined in the reload register */
356   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
357   
358   /* Change IWDG peripheral state */    
359   hiwdg->State = HAL_IWDG_STATE_READY; 
360                   
361   /* Process Unlocked */
362   __HAL_UNLOCK(hiwdg);
363   
364   /* Return function status */
365   return HAL_OK;
366 }
367
368 /**
369   * @}
370   */
371
372 /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions 
373  *  @brief    Peripheral State functions. 
374  *
375 @verbatim   
376  ===============================================================================
377                       ##### Peripheral State functions #####
378  ===============================================================================  
379     [..]
380     This subsection permits to get in run-time the status of the peripheral 
381     and the data flow.
382
383 @endverbatim
384   * @{
385   */
386
387 /**
388   * @brief  Returns the IWDG state.
389   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
390   *                the configuration information for the specified IWDG module.
391   * @retval HAL state
392   */
393 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
394 {
395   return hiwdg->State;
396 }
397
398 /**
399   * @}
400   */
401
402 /**
403   * @}
404   */
405
406 #endif /* HAL_IWDG_MODULE_ENABLED */
407 /**
408   * @}
409   */
410
411 /**
412   * @}
413   */
414
415 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/