提交 | 用户 | age
|
bfc108
|
1 |
/**
|
Q |
2 |
******************************************************************************
|
|
3 |
* @file stm32f0xx_hal_iwdg.c
|
|
4 |
* @author MCD Application Team
|
|
5 |
* @brief IWDG HAL module driver.
|
|
6 |
* This file provides firmware functions to manage the following
|
|
7 |
* functionalities of the Independent Watchdog (IWDG) peripheral:
|
|
8 |
* + Initialization and Start functions
|
|
9 |
* + IO operation functions
|
|
10 |
*
|
|
11 |
@verbatim
|
|
12 |
==============================================================================
|
|
13 |
##### IWDG Generic features #####
|
|
14 |
==============================================================================
|
|
15 |
[..]
|
|
16 |
(+) The IWDG can be started by either software or hardware (configurable
|
|
17 |
through option byte).
|
|
18 |
|
|
19 |
(+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even
|
|
20 |
if the main clock fails.
|
|
21 |
|
|
22 |
(+) Once the IWDG is started, the LSI is forced ON and both can not be
|
|
23 |
disabled. The counter starts counting down from the reset value (0xFFF).
|
|
24 |
When it reaches the end of count value (0x000) a reset signal is
|
|
25 |
generated (IWDG reset).
|
|
26 |
|
|
27 |
(+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
|
|
28 |
the IWDG_RLR value is reloaded in the counter and the watchdog reset is
|
|
29 |
prevented.
|
|
30 |
|
|
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 |
|
|
36 |
(+) Debug mode : When the microcontroller enters debug mode (core halted),
|
|
37 |
the IWDG counter either continues to work normally or stops, depending
|
|
38 |
on DBG_IWDG_STOP configuration bit in DBG module, accessible through
|
|
39 |
__HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros
|
|
40 |
|
|
41 |
[..] Min-max timeout value @40KHz (LSI): ~0.1ms / ~26.2s
|
|
42 |
The IWDG timeout may vary due to LSI frequency dispersion. STM32F0xx
|
|
43 |
devices provide the capability to measure the LSI frequency (LSI clock
|
|
44 |
connected internally to TIM16 CH1 input capture). The measured value
|
|
45 |
can be used to have an IWDG timeout with an acceptable accuracy.
|
|
46 |
|
|
47 |
##### How to use this driver #####
|
|
48 |
==============================================================================
|
|
49 |
[..]
|
|
50 |
(#) Use IWDG using HAL_IWDG_Init() function to :
|
|
51 |
(++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
|
|
52 |
clock is forced ON and IWDG counter starts downcounting.
|
|
53 |
(++) Enable write access to configuration register: IWDG_PR, IWDG_RLR &
|
|
54 |
IWDG_WINR.
|
|
55 |
(++) Configure the IWDG prescaler and counter reload value. This reload
|
|
56 |
value will be loaded in the IWDG counter each time the watchdog is
|
|
57 |
reloaded, then the IWDG will start counting down from this value.
|
|
58 |
(++) wait for status flags to be reset"
|
|
59 |
(++) Depending on window parameter:
|
|
60 |
(+++) If Window Init parameter is same as Window register value,
|
|
61 |
nothing more is done but reload counter value in order to exit
|
|
62 |
function withy exact time base.
|
|
63 |
(+++) Else modify Window register. This will automatically reload
|
|
64 |
watchdog counter.
|
|
65 |
|
|
66 |
(#) Then the application program must refresh the IWDG counter at regular
|
|
67 |
intervals during normal operation to prevent an MCU reset, using
|
|
68 |
HAL_IWDG_Refresh() function.
|
|
69 |
|
|
70 |
*** IWDG HAL driver macros list ***
|
|
71 |
====================================
|
|
72 |
[..]
|
|
73 |
Below the list of most used macros in IWDG HAL driver:
|
|
74 |
(+) __HAL_IWDG_START: Enable the IWDG peripheral
|
|
75 |
(+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
|
|
76 |
the reload register
|
|
77 |
|
|
78 |
@endverbatim
|
|
79 |
******************************************************************************
|
|
80 |
* @attention
|
|
81 |
*
|
|
82 |
* <h2><center>© COPYRIGHT(c) 2016 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 "stm32f0xx_hal.h"
|
|
111 |
|
|
112 |
/** @addtogroup STM32F0xx_HAL_Driver
|
|
113 |
* @{
|
|
114 |
*/
|
|
115 |
|
|
116 |
#ifdef HAL_IWDG_MODULE_ENABLED
|
|
117 |
/** @addtogroup IWDG
|
|
118 |
* @brief IWDG HAL module driver.
|
|
119 |
* @{
|
|
120 |
*/
|
|
121 |
|
|
122 |
/* Private typedef -----------------------------------------------------------*/
|
|
123 |
/* Private define ------------------------------------------------------------*/
|
|
124 |
/** @defgroup IWDG_Private_Defines IWDG Private Defines
|
|
125 |
* @{
|
|
126 |
*/
|
|
127 |
/* Status register need 5 RC LSI divided by prescaler clock to be updated. With
|
|
128 |
higher prescaler (256), and according to LSI variation, we need to wait at
|
|
129 |
least 6 cycles so 39 ms. */
|
|
130 |
#define HAL_IWDG_DEFAULT_TIMEOUT 39U
|
|
131 |
/**
|
|
132 |
* @}
|
|
133 |
*/
|
|
134 |
|
|
135 |
/* Private macro -------------------------------------------------------------*/
|
|
136 |
/* Private variables ---------------------------------------------------------*/
|
|
137 |
/* Private function prototypes -----------------------------------------------*/
|
|
138 |
/* Exported functions --------------------------------------------------------*/
|
|
139 |
|
|
140 |
/** @addtogroup IWDG_Exported_Functions
|
|
141 |
* @{
|
|
142 |
*/
|
|
143 |
|
|
144 |
/** @addtogroup IWDG_Exported_Functions_Group1
|
|
145 |
* @brief Initialization and Start functions.
|
|
146 |
*
|
|
147 |
@verbatim
|
|
148 |
===============================================================================
|
|
149 |
##### Initialization and Start functions #####
|
|
150 |
===============================================================================
|
|
151 |
[..] This section provides functions allowing to:
|
|
152 |
(+) Initialize the IWDG according to the specified parameters in the
|
|
153 |
IWDG_InitTypeDef of associated handle.
|
|
154 |
(+) Manage Window option.
|
|
155 |
(+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
|
|
156 |
is reloaded in order to exit function with correct time base.
|
|
157 |
|
|
158 |
@endverbatim
|
|
159 |
* @{
|
|
160 |
*/
|
|
161 |
|
|
162 |
/**
|
|
163 |
* @brief Initialize the IWDG according to the specified parameters in the
|
|
164 |
* IWDG_InitTypeDef and start watchdog. Before exiting function,
|
|
165 |
* watchdog is refreshed in order to have correct time base.
|
|
166 |
* @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
|
|
167 |
* the configuration information for the specified IWDG module.
|
|
168 |
* @retval HAL status
|
|
169 |
*/
|
|
170 |
HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
|
|
171 |
{
|
|
172 |
uint32_t tickstart;
|
|
173 |
|
|
174 |
/* Check the IWDG handle allocation */
|
|
175 |
if(hiwdg == NULL)
|
|
176 |
{
|
|
177 |
return HAL_ERROR;
|
|
178 |
}
|
|
179 |
|
|
180 |
/* Check the parameters */
|
|
181 |
assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
|
|
182 |
assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
|
|
183 |
assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
|
|
184 |
assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
|
|
185 |
|
|
186 |
/* Enable IWDG. LSI is turned on automaticaly */
|
|
187 |
__HAL_IWDG_START(hiwdg);
|
|
188 |
|
|
189 |
/* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
|
|
190 |
0x5555 in KR */
|
|
191 |
IWDG_ENABLE_WRITE_ACCESS(hiwdg);
|
|
192 |
|
|
193 |
/* Write to IWDG registers the Prescaler & Reload values to work with */
|
|
194 |
hiwdg->Instance->PR = hiwdg->Init.Prescaler;
|
|
195 |
hiwdg->Instance->RLR = hiwdg->Init.Reload;
|
|
196 |
|
|
197 |
/* Check pending flag, if previous update not done, return timeout */
|
|
198 |
tickstart = HAL_GetTick();
|
|
199 |
|
|
200 |
/* Wait for register to be updated */
|
|
201 |
while(hiwdg->Instance->SR != RESET)
|
|
202 |
{
|
|
203 |
if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
|
|
204 |
{
|
|
205 |
return HAL_TIMEOUT;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
/* If window parameter is different than current value, modify window
|
|
210 |
register */
|
|
211 |
if(hiwdg->Instance->WINR != hiwdg->Init.Window)
|
|
212 |
{
|
|
213 |
/* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
|
|
214 |
even if window feature is disabled, Watchdog will be reloaded by writing
|
|
215 |
windows register */
|
|
216 |
hiwdg->Instance->WINR = hiwdg->Init.Window;
|
|
217 |
}
|
|
218 |
else
|
|
219 |
{
|
|
220 |
/* Reload IWDG counter with value defined in the reload register */
|
|
221 |
__HAL_IWDG_RELOAD_COUNTER(hiwdg);
|
|
222 |
}
|
|
223 |
|
|
224 |
/* Return function status */
|
|
225 |
return HAL_OK;
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* @}
|
|
230 |
*/
|
|
231 |
|
|
232 |
|
|
233 |
/** @addtogroup IWDG_Exported_Functions_Group2
|
|
234 |
* @brief IO operation functions
|
|
235 |
*
|
|
236 |
@verbatim
|
|
237 |
===============================================================================
|
|
238 |
##### IO operation functions #####
|
|
239 |
===============================================================================
|
|
240 |
[..] This section provides functions allowing to:
|
|
241 |
(+) Refresh the IWDG.
|
|
242 |
|
|
243 |
@endverbatim
|
|
244 |
* @{
|
|
245 |
*/
|
|
246 |
|
|
247 |
|
|
248 |
/**
|
|
249 |
* @brief Refresh the IWDG.
|
|
250 |
* @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains
|
|
251 |
* the configuration information for the specified IWDG module.
|
|
252 |
* @retval HAL status
|
|
253 |
*/
|
|
254 |
HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
|
|
255 |
{
|
|
256 |
/* Reload IWDG counter with value defined in the reload register */
|
|
257 |
__HAL_IWDG_RELOAD_COUNTER(hiwdg);
|
|
258 |
|
|
259 |
/* Return function status */
|
|
260 |
return HAL_OK;
|
|
261 |
}
|
|
262 |
|
|
263 |
/**
|
|
264 |
* @}
|
|
265 |
*/
|
|
266 |
|
|
267 |
/**
|
|
268 |
* @}
|
|
269 |
*/
|
|
270 |
|
|
271 |
#endif /* HAL_IWDG_MODULE_ENABLED */
|
|
272 |
/**
|
|
273 |
* @}
|
|
274 |
*/
|
|
275 |
|
|
276 |
/**
|
|
277 |
* @}
|
|
278 |
*/
|
|
279 |
|
|
280 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|