f303_h_p1 1

Dependencies:   mbed

Committer:
caa45040
Date:
Fri May 06 14:22:30 2022 +0000
Revision:
26:a047b389adfd
Parent:
19:212136804d25
i2c_slave_HC_SR04_010_BIN re

Who changed what in which revision?

UserRevisionLine numberNew contents of line
caa45040 19:212136804d25 1 /**
caa45040 19:212136804d25 2 ******************************************************************************
caa45040 19:212136804d25 3 * @file system_stm32g0xx.c
caa45040 19:212136804d25 4 * @author MCD Application Team
caa45040 19:212136804d25 5 * @brief CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File
caa45040 19:212136804d25 6 *
caa45040 19:212136804d25 7 * This file provides two functions and one global variable to be called from
caa45040 19:212136804d25 8 * user application:
caa45040 19:212136804d25 9 * - SystemInit(): This function is called at startup just after reset and
caa45040 19:212136804d25 10 * before branch to main program. This call is made inside
caa45040 19:212136804d25 11 * the "startup_stm32g0xx.s" file.
caa45040 19:212136804d25 12 *
caa45040 19:212136804d25 13 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
caa45040 19:212136804d25 14 * by the user application to setup the SysTick
caa45040 19:212136804d25 15 * timer or configure other parameters.
caa45040 19:212136804d25 16 *
caa45040 19:212136804d25 17 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
caa45040 19:212136804d25 18 * be called whenever the core clock is changed
caa45040 19:212136804d25 19 * during program execution.
caa45040 19:212136804d25 20 *
caa45040 19:212136804d25 21 * After each device reset the HSI (8 MHz then 16 MHz) is used as system clock source.
caa45040 19:212136804d25 22 * Then SystemInit() function is called, in "startup_stm32g0xx.s" file, to
caa45040 19:212136804d25 23 * configure the system clock before to branch to main program.
caa45040 19:212136804d25 24 *
caa45040 19:212136804d25 25 * This file configures the system clock as follows:
caa45040 19:212136804d25 26 *=============================================================================
caa45040 19:212136804d25 27 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 28 * System Clock source | HSI
caa45040 19:212136804d25 29 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 30 * SYSCLK(Hz) | 16000000
caa45040 19:212136804d25 31 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 32 * HCLK(Hz) | 16000000
caa45040 19:212136804d25 33 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 34 * AHB Prescaler | 1
caa45040 19:212136804d25 35 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 36 * APB Prescaler | 1
caa45040 19:212136804d25 37 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 38 * HSI Division factor | 1
caa45040 19:212136804d25 39 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 40 * PLL_M | 1
caa45040 19:212136804d25 41 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 42 * PLL_N | 8
caa45040 19:212136804d25 43 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 44 * PLL_P | 7
caa45040 19:212136804d25 45 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 46 * PLL_Q | 2
caa45040 19:212136804d25 47 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 48 * PLL_R | 2
caa45040 19:212136804d25 49 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 50 * Require 48MHz for RNG | Disabled
caa45040 19:212136804d25 51 *-----------------------------------------------------------------------------
caa45040 19:212136804d25 52 *=============================================================================
caa45040 19:212136804d25 53 ******************************************************************************
caa45040 19:212136804d25 54 * @attention
caa45040 19:212136804d25 55 *
caa45040 19:212136804d25 56 * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
caa45040 19:212136804d25 57 * All rights reserved.</center></h2>
caa45040 19:212136804d25 58 *
caa45040 19:212136804d25 59 * This software component is licensed by ST under Apache License, Version 2.0,
caa45040 19:212136804d25 60 * the "License"; You may not use this file except in compliance with the
caa45040 19:212136804d25 61 * License. You may obtain a copy of the License at:
caa45040 19:212136804d25 62 * opensource.org/licenses/Apache-2.0
caa45040 19:212136804d25 63 *
caa45040 19:212136804d25 64 ******************************************************************************
caa45040 19:212136804d25 65 */
caa45040 19:212136804d25 66
caa45040 19:212136804d25 67 /** @addtogroup CMSIS
caa45040 19:212136804d25 68 * @{
caa45040 19:212136804d25 69 */
caa45040 19:212136804d25 70
caa45040 19:212136804d25 71 /** @addtogroup stm32g0xx_system
caa45040 19:212136804d25 72 * @{
caa45040 19:212136804d25 73 */
caa45040 19:212136804d25 74
caa45040 19:212136804d25 75 /** @addtogroup STM32G0xx_System_Private_Includes
caa45040 19:212136804d25 76 * @{
caa45040 19:212136804d25 77 */
caa45040 19:212136804d25 78
caa45040 19:212136804d25 79 #include "stm32g0xx.h"
caa45040 19:212136804d25 80
caa45040 19:212136804d25 81 #if !defined (HSE_VALUE)
caa45040 19:212136804d25 82 #define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */
caa45040 19:212136804d25 83 #endif /* HSE_VALUE */
caa45040 19:212136804d25 84
caa45040 19:212136804d25 85 #if !defined (HSI_VALUE)
caa45040 19:212136804d25 86 #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/
caa45040 19:212136804d25 87 #endif /* HSI_VALUE */
caa45040 19:212136804d25 88
caa45040 19:212136804d25 89 #if !defined (LSI_VALUE)
caa45040 19:212136804d25 90 #define LSI_VALUE (32000UL) /*!< Value of LSI in Hz*/
caa45040 19:212136804d25 91 #endif /* LSI_VALUE */
caa45040 19:212136804d25 92
caa45040 19:212136804d25 93 #if !defined (LSE_VALUE)
caa45040 19:212136804d25 94 #define LSE_VALUE (32768UL) /*!< Value of LSE in Hz*/
caa45040 19:212136804d25 95 #endif /* LSE_VALUE */
caa45040 19:212136804d25 96
caa45040 19:212136804d25 97 /**
caa45040 19:212136804d25 98 * @}
caa45040 19:212136804d25 99 */
caa45040 19:212136804d25 100
caa45040 19:212136804d25 101 /** @addtogroup STM32G0xx_System_Private_TypesDefinitions
caa45040 19:212136804d25 102 * @{
caa45040 19:212136804d25 103 */
caa45040 19:212136804d25 104
caa45040 19:212136804d25 105 /**
caa45040 19:212136804d25 106 * @}
caa45040 19:212136804d25 107 */
caa45040 19:212136804d25 108
caa45040 19:212136804d25 109 /** @addtogroup STM32G0xx_System_Private_Defines
caa45040 19:212136804d25 110 * @{
caa45040 19:212136804d25 111 */
caa45040 19:212136804d25 112
caa45040 19:212136804d25 113 /************************* Miscellaneous Configuration ************************/
caa45040 19:212136804d25 114 /*!< Uncomment the following line if you need to relocate your vector Table in
caa45040 19:212136804d25 115 Internal SRAM. */
caa45040 19:212136804d25 116 /* #define VECT_TAB_SRAM */
caa45040 19:212136804d25 117 #define VECT_TAB_OFFSET 0x0U /*!< Vector Table base offset field.
caa45040 19:212136804d25 118 This value must be a multiple of 0x100. */
caa45040 19:212136804d25 119 /******************************************************************************/
caa45040 19:212136804d25 120 /**
caa45040 19:212136804d25 121 * @}
caa45040 19:212136804d25 122 */
caa45040 19:212136804d25 123
caa45040 19:212136804d25 124 /** @addtogroup STM32G0xx_System_Private_Macros
caa45040 19:212136804d25 125 * @{
caa45040 19:212136804d25 126 */
caa45040 19:212136804d25 127
caa45040 19:212136804d25 128 /**
caa45040 19:212136804d25 129 * @}
caa45040 19:212136804d25 130 */
caa45040 19:212136804d25 131
caa45040 19:212136804d25 132 /** @addtogroup STM32G0xx_System_Private_Variables
caa45040 19:212136804d25 133 * @{
caa45040 19:212136804d25 134 */
caa45040 19:212136804d25 135 /* The SystemCoreClock variable is updated in three ways:
caa45040 19:212136804d25 136 1) by calling CMSIS function SystemCoreClockUpdate()
caa45040 19:212136804d25 137 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
caa45040 19:212136804d25 138 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
caa45040 19:212136804d25 139 Note: If you use this function to configure the system clock; then there
caa45040 19:212136804d25 140 is no need to call the 2 first functions listed above, since SystemCoreClock
caa45040 19:212136804d25 141 variable is updated automatically.
caa45040 19:212136804d25 142 */
caa45040 19:212136804d25 143 uint32_t SystemCoreClock = 16000000UL;
caa45040 19:212136804d25 144
caa45040 19:212136804d25 145 const uint32_t AHBPrescTable[16UL] = {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL, 6UL, 7UL, 8UL, 9UL};
caa45040 19:212136804d25 146 const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL};
caa45040 19:212136804d25 147
caa45040 19:212136804d25 148 /**
caa45040 19:212136804d25 149 * @}
caa45040 19:212136804d25 150 */
caa45040 19:212136804d25 151
caa45040 19:212136804d25 152 /** @addtogroup STM32G0xx_System_Private_FunctionPrototypes
caa45040 19:212136804d25 153 * @{
caa45040 19:212136804d25 154 */
caa45040 19:212136804d25 155
caa45040 19:212136804d25 156 /**
caa45040 19:212136804d25 157 * @}
caa45040 19:212136804d25 158 */
caa45040 19:212136804d25 159
caa45040 19:212136804d25 160 /** @addtogroup STM32G0xx_System_Private_Functions
caa45040 19:212136804d25 161 * @{
caa45040 19:212136804d25 162 */
caa45040 19:212136804d25 163
caa45040 19:212136804d25 164 /**
caa45040 19:212136804d25 165 * @brief Setup the microcontroller system.
caa45040 19:212136804d25 166 * @param None
caa45040 19:212136804d25 167 * @retval None
caa45040 19:212136804d25 168 */
caa45040 19:212136804d25 169 void SystemInit(void)
caa45040 19:212136804d25 170 {
caa45040 19:212136804d25 171 /* Configure the Vector Table location add offset address ------------------*/
caa45040 19:212136804d25 172 #ifdef VECT_TAB_SRAM
caa45040 19:212136804d25 173 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
caa45040 19:212136804d25 174 #else
caa45040 19:212136804d25 175 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
caa45040 19:212136804d25 176 #endif
caa45040 19:212136804d25 177 }
caa45040 19:212136804d25 178
caa45040 19:212136804d25 179 /**
caa45040 19:212136804d25 180 * @brief Update SystemCoreClock variable according to Clock Register Values.
caa45040 19:212136804d25 181 * The SystemCoreClock variable contains the core clock (HCLK), it can
caa45040 19:212136804d25 182 * be used by the user application to setup the SysTick timer or configure
caa45040 19:212136804d25 183 * other parameters.
caa45040 19:212136804d25 184 *
caa45040 19:212136804d25 185 * @note Each time the core clock (HCLK) changes, this function must be called
caa45040 19:212136804d25 186 * to update SystemCoreClock variable value. Otherwise, any configuration
caa45040 19:212136804d25 187 * based on this variable will be incorrect.
caa45040 19:212136804d25 188 *
caa45040 19:212136804d25 189 * @note - The system frequency computed by this function is not the real
caa45040 19:212136804d25 190 * frequency in the chip. It is calculated based on the predefined
caa45040 19:212136804d25 191 * constant and the selected clock source:
caa45040 19:212136804d25 192 *
caa45040 19:212136804d25 193 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) / HSI division factor
caa45040 19:212136804d25 194 *
caa45040 19:212136804d25 195 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
caa45040 19:212136804d25 196 *
caa45040 19:212136804d25 197 * - If SYSCLK source is LSI, SystemCoreClock will contain the LSI_VALUE
caa45040 19:212136804d25 198 *
caa45040 19:212136804d25 199 * - If SYSCLK source is LSE, SystemCoreClock will contain the LSE_VALUE
caa45040 19:212136804d25 200 *
caa45040 19:212136804d25 201 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
caa45040 19:212136804d25 202 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
caa45040 19:212136804d25 203 *
caa45040 19:212136804d25 204 * (**) HSI_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
caa45040 19:212136804d25 205 * 16 MHz) but the real value may vary depending on the variations
caa45040 19:212136804d25 206 * in voltage and temperature.
caa45040 19:212136804d25 207 *
caa45040 19:212136804d25 208 * (***) HSE_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
caa45040 19:212136804d25 209 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
caa45040 19:212136804d25 210 * frequency of the crystal used. Otherwise, this function may
caa45040 19:212136804d25 211 * have wrong result.
caa45040 19:212136804d25 212 *
caa45040 19:212136804d25 213 * - The result of this function could be not correct when using fractional
caa45040 19:212136804d25 214 * value for HSE crystal.
caa45040 19:212136804d25 215 *
caa45040 19:212136804d25 216 * @param None
caa45040 19:212136804d25 217 * @retval None
caa45040 19:212136804d25 218 */
caa45040 19:212136804d25 219 void SystemCoreClockUpdate(void)
caa45040 19:212136804d25 220 {
caa45040 19:212136804d25 221 uint32_t tmp;
caa45040 19:212136804d25 222 uint32_t pllvco;
caa45040 19:212136804d25 223 uint32_t pllr;
caa45040 19:212136804d25 224 uint32_t pllsource;
caa45040 19:212136804d25 225 uint32_t pllm;
caa45040 19:212136804d25 226 uint32_t hsidiv;
caa45040 19:212136804d25 227
caa45040 19:212136804d25 228 /* Get SYSCLK source -------------------------------------------------------*/
caa45040 19:212136804d25 229 switch (RCC->CFGR & RCC_CFGR_SWS)
caa45040 19:212136804d25 230 {
caa45040 19:212136804d25 231 case RCC_CFGR_SWS_0: /* HSE used as system clock */
caa45040 19:212136804d25 232 SystemCoreClock = HSE_VALUE;
caa45040 19:212136804d25 233 break;
caa45040 19:212136804d25 234
caa45040 19:212136804d25 235 case (RCC_CFGR_SWS_1 | RCC_CFGR_SWS_0): /* LSI used as system clock */
caa45040 19:212136804d25 236 SystemCoreClock = LSI_VALUE;
caa45040 19:212136804d25 237 break;
caa45040 19:212136804d25 238
caa45040 19:212136804d25 239 case RCC_CFGR_SWS_2: /* LSE used as system clock */
caa45040 19:212136804d25 240 SystemCoreClock = LSE_VALUE;
caa45040 19:212136804d25 241 break;
caa45040 19:212136804d25 242
caa45040 19:212136804d25 243 case RCC_CFGR_SWS_1: /* PLL used as system clock */
caa45040 19:212136804d25 244 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
caa45040 19:212136804d25 245 SYSCLK = PLL_VCO / PLLR
caa45040 19:212136804d25 246 */
caa45040 19:212136804d25 247 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
caa45040 19:212136804d25 248 pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1UL;
caa45040 19:212136804d25 249
caa45040 19:212136804d25 250 if(pllsource == 0x03UL) /* HSE used as PLL clock source */
caa45040 19:212136804d25 251 {
caa45040 19:212136804d25 252 pllvco = (HSE_VALUE / pllm);
caa45040 19:212136804d25 253 }
caa45040 19:212136804d25 254 else /* HSI used as PLL clock source */
caa45040 19:212136804d25 255 {
caa45040 19:212136804d25 256 pllvco = (HSI_VALUE / pllm);
caa45040 19:212136804d25 257 }
caa45040 19:212136804d25 258 pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
caa45040 19:212136804d25 259 pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1UL);
caa45040 19:212136804d25 260
caa45040 19:212136804d25 261 SystemCoreClock = pllvco/pllr;
caa45040 19:212136804d25 262 break;
caa45040 19:212136804d25 263
caa45040 19:212136804d25 264 case 0x00000000U: /* HSI used as system clock */
caa45040 19:212136804d25 265 default: /* HSI used as system clock */
caa45040 19:212136804d25 266 hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV))>> RCC_CR_HSIDIV_Pos));
caa45040 19:212136804d25 267 SystemCoreClock = (HSI_VALUE/hsidiv);
caa45040 19:212136804d25 268 break;
caa45040 19:212136804d25 269 }
caa45040 19:212136804d25 270 /* Compute HCLK clock frequency --------------------------------------------*/
caa45040 19:212136804d25 271 /* Get HCLK prescaler */
caa45040 19:212136804d25 272 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
caa45040 19:212136804d25 273 /* HCLK clock frequency */
caa45040 19:212136804d25 274 SystemCoreClock >>= tmp;
caa45040 19:212136804d25 275 }
caa45040 19:212136804d25 276
caa45040 19:212136804d25 277
caa45040 19:212136804d25 278 /**
caa45040 19:212136804d25 279 * @}
caa45040 19:212136804d25 280 */
caa45040 19:212136804d25 281
caa45040 19:212136804d25 282 /**
caa45040 19:212136804d25 283 * @}
caa45040 19:212136804d25 284 */
caa45040 19:212136804d25 285
caa45040 19:212136804d25 286 /**
caa45040 19:212136804d25 287 * @}
caa45040 19:212136804d25 288 */
caa45040 19:212136804d25 289
caa45040 19:212136804d25 290 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
caa45040 19:212136804d25 291