PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_iwdg.c

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