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