TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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