]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/stm32f4xx_hal_iwdg.c
Merge commit '22b6e15a179031afb7c3534cf7b109b0668b602c'
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F4 / stm32f4xx_hal_iwdg.c
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_hal_iwdg.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    19-June-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 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): ~125us / ~32.7s
38          The IWDG timeout may vary due to LSI frequency dispersion. STM32F4xx
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       (+) Use IWDG using HAL_IWDG_Init() function to :
49          (++) Enable write access to IWDG_PR, IWDG_RLR.
50          (++) Configure the IWDG prescaler, counter reload value.
51               This reload value will be loaded in the IWDG counter each time the counter
52               is reloaded, then the IWDG will start counting down from this value.
53     [..]
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       (+) __HAL_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) 2014 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 "stm32f4xx_hal.h"
116
117 /** @addtogroup STM32F4xx_HAL_Driver
118   * @{
119   */
120
121 /** @defgroup IWDG 
122   * @brief IWDG HAL module driver.
123   * @{
124   */
125
126 #ifdef HAL_IWDG_MODULE_ENABLED
127
128 /* Private typedef -----------------------------------------------------------*/
129 /* Private define ------------------------------------------------------------*/
130 #define IWDG_TIMEOUT_FLAG          ((uint32_t)1000)     /* 1 s */
131 /* Private macro -------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 /* Private functions ---------------------------------------------------------*/
135
136 /** @defgroup IWDG_Private_Functions
137   * @{
138   */
139
140 /** @defgroup IWDG_Group1 Initialization and de-initialization functions 
141  *  @brief    Initialization and Configuration functions. 
142  *
143 @verbatim    
144  ===============================================================================
145           ##### Initialization and de-initialization functions #####
146  ===============================================================================
147     [..]  This section provides functions allowing to:
148       (+) Initialize the IWDG according to the specified parameters 
149           in the IWDG_InitTypeDef and create the associated handle
150       (+) Initialize the IWDG MSP
151       (+) DeInitialize IWDG MSP 
152  
153 @endverbatim
154   * @{
155   */
156
157 /**
158   * @brief  Initializes the IWDG according to the specified
159   *         parameters in the IWDG_InitTypeDef and creates the associated handle.
160   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
161   *                the configuration information for the specified IWDG module.
162   * @retval HAL status
163   */
164 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
165 {
166   /* Check the IWDG handle allocation */
167   if(hiwdg == HAL_NULL)
168   {
169     return HAL_ERROR;
170   }
171
172   /* Check the parameters */
173   assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
174   assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
175   assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload)); 
176   
177   if(hiwdg->State == HAL_IWDG_STATE_RESET)
178   {  
179     /* Init the low level hardware */
180     HAL_IWDG_MspInit(hiwdg);
181   }
182   
183   /* Change IWDG peripheral state */
184   hiwdg->State = HAL_IWDG_STATE_BUSY;  
185   
186   /* Enable write access to IWDG_PR and IWDG_RLR registers */  
187   __HAL_IWDG_ENABLE_WRITE_ACCESS(hiwdg);
188   
189   /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
190   MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
191   MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
192  
193   /* Change IWDG peripheral state */
194   hiwdg->State = HAL_IWDG_STATE_READY;
195   
196   /* Return function status */
197   return HAL_OK;
198 }
199
200 /**
201   * @brief  Initializes the IWDG MSP.
202   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
203   *                the configuration information for the specified IWDG module.
204   * @retval None
205   */
206 __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
207 {
208   /* NOTE : This function Should not be modified, when the callback is needed,
209             the HAL_IWDG_MspInit could be implemented in the user file
210    */
211 }
212
213 /**
214   * @}
215   */
216
217 /** @defgroup IWDG_Group2 IO operation functions  
218  *  @brief   IO operation functions  
219  *
220 @verbatim   
221  ===============================================================================
222                       ##### IO operation functions #####
223  ===============================================================================  
224     [..]  This section provides functions allowing to:
225       (+) Start the IWDG.
226       (+) Refresh the IWDG.
227
228 @endverbatim
229   * @{
230   */
231
232 /**
233   * @brief  Starts the IWDG.
234   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
235   *                the configuration information for the specified IWDG module.
236   * @retval HAL status
237   */
238 HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
239 {
240   /* Process Locked */
241   __HAL_LOCK(hiwdg); 
242   
243     /* Change IWDG peripheral state */  
244   hiwdg->State = HAL_IWDG_STATE_BUSY;
245
246   /* Start the IWDG peripheral */
247   __HAL_IWDG_START(hiwdg);
248   
249     /* Reload IWDG counter with value defined in the RLR register */
250   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
251   
252   /* Change IWDG peripheral state */    
253   hiwdg->State = HAL_IWDG_STATE_READY; 
254   
255   /* Process Unlocked */
256   __HAL_UNLOCK(hiwdg);
257   
258   /* Return function status */
259   return HAL_OK;
260 }
261
262 /**
263   * @brief  Refreshes the IWDG.
264   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
265   *                the configuration information for the specified IWDG module.
266   * @retval HAL status
267   */
268 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
269 {
270   uint32_t tickstart = 0;
271
272   /* Process Locked */
273   __HAL_LOCK(hiwdg);
274
275     /* Change IWDG peripheral state */
276   hiwdg->State = HAL_IWDG_STATE_BUSY;
277
278   tickstart = HAL_GetTick();
279
280   /* Wait until RVU flag is RESET */
281   while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
282   {
283     if((HAL_GetTick() - tickstart ) > IWDG_TIMEOUT_FLAG)
284     {
285       /* Set IWDG state */
286       hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
287
288        /* Process unlocked */
289       __HAL_UNLOCK(hiwdg);
290
291       return HAL_TIMEOUT;
292     }
293   }
294   
295   /* Reload IWDG counter with value defined in the reload register */
296   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
297     
298   /* Change IWDG peripheral state */    
299   hiwdg->State = HAL_IWDG_STATE_READY; 
300   
301   /* Process Unlocked */
302   __HAL_UNLOCK(hiwdg);
303   
304   /* Return function status */
305   return HAL_OK;
306 }
307
308 /**
309   * @}
310   */
311
312 /** @defgroup IWDG_Group3 Peripheral State functions 
313  *  @brief    Peripheral State functions. 
314  *
315 @verbatim   
316  ===============================================================================
317                       ##### Peripheral State functions #####
318  ===============================================================================  
319     [..]
320     This subsection permits to get in run-time the status of the peripheral 
321     and the data flow.
322
323 @endverbatim
324   * @{
325   */
326
327 /**
328   * @brief  Returns the IWDG state.
329   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
330   *                the configuration information for the specified IWDG module.
331   * @retval HAL state
332   */
333 HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
334 {
335   return hiwdg->State;
336 }
337
338 /**
339   * @}
340   */
341
342 /**
343   * @}
344   */
345
346 #endif /* HAL_IWDG_MODULE_ENABLED */
347 /**
348   * @}
349   */
350
351 /**
352   * @}
353   */
354
355 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/