stm32f7xx_hal_iwdg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_iwdg.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief IWDG HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the Independent Watchdog (IWDG) peripheral:
  11. * + Initialization and de-initialization functions
  12. * + IO operation functions
  13. * + Peripheral State functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### IWDG Generic features #####
  18. ==============================================================================
  19. [..]
  20. (+) The IWDG can be started by either software or hardware (configurable
  21. through option byte).
  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. (+) The IWDG counter should be refreshed at regular intervals, otherwise the
  29. watchdog generates an MCU reset when the counter reaches 0.
  30. (+) The IWDG is implemented in the VDD voltage domain that is still functional
  31. in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
  32. IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
  33. reset occurs.
  34. [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
  35. The IWDG timeout may vary due to LSI frequency dispersion. STM32L4xx
  36. devices provide the capability to measure the LSI frequency (LSI clock
  37. connected internally to TIM16 CH1 input capture). The measured value
  38. can be used to have an IWDG timeout with an acceptable accuracy.
  39. ##### How to use this driver #####
  40. ==============================================================================
  41. [..]
  42. If Window option is disabled
  43. (+) Use IWDG using HAL_IWDG_Init() function to :
  44. (++) Enable write access to IWDG_PR, IWDG_RLR.
  45. (++) Configure the IWDG prescaler, counter reload value.
  46. This reload value will be loaded in the IWDG counter each time the counter
  47. is reloaded, then the IWDG will start counting down from this value.
  48. (+) Use IWDG using HAL_IWDG_Start() function to :
  49. (++) Reload IWDG counter with value defined in the IWDG_RLR register.
  50. (++) Start the IWDG, when the IWDG is used in software mode (no need
  51. to enable the LSI, it will be enabled by hardware).
  52. (+) Then the application program must refresh the IWDG counter at regular
  53. intervals during normal operation to prevent an MCU reset, using
  54. HAL_IWDG_Refresh() function.
  55. [..]
  56. if Window option is enabled:
  57. (+) Use IWDG using HAL_IWDG_Start() function to enable IWDG downcounter
  58. (+) Use IWDG using HAL_IWDG_Init() function to :
  59. (++) Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.
  60. (++) Configure the IWDG prescaler, reload value and window value.
  61. (+) Then the application program must refresh the IWDG counter at regular
  62. intervals during normal operation to prevent an MCU reset, using
  63. HAL_IWDG_Refresh() function.
  64. *** IWDG HAL driver macros list ***
  65. ====================================
  66. [..]
  67. Below the list of most used macros in IWDG HAL driver.
  68. (+) __HAL_IWDG_START: Enable the IWDG peripheral
  69. (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register
  70. (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
  71. (+) IWDG_ENABLE_WRITE_ACCESS : Enable write access to IWDG_PR and IWDG_RLR registers
  72. (+) IWDG_DISABLE_WRITE_ACCESS : Disable write access to IWDG_PR and IWDG_RLR registers
  73. @endverbatim
  74. ******************************************************************************
  75. * @attention
  76. *
  77. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  78. *
  79. * Redistribution and use in source and binary forms, with or without modification,
  80. * are permitted provided that the following conditions are met:
  81. * 1. Redistributions of source code must retain the above copyright notice,
  82. * this list of conditions and the following disclaimer.
  83. * 2. Redistributions in binary form must reproduce the above copyright notice,
  84. * this list of conditions and the following disclaimer in the documentation
  85. * and/or other materials provided with the distribution.
  86. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  87. * may be used to endorse or promote products derived from this software
  88. * without specific prior written permission.
  89. *
  90. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  91. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  92. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  93. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  94. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  95. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  96. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  97. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  98. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  99. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  100. *
  101. ******************************************************************************
  102. */
  103. /* Includes ------------------------------------------------------------------*/
  104. #include "stm32f7xx_hal.h"
  105. /** @addtogroup STM32F7xx_HAL_Driver
  106. * @{
  107. */
  108. /** @defgroup IWDG IWDG HAL module driver
  109. * @brief IWDG HAL module driver.
  110. * @{
  111. */
  112. #ifdef HAL_IWDG_MODULE_ENABLED
  113. /* Private typedef -----------------------------------------------------------*/
  114. /* Private define ------------------------------------------------------------*/
  115. /** @defgroup IWDG_Private_Defines IWDG Private Defines
  116. * @{
  117. */
  118. #define HAL_IWDG_DEFAULT_TIMEOUT (uint32_t)1000
  119. /**
  120. * @}
  121. */
  122. /* Private macro -------------------------------------------------------------*/
  123. /* Private variables ---------------------------------------------------------*/
  124. /* Private function prototypes -----------------------------------------------*/
  125. /* Exported functions --------------------------------------------------------*/
  126. /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
  127. * @{
  128. */
  129. /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
  130. * @brief Initialization and Configuration functions.
  131. *
  132. @verbatim
  133. ===============================================================================
  134. ##### Initialization and de-initialization functions #####
  135. ===============================================================================
  136. [..] This section provides functions allowing to:
  137. (+) Initialize the IWDG according to the specified parameters
  138. in the IWDG_InitTypeDef and create the associated handle
  139. (+) Manage Window option
  140. (+) Initialize the IWDG MSP
  141. (+) DeInitialize IWDG MSP
  142. @endverbatim
  143. * @{
  144. */
  145. /**
  146. * @brief Initializes the IWDG according to the specified
  147. * parameters in the IWDG_InitTypeDef and creates the associated handle.
  148. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  149. * the configuration information for the specified IWDG module.
  150. * @retval HAL status
  151. */
  152. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  153. {
  154. uint32_t tickstart = 0;
  155. /* Check the IWDG handle allocation */
  156. if(hiwdg == NULL)
  157. {
  158. return HAL_ERROR;
  159. }
  160. /* Check the parameters */
  161. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  162. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  163. assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
  164. /* Check pending flag, if previous update not done, return error */
  165. if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
  166. &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
  167. &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET))
  168. {
  169. return HAL_ERROR;
  170. }
  171. if(hiwdg->State == HAL_IWDG_STATE_RESET)
  172. {
  173. /* Allocate lock resource and initialize it */
  174. hiwdg->Lock = HAL_UNLOCKED;
  175. /* Init the low level hardware */
  176. HAL_IWDG_MspInit(hiwdg);
  177. }
  178. /* Change IWDG peripheral state */
  179. hiwdg->State = HAL_IWDG_STATE_BUSY;
  180. /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers */
  181. /* by writing 0x5555 in KR */
  182. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  183. /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
  184. MODIFY_REG(hiwdg->Instance->PR, (uint32_t)IWDG_PR_PR, hiwdg->Init.Prescaler);
  185. MODIFY_REG(hiwdg->Instance->RLR, (uint32_t)IWDG_RLR_RL, hiwdg->Init.Reload);
  186. /* check if window option is enabled */
  187. if (((hiwdg->Init.Window) != IWDG_WINDOW_DISABLE) || ((hiwdg->Instance->WINR) != IWDG_WINDOW_DISABLE))
  188. {
  189. tickstart = HAL_GetTick();
  190. /* Wait for register to be updated */
  191. while((uint32_t)(hiwdg->Instance->SR) != RESET)
  192. {
  193. if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
  194. {
  195. /* Set IWDG state */
  196. hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
  197. return HAL_TIMEOUT;
  198. }
  199. }
  200. /* Write to IWDG WINR the IWDG_Window value to compare with */
  201. MODIFY_REG(hiwdg->Instance->WINR, (uint32_t)IWDG_WINR_WIN, hiwdg->Init.Window);
  202. }
  203. /* Change IWDG peripheral state */
  204. hiwdg->State = HAL_IWDG_STATE_READY;
  205. /* Return function status */
  206. return HAL_OK;
  207. }
  208. /**
  209. * @brief Initializes the IWDG MSP.
  210. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  211. * the configuration information for the specified IWDG module.
  212. * @retval None
  213. */
  214. __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
  215. {
  216. /* NOTE : This function should not be modified, when the callback is needed,
  217. the HAL_IWDG_MspInit could be implemented in the user file
  218. */
  219. }
  220. /**
  221. * @}
  222. */
  223. /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
  224. * @brief IO operation functions
  225. *
  226. @verbatim
  227. ===============================================================================
  228. ##### IO operation functions #####
  229. ===============================================================================
  230. [..] This section provides functions allowing to:
  231. (+) Start the IWDG.
  232. (+) Refresh the IWDG.
  233. @endverbatim
  234. * @{
  235. */
  236. /**
  237. * @brief Starts the IWDG.
  238. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  239. * the configuration information for the specified IWDG module.
  240. * @retval HAL status
  241. */
  242. HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
  243. {
  244. uint32_t tickstart = 0;
  245. /* Process locked */
  246. __HAL_LOCK(hiwdg);
  247. /* Change IWDG peripheral state */
  248. hiwdg->State = HAL_IWDG_STATE_BUSY;
  249. /* Reload IWDG counter with value defined in the RLR register */
  250. if ((hiwdg->Init.Window) == IWDG_WINDOW_DISABLE)
  251. {
  252. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  253. }
  254. /* Start the IWDG peripheral */
  255. __HAL_IWDG_START(hiwdg);
  256. tickstart = HAL_GetTick();
  257. /* Wait until PVU, RVU, WVU flag are RESET */
  258. while( (__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
  259. &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
  260. &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_WVU) != RESET) )
  261. {
  262. if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
  263. {
  264. /* Set IWDG state */
  265. hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
  266. /* Process unlocked */
  267. __HAL_UNLOCK(hiwdg);
  268. return HAL_TIMEOUT;
  269. }
  270. }
  271. /* Change IWDG peripheral state */
  272. hiwdg->State = HAL_IWDG_STATE_READY;
  273. /* Process Unlocked */
  274. __HAL_UNLOCK(hiwdg);
  275. /* Return function status */
  276. return HAL_OK;
  277. }
  278. /**
  279. * @brief Refreshes the IWDG.
  280. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  281. * the configuration information for the specified IWDG module.
  282. * @retval HAL status
  283. */
  284. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  285. {
  286. uint32_t tickstart = 0;
  287. /* Process Locked */
  288. __HAL_LOCK(hiwdg);
  289. /* Change IWDG peripheral state */
  290. hiwdg->State = HAL_IWDG_STATE_BUSY;
  291. tickstart = HAL_GetTick();
  292. /* Wait until RVU flag is RESET */
  293. while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
  294. {
  295. if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT)
  296. {
  297. /* Set IWDG state */
  298. hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
  299. /* Process unlocked */
  300. __HAL_UNLOCK(hiwdg);
  301. return HAL_TIMEOUT;
  302. }
  303. }
  304. /* Reload IWDG counter with value defined in the reload register */
  305. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  306. /* Change IWDG peripheral state */
  307. hiwdg->State = HAL_IWDG_STATE_READY;
  308. /* Process Unlocked */
  309. __HAL_UNLOCK(hiwdg);
  310. /* Return function status */
  311. return HAL_OK;
  312. }
  313. /**
  314. * @}
  315. */
  316. /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
  317. * @brief Peripheral State functions.
  318. *
  319. @verbatim
  320. ===============================================================================
  321. ##### Peripheral State functions #####
  322. ===============================================================================
  323. [..]
  324. This subsection permits to get in run-time the status of the peripheral.
  325. @endverbatim
  326. * @{
  327. */
  328. /**
  329. * @brief Returns the IWDG state.
  330. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  331. * the configuration information for the specified IWDG module.
  332. * @retval HAL state
  333. */
  334. HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
  335. {
  336. return hiwdg->State;
  337. }
  338. /**
  339. * @}
  340. */
  341. /**
  342. * @}
  343. */
  344. #endif /* HAL_IWDG_MODULE_ENABLED */
  345. /**
  346. * @}
  347. */
  348. /**
  349. * @}
  350. */
  351. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/