Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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