001
targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_iwdg.c@0:13413ea9a877, 2022-06-12 (annotated)
- Committer:
- ganlikun
- Date:
- Sun Jun 12 14:02:44 2022 +0000
- Revision:
- 0:13413ea9a877
00
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ganlikun | 0:13413ea9a877 | 1 | /** |
ganlikun | 0:13413ea9a877 | 2 | ****************************************************************************** |
ganlikun | 0:13413ea9a877 | 3 | * @file stm32f4xx_hal_iwdg.c |
ganlikun | 0:13413ea9a877 | 4 | * @author MCD Application Team |
ganlikun | 0:13413ea9a877 | 5 | * @version V1.7.1 |
ganlikun | 0:13413ea9a877 | 6 | * @date 14-April-2017 |
ganlikun | 0:13413ea9a877 | 7 | * @brief IWDG HAL module driver. |
ganlikun | 0:13413ea9a877 | 8 | * This file provides firmware functions to manage the following |
ganlikun | 0:13413ea9a877 | 9 | * functionalities of the Independent Watchdog (IWDG) peripheral: |
ganlikun | 0:13413ea9a877 | 10 | * + Initialization and Start functions |
ganlikun | 0:13413ea9a877 | 11 | * + IO operation functions |
ganlikun | 0:13413ea9a877 | 12 | * |
ganlikun | 0:13413ea9a877 | 13 | @verbatim |
ganlikun | 0:13413ea9a877 | 14 | ============================================================================== |
ganlikun | 0:13413ea9a877 | 15 | ##### IWDG Generic features ##### |
ganlikun | 0:13413ea9a877 | 16 | ============================================================================== |
ganlikun | 0:13413ea9a877 | 17 | [..] |
ganlikun | 0:13413ea9a877 | 18 | (+) The IWDG can be started by either software or hardware (configurable |
ganlikun | 0:13413ea9a877 | 19 | through option byte). |
ganlikun | 0:13413ea9a877 | 20 | |
ganlikun | 0:13413ea9a877 | 21 | (+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even |
ganlikun | 0:13413ea9a877 | 22 | if the main clock fails. |
ganlikun | 0:13413ea9a877 | 23 | |
ganlikun | 0:13413ea9a877 | 24 | (+) Once the IWDG is started, the LSI is forced ON and both can not be |
ganlikun | 0:13413ea9a877 | 25 | disabled. The counter starts counting down from the reset value (0xFFF). |
ganlikun | 0:13413ea9a877 | 26 | When it reaches the end of count value (0x000) a reset signal is |
ganlikun | 0:13413ea9a877 | 27 | generated (IWDG reset). |
ganlikun | 0:13413ea9a877 | 28 | |
ganlikun | 0:13413ea9a877 | 29 | (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register, |
ganlikun | 0:13413ea9a877 | 30 | the IWDG_RLR value is reloaded in the counter and the watchdog reset is |
ganlikun | 0:13413ea9a877 | 31 | prevented. |
ganlikun | 0:13413ea9a877 | 32 | |
ganlikun | 0:13413ea9a877 | 33 | (+) The IWDG is implemented in the VDD voltage domain that is still functional |
ganlikun | 0:13413ea9a877 | 34 | in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY). |
ganlikun | 0:13413ea9a877 | 35 | IWDGRST flag in RCC_CSR register can be used to inform when an IWDG |
ganlikun | 0:13413ea9a877 | 36 | reset occurs. |
ganlikun | 0:13413ea9a877 | 37 | |
ganlikun | 0:13413ea9a877 | 38 | (+) Debug mode : When the microcontroller enters debug mode (core halted), |
ganlikun | 0:13413ea9a877 | 39 | the IWDG counter either continues to work normally or stops, depending |
ganlikun | 0:13413ea9a877 | 40 | on DBG_IWDG_STOP configuration bit in DBG module, accessible through |
ganlikun | 0:13413ea9a877 | 41 | __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros |
ganlikun | 0:13413ea9a877 | 42 | |
ganlikun | 0:13413ea9a877 | 43 | [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s |
ganlikun | 0:13413ea9a877 | 44 | The IWDG timeout may vary due to LSI frequency dispersion. STM32F4xx |
ganlikun | 0:13413ea9a877 | 45 | devices provide the capability to measure the LSI frequency (LSI clock |
ganlikun | 0:13413ea9a877 | 46 | connected internally to TIM5 CH4 input capture). The measured value |
ganlikun | 0:13413ea9a877 | 47 | can be used to have an IWDG timeout with an acceptable accuracy. |
ganlikun | 0:13413ea9a877 | 48 | |
ganlikun | 0:13413ea9a877 | 49 | ##### How to use this driver ##### |
ganlikun | 0:13413ea9a877 | 50 | ============================================================================== |
ganlikun | 0:13413ea9a877 | 51 | [..] |
ganlikun | 0:13413ea9a877 | 52 | (#) Use IWDG using HAL_IWDG_Init() function to : |
ganlikun | 0:13413ea9a877 | 53 | (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI |
ganlikun | 0:13413ea9a877 | 54 | clock is forced ON and IWDG counter starts downcounting. |
ganlikun | 0:13413ea9a877 | 55 | (++) Enable write access to configuration register: IWDG_PR & IWDG_RLR. |
ganlikun | 0:13413ea9a877 | 56 | (++) Configure the IWDG prescaler and counter reload value. This reload |
ganlikun | 0:13413ea9a877 | 57 | value will be loaded in the IWDG counter each time the watchdog is |
ganlikun | 0:13413ea9a877 | 58 | reloaded, then the IWDG will start counting down from this value. |
ganlikun | 0:13413ea9a877 | 59 | (++) wait for status flags to be reset" |
ganlikun | 0:13413ea9a877 | 60 | |
ganlikun | 0:13413ea9a877 | 61 | (#) Then the application program must refresh the IWDG counter at regular |
ganlikun | 0:13413ea9a877 | 62 | intervals during normal operation to prevent an MCU reset, using |
ganlikun | 0:13413ea9a877 | 63 | HAL_IWDG_Refresh() function. |
ganlikun | 0:13413ea9a877 | 64 | |
ganlikun | 0:13413ea9a877 | 65 | *** IWDG HAL driver macros list *** |
ganlikun | 0:13413ea9a877 | 66 | ==================================== |
ganlikun | 0:13413ea9a877 | 67 | [..] |
ganlikun | 0:13413ea9a877 | 68 | Below the list of most used macros in IWDG HAL driver: |
ganlikun | 0:13413ea9a877 | 69 | (+) __HAL_IWDG_START: Enable the IWDG peripheral |
ganlikun | 0:13413ea9a877 | 70 | (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in |
ganlikun | 0:13413ea9a877 | 71 | the reload register |
ganlikun | 0:13413ea9a877 | 72 | |
ganlikun | 0:13413ea9a877 | 73 | @endverbatim |
ganlikun | 0:13413ea9a877 | 74 | ****************************************************************************** |
ganlikun | 0:13413ea9a877 | 75 | * @attention |
ganlikun | 0:13413ea9a877 | 76 | * |
ganlikun | 0:13413ea9a877 | 77 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
ganlikun | 0:13413ea9a877 | 78 | * |
ganlikun | 0:13413ea9a877 | 79 | * Redistribution and use in source and binary forms, with or without modification, |
ganlikun | 0:13413ea9a877 | 80 | * are permitted provided that the following conditions are met: |
ganlikun | 0:13413ea9a877 | 81 | * 1. Redistributions of source code must retain the above copyright notice, |
ganlikun | 0:13413ea9a877 | 82 | * this list of conditions and the following disclaimer. |
ganlikun | 0:13413ea9a877 | 83 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
ganlikun | 0:13413ea9a877 | 84 | * this list of conditions and the following disclaimer in the documentation |
ganlikun | 0:13413ea9a877 | 85 | * and/or other materials provided with the distribution |
ganlikun | 0:13413ea9a877 | 86 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
ganlikun | 0:13413ea9a877 | 87 | * may be used to endorse or promote products derived from this software |
ganlikun | 0:13413ea9a877 | 88 | * without specific prior written permission. |
ganlikun | 0:13413ea9a877 | 89 | * |
ganlikun | 0:13413ea9a877 | 90 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
ganlikun | 0:13413ea9a877 | 91 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
ganlikun | 0:13413ea9a877 | 92 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
ganlikun | 0:13413ea9a877 | 93 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
ganlikun | 0:13413ea9a877 | 94 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
ganlikun | 0:13413ea9a877 | 95 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
ganlikun | 0:13413ea9a877 | 96 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
ganlikun | 0:13413ea9a877 | 97 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
ganlikun | 0:13413ea9a877 | 98 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
ganlikun | 0:13413ea9a877 | 99 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
ganlikun | 0:13413ea9a877 | 100 | * |
ganlikun | 0:13413ea9a877 | 101 | ****************************************************************************** |
ganlikun | 0:13413ea9a877 | 102 | */ |
ganlikun | 0:13413ea9a877 | 103 | |
ganlikun | 0:13413ea9a877 | 104 | /* Includes ------------------------------------------------------------------*/ |
ganlikun | 0:13413ea9a877 | 105 | #include "stm32f4xx_hal.h" |
ganlikun | 0:13413ea9a877 | 106 | |
ganlikun | 0:13413ea9a877 | 107 | /** @addtogroup STM32F4xx_HAL_Driver |
ganlikun | 0:13413ea9a877 | 108 | * @{ |
ganlikun | 0:13413ea9a877 | 109 | */ |
ganlikun | 0:13413ea9a877 | 110 | |
ganlikun | 0:13413ea9a877 | 111 | #ifdef HAL_IWDG_MODULE_ENABLED |
ganlikun | 0:13413ea9a877 | 112 | /** @defgroup IWDG IWDG |
ganlikun | 0:13413ea9a877 | 113 | * @brief IWDG HAL module driver. |
ganlikun | 0:13413ea9a877 | 114 | * @{ |
ganlikun | 0:13413ea9a877 | 115 | */ |
ganlikun | 0:13413ea9a877 | 116 | |
ganlikun | 0:13413ea9a877 | 117 | /* Private typedef -----------------------------------------------------------*/ |
ganlikun | 0:13413ea9a877 | 118 | /* Private define ------------------------------------------------------------*/ |
ganlikun | 0:13413ea9a877 | 119 | /** @defgroup IWDG_Private_Defines IWDG Private Defines |
ganlikun | 0:13413ea9a877 | 120 | * @{ |
ganlikun | 0:13413ea9a877 | 121 | */ |
ganlikun | 0:13413ea9a877 | 122 | /* Status register need 5 RC LSI divided by prescaler clock to be updated. With |
ganlikun | 0:13413ea9a877 | 123 | higher prescaler (256), and according to HSI variation, we need to wait at |
ganlikun | 0:13413ea9a877 | 124 | least 6 cycles so 48 ms. */ |
ganlikun | 0:13413ea9a877 | 125 | #define HAL_IWDG_DEFAULT_TIMEOUT 48U |
ganlikun | 0:13413ea9a877 | 126 | /** |
ganlikun | 0:13413ea9a877 | 127 | * @} |
ganlikun | 0:13413ea9a877 | 128 | */ |
ganlikun | 0:13413ea9a877 | 129 | |
ganlikun | 0:13413ea9a877 | 130 | /* Private macro -------------------------------------------------------------*/ |
ganlikun | 0:13413ea9a877 | 131 | /* Private variables ---------------------------------------------------------*/ |
ganlikun | 0:13413ea9a877 | 132 | /* Private function prototypes -----------------------------------------------*/ |
ganlikun | 0:13413ea9a877 | 133 | /* Exported functions --------------------------------------------------------*/ |
ganlikun | 0:13413ea9a877 | 134 | |
ganlikun | 0:13413ea9a877 | 135 | /** @addtogroup IWDG_Exported_Functions |
ganlikun | 0:13413ea9a877 | 136 | * @{ |
ganlikun | 0:13413ea9a877 | 137 | */ |
ganlikun | 0:13413ea9a877 | 138 | |
ganlikun | 0:13413ea9a877 | 139 | /** @addtogroup IWDG_Exported_Functions_Group1 |
ganlikun | 0:13413ea9a877 | 140 | * @brief Initialization and Start functions. |
ganlikun | 0:13413ea9a877 | 141 | * |
ganlikun | 0:13413ea9a877 | 142 | @verbatim |
ganlikun | 0:13413ea9a877 | 143 | =============================================================================== |
ganlikun | 0:13413ea9a877 | 144 | ##### Initialization and Start functions ##### |
ganlikun | 0:13413ea9a877 | 145 | =============================================================================== |
ganlikun | 0:13413ea9a877 | 146 | [..] This section provides functions allowing to: |
ganlikun | 0:13413ea9a877 | 147 | (+) Initialize the IWDG according to the specified parameters in the |
ganlikun | 0:13413ea9a877 | 148 | IWDG_InitTypeDef of associated handle. |
ganlikun | 0:13413ea9a877 | 149 | (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog |
ganlikun | 0:13413ea9a877 | 150 | is reloaded in order to exit function with correct time base. |
ganlikun | 0:13413ea9a877 | 151 | |
ganlikun | 0:13413ea9a877 | 152 | @endverbatim |
ganlikun | 0:13413ea9a877 | 153 | * @{ |
ganlikun | 0:13413ea9a877 | 154 | */ |
ganlikun | 0:13413ea9a877 | 155 | |
ganlikun | 0:13413ea9a877 | 156 | /** |
ganlikun | 0:13413ea9a877 | 157 | * @brief Initialize the IWDG according to the specified parameters in the |
ganlikun | 0:13413ea9a877 | 158 | * IWDG_InitTypeDef and start watchdog. Before exiting function, |
ganlikun | 0:13413ea9a877 | 159 | * watchdog is refreshed in order to have correct time base. |
ganlikun | 0:13413ea9a877 | 160 | * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains |
ganlikun | 0:13413ea9a877 | 161 | * the configuration information for the specified IWDG module. |
ganlikun | 0:13413ea9a877 | 162 | * @retval HAL status |
ganlikun | 0:13413ea9a877 | 163 | */ |
ganlikun | 0:13413ea9a877 | 164 | HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg) |
ganlikun | 0:13413ea9a877 | 165 | { |
ganlikun | 0:13413ea9a877 | 166 | uint32_t tickstart; |
ganlikun | 0:13413ea9a877 | 167 | |
ganlikun | 0:13413ea9a877 | 168 | /* Check the IWDG handle allocation */ |
ganlikun | 0:13413ea9a877 | 169 | if(hiwdg == NULL) |
ganlikun | 0:13413ea9a877 | 170 | { |
ganlikun | 0:13413ea9a877 | 171 | return HAL_ERROR; |
ganlikun | 0:13413ea9a877 | 172 | } |
ganlikun | 0:13413ea9a877 | 173 | |
ganlikun | 0:13413ea9a877 | 174 | /* Check the parameters */ |
ganlikun | 0:13413ea9a877 | 175 | assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance)); |
ganlikun | 0:13413ea9a877 | 176 | assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler)); |
ganlikun | 0:13413ea9a877 | 177 | assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload)); |
ganlikun | 0:13413ea9a877 | 178 | |
ganlikun | 0:13413ea9a877 | 179 | /* Enable IWDG. LSI is turned on automaticaly */ |
ganlikun | 0:13413ea9a877 | 180 | __HAL_IWDG_START(hiwdg); |
ganlikun | 0:13413ea9a877 | 181 | |
ganlikun | 0:13413ea9a877 | 182 | /* Enable write access to IWDG_PR and IWDG_RLR registers by writing 0x5555 in KR */ |
ganlikun | 0:13413ea9a877 | 183 | IWDG_ENABLE_WRITE_ACCESS(hiwdg); |
ganlikun | 0:13413ea9a877 | 184 | |
ganlikun | 0:13413ea9a877 | 185 | /* Write to IWDG registers the Prescaler & Reload values to work with */ |
ganlikun | 0:13413ea9a877 | 186 | hiwdg->Instance->PR = hiwdg->Init.Prescaler; |
ganlikun | 0:13413ea9a877 | 187 | hiwdg->Instance->RLR = hiwdg->Init.Reload; |
ganlikun | 0:13413ea9a877 | 188 | |
ganlikun | 0:13413ea9a877 | 189 | /* Check pending flag, if previous update not done, return timeout */ |
ganlikun | 0:13413ea9a877 | 190 | tickstart = HAL_GetTick(); |
ganlikun | 0:13413ea9a877 | 191 | |
ganlikun | 0:13413ea9a877 | 192 | /* Wait for register to be updated */ |
ganlikun | 0:13413ea9a877 | 193 | while(hiwdg->Instance->SR != RESET) |
ganlikun | 0:13413ea9a877 | 194 | { |
ganlikun | 0:13413ea9a877 | 195 | if((HAL_GetTick() - tickstart ) > HAL_IWDG_DEFAULT_TIMEOUT) |
ganlikun | 0:13413ea9a877 | 196 | { |
ganlikun | 0:13413ea9a877 | 197 | return HAL_TIMEOUT; |
ganlikun | 0:13413ea9a877 | 198 | } |
ganlikun | 0:13413ea9a877 | 199 | } |
ganlikun | 0:13413ea9a877 | 200 | |
ganlikun | 0:13413ea9a877 | 201 | /* Reload IWDG counter with value defined in the reload register */ |
ganlikun | 0:13413ea9a877 | 202 | __HAL_IWDG_RELOAD_COUNTER(hiwdg); |
ganlikun | 0:13413ea9a877 | 203 | |
ganlikun | 0:13413ea9a877 | 204 | /* Return function status */ |
ganlikun | 0:13413ea9a877 | 205 | return HAL_OK; |
ganlikun | 0:13413ea9a877 | 206 | } |
ganlikun | 0:13413ea9a877 | 207 | |
ganlikun | 0:13413ea9a877 | 208 | /** |
ganlikun | 0:13413ea9a877 | 209 | * @} |
ganlikun | 0:13413ea9a877 | 210 | */ |
ganlikun | 0:13413ea9a877 | 211 | |
ganlikun | 0:13413ea9a877 | 212 | /** @addtogroup IWDG_Exported_Functions_Group2 |
ganlikun | 0:13413ea9a877 | 213 | * @brief IO operation functions |
ganlikun | 0:13413ea9a877 | 214 | * |
ganlikun | 0:13413ea9a877 | 215 | @verbatim |
ganlikun | 0:13413ea9a877 | 216 | =============================================================================== |
ganlikun | 0:13413ea9a877 | 217 | ##### IO operation functions ##### |
ganlikun | 0:13413ea9a877 | 218 | =============================================================================== |
ganlikun | 0:13413ea9a877 | 219 | [..] This section provides functions allowing to: |
ganlikun | 0:13413ea9a877 | 220 | (+) Refresh the IWDG. |
ganlikun | 0:13413ea9a877 | 221 | |
ganlikun | 0:13413ea9a877 | 222 | @endverbatim |
ganlikun | 0:13413ea9a877 | 223 | * @{ |
ganlikun | 0:13413ea9a877 | 224 | */ |
ganlikun | 0:13413ea9a877 | 225 | |
ganlikun | 0:13413ea9a877 | 226 | /** |
ganlikun | 0:13413ea9a877 | 227 | * @brief Refresh the IWDG. |
ganlikun | 0:13413ea9a877 | 228 | * @param hiwdg pointer to a IWDG_HandleTypeDef structure that contains |
ganlikun | 0:13413ea9a877 | 229 | * the configuration information for the specified IWDG module. |
ganlikun | 0:13413ea9a877 | 230 | * @retval HAL status |
ganlikun | 0:13413ea9a877 | 231 | */ |
ganlikun | 0:13413ea9a877 | 232 | HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg) |
ganlikun | 0:13413ea9a877 | 233 | { |
ganlikun | 0:13413ea9a877 | 234 | /* Reload IWDG counter with value defined in the reload register */ |
ganlikun | 0:13413ea9a877 | 235 | __HAL_IWDG_RELOAD_COUNTER(hiwdg); |
ganlikun | 0:13413ea9a877 | 236 | |
ganlikun | 0:13413ea9a877 | 237 | /* Return function status */ |
ganlikun | 0:13413ea9a877 | 238 | return HAL_OK; |
ganlikun | 0:13413ea9a877 | 239 | } |
ganlikun | 0:13413ea9a877 | 240 | |
ganlikun | 0:13413ea9a877 | 241 | /** |
ganlikun | 0:13413ea9a877 | 242 | * @} |
ganlikun | 0:13413ea9a877 | 243 | */ |
ganlikun | 0:13413ea9a877 | 244 | |
ganlikun | 0:13413ea9a877 | 245 | /** |
ganlikun | 0:13413ea9a877 | 246 | * @} |
ganlikun | 0:13413ea9a877 | 247 | */ |
ganlikun | 0:13413ea9a877 | 248 | |
ganlikun | 0:13413ea9a877 | 249 | #endif /* HAL_IWDG_MODULE_ENABLED */ |
ganlikun | 0:13413ea9a877 | 250 | /** |
ganlikun | 0:13413ea9a877 | 251 | * @} |
ganlikun | 0:13413ea9a877 | 252 | */ |
ganlikun | 0:13413ea9a877 | 253 | |
ganlikun | 0:13413ea9a877 | 254 | /** |
ganlikun | 0:13413ea9a877 | 255 | * @} |
ganlikun | 0:13413ea9a877 | 256 | */ |
ganlikun | 0:13413ea9a877 | 257 | |
ganlikun | 0:13413ea9a877 | 258 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
ganlikun | 0:13413ea9a877 | 259 |