Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /** |
| 2 | ****************************************************************************** |
||
| 3 | * @file stm32f1xx_hal_wwdg.c |
||
| 4 | * @author MCD Application Team |
||
| 5 | * @brief WWDG HAL module driver. |
||
| 6 | * This file provides firmware functions to manage the following |
||
| 7 | * functionalities of the Window Watchdog (WWDG) peripheral: |
||
| 8 | * + Initialization and de-initialization functions |
||
| 9 | * + IO operation functions |
||
| 10 | * + Peripheral State functions |
||
| 11 | @verbatim |
||
| 12 | ============================================================================== |
||
| 13 | ##### WWDG specific features ##### |
||
| 14 | ============================================================================== |
||
| 15 | [..] |
||
| 16 | Once enabled the WWDG generates a system reset on expiry of a programmed |
||
| 17 | time period, unless the program refreshes the counter (downcounter) |
||
| 18 | before reaching 0x3F value (i.e. a reset is generated when the counter |
||
| 19 | value rolls over from 0x40 to 0x3F). |
||
| 20 | |||
| 21 | (+) An MCU reset is also generated if the counter value is refreshed |
||
| 22 | before the counter has reached the refresh window value. This |
||
| 23 | implies that the counter must be refreshed in a limited window. |
||
| 24 | (+) Once enabled the WWDG cannot be disabled except by a system reset. |
||
| 25 | (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG |
||
| 26 | reset occurs. |
||
| 27 | (+) The WWDG counter input clock is derived from the APB clock divided |
||
| 28 | by a programmable prescaler. |
||
| 29 | (+) WWDG clock (Hz) = PCLK1 / (4096 * Prescaler) |
||
| 30 | (+) WWDG timeout (mS) = 1000 * Counter / WWDG clock |
||
| 31 | (+) WWDG Counter refresh is allowed between the following limits : |
||
| 32 | (++) min time (mS) = 1000 * (Counter _ Window) / WWDG clock |
||
| 33 | (++) max time (mS) = 1000 * (Counter _ 0x40) / WWDG clock |
||
| 34 | |||
| 35 | (+) Min-max timeout value at 36 MHz(PCLK1): 910 us / 58.25 ms |
||
| 36 | |||
| 37 | (+) The Early Wakeup Interrupt (EWI) can be used if specific safety |
||
| 38 | operations or data logging must be performed before the actual reset is |
||
| 39 | generated. When the downcounter reaches the value 0x40, an EWI interrupt |
||
| 40 | is generated and the corresponding interrupt service routine (ISR) can |
||
| 41 | be used to trigger specific actions (such as communications or data |
||
| 42 | logging), before resetting the device. |
||
| 43 | In some applications, the EWI interrupt can be used to manage a software |
||
| 44 | system check and/or system recovery/graceful degradation, without |
||
| 45 | generating a WWDG reset. In this case, the corresponding interrupt |
||
| 46 | service routine (ISR) should reload the WWDG counter to avoid the WWDG |
||
| 47 | reset, then trigger the required actions. |
||
| 48 | Note:When the EWI interrupt cannot be served, e.g. due to a system lock |
||
| 49 | in a higher priority task, the WWDG reset will eventually be generated. |
||
| 50 | |||
| 51 | (+) Debug mode : When the microcontroller enters debug mode (core halted), |
||
| 52 | the WWDG counter either continues to work normally or stops, depending |
||
| 53 | on DBG_WWDG_STOP configuration bit in DBG module, accessible through |
||
| 54 | __HAL_DBGMCU_FREEZE_WWDG() and __HAL_DBGMCU_UNFREEZE_WWDG() macros |
||
| 55 | |||
| 56 | ##### How to use this driver ##### |
||
| 57 | ============================================================================== |
||
| 58 | [..] |
||
| 59 | (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE(). |
||
| 60 | |||
| 61 | (+) Set the WWDG prescaler, refresh window, counter value and Early Wakeup |
||
| 62 | Interrupt mode using using HAL_WWDG_Init() function. |
||
| 63 | This enables WWDG peripheral and the downcounter starts downcounting |
||
| 64 | from given counter value. |
||
| 65 | Init function can be called again to modify all watchdog parameters, |
||
| 66 | however if EWI mode has been set once, it can't be clear until next |
||
| 67 | reset. |
||
| 68 | |||
| 69 | (+) The application program must refresh the WWDG counter at regular |
||
| 70 | intervals during normal operation to prevent an MCU reset using |
||
| 71 | HAL_WWDG_Refresh() function. This operation must occur only when |
||
| 72 | the counter is lower than the window value already programmed. |
||
| 73 | |||
| 74 | (+) if Early Wakeup Interrupt mode is enable an interrupt is generated when |
||
| 75 | the counter reaches 0x40. User can add his own code in weak function |
||
| 76 | HAL_WWDG_EarlyWakeupCallback(). |
||
| 77 | |||
| 78 | *** WWDG HAL driver macros list *** |
||
| 79 | ================================== |
||
| 80 | [..] |
||
| 81 | Below the list of most used macros in WWDG HAL driver. |
||
| 82 | |||
| 83 | (+) __HAL_WWDG_GET_IT_SOURCE: Check the selected WWDG's interrupt source. |
||
| 84 | (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status. |
||
| 85 | (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags. |
||
| 86 | |||
| 87 | @endverbatim |
||
| 88 | ****************************************************************************** |
||
| 89 | * @attention |
||
| 90 | * |
||
| 91 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
||
| 92 | * |
||
| 93 | * Redistribution and use in source and binary forms, with or without modification, |
||
| 94 | * are permitted provided that the following conditions are met: |
||
| 95 | * 1. Redistributions of source code must retain the above copyright notice, |
||
| 96 | * this list of conditions and the following disclaimer. |
||
| 97 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
| 98 | * this list of conditions and the following disclaimer in the documentation |
||
| 99 | * and/or other materials provided with the distribution. |
||
| 100 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
||
| 101 | * may be used to endorse or promote products derived from this software |
||
| 102 | * without specific prior written permission. |
||
| 103 | * |
||
| 104 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
| 105 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
| 106 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
| 107 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
||
| 108 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
| 109 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||
| 110 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
| 111 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
||
| 112 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
| 113 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 114 | * |
||
| 115 | ****************************************************************************** |
||
| 116 | */ |
||
| 117 | |||
| 118 | /* Includes ------------------------------------------------------------------*/ |
||
| 119 | #include "stm32f1xx_hal.h" |
||
| 120 | |||
| 121 | /** @addtogroup STM32F1xx_HAL_Driver |
||
| 122 | * @{ |
||
| 123 | */ |
||
| 124 | |||
| 125 | #ifdef HAL_WWDG_MODULE_ENABLED |
||
| 126 | /** @defgroup WWDG WWDG |
||
| 127 | * @brief WWDG HAL module driver. |
||
| 128 | * @{ |
||
| 129 | */ |
||
| 130 | |||
| 131 | /* Private typedef -----------------------------------------------------------*/ |
||
| 132 | /* Private define ------------------------------------------------------------*/ |
||
| 133 | /* Private macro -------------------------------------------------------------*/ |
||
| 134 | /* Private variables ---------------------------------------------------------*/ |
||
| 135 | /* Private function prototypes -----------------------------------------------*/ |
||
| 136 | /* Exported functions --------------------------------------------------------*/ |
||
| 137 | |||
| 138 | /** @defgroup WWDG_Exported_Functions WWDG Exported Functions |
||
| 139 | * @{ |
||
| 140 | */ |
||
| 141 | |||
| 142 | /** @defgroup WWDG_Exported_Functions_Group1 Initialization and Configuration functions |
||
| 143 | * @brief Initialization and Configuration functions. |
||
| 144 | * |
||
| 145 | @verbatim |
||
| 146 | ============================================================================== |
||
| 147 | ##### Initialization and Configuration functions ##### |
||
| 148 | ============================================================================== |
||
| 149 | [..] |
||
| 150 | This section provides functions allowing to: |
||
| 151 | (+) Initialize and start the WWDG according to the specified parameters |
||
| 152 | in the WWDG_InitTypeDef of associated handle. |
||
| 153 | (+) Initialize the WWDG MSP. |
||
| 154 | |||
| 155 | @endverbatim |
||
| 156 | * @{ |
||
| 157 | */ |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @brief Initialize the WWDG according to the specified. |
||
| 161 | * parameters in the WWDG_InitTypeDef of associated handle. |
||
| 162 | * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains |
||
| 163 | * the configuration information for the specified WWDG module. |
||
| 164 | * @retval HAL status |
||
| 165 | */ |
||
| 166 | HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg) |
||
| 167 | { |
||
| 168 | /* Check the WWDG handle allocation */ |
||
| 169 | if (hwwdg == NULL) |
||
| 170 | { |
||
| 171 | return HAL_ERROR; |
||
| 172 | } |
||
| 173 | |||
| 174 | /* Check the parameters */ |
||
| 175 | assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance)); |
||
| 176 | assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler)); |
||
| 177 | assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window)); |
||
| 178 | assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter)); |
||
| 179 | assert_param(IS_WWDG_EWI_MODE(hwwdg->Init.EWIMode)); |
||
| 180 | |||
| 181 | /* Init the low level hardware */ |
||
| 182 | HAL_WWDG_MspInit(hwwdg); |
||
| 183 | |||
| 184 | /* Set WWDG Counter */ |
||
| 185 | WRITE_REG(hwwdg->Instance->CR, (WWDG_CR_WDGA | hwwdg->Init.Counter)); |
||
| 186 | |||
| 187 | /* Set WWDG Prescaler and Window */ |
||
| 188 | WRITE_REG(hwwdg->Instance->CFR, (hwwdg->Init.EWIMode | hwwdg->Init.Prescaler | hwwdg->Init.Window)); |
||
| 189 | |||
| 190 | /* Return function status */ |
||
| 191 | return HAL_OK; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @brief Initialize the WWDG MSP. |
||
| 196 | * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains |
||
| 197 | * the configuration information for the specified WWDG module. |
||
| 198 | * @note When rewriting this function in user file, mechanism may be added |
||
| 199 | * to avoid multiple initialize when HAL_WWDG_Init function is called |
||
| 200 | * again to change parameters. |
||
| 201 | * @retval None |
||
| 202 | */ |
||
| 203 | __weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg) |
||
| 204 | { |
||
| 205 | /* Prevent unused argument(s) compilation warning */ |
||
| 206 | UNUSED(hwwdg); |
||
| 207 | |||
| 208 | /* NOTE: This function should not be modified, when the callback is needed, |
||
| 209 | the HAL_WWDG_MspInit could be implemented in the user file |
||
| 210 | */ |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @} |
||
| 215 | */ |
||
| 216 | |||
| 217 | /** @defgroup WWDG_Exported_Functions_Group2 IO operation functions |
||
| 218 | * @brief IO operation functions |
||
| 219 | * |
||
| 220 | @verbatim |
||
| 221 | ============================================================================== |
||
| 222 | ##### IO operation functions ##### |
||
| 223 | ============================================================================== |
||
| 224 | [..] |
||
| 225 | This section provides functions allowing to: |
||
| 226 | (+) Refresh the WWDG. |
||
| 227 | (+) Handle WWDG interrupt request and associated function callback. |
||
| 228 | |||
| 229 | @endverbatim |
||
| 230 | * @{ |
||
| 231 | */ |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @brief Refresh the WWDG. |
||
| 235 | * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains |
||
| 236 | * the configuration information for the specified WWDG module. |
||
| 237 | * @retval HAL status |
||
| 238 | */ |
||
| 239 | HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg) |
||
| 240 | { |
||
| 241 | /* Write to WWDG CR the WWDG Counter value to refresh with */ |
||
| 242 | WRITE_REG(hwwdg->Instance->CR, (hwwdg->Init.Counter)); |
||
| 243 | |||
| 244 | /* Return function status */ |
||
| 245 | return HAL_OK; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @brief Handle WWDG interrupt request. |
||
| 250 | * @note The Early Wakeup Interrupt (EWI) can be used if specific safety operations |
||
| 251 | * or data logging must be performed before the actual reset is generated. |
||
| 252 | * The EWI interrupt is enabled by calling HAL_WWDG_Init function with |
||
| 253 | * EWIMode set to WWDG_EWI_ENABLE. |
||
| 254 | * When the downcounter reaches the value 0x40, and EWI interrupt is |
||
| 255 | * generated and the corresponding Interrupt Service Routine (ISR) can |
||
| 256 | * be used to trigger specific actions (such as communications or data |
||
| 257 | * logging), before resetting the device. |
||
| 258 | * @param hwwdg pointer to a WWDG_HandleTypeDef structure that contains |
||
| 259 | * the configuration information for the specified WWDG module. |
||
| 260 | * @retval None |
||
| 261 | */ |
||
| 262 | void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg) |
||
| 263 | { |
||
| 264 | /* Check if Early Wakeup Interrupt is enable */ |
||
| 265 | if (__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET) |
||
| 266 | { |
||
| 267 | /* Check if WWDG Early Wakeup Interrupt occurred */ |
||
| 268 | if (__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET) |
||
| 269 | { |
||
| 270 | /* Clear the WWDG Early Wakeup flag */ |
||
| 271 | __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF); |
||
| 272 | |||
| 273 | /* Early Wakeup callback */ |
||
| 274 | HAL_WWDG_EarlyWakeupCallback(hwwdg); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @brief WWDG Early Wakeup callback. |
||
| 281 | * @param hwwdg : pointer to a WWDG_HandleTypeDef structure that contains |
||
| 282 | * the configuration information for the specified WWDG module. |
||
| 283 | * @retval None |
||
| 284 | */ |
||
| 285 | __weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg) |
||
| 286 | { |
||
| 287 | /* Prevent unused argument(s) compilation warning */ |
||
| 288 | UNUSED(hwwdg); |
||
| 289 | |||
| 290 | /* NOTE: This function should not be modified, when the callback is needed, |
||
| 291 | the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file |
||
| 292 | */ |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @} |
||
| 297 | */ |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @} |
||
| 301 | */ |
||
| 302 | |||
| 303 | #endif /* HAL_WWDG_MODULE_ENABLED */ |
||
| 304 | /** |
||
| 305 | * @} |
||
| 306 | */ |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @} |
||
| 310 | */ |
||
| 311 | |||
| 312 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |