inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /**
NYX 0:85b3fd62ea1a 2 ******************************************************************************
NYX 0:85b3fd62ea1a 3 * @file stm32f4xx_ll_utils.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 UTILS LL module driver.
NYX 0:85b3fd62ea1a 8 ******************************************************************************
NYX 0:85b3fd62ea1a 9 * @attention
NYX 0:85b3fd62ea1a 10 *
NYX 0:85b3fd62ea1a 11 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
NYX 0:85b3fd62ea1a 12 *
NYX 0:85b3fd62ea1a 13 * Redistribution and use in source and binary forms, with or without modification,
NYX 0:85b3fd62ea1a 14 * are permitted provided that the following conditions are met:
NYX 0:85b3fd62ea1a 15 * 1. Redistributions of source code must retain the above copyright notice,
NYX 0:85b3fd62ea1a 16 * this list of conditions and the following disclaimer.
NYX 0:85b3fd62ea1a 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
NYX 0:85b3fd62ea1a 18 * this list of conditions and the following disclaimer in the documentation
NYX 0:85b3fd62ea1a 19 * and/or other materials provided with the distribution.
NYX 0:85b3fd62ea1a 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
NYX 0:85b3fd62ea1a 21 * may be used to endorse or promote products derived from this software
NYX 0:85b3fd62ea1a 22 * without specific prior written permission.
NYX 0:85b3fd62ea1a 23 *
NYX 0:85b3fd62ea1a 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
NYX 0:85b3fd62ea1a 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
NYX 0:85b3fd62ea1a 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NYX 0:85b3fd62ea1a 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
NYX 0:85b3fd62ea1a 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
NYX 0:85b3fd62ea1a 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
NYX 0:85b3fd62ea1a 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
NYX 0:85b3fd62ea1a 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
NYX 0:85b3fd62ea1a 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
NYX 0:85b3fd62ea1a 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NYX 0:85b3fd62ea1a 34 *
NYX 0:85b3fd62ea1a 35 ******************************************************************************
NYX 0:85b3fd62ea1a 36 */
NYX 0:85b3fd62ea1a 37 /* Includes ------------------------------------------------------------------*/
NYX 0:85b3fd62ea1a 38 #include "stm32f4xx_ll_utils.h"
NYX 0:85b3fd62ea1a 39 #include "stm32f4xx_ll_rcc.h"
NYX 0:85b3fd62ea1a 40 #include "stm32f4xx_ll_system.h"
NYX 0:85b3fd62ea1a 41 #include "stm32f4xx_ll_pwr.h"
NYX 0:85b3fd62ea1a 42
NYX 0:85b3fd62ea1a 43 /** @addtogroup STM32F4xx_LL_Driver
NYX 0:85b3fd62ea1a 44 * @{
NYX 0:85b3fd62ea1a 45 */
NYX 0:85b3fd62ea1a 46
NYX 0:85b3fd62ea1a 47 /** @addtogroup UTILS_LL
NYX 0:85b3fd62ea1a 48 * @{
NYX 0:85b3fd62ea1a 49 */
NYX 0:85b3fd62ea1a 50
NYX 0:85b3fd62ea1a 51 /* Private types -------------------------------------------------------------*/
NYX 0:85b3fd62ea1a 52 /* Private variables ---------------------------------------------------------*/
NYX 0:85b3fd62ea1a 53 /* Private constants ---------------------------------------------------------*/
NYX 0:85b3fd62ea1a 54 /** @addtogroup UTILS_LL_Private_Constants
NYX 0:85b3fd62ea1a 55 * @{
NYX 0:85b3fd62ea1a 56 */
NYX 0:85b3fd62ea1a 57 #if defined(RCC_MAX_FREQUENCY_SCALE1)
NYX 0:85b3fd62ea1a 58 #define UTILS_MAX_FREQUENCY_SCALE1 RCC_MAX_FREQUENCY /*!< Maximum frequency for system clock at power scale1, in Hz */
NYX 0:85b3fd62ea1a 59 #endif /*RCC_MAX_FREQUENCY_SCALE1 */
NYX 0:85b3fd62ea1a 60 #define UTILS_MAX_FREQUENCY_SCALE2 RCC_MAX_FREQUENCY_SCALE2 /*!< Maximum frequency for system clock at power scale2, in Hz */
NYX 0:85b3fd62ea1a 61 #if defined(RCC_MAX_FREQUENCY_SCALE3)
NYX 0:85b3fd62ea1a 62 #define UTILS_MAX_FREQUENCY_SCALE3 RCC_MAX_FREQUENCY_SCALE3 /*!< Maximum frequency for system clock at power scale3, in Hz */
NYX 0:85b3fd62ea1a 63 #endif /* MAX_FREQUENCY_SCALE3 */
NYX 0:85b3fd62ea1a 64
NYX 0:85b3fd62ea1a 65 /* Defines used for PLL range */
NYX 0:85b3fd62ea1a 66 #define UTILS_PLLVCO_INPUT_MIN RCC_PLLVCO_INPUT_MIN /*!< Frequency min for PLLVCO input, in Hz */
NYX 0:85b3fd62ea1a 67 #define UTILS_PLLVCO_INPUT_MAX RCC_PLLVCO_INPUT_MAX /*!< Frequency max for PLLVCO input, in Hz */
NYX 0:85b3fd62ea1a 68 #define UTILS_PLLVCO_OUTPUT_MIN RCC_PLLVCO_OUTPUT_MIN /*!< Frequency min for PLLVCO output, in Hz */
NYX 0:85b3fd62ea1a 69 #define UTILS_PLLVCO_OUTPUT_MAX RCC_PLLVCO_OUTPUT_MAX /*!< Frequency max for PLLVCO output, in Hz */
NYX 0:85b3fd62ea1a 70
NYX 0:85b3fd62ea1a 71 /* Defines used for HSE range */
NYX 0:85b3fd62ea1a 72 #define UTILS_HSE_FREQUENCY_MIN 4000000U /*!< Frequency min for HSE frequency, in Hz */
NYX 0:85b3fd62ea1a 73 #define UTILS_HSE_FREQUENCY_MAX 26000000U /*!< Frequency max for HSE frequency, in Hz */
NYX 0:85b3fd62ea1a 74
NYX 0:85b3fd62ea1a 75 /* Defines used for FLASH latency according to HCLK Frequency */
NYX 0:85b3fd62ea1a 76 #if defined(FLASH_SCALE1_LATENCY1_FREQ)
NYX 0:85b3fd62ea1a 77 #define UTILS_SCALE1_LATENCY1_FREQ FLASH_SCALE1_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
NYX 0:85b3fd62ea1a 78 #endif
NYX 0:85b3fd62ea1a 79 #if defined(FLASH_SCALE1_LATENCY2_FREQ)
NYX 0:85b3fd62ea1a 80 #define UTILS_SCALE1_LATENCY2_FREQ FLASH_SCALE1_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
NYX 0:85b3fd62ea1a 81 #endif
NYX 0:85b3fd62ea1a 82 #if defined(FLASH_SCALE1_LATENCY3_FREQ)
NYX 0:85b3fd62ea1a 83 #define UTILS_SCALE1_LATENCY3_FREQ FLASH_SCALE1_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 3 in power scale 1 */
NYX 0:85b3fd62ea1a 84 #endif
NYX 0:85b3fd62ea1a 85 #if defined(FLASH_SCALE1_LATENCY4_FREQ)
NYX 0:85b3fd62ea1a 86 #define UTILS_SCALE1_LATENCY4_FREQ FLASH_SCALE1_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 1 */
NYX 0:85b3fd62ea1a 87 #endif
NYX 0:85b3fd62ea1a 88 #if defined(FLASH_SCALE1_LATENCY5_FREQ)
NYX 0:85b3fd62ea1a 89 #define UTILS_SCALE1_LATENCY5_FREQ FLASH_SCALE1_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 1 */
NYX 0:85b3fd62ea1a 90 #endif
NYX 0:85b3fd62ea1a 91 #define UTILS_SCALE2_LATENCY1_FREQ FLASH_SCALE2_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 2 */
NYX 0:85b3fd62ea1a 92 #define UTILS_SCALE2_LATENCY2_FREQ FLASH_SCALE2_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
NYX 0:85b3fd62ea1a 93 #if defined(FLASH_SCALE2_LATENCY3_FREQ)
NYX 0:85b3fd62ea1a 94 #define UTILS_SCALE2_LATENCY3_FREQ FLASH_SCALE2_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 2 */
NYX 0:85b3fd62ea1a 95 #endif
NYX 0:85b3fd62ea1a 96 #if defined(FLASH_SCALE2_LATENCY4_FREQ)
NYX 0:85b3fd62ea1a 97 #define UTILS_SCALE2_LATENCY4_FREQ FLASH_SCALE2_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 2 */
NYX 0:85b3fd62ea1a 98 #endif
NYX 0:85b3fd62ea1a 99 #if defined(FLASH_SCALE2_LATENCY5_FREQ)
NYX 0:85b3fd62ea1a 100 #define UTILS_SCALE2_LATENCY5_FREQ FLASH_SCALE2_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 2 */
NYX 0:85b3fd62ea1a 101 #endif
NYX 0:85b3fd62ea1a 102 #if defined(FLASH_SCALE3_LATENCY1_FREQ)
NYX 0:85b3fd62ea1a 103 #define UTILS_SCALE3_LATENCY1_FREQ FLASH_SCALE3_LATENCY1_FREQ /*!< HCLK frequency to set FLASH latency 1 in power scale 3 */
NYX 0:85b3fd62ea1a 104 #endif
NYX 0:85b3fd62ea1a 105 #if defined(FLASH_SCALE3_LATENCY2_FREQ)
NYX 0:85b3fd62ea1a 106 #define UTILS_SCALE3_LATENCY2_FREQ FLASH_SCALE3_LATENCY2_FREQ /*!< HCLK frequency to set FLASH latency 2 in power scale 3 */
NYX 0:85b3fd62ea1a 107 #endif
NYX 0:85b3fd62ea1a 108 #if defined(FLASH_SCALE3_LATENCY3_FREQ)
NYX 0:85b3fd62ea1a 109 #define UTILS_SCALE3_LATENCY3_FREQ FLASH_SCALE3_LATENCY3_FREQ /*!< HCLK frequency to set FLASH latency 3 in power scale 3 */
NYX 0:85b3fd62ea1a 110 #endif
NYX 0:85b3fd62ea1a 111 #if defined(FLASH_SCALE3_LATENCY4_FREQ)
NYX 0:85b3fd62ea1a 112 #define UTILS_SCALE3_LATENCY4_FREQ FLASH_SCALE3_LATENCY4_FREQ /*!< HCLK frequency to set FLASH latency 4 in power scale 3 */
NYX 0:85b3fd62ea1a 113 #endif
NYX 0:85b3fd62ea1a 114 #if defined(FLASH_SCALE3_LATENCY5_FREQ)
NYX 0:85b3fd62ea1a 115 #define UTILS_SCALE3_LATENCY5_FREQ FLASH_SCALE3_LATENCY5_FREQ /*!< HCLK frequency to set FLASH latency 5 in power scale 3 */
NYX 0:85b3fd62ea1a 116 #endif
NYX 0:85b3fd62ea1a 117 /**
NYX 0:85b3fd62ea1a 118 * @}
NYX 0:85b3fd62ea1a 119 */
NYX 0:85b3fd62ea1a 120
NYX 0:85b3fd62ea1a 121 /* Private macros ------------------------------------------------------------*/
NYX 0:85b3fd62ea1a 122 /** @addtogroup UTILS_LL_Private_Macros
NYX 0:85b3fd62ea1a 123 * @{
NYX 0:85b3fd62ea1a 124 */
NYX 0:85b3fd62ea1a 125 #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
NYX 0:85b3fd62ea1a 126 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
NYX 0:85b3fd62ea1a 127 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
NYX 0:85b3fd62ea1a 128 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
NYX 0:85b3fd62ea1a 129 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
NYX 0:85b3fd62ea1a 130 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
NYX 0:85b3fd62ea1a 131 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
NYX 0:85b3fd62ea1a 132 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
NYX 0:85b3fd62ea1a 133 || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
NYX 0:85b3fd62ea1a 134
NYX 0:85b3fd62ea1a 135 #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
NYX 0:85b3fd62ea1a 136 || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
NYX 0:85b3fd62ea1a 137 || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
NYX 0:85b3fd62ea1a 138 || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
NYX 0:85b3fd62ea1a 139 || ((__VALUE__) == LL_RCC_APB1_DIV_16))
NYX 0:85b3fd62ea1a 140
NYX 0:85b3fd62ea1a 141 #define IS_LL_UTILS_APB2_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB2_DIV_1) \
NYX 0:85b3fd62ea1a 142 || ((__VALUE__) == LL_RCC_APB2_DIV_2) \
NYX 0:85b3fd62ea1a 143 || ((__VALUE__) == LL_RCC_APB2_DIV_4) \
NYX 0:85b3fd62ea1a 144 || ((__VALUE__) == LL_RCC_APB2_DIV_8) \
NYX 0:85b3fd62ea1a 145 || ((__VALUE__) == LL_RCC_APB2_DIV_16))
NYX 0:85b3fd62ea1a 146
NYX 0:85b3fd62ea1a 147 #define IS_LL_UTILS_PLLM_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLM_DIV_2) \
NYX 0:85b3fd62ea1a 148 || ((__VALUE__) == LL_RCC_PLLM_DIV_3) \
NYX 0:85b3fd62ea1a 149 || ((__VALUE__) == LL_RCC_PLLM_DIV_4) \
NYX 0:85b3fd62ea1a 150 || ((__VALUE__) == LL_RCC_PLLM_DIV_5) \
NYX 0:85b3fd62ea1a 151 || ((__VALUE__) == LL_RCC_PLLM_DIV_6) \
NYX 0:85b3fd62ea1a 152 || ((__VALUE__) == LL_RCC_PLLM_DIV_7) \
NYX 0:85b3fd62ea1a 153 || ((__VALUE__) == LL_RCC_PLLM_DIV_8) \
NYX 0:85b3fd62ea1a 154 || ((__VALUE__) == LL_RCC_PLLM_DIV_9) \
NYX 0:85b3fd62ea1a 155 || ((__VALUE__) == LL_RCC_PLLM_DIV_10) \
NYX 0:85b3fd62ea1a 156 || ((__VALUE__) == LL_RCC_PLLM_DIV_11) \
NYX 0:85b3fd62ea1a 157 || ((__VALUE__) == LL_RCC_PLLM_DIV_12) \
NYX 0:85b3fd62ea1a 158 || ((__VALUE__) == LL_RCC_PLLM_DIV_13) \
NYX 0:85b3fd62ea1a 159 || ((__VALUE__) == LL_RCC_PLLM_DIV_14) \
NYX 0:85b3fd62ea1a 160 || ((__VALUE__) == LL_RCC_PLLM_DIV_15) \
NYX 0:85b3fd62ea1a 161 || ((__VALUE__) == LL_RCC_PLLM_DIV_16) \
NYX 0:85b3fd62ea1a 162 || ((__VALUE__) == LL_RCC_PLLM_DIV_17) \
NYX 0:85b3fd62ea1a 163 || ((__VALUE__) == LL_RCC_PLLM_DIV_18) \
NYX 0:85b3fd62ea1a 164 || ((__VALUE__) == LL_RCC_PLLM_DIV_19) \
NYX 0:85b3fd62ea1a 165 || ((__VALUE__) == LL_RCC_PLLM_DIV_20) \
NYX 0:85b3fd62ea1a 166 || ((__VALUE__) == LL_RCC_PLLM_DIV_21) \
NYX 0:85b3fd62ea1a 167 || ((__VALUE__) == LL_RCC_PLLM_DIV_22) \
NYX 0:85b3fd62ea1a 168 || ((__VALUE__) == LL_RCC_PLLM_DIV_23) \
NYX 0:85b3fd62ea1a 169 || ((__VALUE__) == LL_RCC_PLLM_DIV_24) \
NYX 0:85b3fd62ea1a 170 || ((__VALUE__) == LL_RCC_PLLM_DIV_25) \
NYX 0:85b3fd62ea1a 171 || ((__VALUE__) == LL_RCC_PLLM_DIV_26) \
NYX 0:85b3fd62ea1a 172 || ((__VALUE__) == LL_RCC_PLLM_DIV_27) \
NYX 0:85b3fd62ea1a 173 || ((__VALUE__) == LL_RCC_PLLM_DIV_28) \
NYX 0:85b3fd62ea1a 174 || ((__VALUE__) == LL_RCC_PLLM_DIV_29) \
NYX 0:85b3fd62ea1a 175 || ((__VALUE__) == LL_RCC_PLLM_DIV_30) \
NYX 0:85b3fd62ea1a 176 || ((__VALUE__) == LL_RCC_PLLM_DIV_31) \
NYX 0:85b3fd62ea1a 177 || ((__VALUE__) == LL_RCC_PLLM_DIV_32) \
NYX 0:85b3fd62ea1a 178 || ((__VALUE__) == LL_RCC_PLLM_DIV_33) \
NYX 0:85b3fd62ea1a 179 || ((__VALUE__) == LL_RCC_PLLM_DIV_34) \
NYX 0:85b3fd62ea1a 180 || ((__VALUE__) == LL_RCC_PLLM_DIV_35) \
NYX 0:85b3fd62ea1a 181 || ((__VALUE__) == LL_RCC_PLLM_DIV_36) \
NYX 0:85b3fd62ea1a 182 || ((__VALUE__) == LL_RCC_PLLM_DIV_37) \
NYX 0:85b3fd62ea1a 183 || ((__VALUE__) == LL_RCC_PLLM_DIV_38) \
NYX 0:85b3fd62ea1a 184 || ((__VALUE__) == LL_RCC_PLLM_DIV_39) \
NYX 0:85b3fd62ea1a 185 || ((__VALUE__) == LL_RCC_PLLM_DIV_40) \
NYX 0:85b3fd62ea1a 186 || ((__VALUE__) == LL_RCC_PLLM_DIV_41) \
NYX 0:85b3fd62ea1a 187 || ((__VALUE__) == LL_RCC_PLLM_DIV_42) \
NYX 0:85b3fd62ea1a 188 || ((__VALUE__) == LL_RCC_PLLM_DIV_43) \
NYX 0:85b3fd62ea1a 189 || ((__VALUE__) == LL_RCC_PLLM_DIV_44) \
NYX 0:85b3fd62ea1a 190 || ((__VALUE__) == LL_RCC_PLLM_DIV_45) \
NYX 0:85b3fd62ea1a 191 || ((__VALUE__) == LL_RCC_PLLM_DIV_46) \
NYX 0:85b3fd62ea1a 192 || ((__VALUE__) == LL_RCC_PLLM_DIV_47) \
NYX 0:85b3fd62ea1a 193 || ((__VALUE__) == LL_RCC_PLLM_DIV_48) \
NYX 0:85b3fd62ea1a 194 || ((__VALUE__) == LL_RCC_PLLM_DIV_49) \
NYX 0:85b3fd62ea1a 195 || ((__VALUE__) == LL_RCC_PLLM_DIV_50) \
NYX 0:85b3fd62ea1a 196 || ((__VALUE__) == LL_RCC_PLLM_DIV_51) \
NYX 0:85b3fd62ea1a 197 || ((__VALUE__) == LL_RCC_PLLM_DIV_52) \
NYX 0:85b3fd62ea1a 198 || ((__VALUE__) == LL_RCC_PLLM_DIV_53) \
NYX 0:85b3fd62ea1a 199 || ((__VALUE__) == LL_RCC_PLLM_DIV_54) \
NYX 0:85b3fd62ea1a 200 || ((__VALUE__) == LL_RCC_PLLM_DIV_55) \
NYX 0:85b3fd62ea1a 201 || ((__VALUE__) == LL_RCC_PLLM_DIV_56) \
NYX 0:85b3fd62ea1a 202 || ((__VALUE__) == LL_RCC_PLLM_DIV_57) \
NYX 0:85b3fd62ea1a 203 || ((__VALUE__) == LL_RCC_PLLM_DIV_58) \
NYX 0:85b3fd62ea1a 204 || ((__VALUE__) == LL_RCC_PLLM_DIV_59) \
NYX 0:85b3fd62ea1a 205 || ((__VALUE__) == LL_RCC_PLLM_DIV_60) \
NYX 0:85b3fd62ea1a 206 || ((__VALUE__) == LL_RCC_PLLM_DIV_61) \
NYX 0:85b3fd62ea1a 207 || ((__VALUE__) == LL_RCC_PLLM_DIV_62) \
NYX 0:85b3fd62ea1a 208 || ((__VALUE__) == LL_RCC_PLLM_DIV_63))
NYX 0:85b3fd62ea1a 209
NYX 0:85b3fd62ea1a 210 #define IS_LL_UTILS_PLLN_VALUE(__VALUE__) ((RCC_PLLN_MIN_VALUE <= (__VALUE__)) && ((__VALUE__) <= RCC_PLLN_MAX_VALUE))
NYX 0:85b3fd62ea1a 211
NYX 0:85b3fd62ea1a 212 #define IS_LL_UTILS_PLLP_VALUE(__VALUE__) (((__VALUE__) == LL_RCC_PLLP_DIV_2) \
NYX 0:85b3fd62ea1a 213 || ((__VALUE__) == LL_RCC_PLLP_DIV_4) \
NYX 0:85b3fd62ea1a 214 || ((__VALUE__) == LL_RCC_PLLP_DIV_6) \
NYX 0:85b3fd62ea1a 215 || ((__VALUE__) == LL_RCC_PLLP_DIV_8))
NYX 0:85b3fd62ea1a 216
NYX 0:85b3fd62ea1a 217 #define IS_LL_UTILS_PLLVCO_INPUT(__VALUE__) ((UTILS_PLLVCO_INPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_INPUT_MAX))
NYX 0:85b3fd62ea1a 218
NYX 0:85b3fd62ea1a 219 #define IS_LL_UTILS_PLLVCO_OUTPUT(__VALUE__) ((UTILS_PLLVCO_OUTPUT_MIN <= (__VALUE__)) && ((__VALUE__) <= UTILS_PLLVCO_OUTPUT_MAX))
NYX 0:85b3fd62ea1a 220
NYX 0:85b3fd62ea1a 221 #if !defined(RCC_MAX_FREQUENCY_SCALE1)
NYX 0:85b3fd62ea1a 222 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
NYX 0:85b3fd62ea1a 223 ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
NYX 0:85b3fd62ea1a 224
NYX 0:85b3fd62ea1a 225 #elif defined(RCC_MAX_FREQUENCY_SCALE3)
NYX 0:85b3fd62ea1a 226 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
NYX 0:85b3fd62ea1a 227 (LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2) : \
NYX 0:85b3fd62ea1a 228 ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE3))
NYX 0:85b3fd62ea1a 229
NYX 0:85b3fd62ea1a 230 #else
NYX 0:85b3fd62ea1a 231 #define IS_LL_UTILS_PLL_FREQUENCY(__VALUE__) ((LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1) ? ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE1) : \
NYX 0:85b3fd62ea1a 232 ((__VALUE__) <= UTILS_MAX_FREQUENCY_SCALE2))
NYX 0:85b3fd62ea1a 233
NYX 0:85b3fd62ea1a 234 #endif /* RCC_MAX_FREQUENCY_SCALE1*/
NYX 0:85b3fd62ea1a 235 #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
NYX 0:85b3fd62ea1a 236 || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
NYX 0:85b3fd62ea1a 237
NYX 0:85b3fd62ea1a 238 #define IS_LL_UTILS_HSE_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) >= UTILS_HSE_FREQUENCY_MIN) && ((__FREQUENCY__) <= UTILS_HSE_FREQUENCY_MAX))
NYX 0:85b3fd62ea1a 239 /**
NYX 0:85b3fd62ea1a 240 * @}
NYX 0:85b3fd62ea1a 241 */
NYX 0:85b3fd62ea1a 242 /* Private function prototypes -----------------------------------------------*/
NYX 0:85b3fd62ea1a 243 /** @defgroup UTILS_LL_Private_Functions UTILS Private functions
NYX 0:85b3fd62ea1a 244 * @{
NYX 0:85b3fd62ea1a 245 */
NYX 0:85b3fd62ea1a 246 static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency,
NYX 0:85b3fd62ea1a 247 LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct);
NYX 0:85b3fd62ea1a 248 static ErrorStatus UTILS_SetFlashLatency(uint32_t HCLK_Frequency);
NYX 0:85b3fd62ea1a 249 static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct);
NYX 0:85b3fd62ea1a 250 static ErrorStatus UTILS_PLL_IsBusy(void);
NYX 0:85b3fd62ea1a 251 /**
NYX 0:85b3fd62ea1a 252 * @}
NYX 0:85b3fd62ea1a 253 */
NYX 0:85b3fd62ea1a 254
NYX 0:85b3fd62ea1a 255 /* Exported functions --------------------------------------------------------*/
NYX 0:85b3fd62ea1a 256 /** @addtogroup UTILS_LL_Exported_Functions
NYX 0:85b3fd62ea1a 257 * @{
NYX 0:85b3fd62ea1a 258 */
NYX 0:85b3fd62ea1a 259
NYX 0:85b3fd62ea1a 260 /** @addtogroup UTILS_LL_EF_DELAY
NYX 0:85b3fd62ea1a 261 * @{
NYX 0:85b3fd62ea1a 262 */
NYX 0:85b3fd62ea1a 263
NYX 0:85b3fd62ea1a 264 /**
NYX 0:85b3fd62ea1a 265 * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
NYX 0:85b3fd62ea1a 266 * @note When a RTOS is used, it is recommended to avoid changing the Systick
NYX 0:85b3fd62ea1a 267 * configuration by calling this function, for a delay use rather osDelay RTOS service.
NYX 0:85b3fd62ea1a 268 * @param HCLKFrequency HCLK frequency in Hz
NYX 0:85b3fd62ea1a 269 * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
NYX 0:85b3fd62ea1a 270 * @retval None
NYX 0:85b3fd62ea1a 271 */
NYX 0:85b3fd62ea1a 272 void LL_Init1msTick(uint32_t HCLKFrequency)
NYX 0:85b3fd62ea1a 273 {
NYX 0:85b3fd62ea1a 274 /* Use frequency provided in argument */
NYX 0:85b3fd62ea1a 275 LL_InitTick(HCLKFrequency, 1000U);
NYX 0:85b3fd62ea1a 276 }
NYX 0:85b3fd62ea1a 277
NYX 0:85b3fd62ea1a 278 /**
NYX 0:85b3fd62ea1a 279 * @brief This function provides accurate delay (in milliseconds) based
NYX 0:85b3fd62ea1a 280 * on SysTick counter flag
NYX 0:85b3fd62ea1a 281 * @note When a RTOS is used, it is recommended to avoid using blocking delay
NYX 0:85b3fd62ea1a 282 * and use rather osDelay service.
NYX 0:85b3fd62ea1a 283 * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
NYX 0:85b3fd62ea1a 284 * will configure Systick to 1ms
NYX 0:85b3fd62ea1a 285 * @param Delay specifies the delay time length, in milliseconds.
NYX 0:85b3fd62ea1a 286 * @retval None
NYX 0:85b3fd62ea1a 287 */
NYX 0:85b3fd62ea1a 288 void LL_mDelay(uint32_t Delay)
NYX 0:85b3fd62ea1a 289 {
NYX 0:85b3fd62ea1a 290 __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
NYX 0:85b3fd62ea1a 291 /* Add this code to indicate that local variable is not used */
NYX 0:85b3fd62ea1a 292 ((void)tmp);
NYX 0:85b3fd62ea1a 293
NYX 0:85b3fd62ea1a 294 /* Add a period to guaranty minimum wait */
NYX 0:85b3fd62ea1a 295 if(Delay < LL_MAX_DELAY)
NYX 0:85b3fd62ea1a 296 {
NYX 0:85b3fd62ea1a 297 Delay++;
NYX 0:85b3fd62ea1a 298 }
NYX 0:85b3fd62ea1a 299
NYX 0:85b3fd62ea1a 300 while (Delay)
NYX 0:85b3fd62ea1a 301 {
NYX 0:85b3fd62ea1a 302 if((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
NYX 0:85b3fd62ea1a 303 {
NYX 0:85b3fd62ea1a 304 Delay--;
NYX 0:85b3fd62ea1a 305 }
NYX 0:85b3fd62ea1a 306 }
NYX 0:85b3fd62ea1a 307 }
NYX 0:85b3fd62ea1a 308
NYX 0:85b3fd62ea1a 309 /**
NYX 0:85b3fd62ea1a 310 * @}
NYX 0:85b3fd62ea1a 311 */
NYX 0:85b3fd62ea1a 312
NYX 0:85b3fd62ea1a 313 /** @addtogroup UTILS_EF_SYSTEM
NYX 0:85b3fd62ea1a 314 * @brief System Configuration functions
NYX 0:85b3fd62ea1a 315 *
NYX 0:85b3fd62ea1a 316 @verbatim
NYX 0:85b3fd62ea1a 317 ===============================================================================
NYX 0:85b3fd62ea1a 318 ##### System Configuration functions #####
NYX 0:85b3fd62ea1a 319 ===============================================================================
NYX 0:85b3fd62ea1a 320 [..]
NYX 0:85b3fd62ea1a 321 System, AHB and APB buses clocks configuration
NYX 0:85b3fd62ea1a 322
NYX 0:85b3fd62ea1a 323 (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 180000000 Hz.
NYX 0:85b3fd62ea1a 324 @endverbatim
NYX 0:85b3fd62ea1a 325 @internal
NYX 0:85b3fd62ea1a 326 Depending on the device voltage range, the maximum frequency should be
NYX 0:85b3fd62ea1a 327 adapted accordingly to the Refenece manual.
NYX 0:85b3fd62ea1a 328 @endinternal
NYX 0:85b3fd62ea1a 329 * @{
NYX 0:85b3fd62ea1a 330 */
NYX 0:85b3fd62ea1a 331
NYX 0:85b3fd62ea1a 332 /**
NYX 0:85b3fd62ea1a 333 * @brief This function sets directly SystemCoreClock CMSIS variable.
NYX 0:85b3fd62ea1a 334 * @note Variable can be calculated also through SystemCoreClockUpdate function.
NYX 0:85b3fd62ea1a 335 * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
NYX 0:85b3fd62ea1a 336 * @retval None
NYX 0:85b3fd62ea1a 337 */
NYX 0:85b3fd62ea1a 338 void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
NYX 0:85b3fd62ea1a 339 {
NYX 0:85b3fd62ea1a 340 /* HCLK clock frequency */
NYX 0:85b3fd62ea1a 341 SystemCoreClock = HCLKFrequency;
NYX 0:85b3fd62ea1a 342 }
NYX 0:85b3fd62ea1a 343
NYX 0:85b3fd62ea1a 344 /**
NYX 0:85b3fd62ea1a 345 * @brief This function configures system clock at maximum frequency with HSI as clock source of the PLL
NYX 0:85b3fd62ea1a 346 * @note The application need to ensure that PLL is disabled.
NYX 0:85b3fd62ea1a 347 * @note Function is based on the following formula:
NYX 0:85b3fd62ea1a 348 * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
NYX 0:85b3fd62ea1a 349 * - PLLM: ensure that the VCO input frequency ranges from @ref RCC_PLLVCO_INPUT_MIN to @ref RCC_PLLVCO_INPUT_MAX (PLLVCO_input = HSI frequency / PLLM)
NYX 0:85b3fd62ea1a 350 * - PLLN: ensure that the VCO output frequency is between @ref RCC_PLLVCO_OUTPUT_MIN and @ref RCC_PLLVCO_OUTPUT_MAX (PLLVCO_output = PLLVCO_input * PLLN)
NYX 0:85b3fd62ea1a 351 * - PLLP: ensure that max frequency at 180000000 Hz is reach (PLLVCO_output / PLLP)
NYX 0:85b3fd62ea1a 352 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
NYX 0:85b3fd62ea1a 353 * the configuration information for the PLL.
NYX 0:85b3fd62ea1a 354 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
NYX 0:85b3fd62ea1a 355 * the configuration information for the BUS prescalers.
NYX 0:85b3fd62ea1a 356 * @retval An ErrorStatus enumeration value:
NYX 0:85b3fd62ea1a 357 * - SUCCESS: Max frequency configuration done
NYX 0:85b3fd62ea1a 358 * - ERROR: Max frequency configuration not done
NYX 0:85b3fd62ea1a 359 */
NYX 0:85b3fd62ea1a 360 ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct,
NYX 0:85b3fd62ea1a 361 LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
NYX 0:85b3fd62ea1a 362 {
NYX 0:85b3fd62ea1a 363 ErrorStatus status = SUCCESS;
NYX 0:85b3fd62ea1a 364 uint32_t pllfreq = 0U;
NYX 0:85b3fd62ea1a 365
NYX 0:85b3fd62ea1a 366 /* Check if one of the PLL is enabled */
NYX 0:85b3fd62ea1a 367 if(UTILS_PLL_IsBusy() == SUCCESS)
NYX 0:85b3fd62ea1a 368 {
NYX 0:85b3fd62ea1a 369 /* Calculate the new PLL output frequency */
NYX 0:85b3fd62ea1a 370 pllfreq = UTILS_GetPLLOutputFrequency(HSI_VALUE, UTILS_PLLInitStruct);
NYX 0:85b3fd62ea1a 371
NYX 0:85b3fd62ea1a 372 /* Enable HSI if not enabled */
NYX 0:85b3fd62ea1a 373 if(LL_RCC_HSI_IsReady() != 1U)
NYX 0:85b3fd62ea1a 374 {
NYX 0:85b3fd62ea1a 375 LL_RCC_HSI_Enable();
NYX 0:85b3fd62ea1a 376 while (LL_RCC_HSI_IsReady() != 1U)
NYX 0:85b3fd62ea1a 377 {
NYX 0:85b3fd62ea1a 378 /* Wait for HSI ready */
NYX 0:85b3fd62ea1a 379 }
NYX 0:85b3fd62ea1a 380 }
NYX 0:85b3fd62ea1a 381
NYX 0:85b3fd62ea1a 382 /* Configure PLL */
NYX 0:85b3fd62ea1a 383 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
NYX 0:85b3fd62ea1a 384 UTILS_PLLInitStruct->PLLP);
NYX 0:85b3fd62ea1a 385
NYX 0:85b3fd62ea1a 386 /* Enable PLL and switch system clock to PLL */
NYX 0:85b3fd62ea1a 387 status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
NYX 0:85b3fd62ea1a 388 }
NYX 0:85b3fd62ea1a 389 else
NYX 0:85b3fd62ea1a 390 {
NYX 0:85b3fd62ea1a 391 /* Current PLL configuration cannot be modified */
NYX 0:85b3fd62ea1a 392 status = ERROR;
NYX 0:85b3fd62ea1a 393 }
NYX 0:85b3fd62ea1a 394
NYX 0:85b3fd62ea1a 395 return status;
NYX 0:85b3fd62ea1a 396 }
NYX 0:85b3fd62ea1a 397
NYX 0:85b3fd62ea1a 398 /**
NYX 0:85b3fd62ea1a 399 * @brief This function configures system clock with HSE as clock source of the PLL
NYX 0:85b3fd62ea1a 400 * @note The application need to ensure that PLL is disabled.
NYX 0:85b3fd62ea1a 401 * - PLL output frequency = (((HSI frequency / PLLM) * PLLN) / PLLP)
NYX 0:85b3fd62ea1a 402 * - PLLM: ensure that the VCO input frequency ranges from @ref RCC_PLLVCO_INPUT_MIN to @ref RCC_PLLVCO_INPUT_MAX (PLLVCO_input = HSI frequency / PLLM)
NYX 0:85b3fd62ea1a 403 * - PLLN: ensure that the VCO output frequency is between @ref RCC_PLLVCO_OUTPUT_MIN and @ref RCC_PLLVCO_OUTPUT_MAX (PLLVCO_output = PLLVCO_input * PLLN)
NYX 0:85b3fd62ea1a 404 * - PLLP: ensure that max frequency at 180000000 Hz is reach (PLLVCO_output / PLLP)
NYX 0:85b3fd62ea1a 405 * @param HSEFrequency Value between Min_Data = 4000000 and Max_Data = 26000000
NYX 0:85b3fd62ea1a 406 * @param HSEBypass This parameter can be one of the following values:
NYX 0:85b3fd62ea1a 407 * @arg @ref LL_UTILS_HSEBYPASS_ON
NYX 0:85b3fd62ea1a 408 * @arg @ref LL_UTILS_HSEBYPASS_OFF
NYX 0:85b3fd62ea1a 409 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
NYX 0:85b3fd62ea1a 410 * the configuration information for the PLL.
NYX 0:85b3fd62ea1a 411 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
NYX 0:85b3fd62ea1a 412 * the configuration information for the BUS prescalers.
NYX 0:85b3fd62ea1a 413 * @retval An ErrorStatus enumeration value:
NYX 0:85b3fd62ea1a 414 * - SUCCESS: Max frequency configuration done
NYX 0:85b3fd62ea1a 415 * - ERROR: Max frequency configuration not done
NYX 0:85b3fd62ea1a 416 */
NYX 0:85b3fd62ea1a 417 ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass,
NYX 0:85b3fd62ea1a 418 LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
NYX 0:85b3fd62ea1a 419 {
NYX 0:85b3fd62ea1a 420 ErrorStatus status = SUCCESS;
NYX 0:85b3fd62ea1a 421 uint32_t pllfreq = 0U;
NYX 0:85b3fd62ea1a 422
NYX 0:85b3fd62ea1a 423 /* Check the parameters */
NYX 0:85b3fd62ea1a 424 assert_param(IS_LL_UTILS_HSE_FREQUENCY(HSEFrequency));
NYX 0:85b3fd62ea1a 425 assert_param(IS_LL_UTILS_HSE_BYPASS(HSEBypass));
NYX 0:85b3fd62ea1a 426
NYX 0:85b3fd62ea1a 427 /* Check if one of the PLL is enabled */
NYX 0:85b3fd62ea1a 428 if(UTILS_PLL_IsBusy() == SUCCESS)
NYX 0:85b3fd62ea1a 429 {
NYX 0:85b3fd62ea1a 430 /* Calculate the new PLL output frequency */
NYX 0:85b3fd62ea1a 431 pllfreq = UTILS_GetPLLOutputFrequency(HSEFrequency, UTILS_PLLInitStruct);
NYX 0:85b3fd62ea1a 432
NYX 0:85b3fd62ea1a 433 /* Enable HSE if not enabled */
NYX 0:85b3fd62ea1a 434 if(LL_RCC_HSE_IsReady() != 1U)
NYX 0:85b3fd62ea1a 435 {
NYX 0:85b3fd62ea1a 436 /* Check if need to enable HSE bypass feature or not */
NYX 0:85b3fd62ea1a 437 if(HSEBypass == LL_UTILS_HSEBYPASS_ON)
NYX 0:85b3fd62ea1a 438 {
NYX 0:85b3fd62ea1a 439 LL_RCC_HSE_EnableBypass();
NYX 0:85b3fd62ea1a 440 }
NYX 0:85b3fd62ea1a 441 else
NYX 0:85b3fd62ea1a 442 {
NYX 0:85b3fd62ea1a 443 LL_RCC_HSE_DisableBypass();
NYX 0:85b3fd62ea1a 444 }
NYX 0:85b3fd62ea1a 445
NYX 0:85b3fd62ea1a 446 /* Enable HSE */
NYX 0:85b3fd62ea1a 447 LL_RCC_HSE_Enable();
NYX 0:85b3fd62ea1a 448 while (LL_RCC_HSE_IsReady() != 1U)
NYX 0:85b3fd62ea1a 449 {
NYX 0:85b3fd62ea1a 450 /* Wait for HSE ready */
NYX 0:85b3fd62ea1a 451 }
NYX 0:85b3fd62ea1a 452 }
NYX 0:85b3fd62ea1a 453
NYX 0:85b3fd62ea1a 454 /* Configure PLL */
NYX 0:85b3fd62ea1a 455 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, UTILS_PLLInitStruct->PLLM, UTILS_PLLInitStruct->PLLN,
NYX 0:85b3fd62ea1a 456 UTILS_PLLInitStruct->PLLP);
NYX 0:85b3fd62ea1a 457
NYX 0:85b3fd62ea1a 458 /* Enable PLL and switch system clock to PLL */
NYX 0:85b3fd62ea1a 459 status = UTILS_EnablePLLAndSwitchSystem(pllfreq, UTILS_ClkInitStruct);
NYX 0:85b3fd62ea1a 460 }
NYX 0:85b3fd62ea1a 461 else
NYX 0:85b3fd62ea1a 462 {
NYX 0:85b3fd62ea1a 463 /* Current PLL configuration cannot be modified */
NYX 0:85b3fd62ea1a 464 status = ERROR;
NYX 0:85b3fd62ea1a 465 }
NYX 0:85b3fd62ea1a 466
NYX 0:85b3fd62ea1a 467 return status;
NYX 0:85b3fd62ea1a 468 }
NYX 0:85b3fd62ea1a 469
NYX 0:85b3fd62ea1a 470 /**
NYX 0:85b3fd62ea1a 471 * @}
NYX 0:85b3fd62ea1a 472 */
NYX 0:85b3fd62ea1a 473
NYX 0:85b3fd62ea1a 474 /**
NYX 0:85b3fd62ea1a 475 * @}
NYX 0:85b3fd62ea1a 476 */
NYX 0:85b3fd62ea1a 477
NYX 0:85b3fd62ea1a 478 /** @addtogroup UTILS_LL_Private_Functions
NYX 0:85b3fd62ea1a 479 * @{
NYX 0:85b3fd62ea1a 480 */
NYX 0:85b3fd62ea1a 481 /**
NYX 0:85b3fd62ea1a 482 * @brief Update number of Flash wait states in line with new frequency and current
NYX 0:85b3fd62ea1a 483 voltage range.
NYX 0:85b3fd62ea1a 484 * @note This Function support ONLY devices with supply voltage (voltage range) between 2.7V and 3.6V
NYX 0:85b3fd62ea1a 485 * @param HCLK_Frequency HCLK frequency
NYX 0:85b3fd62ea1a 486 * @retval An ErrorStatus enumeration value:
NYX 0:85b3fd62ea1a 487 * - SUCCESS: Latency has been modified
NYX 0:85b3fd62ea1a 488 * - ERROR: Latency cannot be modified
NYX 0:85b3fd62ea1a 489 */
NYX 0:85b3fd62ea1a 490 static ErrorStatus UTILS_SetFlashLatency(uint32_t HCLK_Frequency)
NYX 0:85b3fd62ea1a 491 {
NYX 0:85b3fd62ea1a 492 ErrorStatus status = SUCCESS;
NYX 0:85b3fd62ea1a 493
NYX 0:85b3fd62ea1a 494 uint32_t latency = LL_FLASH_LATENCY_0; /* default value 0WS */
NYX 0:85b3fd62ea1a 495
NYX 0:85b3fd62ea1a 496 /* Frequency cannot be equal to 0 */
NYX 0:85b3fd62ea1a 497 if(HCLK_Frequency == 0U)
NYX 0:85b3fd62ea1a 498 {
NYX 0:85b3fd62ea1a 499 status = ERROR;
NYX 0:85b3fd62ea1a 500 }
NYX 0:85b3fd62ea1a 501 else
NYX 0:85b3fd62ea1a 502 {
NYX 0:85b3fd62ea1a 503 if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE1)
NYX 0:85b3fd62ea1a 504 {
NYX 0:85b3fd62ea1a 505 #if defined (UTILS_SCALE1_LATENCY5_FREQ)
NYX 0:85b3fd62ea1a 506 if((HCLK_Frequency > UTILS_SCALE1_LATENCY5_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 507 {
NYX 0:85b3fd62ea1a 508 latency = LL_FLASH_LATENCY_5;
NYX 0:85b3fd62ea1a 509 }
NYX 0:85b3fd62ea1a 510 #endif /*UTILS_SCALE1_LATENCY5_FREQ */
NYX 0:85b3fd62ea1a 511 #if defined (UTILS_SCALE1_LATENCY4_FREQ)
NYX 0:85b3fd62ea1a 512 if((HCLK_Frequency > UTILS_SCALE1_LATENCY4_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 513 {
NYX 0:85b3fd62ea1a 514 latency = LL_FLASH_LATENCY_4;
NYX 0:85b3fd62ea1a 515 }
NYX 0:85b3fd62ea1a 516 #endif /* UTILS_SCALE1_LATENCY4_FREQ */
NYX 0:85b3fd62ea1a 517 #if defined (UTILS_SCALE1_LATENCY3_FREQ)
NYX 0:85b3fd62ea1a 518 if((HCLK_Frequency > UTILS_SCALE1_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 519 {
NYX 0:85b3fd62ea1a 520 latency = LL_FLASH_LATENCY_3;
NYX 0:85b3fd62ea1a 521 }
NYX 0:85b3fd62ea1a 522 #endif /* UTILS_SCALE1_LATENCY3_FREQ */
NYX 0:85b3fd62ea1a 523 #if defined (UTILS_SCALE1_LATENCY2_FREQ)
NYX 0:85b3fd62ea1a 524 if((HCLK_Frequency > UTILS_SCALE1_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 525 {
NYX 0:85b3fd62ea1a 526 latency = LL_FLASH_LATENCY_2;
NYX 0:85b3fd62ea1a 527 }
NYX 0:85b3fd62ea1a 528 else
NYX 0:85b3fd62ea1a 529 {
NYX 0:85b3fd62ea1a 530 if((HCLK_Frequency > UTILS_SCALE1_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 531 {
NYX 0:85b3fd62ea1a 532 latency = LL_FLASH_LATENCY_1;
NYX 0:85b3fd62ea1a 533 }
NYX 0:85b3fd62ea1a 534 }
NYX 0:85b3fd62ea1a 535 #endif /* UTILS_SCALE1_LATENCY2_FREQ */
NYX 0:85b3fd62ea1a 536 }
NYX 0:85b3fd62ea1a 537 if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE2)
NYX 0:85b3fd62ea1a 538 {
NYX 0:85b3fd62ea1a 539 #if defined (UTILS_SCALE2_LATENCY5_FREQ)
NYX 0:85b3fd62ea1a 540 if((HCLK_Frequency > UTILS_SCALE2_LATENCY5_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 541 {
NYX 0:85b3fd62ea1a 542 latency = LL_FLASH_LATENCY_5;
NYX 0:85b3fd62ea1a 543 }
NYX 0:85b3fd62ea1a 544 #endif /*UTILS_SCALE1_LATENCY5_FREQ */
NYX 0:85b3fd62ea1a 545 #if defined (UTILS_SCALE2_LATENCY4_FREQ)
NYX 0:85b3fd62ea1a 546 if((HCLK_Frequency > UTILS_SCALE2_LATENCY4_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 547 {
NYX 0:85b3fd62ea1a 548 latency = LL_FLASH_LATENCY_4;
NYX 0:85b3fd62ea1a 549 }
NYX 0:85b3fd62ea1a 550 #endif /*UTILS_SCALE1_LATENCY4_FREQ */
NYX 0:85b3fd62ea1a 551 #if defined (UTILS_SCALE2_LATENCY3_FREQ)
NYX 0:85b3fd62ea1a 552 if((HCLK_Frequency > UTILS_SCALE2_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 553 {
NYX 0:85b3fd62ea1a 554 latency = LL_FLASH_LATENCY_3;
NYX 0:85b3fd62ea1a 555 }
NYX 0:85b3fd62ea1a 556 #endif /*UTILS_SCALE1_LATENCY3_FREQ */
NYX 0:85b3fd62ea1a 557 if((HCLK_Frequency > UTILS_SCALE2_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 558 {
NYX 0:85b3fd62ea1a 559 latency = LL_FLASH_LATENCY_2;
NYX 0:85b3fd62ea1a 560 }
NYX 0:85b3fd62ea1a 561 else
NYX 0:85b3fd62ea1a 562 {
NYX 0:85b3fd62ea1a 563 if((HCLK_Frequency > UTILS_SCALE2_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 564 {
NYX 0:85b3fd62ea1a 565 latency = LL_FLASH_LATENCY_1;
NYX 0:85b3fd62ea1a 566 }
NYX 0:85b3fd62ea1a 567 }
NYX 0:85b3fd62ea1a 568 }
NYX 0:85b3fd62ea1a 569 #if defined (LL_PWR_REGU_VOLTAGE_SCALE3)
NYX 0:85b3fd62ea1a 570 if(LL_PWR_GetRegulVoltageScaling() == LL_PWR_REGU_VOLTAGE_SCALE3)
NYX 0:85b3fd62ea1a 571 {
NYX 0:85b3fd62ea1a 572 #if defined (UTILS_SCALE3_LATENCY3_FREQ)
NYX 0:85b3fd62ea1a 573 if((HCLK_Frequency > UTILS_SCALE3_LATENCY3_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 574 {
NYX 0:85b3fd62ea1a 575 latency = LL_FLASH_LATENCY_3;
NYX 0:85b3fd62ea1a 576 }
NYX 0:85b3fd62ea1a 577 #endif /*UTILS_SCALE1_LATENCY3_FREQ */
NYX 0:85b3fd62ea1a 578 #if defined (UTILS_SCALE3_LATENCY2_FREQ)
NYX 0:85b3fd62ea1a 579 if((HCLK_Frequency > UTILS_SCALE3_LATENCY2_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 580 {
NYX 0:85b3fd62ea1a 581 latency = LL_FLASH_LATENCY_2;
NYX 0:85b3fd62ea1a 582 }
NYX 0:85b3fd62ea1a 583 else
NYX 0:85b3fd62ea1a 584 {
NYX 0:85b3fd62ea1a 585 if((HCLK_Frequency > UTILS_SCALE3_LATENCY1_FREQ)&&(latency == LL_FLASH_LATENCY_0))
NYX 0:85b3fd62ea1a 586 {
NYX 0:85b3fd62ea1a 587 latency = LL_FLASH_LATENCY_1;
NYX 0:85b3fd62ea1a 588 }
NYX 0:85b3fd62ea1a 589 }
NYX 0:85b3fd62ea1a 590 }
NYX 0:85b3fd62ea1a 591 #endif /*UTILS_SCALE1_LATENCY2_FREQ */
NYX 0:85b3fd62ea1a 592 #endif /* LL_PWR_REGU_VOLTAGE_SCALE3 */
NYX 0:85b3fd62ea1a 593
NYX 0:85b3fd62ea1a 594 LL_FLASH_SetLatency(latency);
NYX 0:85b3fd62ea1a 595
NYX 0:85b3fd62ea1a 596 /* Check that the new number of wait states is taken into account to access the Flash
NYX 0:85b3fd62ea1a 597 memory by reading the FLASH_ACR register */
NYX 0:85b3fd62ea1a 598 if(LL_FLASH_GetLatency() != latency)
NYX 0:85b3fd62ea1a 599 {
NYX 0:85b3fd62ea1a 600 status = ERROR;
NYX 0:85b3fd62ea1a 601 }
NYX 0:85b3fd62ea1a 602 }
NYX 0:85b3fd62ea1a 603 return status;
NYX 0:85b3fd62ea1a 604 }
NYX 0:85b3fd62ea1a 605
NYX 0:85b3fd62ea1a 606 /**
NYX 0:85b3fd62ea1a 607 * @brief Function to check that PLL can be modified
NYX 0:85b3fd62ea1a 608 * @param PLL_InputFrequency PLL input frequency (in Hz)
NYX 0:85b3fd62ea1a 609 * @param UTILS_PLLInitStruct pointer to a @ref LL_UTILS_PLLInitTypeDef structure that contains
NYX 0:85b3fd62ea1a 610 * the configuration information for the PLL.
NYX 0:85b3fd62ea1a 611 * @retval PLL output frequency (in Hz)
NYX 0:85b3fd62ea1a 612 */
NYX 0:85b3fd62ea1a 613 static uint32_t UTILS_GetPLLOutputFrequency(uint32_t PLL_InputFrequency, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct)
NYX 0:85b3fd62ea1a 614 {
NYX 0:85b3fd62ea1a 615 uint32_t pllfreq = 0U;
NYX 0:85b3fd62ea1a 616
NYX 0:85b3fd62ea1a 617 /* Check the parameters */
NYX 0:85b3fd62ea1a 618 assert_param(IS_LL_UTILS_PLLM_VALUE(UTILS_PLLInitStruct->PLLM));
NYX 0:85b3fd62ea1a 619 assert_param(IS_LL_UTILS_PLLN_VALUE(UTILS_PLLInitStruct->PLLN));
NYX 0:85b3fd62ea1a 620 assert_param(IS_LL_UTILS_PLLP_VALUE(UTILS_PLLInitStruct->PLLP));
NYX 0:85b3fd62ea1a 621
NYX 0:85b3fd62ea1a 622 /* Check different PLL parameters according to RM */
NYX 0:85b3fd62ea1a 623 /* - PLLM: ensure that the VCO input frequency ranges from @ref UTILS_PLLVCO_INPUT_MIN to @ref UTILS_PLLVCO_INPUT_MAX MHz. */
NYX 0:85b3fd62ea1a 624 pllfreq = PLL_InputFrequency / (UTILS_PLLInitStruct->PLLM & (RCC_PLLCFGR_PLLM >> RCC_PLLCFGR_PLLM_Pos));
NYX 0:85b3fd62ea1a 625 assert_param(IS_LL_UTILS_PLLVCO_INPUT(pllfreq));
NYX 0:85b3fd62ea1a 626
NYX 0:85b3fd62ea1a 627 /* - PLLN: ensure that the VCO output frequency is between @ref UTILS_PLLVCO_OUTPUT_MIN and @ref UTILS_PLLVCO_OUTPUT_MAX .*/
NYX 0:85b3fd62ea1a 628 pllfreq = pllfreq * (UTILS_PLLInitStruct->PLLN & (RCC_PLLCFGR_PLLN >> RCC_PLLCFGR_PLLN_Pos));
NYX 0:85b3fd62ea1a 629 assert_param(IS_LL_UTILS_PLLVCO_OUTPUT(pllfreq));
NYX 0:85b3fd62ea1a 630
NYX 0:85b3fd62ea1a 631 /* - PLLP: ensure that max frequency at @ref RCC_MAX_FREQUENCY Hz is reached */
NYX 0:85b3fd62ea1a 632 pllfreq = pllfreq / (((UTILS_PLLInitStruct->PLLP >> RCC_PLLCFGR_PLLP_Pos) + 1) * 2);
NYX 0:85b3fd62ea1a 633 assert_param(IS_LL_UTILS_PLL_FREQUENCY(pllfreq));
NYX 0:85b3fd62ea1a 634
NYX 0:85b3fd62ea1a 635 return pllfreq;
NYX 0:85b3fd62ea1a 636 }
NYX 0:85b3fd62ea1a 637
NYX 0:85b3fd62ea1a 638 /**
NYX 0:85b3fd62ea1a 639 * @brief Function to check that PLL can be modified
NYX 0:85b3fd62ea1a 640 * @retval An ErrorStatus enumeration value:
NYX 0:85b3fd62ea1a 641 * - SUCCESS: PLL modification can be done
NYX 0:85b3fd62ea1a 642 * - ERROR: PLL is busy
NYX 0:85b3fd62ea1a 643 */
NYX 0:85b3fd62ea1a 644 static ErrorStatus UTILS_PLL_IsBusy(void)
NYX 0:85b3fd62ea1a 645 {
NYX 0:85b3fd62ea1a 646 ErrorStatus status = SUCCESS;
NYX 0:85b3fd62ea1a 647
NYX 0:85b3fd62ea1a 648 /* Check if PLL is busy*/
NYX 0:85b3fd62ea1a 649 if(LL_RCC_PLL_IsReady() != 0U)
NYX 0:85b3fd62ea1a 650 {
NYX 0:85b3fd62ea1a 651 /* PLL configuration cannot be modified */
NYX 0:85b3fd62ea1a 652 status = ERROR;
NYX 0:85b3fd62ea1a 653 }
NYX 0:85b3fd62ea1a 654
NYX 0:85b3fd62ea1a 655 #if defined(RCC_PLLSAI_SUPPORT)
NYX 0:85b3fd62ea1a 656 /* Check if PLLSAI is busy*/
NYX 0:85b3fd62ea1a 657 if(LL_RCC_PLLSAI_IsReady() != 0U)
NYX 0:85b3fd62ea1a 658 {
NYX 0:85b3fd62ea1a 659 /* PLLSAI1 configuration cannot be modified */
NYX 0:85b3fd62ea1a 660 status = ERROR;
NYX 0:85b3fd62ea1a 661 }
NYX 0:85b3fd62ea1a 662 #endif /*RCC_PLLSAI_SUPPORT*/
NYX 0:85b3fd62ea1a 663 #if defined(RCC_PLLI2S_SUPPORT)
NYX 0:85b3fd62ea1a 664 /* Check if PLLI2S is busy*/
NYX 0:85b3fd62ea1a 665 if(LL_RCC_PLLI2S_IsReady() != 0U)
NYX 0:85b3fd62ea1a 666 {
NYX 0:85b3fd62ea1a 667 /* PLLI2S configuration cannot be modified */
NYX 0:85b3fd62ea1a 668 status = ERROR;
NYX 0:85b3fd62ea1a 669 }
NYX 0:85b3fd62ea1a 670 #endif /*RCC_PLLI2S_SUPPORT*/
NYX 0:85b3fd62ea1a 671 return status;
NYX 0:85b3fd62ea1a 672 }
NYX 0:85b3fd62ea1a 673
NYX 0:85b3fd62ea1a 674 /**
NYX 0:85b3fd62ea1a 675 * @brief Function to enable PLL and switch system clock to PLL
NYX 0:85b3fd62ea1a 676 * @param SYSCLK_Frequency SYSCLK frequency
NYX 0:85b3fd62ea1a 677 * @param UTILS_ClkInitStruct pointer to a @ref LL_UTILS_ClkInitTypeDef structure that contains
NYX 0:85b3fd62ea1a 678 * the configuration information for the BUS prescalers.
NYX 0:85b3fd62ea1a 679 * @retval An ErrorStatus enumeration value:
NYX 0:85b3fd62ea1a 680 * - SUCCESS: No problem to switch system to PLL
NYX 0:85b3fd62ea1a 681 * - ERROR: Problem to switch system to PLL
NYX 0:85b3fd62ea1a 682 */
NYX 0:85b3fd62ea1a 683 static ErrorStatus UTILS_EnablePLLAndSwitchSystem(uint32_t SYSCLK_Frequency, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct)
NYX 0:85b3fd62ea1a 684 {
NYX 0:85b3fd62ea1a 685 ErrorStatus status = SUCCESS;
NYX 0:85b3fd62ea1a 686 uint32_t hclk_frequency = 0U;
NYX 0:85b3fd62ea1a 687
NYX 0:85b3fd62ea1a 688 assert_param(IS_LL_UTILS_SYSCLK_DIV(UTILS_ClkInitStruct->AHBCLKDivider));
NYX 0:85b3fd62ea1a 689 assert_param(IS_LL_UTILS_APB1_DIV(UTILS_ClkInitStruct->APB1CLKDivider));
NYX 0:85b3fd62ea1a 690 assert_param(IS_LL_UTILS_APB2_DIV(UTILS_ClkInitStruct->APB2CLKDivider));
NYX 0:85b3fd62ea1a 691
NYX 0:85b3fd62ea1a 692 /* Calculate HCLK frequency */
NYX 0:85b3fd62ea1a 693 hclk_frequency = __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, UTILS_ClkInitStruct->AHBCLKDivider);
NYX 0:85b3fd62ea1a 694
NYX 0:85b3fd62ea1a 695 /* Increasing the number of wait states because of higher CPU frequency */
NYX 0:85b3fd62ea1a 696 if(SystemCoreClock < hclk_frequency)
NYX 0:85b3fd62ea1a 697 {
NYX 0:85b3fd62ea1a 698 /* Set FLASH latency to highest latency */
NYX 0:85b3fd62ea1a 699 status = UTILS_SetFlashLatency(hclk_frequency);
NYX 0:85b3fd62ea1a 700 }
NYX 0:85b3fd62ea1a 701
NYX 0:85b3fd62ea1a 702 /* Update system clock configuration */
NYX 0:85b3fd62ea1a 703 if(status == SUCCESS)
NYX 0:85b3fd62ea1a 704 {
NYX 0:85b3fd62ea1a 705 /* Enable PLL */
NYX 0:85b3fd62ea1a 706 LL_RCC_PLL_Enable();
NYX 0:85b3fd62ea1a 707 while (LL_RCC_PLL_IsReady() != 1U)
NYX 0:85b3fd62ea1a 708 {
NYX 0:85b3fd62ea1a 709 /* Wait for PLL ready */
NYX 0:85b3fd62ea1a 710 }
NYX 0:85b3fd62ea1a 711
NYX 0:85b3fd62ea1a 712 /* Sysclk activation on the main PLL */
NYX 0:85b3fd62ea1a 713 LL_RCC_SetAHBPrescaler(UTILS_ClkInitStruct->AHBCLKDivider);
NYX 0:85b3fd62ea1a 714 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
NYX 0:85b3fd62ea1a 715 while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
NYX 0:85b3fd62ea1a 716 {
NYX 0:85b3fd62ea1a 717 /* Wait for system clock switch to PLL */
NYX 0:85b3fd62ea1a 718 }
NYX 0:85b3fd62ea1a 719
NYX 0:85b3fd62ea1a 720 /* Set APB1 & APB2 prescaler*/
NYX 0:85b3fd62ea1a 721 LL_RCC_SetAPB1Prescaler(UTILS_ClkInitStruct->APB1CLKDivider);
NYX 0:85b3fd62ea1a 722 LL_RCC_SetAPB2Prescaler(UTILS_ClkInitStruct->APB2CLKDivider);
NYX 0:85b3fd62ea1a 723 }
NYX 0:85b3fd62ea1a 724
NYX 0:85b3fd62ea1a 725 /* Decreasing the number of wait states because of lower CPU frequency */
NYX 0:85b3fd62ea1a 726 if(SystemCoreClock > hclk_frequency)
NYX 0:85b3fd62ea1a 727 {
NYX 0:85b3fd62ea1a 728 /* Set FLASH latency to lowest latency */
NYX 0:85b3fd62ea1a 729 status = UTILS_SetFlashLatency(hclk_frequency);
NYX 0:85b3fd62ea1a 730 }
NYX 0:85b3fd62ea1a 731
NYX 0:85b3fd62ea1a 732 /* Update SystemCoreClock variable */
NYX 0:85b3fd62ea1a 733 if(status == SUCCESS)
NYX 0:85b3fd62ea1a 734 {
NYX 0:85b3fd62ea1a 735 LL_SetSystemCoreClock(hclk_frequency);
NYX 0:85b3fd62ea1a 736 }
NYX 0:85b3fd62ea1a 737
NYX 0:85b3fd62ea1a 738 return status;
NYX 0:85b3fd62ea1a 739 }
NYX 0:85b3fd62ea1a 740
NYX 0:85b3fd62ea1a 741 /**
NYX 0:85b3fd62ea1a 742 * @}
NYX 0:85b3fd62ea1a 743 */
NYX 0:85b3fd62ea1a 744
NYX 0:85b3fd62ea1a 745 /**
NYX 0:85b3fd62ea1a 746 * @}
NYX 0:85b3fd62ea1a 747 */
NYX 0:85b3fd62ea1a 748
NYX 0:85b3fd62ea1a 749 /**
NYX 0:85b3fd62ea1a 750 * @}
NYX 0:85b3fd62ea1a 751 */
NYX 0:85b3fd62ea1a 752
NYX 0:85b3fd62ea1a 753 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/