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