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