stm32f4xx_hal_iwdg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_iwdg.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  7. * @brief IWDG HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Independent Watchdog (IWDG) peripheral:
  10. * + Initialization and Configuration functions
  11. * + IO operation functions
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### IWDG Specific features #####
  17. ==============================================================================
  18. [..]
  19. (+) The IWDG can be started by either software or hardware (configurable
  20. through option byte).
  21. (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
  22. thus stays active even if the main clock fails.
  23. Once the IWDG is started, the LSI is forced ON and cannot be disabled
  24. (LSI cannot be disabled too), and the counter starts counting down from
  25. the reset value of 0xFFF. When it reaches the end of count value (0x000)
  26. a system reset is generated.
  27. (+) The IWDG counter should be refreshed at regular intervals, otherwise the
  28. watchdog generates an MCU reset when the counter reaches 0.
  29. (+) The IWDG is implemented in the VDD voltage domain that is still functional
  30. in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
  31. IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
  32. reset occurs.
  33. (+) Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
  34. The IWDG timeout may vary due to LSI frequency dispersion. STM32F4xx
  35. devices provide the capability to measure the LSI frequency (LSI clock
  36. connected internally to TIM5 CH4 input capture). The measured value
  37. can be used to have an IWDG timeout with an acceptable accuracy.
  38. ##### How to use this driver #####
  39. ==============================================================================
  40. [..]
  41. If Window option is disabled
  42. (+) Use IWDG using HAL_IWDG_Init() function to :
  43. (++) Enable write access to IWDG_PR, IWDG_RLR.
  44. (++) Configure the IWDG prescaler, counter reload value.
  45. This reload value will be loaded in the IWDG counter each time the counter
  46. is reloaded, then the IWDG will start counting down from this value.
  47. [..]
  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. @endverbatim
  72. ******************************************************************************
  73. * @attention
  74. *
  75. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  76. *
  77. * Redistribution and use in source and binary forms, with or without modification,
  78. * are permitted provided that the following conditions are met:
  79. * 1. Redistributions of source code must retain the above copyright notice,
  80. * this list of conditions and the following disclaimer.
  81. * 2. Redistributions in binary form must reproduce the above copyright notice,
  82. * this list of conditions and the following disclaimer in the documentation
  83. * and/or other materials provided with the distribution.
  84. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  85. * may be used to endorse or promote products derived from this software
  86. * without specific prior written permission.
  87. *
  88. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  89. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  90. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  91. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  92. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  93. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  94. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  95. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  96. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  97. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  98. *
  99. ******************************************************************************
  100. */
  101. /* Includes ------------------------------------------------------------------*/
  102. #include "stm32f4xx_hal.h"
  103. /** @addtogroup STM32F4xx_HAL_Driver
  104. * @{
  105. */
  106. /** @defgroup IWDG IWDG
  107. * @brief IWDG HAL module driver.
  108. * @{
  109. */
  110. #ifdef HAL_IWDG_MODULE_ENABLED
  111. /* Private typedef -----------------------------------------------------------*/
  112. /* Private define ------------------------------------------------------------*/
  113. /** @addtogroup IWDG_Private_Constants
  114. * @{
  115. */
  116. #define IWDG_TIMEOUT_FLAG ((uint32_t)1000) /* 1 s */
  117. /**
  118. * @}
  119. */
  120. /* Private macro -------------------------------------------------------------*/
  121. /* Private variables ---------------------------------------------------------*/
  122. /* Private function prototypes -----------------------------------------------*/
  123. /* Private functions ---------------------------------------------------------*/
  124. /* Exported functions --------------------------------------------------------*/
  125. /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
  126. * @{
  127. */
  128. /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
  129. * @brief Initialization and Configuration functions.
  130. *
  131. @verbatim
  132. ===============================================================================
  133. ##### Initialization and de-initialization functions #####
  134. ===============================================================================
  135. [..] This section provides functions allowing to:
  136. (+) Initialize the IWDG according to the specified parameters
  137. in the IWDG_InitTypeDef and create the associated handle
  138. (+) Initialize the IWDG MSP
  139. (+) DeInitialize IWDG MSP
  140. @endverbatim
  141. * @{
  142. */
  143. /**
  144. * @brief Initializes the IWDG according to the specified
  145. * parameters in the IWDG_InitTypeDef and creates the associated handle.
  146. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  147. * the configuration information for the specified IWDG module.
  148. * @retval HAL status
  149. */
  150. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  151. {
  152. /* Check the IWDG handle allocation */
  153. if(hiwdg == NULL)
  154. {
  155. return HAL_ERROR;
  156. }
  157. /* Check the parameters */
  158. assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  159. assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  160. assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  161. if(hiwdg->State == HAL_IWDG_STATE_RESET)
  162. {
  163. /* Allocate lock resource and initialize it */
  164. hiwdg->Lock = HAL_UNLOCKED;
  165. /* Init the low level hardware */
  166. HAL_IWDG_MspInit(hiwdg);
  167. }
  168. /* Change IWDG peripheral state */
  169. hiwdg->State = HAL_IWDG_STATE_BUSY;
  170. /* Enable write access to IWDG_PR and IWDG_RLR registers */
  171. IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  172. /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
  173. MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
  174. MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
  175. /* Change IWDG peripheral state */
  176. hiwdg->State = HAL_IWDG_STATE_READY;
  177. /* Return function status */
  178. return HAL_OK;
  179. }
  180. /**
  181. * @brief Initializes the IWDG MSP.
  182. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  183. * the configuration information for the specified IWDG module.
  184. * @retval None
  185. */
  186. __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
  187. {
  188. /* Prevent unused argument(s) compilation warning */
  189. UNUSED(hiwdg);
  190. /* NOTE : This function Should not be modified, when the callback is needed,
  191. the HAL_IWDG_MspInit could be implemented in the user file
  192. */
  193. }
  194. /**
  195. * @}
  196. */
  197. /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
  198. * @brief IO operation functions
  199. *
  200. @verbatim
  201. ===============================================================================
  202. ##### IO operation functions #####
  203. ===============================================================================
  204. [..] This section provides functions allowing to:
  205. (+) Start the IWDG.
  206. (+) Refresh the IWDG.
  207. @endverbatim
  208. * @{
  209. */
  210. /**
  211. * @brief Starts the IWDG.
  212. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  213. * the configuration information for the specified IWDG module.
  214. * @retval HAL status
  215. */
  216. HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
  217. {
  218. /* Process Locked */
  219. __HAL_LOCK(hiwdg);
  220. /* Change IWDG peripheral state */
  221. hiwdg->State = HAL_IWDG_STATE_BUSY;
  222. /* Start the IWDG peripheral */
  223. __HAL_IWDG_START(hiwdg);
  224. /* Reload IWDG counter with value defined in the RLR register */
  225. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  226. /* Change IWDG peripheral state */
  227. hiwdg->State = HAL_IWDG_STATE_READY;
  228. /* Process Unlocked */
  229. __HAL_UNLOCK(hiwdg);
  230. /* Return function status */
  231. return HAL_OK;
  232. }
  233. /**
  234. * @brief Refreshes the IWDG.
  235. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  236. * the configuration information for the specified IWDG module.
  237. * @retval HAL status
  238. */
  239. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  240. {
  241. uint32_t tickstart = 0;
  242. /* Process Locked */
  243. __HAL_LOCK(hiwdg);
  244. /* Change IWDG peripheral state */
  245. hiwdg->State = HAL_IWDG_STATE_BUSY;
  246. tickstart = HAL_GetTick();
  247. /* Wait until RVU flag is RESET */
  248. while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
  249. {
  250. if((HAL_GetTick() - tickstart ) > IWDG_TIMEOUT_FLAG)
  251. {
  252. /* Set IWDG state */
  253. hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
  254. /* Process unlocked */
  255. __HAL_UNLOCK(hiwdg);
  256. return HAL_TIMEOUT;
  257. }
  258. }
  259. /* Reload IWDG counter with value defined in the reload register */
  260. __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  261. /* Change IWDG peripheral state */
  262. hiwdg->State = HAL_IWDG_STATE_READY;
  263. /* Process Unlocked */
  264. __HAL_UNLOCK(hiwdg);
  265. /* Return function status */
  266. return HAL_OK;
  267. }
  268. /**
  269. * @}
  270. */
  271. /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
  272. * @brief Peripheral State functions.
  273. *
  274. @verbatim
  275. ===============================================================================
  276. ##### Peripheral State functions #####
  277. ===============================================================================
  278. [..]
  279. This subsection permits to get in run-time the status of the peripheral
  280. and the data flow.
  281. @endverbatim
  282. * @{
  283. */
  284. /**
  285. * @brief Returns the IWDG state.
  286. * @param hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  287. * the configuration information for the specified IWDG module.
  288. * @retval HAL state
  289. */
  290. HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
  291. {
  292. return hiwdg->State;
  293. }
  294. /**
  295. * @}
  296. */
  297. /**
  298. * @}
  299. */
  300. #endif /* HAL_IWDG_MODULE_ENABLED */
  301. /**
  302. * @}
  303. */
  304. /**
  305. * @}
  306. */
  307. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/