Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file stm32f1xx_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 @32KHz (LSI): ~125us / ~32.7s |
||
42 | The IWDG timeout may vary due to LSI frequency dispersion. STM32F1xx |
||
43 | devices provide the capability to measure the LSI frequency (LSI clock |
||
44 | connected internally to TIM5 CH4 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 | (++) Configure the IWDG prescaler and counter reload value. This reload |
||
55 | value will be loaded in the IWDG counter each time the watchdog is |
||
56 | reloaded, then the IWDG will start counting down from this value. |
||
57 | (++) wait for status flags to be reset" |
||
58 | |||
59 | (#) Then the application program must refresh the IWDG counter at regular |
||
60 | intervals during normal operation to prevent an MCU reset, using |
||
61 | HAL_IWDG_Refresh() function. |
||
62 | |||
63 | *** IWDG HAL driver macros list *** |
||
64 | ==================================== |
||
65 | [..] |
||
66 | Below the list of most used macros in IWDG HAL driver: |
||
67 | (+) __HAL_IWDG_START: Enable the IWDG peripheral |
||
68 | (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in |
||
69 | the reload register |
||
70 | |||
71 | @endverbatim |
||
72 | ****************************************************************************** |
||
73 | * @attention |
||
74 | * |
||
75 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
||
76 | * All rights reserved.</center></h2> |
||
77 | * |
||
78 | * This software component is licensed by ST under BSD 3-Clause license, |
||
79 | * the "License"; You may not use this file except in compliance with the |
||
80 | * License. You may obtain a copy of the License at: |
||
81 | * opensource.org/licenses/BSD-3-Clause |
||
82 | * |
||
83 | ****************************************************************************** |
||
84 | */ |
||
85 | |||
86 | /* Includes ------------------------------------------------------------------*/ |
||
87 | #include "stm32f1xx_hal.h" |
||
88 | |||
89 | /** @addtogroup STM32F1xx_HAL_Driver |
||
90 | * @{ |
||
91 | */ |
||
92 | |||
93 | #ifdef HAL_IWDG_MODULE_ENABLED |
||
94 | /** @defgroup IWDG IWDG |
||
95 | * @brief IWDG HAL module driver. |
||
96 | * @{ |
||
97 | */ |
||
98 | |||
99 | /* Private typedef -----------------------------------------------------------*/ |
||
100 | /* Private define ------------------------------------------------------------*/ |
||
101 | /** @defgroup IWDG_Private_Defines IWDG Private Defines |
||
102 | * @{ |
||
103 | */ |
||
104 | /* Status register need 5 RC LSI divided by prescaler clock to be updated. With |
||
105 | higher prescaler (256), and according to HSI variation, we need to wait at |
||
106 | least 6 cycles so 48 ms. */ |
||
107 | #define HAL_IWDG_DEFAULT_TIMEOUT 48U |
||
108 | /** |
||
109 | * @} |
||
110 | */ |
||
111 | |||
112 | /* Private macro -------------------------------------------------------------*/ |
||
113 | /* Private variables ---------------------------------------------------------*/ |
||
114 | /* Private function prototypes -----------------------------------------------*/ |
||
115 | /* Exported functions --------------------------------------------------------*/ |
||
116 | |||
117 | /** @addtogroup IWDG_Exported_Functions |
||
118 | * @{ |
||
119 | */ |
||
120 | |||
121 | /** @addtogroup IWDG_Exported_Functions_Group1 |
||
122 | * @brief Initialization and Start functions. |
||
123 | * |
||
124 | @verbatim |
||
125 | =============================================================================== |
||
126 | ##### Initialization and Start functions ##### |
||
127 | =============================================================================== |
||
128 | [..] This section provides functions allowing to: |
||
129 | (+) Initialize the IWDG according to the specified parameters in the |
||
130 | IWDG_InitTypeDef of associated handle. |
||
131 | (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog |
||
132 | is reloaded in order to exit function with correct time base. |
||
133 | |||
134 | @endverbatim |
||
135 | * @{ |
||
136 | */ |
||
137 | |||
138 | /** |
||
139 | * @brief Initialize the IWDG according to the specified parameters in the |
||
140 | * IWDG_InitTypeDef and start watchdog. Before exiting function, |
||
141 | * watchdog is refreshed in order to have correct time base. |
||
142 | * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains |
||
143 | * the configuration information for the specified IWDG module. |
||
144 | * @retval HAL status |
||
145 | */ |
||
146 | HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg) |
||
147 | { |
||
148 | uint32_t tickstart; |
||
149 | |||
150 | /* Check the IWDG handle allocation */ |
||
151 | if (hiwdg == NULL) |
||
152 | { |
||
153 | return HAL_ERROR; |
||
154 | } |
||
155 | |||
156 | /* Check the parameters */ |
||
157 | assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance)); |
||
158 | assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler)); |
||
159 | assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload)); |
||
160 | |||
161 | /* Enable IWDG. LSI is turned on automaticaly */ |
||
162 | __HAL_IWDG_START(hiwdg); |
||
163 | |||
164 | /* Enable write access to IWDG_PR and IWDG_RLR registers by writing 0x5555 in KR */ |
||
165 | IWDG_ENABLE_WRITE_ACCESS(hiwdg); |
||
166 | |||
167 | /* Write to IWDG registers the Prescaler & Reload values to work with */ |
||
168 | hiwdg->Instance->PR = hiwdg->Init.Prescaler; |
||
169 | hiwdg->Instance->RLR = hiwdg->Init.Reload; |
||
170 | |||
171 | /* Check pending flag, if previous update not done, return timeout */ |
||
172 | tickstart = HAL_GetTick(); |
||
173 | |||
174 | /* Wait for register to be updated */ |
||
175 | while (hiwdg->Instance->SR != RESET) |
||
176 | { |
||
177 | if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT) |
||
178 | { |
||
179 | return HAL_TIMEOUT; |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /* Reload IWDG counter with value defined in the reload register */ |
||
184 | __HAL_IWDG_RELOAD_COUNTER(hiwdg); |
||
185 | |||
186 | /* Return function status */ |
||
187 | return HAL_OK; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @} |
||
192 | */ |
||
193 | |||
194 | /** @addtogroup IWDG_Exported_Functions_Group2 |
||
195 | * @brief IO operation functions |
||
196 | * |
||
197 | @verbatim |
||
198 | =============================================================================== |
||
199 | ##### IO operation functions ##### |
||
200 | =============================================================================== |
||
201 | [..] This section provides functions allowing to: |
||
202 | (+) Refresh the IWDG. |
||
203 | |||
204 | @endverbatim |
||
205 | * @{ |
||
206 | */ |
||
207 | |||
208 | /** |
||
209 | * @brief Refresh the IWDG. |
||
210 | * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains |
||
211 | * the configuration information for the specified IWDG module. |
||
212 | * @retval HAL status |
||
213 | */ |
||
214 | HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg) |
||
215 | { |
||
216 | /* Reload IWDG counter with value defined in the reload register */ |
||
217 | __HAL_IWDG_RELOAD_COUNTER(hiwdg); |
||
218 | |||
219 | /* Return function status */ |
||
220 | return HAL_OK; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @} |
||
225 | */ |
||
226 | |||
227 | /** |
||
228 | * @} |
||
229 | */ |
||
230 | |||
231 | #endif /* HAL_IWDG_MODULE_ENABLED */ |
||
232 | /** |
||
233 | * @} |
||
234 | */ |
||
235 | |||
236 | /** |
||
237 | * @} |
||
238 | */ |
||
239 | |||
240 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |