stm32l1xx_hal_iwdg.c 12 KB

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