STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_iwdg.c
Go to the documentation of this file.
1 
93 /* Includes ------------------------------------------------------------------*/
94 #include "stm32l4xx_hal.h"
95 
100 #ifdef HAL_IWDG_MODULE_ENABLED
101 
106 /* Private typedef -----------------------------------------------------------*/
107 /* Private define ------------------------------------------------------------*/
111 /* Status register need 5 RC LSI divided by prescaler clock to be updated. With
112  higher prescaler (256), and according to LSI variation, we need to wait at
113  least 6 cycles so 48 ms. */
114 #define HAL_IWDG_DEFAULT_TIMEOUT 48u
115 
119 /* Private macro -------------------------------------------------------------*/
120 /* Private variables ---------------------------------------------------------*/
121 /* Private function prototypes -----------------------------------------------*/
122 /* Exported functions --------------------------------------------------------*/
123 
154 HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
155 {
156  uint32_t tickstart;
157 
158  /* Check the IWDG handle allocation */
159  if (hiwdg == NULL)
160  {
161  return HAL_ERROR;
162  }
163 
164  /* Check the parameters */
165  assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
166  assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
167  assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
168  assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
169 
170  /* Enable IWDG. LSI is turned on automaticaly */
171  __HAL_IWDG_START(hiwdg);
172 
173  /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
174  0x5555 in KR */
175  IWDG_ENABLE_WRITE_ACCESS(hiwdg);
176 
177  /* Write to IWDG registers the Prescaler & Reload values to work with */
178  hiwdg->Instance->PR = hiwdg->Init.Prescaler;
179  hiwdg->Instance->RLR = hiwdg->Init.Reload;
180 
181  /* Check pending flag, if previous update not done, return timeout */
182  tickstart = HAL_GetTick();
183 
184  /* Wait for register to be updated */
185  while (hiwdg->Instance->SR != 0x00u)
186  {
187  if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
188  {
189  return HAL_TIMEOUT;
190  }
191  }
192 
193  /* If window parameter is different than current value, modify window
194  register */
195  if (hiwdg->Instance->WINR != hiwdg->Init.Window)
196  {
197  /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
198  even if window feature is disabled, Watchdog will be reloaded by writing
199  windows register */
200  hiwdg->Instance->WINR = hiwdg->Init.Window;
201  }
202  else
203  {
204  /* Reload IWDG counter with value defined in the reload register */
205  __HAL_IWDG_RELOAD_COUNTER(hiwdg);
206  }
207 
208  /* Return function status */
209  return HAL_OK;
210 }
211 
238 HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
239 {
240  /* Reload IWDG counter with value defined in the reload register */
241  __HAL_IWDG_RELOAD_COUNTER(hiwdg);
242 
243  /* Return function status */
244  return HAL_OK;
245 }
246 
255 #endif /* HAL_IWDG_MODULE_ENABLED */
256 
264 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
Refresh the IWDG.
This file contains all the functions prototypes for the HAL module driver.
uint32_t HAL_GetTick(void)
Provide 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...
IWDG_InitTypeDef Init
return HAL_OK
IWDG Handle Structure definition.
IWDG_TypeDef * Instance
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))