STM32F4xx_HAL_Driver  1.8.3
stm32f4xx_hal_iwdg.c
Go to the documentation of this file.
1 
96 /* Includes ------------------------------------------------------------------*/
97 #include "stm32f4xx_hal.h"
98 
103 #ifdef HAL_IWDG_MODULE_ENABLED
109 /* Private typedef -----------------------------------------------------------*/
110 /* Private define ------------------------------------------------------------*/
114 /* Status register needs up to 5 LSI clock periods divided by the clock
115  prescaler to be updated. The number of LSI clock periods is upper-rounded to
116  6 for the timeout value calculation.
117  The timeout value is calculated using the highest prescaler (256) and
118  the LSI_VALUE constant. The value of this constant can be changed by the user
119  to take into account possible LSI clock period variations.
120  The timeout value is multiplied by 1000 to be converted in milliseconds.
121  LSI startup time is also considered here by adding LSI_STARTUP_TIME
122  converted in milliseconds. */
123 #define HAL_IWDG_DEFAULT_TIMEOUT (((6UL * 256UL * 1000UL) / LSI_VALUE) + ((LSI_STARTUP_TIME / 1000UL) + 1UL))
124 #define IWDG_KERNEL_UPDATE_FLAGS (IWDG_SR_RVU | IWDG_SR_PVU)
129 /* Private macro -------------------------------------------------------------*/
130 /* Private variables ---------------------------------------------------------*/
131 /* Private function prototypes -----------------------------------------------*/
132 /* Exported functions --------------------------------------------------------*/
133 
163 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
164 {
165  uint32_t tickstart;
166 
167  /* Check the IWDG handle allocation */
168  if (hiwdg == NULL)
169  {
170  return HAL_ERROR;
171  }
172 
173  /* Check the parameters */
174  assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
175  assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
176  assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
177 
178  /* Enable IWDG. LSI is turned on automatically */
179  __HAL_IWDG_START(hiwdg);
180 
181  /* Enable write access to IWDG_PR and IWDG_RLR registers by writing
182  0x5555 in KR */
183  IWDG_ENABLE_WRITE_ACCESS(hiwdg);
184 
185  /* Write to IWDG registers the Prescaler & Reload values to work with */
186  hiwdg->Instance->PR = hiwdg->Init.Prescaler;
187  hiwdg->Instance->RLR = hiwdg->Init.Reload;
188 
189  /* Check pending flag, if previous update not done, return timeout */
190  tickstart = HAL_GetTick();
191 
192  /* Wait for register to be updated */
193  while ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
194  {
195  if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
196  {
197  if ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
198  {
199  return HAL_TIMEOUT;
200  }
201  }
202  }
203 
204  /* Reload IWDG counter with value defined in the reload register */
205  __HAL_IWDG_RELOAD_COUNTER(hiwdg);
206 
207  /* Return function status */
208  return HAL_OK;
209 }
210 
211 
237 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
238 {
239  /* Reload IWDG counter with value defined in the reload register */
240  __HAL_IWDG_RELOAD_COUNTER(hiwdg);
241 
242  /* Return function status */
243  return HAL_OK;
244 }
245 
246 
255 #endif /* HAL_IWDG_MODULE_ENABLED */
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
Initialize the IWDG according to the specified parameters in the IWDG_InitTypeDef and start watchdog....
HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
Refresh the IWDG.
IWDG_TypeDef * Instance
IWDG_InitTypeDef Init
IWDG Handle Structure definition.
This file contains all the functions prototypes for the HAL module driver.