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