f303_h_p1 1

Dependencies:   mbed

Committer:
caa45040
Date:
Fri May 06 14:22:30 2022 +0000
Revision:
26:a047b389adfd
Parent:
3:93ec394564e8
i2c_slave_HC_SR04_010_BIN re

Who changed what in which revision?

UserRevisionLine numberNew contents of line
caa45040 3:93ec394564e8 1 /**
caa45040 3:93ec394564e8 2 ******************************************************************************
caa45040 3:93ec394564e8 3 * @file system_stm32l0xx.c
caa45040 3:93ec394564e8 4 * @author MCD Application Team
caa45040 3:93ec394564e8 5 * @brief CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File.
caa45040 3:93ec394564e8 6 *
caa45040 3:93ec394564e8 7 * This file provides two functions and one global variable to be called from
caa45040 3:93ec394564e8 8 * user application:
caa45040 3:93ec394564e8 9 * - SystemInit(): This function is called at startup just after reset and
caa45040 3:93ec394564e8 10 * before branch to main program. This call is made inside
caa45040 3:93ec394564e8 11 * the "startup_stm32l0xx.s" file.
caa45040 3:93ec394564e8 12 *
caa45040 3:93ec394564e8 13 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
caa45040 3:93ec394564e8 14 * by the user application to setup the SysTick
caa45040 3:93ec394564e8 15 * timer or configure other parameters.
caa45040 3:93ec394564e8 16 *
caa45040 3:93ec394564e8 17 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
caa45040 3:93ec394564e8 18 * be called whenever the core clock is changed
caa45040 3:93ec394564e8 19 * during program execution.
caa45040 3:93ec394564e8 20 *
caa45040 3:93ec394564e8 21 *
caa45040 3:93ec394564e8 22 ******************************************************************************
caa45040 3:93ec394564e8 23 * @attention
caa45040 3:93ec394564e8 24 *
caa45040 3:93ec394564e8 25 * <h2><center>&copy; Copyright(c) 2016 STMicroelectronics.
caa45040 3:93ec394564e8 26 * All rights reserved.</center></h2>
caa45040 3:93ec394564e8 27 *
caa45040 3:93ec394564e8 28 * This software component is licensed by ST under BSD 3-Clause license,
caa45040 3:93ec394564e8 29 * the "License"; You may not use this file except in compliance with the
caa45040 3:93ec394564e8 30 * License. You may obtain a copy of the License at:
caa45040 3:93ec394564e8 31 * opensource.org/licenses/BSD-3-Clause
caa45040 3:93ec394564e8 32 *
caa45040 3:93ec394564e8 33 ******************************************************************************
caa45040 3:93ec394564e8 34 */
caa45040 3:93ec394564e8 35
caa45040 3:93ec394564e8 36 /** @addtogroup CMSIS
caa45040 3:93ec394564e8 37 * @{
caa45040 3:93ec394564e8 38 */
caa45040 3:93ec394564e8 39
caa45040 3:93ec394564e8 40 /** @addtogroup stm32l0xx_system
caa45040 3:93ec394564e8 41 * @{
caa45040 3:93ec394564e8 42 */
caa45040 3:93ec394564e8 43
caa45040 3:93ec394564e8 44 /** @addtogroup STM32L0xx_System_Private_Includes
caa45040 3:93ec394564e8 45 * @{
caa45040 3:93ec394564e8 46 */
caa45040 3:93ec394564e8 47
caa45040 3:93ec394564e8 48 #include "stm32l0xx.h"
caa45040 3:93ec394564e8 49
caa45040 3:93ec394564e8 50 #if !defined (HSE_VALUE)
caa45040 3:93ec394564e8 51 #define HSE_VALUE ((uint32_t)8000000U) /*!< Value of the External oscillator in Hz */
caa45040 3:93ec394564e8 52 #endif /* HSE_VALUE */
caa45040 3:93ec394564e8 53
caa45040 3:93ec394564e8 54 #if !defined (MSI_VALUE)
caa45040 3:93ec394564e8 55 #define MSI_VALUE ((uint32_t)2097152U) /*!< Value of the Internal oscillator in Hz*/
caa45040 3:93ec394564e8 56 #endif /* MSI_VALUE */
caa45040 3:93ec394564e8 57
caa45040 3:93ec394564e8 58 #if !defined (HSI_VALUE)
caa45040 3:93ec394564e8 59 #define HSI_VALUE ((uint32_t)16000000U) /*!< Value of the Internal oscillator in Hz*/
caa45040 3:93ec394564e8 60 #endif /* HSI_VALUE */
caa45040 3:93ec394564e8 61
caa45040 3:93ec394564e8 62
caa45040 3:93ec394564e8 63 /**
caa45040 3:93ec394564e8 64 * @}
caa45040 3:93ec394564e8 65 */
caa45040 3:93ec394564e8 66
caa45040 3:93ec394564e8 67 /** @addtogroup STM32L0xx_System_Private_TypesDefinitions
caa45040 3:93ec394564e8 68 * @{
caa45040 3:93ec394564e8 69 */
caa45040 3:93ec394564e8 70
caa45040 3:93ec394564e8 71 /**
caa45040 3:93ec394564e8 72 * @}
caa45040 3:93ec394564e8 73 */
caa45040 3:93ec394564e8 74
caa45040 3:93ec394564e8 75 /** @addtogroup STM32L0xx_System_Private_Defines
caa45040 3:93ec394564e8 76 * @{
caa45040 3:93ec394564e8 77 */
caa45040 3:93ec394564e8 78 /************************* Miscellaneous Configuration ************************/
caa45040 3:93ec394564e8 79
caa45040 3:93ec394564e8 80 /* Note: Following vector table addresses must be defined in line with linker
caa45040 3:93ec394564e8 81 configuration. */
caa45040 3:93ec394564e8 82 /*!< Uncomment the following line if you need to relocate the vector table
caa45040 3:93ec394564e8 83 anywhere in Flash or Sram, else the vector table is kept at the automatic
caa45040 3:93ec394564e8 84 remap of boot address selected */
caa45040 3:93ec394564e8 85 /* #define USER_VECT_TAB_ADDRESS */
caa45040 3:93ec394564e8 86
caa45040 3:93ec394564e8 87 #if defined(USER_VECT_TAB_ADDRESS)
caa45040 3:93ec394564e8 88 /*!< Uncomment the following line if you need to relocate your vector Table
caa45040 3:93ec394564e8 89 in Sram else user remap will be done in Flash. */
caa45040 3:93ec394564e8 90 /* #define VECT_TAB_SRAM */
caa45040 3:93ec394564e8 91 #if defined(VECT_TAB_SRAM)
caa45040 3:93ec394564e8 92 #define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
caa45040 3:93ec394564e8 93 This value must be a multiple of 0x200. */
caa45040 3:93ec394564e8 94 #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
caa45040 3:93ec394564e8 95 This value must be a multiple of 0x200. */
caa45040 3:93ec394564e8 96 #else
caa45040 3:93ec394564e8 97 #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
caa45040 3:93ec394564e8 98 This value must be a multiple of 0x200. */
caa45040 3:93ec394564e8 99 #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
caa45040 3:93ec394564e8 100 This value must be a multiple of 0x200. */
caa45040 3:93ec394564e8 101 #endif /* VECT_TAB_SRAM */
caa45040 3:93ec394564e8 102 #endif /* USER_VECT_TAB_ADDRESS */
caa45040 3:93ec394564e8 103
caa45040 3:93ec394564e8 104 /******************************************************************************/
caa45040 3:93ec394564e8 105 /**
caa45040 3:93ec394564e8 106 * @}
caa45040 3:93ec394564e8 107 */
caa45040 3:93ec394564e8 108
caa45040 3:93ec394564e8 109 /** @addtogroup STM32L0xx_System_Private_Macros
caa45040 3:93ec394564e8 110 * @{
caa45040 3:93ec394564e8 111 */
caa45040 3:93ec394564e8 112
caa45040 3:93ec394564e8 113 /**
caa45040 3:93ec394564e8 114 * @}
caa45040 3:93ec394564e8 115 */
caa45040 3:93ec394564e8 116
caa45040 3:93ec394564e8 117 /** @addtogroup STM32L0xx_System_Private_Variables
caa45040 3:93ec394564e8 118 * @{
caa45040 3:93ec394564e8 119 */
caa45040 3:93ec394564e8 120 /* This variable is updated in three ways:
caa45040 3:93ec394564e8 121 1) by calling CMSIS function SystemCoreClockUpdate()
caa45040 3:93ec394564e8 122 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
caa45040 3:93ec394564e8 123 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
caa45040 3:93ec394564e8 124 Note: If you use this function to configure the system clock; then there
caa45040 3:93ec394564e8 125 is no need to call the 2 first functions listed above, since SystemCoreClock
caa45040 3:93ec394564e8 126 variable is updated automatically.
caa45040 3:93ec394564e8 127 */
caa45040 3:93ec394564e8 128 uint32_t SystemCoreClock = 2097152U; /* 32.768 kHz * 2^6 */
caa45040 3:93ec394564e8 129 const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
caa45040 3:93ec394564e8 130 const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
caa45040 3:93ec394564e8 131 const uint8_t PLLMulTable[9] = {3U, 4U, 6U, 8U, 12U, 16U, 24U, 32U, 48U};
caa45040 3:93ec394564e8 132
caa45040 3:93ec394564e8 133 /**
caa45040 3:93ec394564e8 134 * @}
caa45040 3:93ec394564e8 135 */
caa45040 3:93ec394564e8 136
caa45040 3:93ec394564e8 137 /** @addtogroup STM32L0xx_System_Private_FunctionPrototypes
caa45040 3:93ec394564e8 138 * @{
caa45040 3:93ec394564e8 139 */
caa45040 3:93ec394564e8 140
caa45040 3:93ec394564e8 141 /**
caa45040 3:93ec394564e8 142 * @}
caa45040 3:93ec394564e8 143 */
caa45040 3:93ec394564e8 144
caa45040 3:93ec394564e8 145 /** @addtogroup STM32L0xx_System_Private_Functions
caa45040 3:93ec394564e8 146 * @{
caa45040 3:93ec394564e8 147 */
caa45040 3:93ec394564e8 148
caa45040 3:93ec394564e8 149 /**
caa45040 3:93ec394564e8 150 * @brief Setup the microcontroller system.
caa45040 3:93ec394564e8 151 * @param None
caa45040 3:93ec394564e8 152 * @retval None
caa45040 3:93ec394564e8 153 */
caa45040 3:93ec394564e8 154 void SystemInit (void)
caa45040 3:93ec394564e8 155 {
caa45040 3:93ec394564e8 156 /* Configure the Vector Table location add offset address ------------------*/
caa45040 3:93ec394564e8 157 #if defined (USER_VECT_TAB_ADDRESS)
caa45040 3:93ec394564e8 158 SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
caa45040 3:93ec394564e8 159 #endif /* USER_VECT_TAB_ADDRESS */
caa45040 3:93ec394564e8 160 }
caa45040 3:93ec394564e8 161
caa45040 3:93ec394564e8 162 /**
caa45040 3:93ec394564e8 163 * @brief Update SystemCoreClock variable according to Clock Register Values.
caa45040 3:93ec394564e8 164 * The SystemCoreClock variable contains the core clock (HCLK), it can
caa45040 3:93ec394564e8 165 * be used by the user application to setup the SysTick timer or configure
caa45040 3:93ec394564e8 166 * other parameters.
caa45040 3:93ec394564e8 167 *
caa45040 3:93ec394564e8 168 * @note Each time the core clock (HCLK) changes, this function must be called
caa45040 3:93ec394564e8 169 * to update SystemCoreClock variable value. Otherwise, any configuration
caa45040 3:93ec394564e8 170 * based on this variable will be incorrect.
caa45040 3:93ec394564e8 171 *
caa45040 3:93ec394564e8 172 * @note - The system frequency computed by this function is not the real
caa45040 3:93ec394564e8 173 * frequency in the chip. It is calculated based on the predefined
caa45040 3:93ec394564e8 174 * constant and the selected clock source:
caa45040 3:93ec394564e8 175 *
caa45040 3:93ec394564e8 176 * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI
caa45040 3:93ec394564e8 177 * value as defined by the MSI range.
caa45040 3:93ec394564e8 178 *
caa45040 3:93ec394564e8 179 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
caa45040 3:93ec394564e8 180 *
caa45040 3:93ec394564e8 181 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
caa45040 3:93ec394564e8 182 *
caa45040 3:93ec394564e8 183 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
caa45040 3:93ec394564e8 184 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
caa45040 3:93ec394564e8 185 *
caa45040 3:93ec394564e8 186 * (*) HSI_VALUE is a constant defined in stm32l0xx_hal.h file (default value
caa45040 3:93ec394564e8 187 * 16 MHz) but the real value may vary depending on the variations
caa45040 3:93ec394564e8 188 * in voltage and temperature.
caa45040 3:93ec394564e8 189 *
caa45040 3:93ec394564e8 190 * (**) HSE_VALUE is a constant defined in stm32l0xx_hal.h file (default value
caa45040 3:93ec394564e8 191 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
caa45040 3:93ec394564e8 192 * frequency of the crystal used. Otherwise, this function may
caa45040 3:93ec394564e8 193 * have wrong result.
caa45040 3:93ec394564e8 194 *
caa45040 3:93ec394564e8 195 * - The result of this function could be not correct when using fractional
caa45040 3:93ec394564e8 196 * value for HSE crystal.
caa45040 3:93ec394564e8 197 * @param None
caa45040 3:93ec394564e8 198 * @retval None
caa45040 3:93ec394564e8 199 */
caa45040 3:93ec394564e8 200 void SystemCoreClockUpdate (void)
caa45040 3:93ec394564e8 201 {
caa45040 3:93ec394564e8 202 uint32_t tmp = 0U, pllmul = 0U, plldiv = 0U, pllsource = 0U, msirange = 0U;
caa45040 3:93ec394564e8 203
caa45040 3:93ec394564e8 204 /* Get SYSCLK source -------------------------------------------------------*/
caa45040 3:93ec394564e8 205 tmp = RCC->CFGR & RCC_CFGR_SWS;
caa45040 3:93ec394564e8 206
caa45040 3:93ec394564e8 207 switch (tmp)
caa45040 3:93ec394564e8 208 {
caa45040 3:93ec394564e8 209 case 0x00U: /* MSI used as system clock */
caa45040 3:93ec394564e8 210 msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> RCC_ICSCR_MSIRANGE_Pos;
caa45040 3:93ec394564e8 211 SystemCoreClock = (32768U * (1U << (msirange + 1U)));
caa45040 3:93ec394564e8 212 break;
caa45040 3:93ec394564e8 213 case 0x04U: /* HSI used as system clock */
caa45040 3:93ec394564e8 214 if ((RCC->CR & RCC_CR_HSIDIVF) != 0U)
caa45040 3:93ec394564e8 215 {
caa45040 3:93ec394564e8 216 SystemCoreClock = HSI_VALUE / 4U;
caa45040 3:93ec394564e8 217 }
caa45040 3:93ec394564e8 218 else
caa45040 3:93ec394564e8 219 {
caa45040 3:93ec394564e8 220 SystemCoreClock = HSI_VALUE;
caa45040 3:93ec394564e8 221 }
caa45040 3:93ec394564e8 222 break;
caa45040 3:93ec394564e8 223 case 0x08U: /* HSE used as system clock */
caa45040 3:93ec394564e8 224 SystemCoreClock = HSE_VALUE;
caa45040 3:93ec394564e8 225 break;
caa45040 3:93ec394564e8 226 default: /* PLL used as system clock */
caa45040 3:93ec394564e8 227 /* Get PLL clock source and multiplication factor ----------------------*/
caa45040 3:93ec394564e8 228 pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
caa45040 3:93ec394564e8 229 plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
caa45040 3:93ec394564e8 230 pllmul = PLLMulTable[(pllmul >> RCC_CFGR_PLLMUL_Pos)];
caa45040 3:93ec394564e8 231 plldiv = (plldiv >> RCC_CFGR_PLLDIV_Pos) + 1U;
caa45040 3:93ec394564e8 232
caa45040 3:93ec394564e8 233 pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
caa45040 3:93ec394564e8 234
caa45040 3:93ec394564e8 235 if (pllsource == 0x00U)
caa45040 3:93ec394564e8 236 {
caa45040 3:93ec394564e8 237 /* HSI oscillator clock selected as PLL clock entry */
caa45040 3:93ec394564e8 238 if ((RCC->CR & RCC_CR_HSIDIVF) != 0U)
caa45040 3:93ec394564e8 239 {
caa45040 3:93ec394564e8 240 SystemCoreClock = (((HSI_VALUE / 4U) * pllmul) / plldiv);
caa45040 3:93ec394564e8 241 }
caa45040 3:93ec394564e8 242 else
caa45040 3:93ec394564e8 243 {
caa45040 3:93ec394564e8 244 SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
caa45040 3:93ec394564e8 245 }
caa45040 3:93ec394564e8 246 }
caa45040 3:93ec394564e8 247 else
caa45040 3:93ec394564e8 248 {
caa45040 3:93ec394564e8 249 /* HSE selected as PLL clock entry */
caa45040 3:93ec394564e8 250 SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
caa45040 3:93ec394564e8 251 }
caa45040 3:93ec394564e8 252 break;
caa45040 3:93ec394564e8 253 }
caa45040 3:93ec394564e8 254 /* Compute HCLK clock frequency --------------------------------------------*/
caa45040 3:93ec394564e8 255 /* Get HCLK prescaler */
caa45040 3:93ec394564e8 256 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
caa45040 3:93ec394564e8 257 /* HCLK clock frequency */
caa45040 3:93ec394564e8 258 SystemCoreClock >>= tmp;
caa45040 3:93ec394564e8 259 }
caa45040 3:93ec394564e8 260
caa45040 3:93ec394564e8 261
caa45040 3:93ec394564e8 262
caa45040 3:93ec394564e8 263 /**
caa45040 3:93ec394564e8 264 * @}
caa45040 3:93ec394564e8 265 */
caa45040 3:93ec394564e8 266
caa45040 3:93ec394564e8 267 /**
caa45040 3:93ec394564e8 268 * @}
caa45040 3:93ec394564e8 269 */
caa45040 3:93ec394564e8 270
caa45040 3:93ec394564e8 271 /**
caa45040 3:93ec394564e8 272 * @}
caa45040 3:93ec394564e8 273 */
caa45040 3:93ec394564e8 274
caa45040 3:93ec394564e8 275 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
caa45040 3:93ec394564e8 276