mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
186:707f6e361f3e
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 170:19eb464bc2be 1 /**
Kojto 170:19eb464bc2be 2 ******************************************************************************
Kojto 170:19eb464bc2be 3 * @file system_stm32l0xx.c
Kojto 170:19eb464bc2be 4 * @author MCD Application Team
Kojto 170:19eb464bc2be 5 * @brief CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File.
Kojto 170:19eb464bc2be 6 *
Kojto 170:19eb464bc2be 7 * This file provides two functions and one global variable to be called from
Kojto 170:19eb464bc2be 8 * user application:
Kojto 170:19eb464bc2be 9 * - SystemInit(): This function is called at startup just after reset and
Kojto 170:19eb464bc2be 10 * before branch to main program. This call is made inside
Kojto 170:19eb464bc2be 11 * the "startup_stm32l0xx.s" file.
Kojto 170:19eb464bc2be 12 *
Kojto 170:19eb464bc2be 13 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
Kojto 170:19eb464bc2be 14 * by the user application to setup the SysTick
Kojto 170:19eb464bc2be 15 * timer or configure other parameters.
Kojto 170:19eb464bc2be 16 *
Kojto 170:19eb464bc2be 17 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
Kojto 170:19eb464bc2be 18 * be called whenever the core clock is changed
Kojto 170:19eb464bc2be 19 * during program execution.
Kojto 170:19eb464bc2be 20 *
Kojto 170:19eb464bc2be 21 *
Kojto 170:19eb464bc2be 22 ******************************************************************************
Kojto 170:19eb464bc2be 23 * @attention
Kojto 170:19eb464bc2be 24 *
Anna Bridge 186:707f6e361f3e 25 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
Kojto 170:19eb464bc2be 26 *
Kojto 170:19eb464bc2be 27 * Redistribution and use in source and binary forms, with or without modification,
Kojto 170:19eb464bc2be 28 * are permitted provided that the following conditions are met:
Kojto 170:19eb464bc2be 29 * 1. Redistributions of source code must retain the above copyright notice,
Kojto 170:19eb464bc2be 30 * this list of conditions and the following disclaimer.
Kojto 170:19eb464bc2be 31 * 2. Redistributions in binary form must reproduce the above copyright notice,
Kojto 170:19eb464bc2be 32 * this list of conditions and the following disclaimer in the documentation
Kojto 170:19eb464bc2be 33 * and/or other materials provided with the distribution.
Kojto 170:19eb464bc2be 34 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Kojto 170:19eb464bc2be 35 * may be used to endorse or promote products derived from this software
Kojto 170:19eb464bc2be 36 * without specific prior written permission.
Kojto 170:19eb464bc2be 37 *
Kojto 170:19eb464bc2be 38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Kojto 170:19eb464bc2be 39 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Kojto 170:19eb464bc2be 40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Kojto 170:19eb464bc2be 41 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Kojto 170:19eb464bc2be 42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Kojto 170:19eb464bc2be 43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Kojto 170:19eb464bc2be 44 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Kojto 170:19eb464bc2be 45 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Kojto 170:19eb464bc2be 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Kojto 170:19eb464bc2be 47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 170:19eb464bc2be 48 *
Kojto 170:19eb464bc2be 49 ******************************************************************************
Kojto 170:19eb464bc2be 50 */
Kojto 170:19eb464bc2be 51
Kojto 170:19eb464bc2be 52 /** @addtogroup CMSIS
Kojto 170:19eb464bc2be 53 * @{
Kojto 170:19eb464bc2be 54 */
Kojto 170:19eb464bc2be 55
Kojto 170:19eb464bc2be 56 /** @addtogroup stm32l0xx_system
Kojto 170:19eb464bc2be 57 * @{
Kojto 170:19eb464bc2be 58 */
Kojto 170:19eb464bc2be 59
Kojto 170:19eb464bc2be 60 /** @addtogroup STM32L0xx_System_Private_Includes
Kojto 170:19eb464bc2be 61 * @{
Kojto 170:19eb464bc2be 62 */
Kojto 170:19eb464bc2be 63
Kojto 170:19eb464bc2be 64 #include "stm32l0xx.h"
Kojto 170:19eb464bc2be 65
Kojto 170:19eb464bc2be 66 #if !defined (HSE_VALUE)
Kojto 170:19eb464bc2be 67 #define HSE_VALUE ((uint32_t)8000000U) /*!< Value of the External oscillator in Hz */
Kojto 170:19eb464bc2be 68 #endif /* HSE_VALUE */
Kojto 170:19eb464bc2be 69
Kojto 170:19eb464bc2be 70 #if !defined (MSI_VALUE)
Kojto 170:19eb464bc2be 71 #define MSI_VALUE ((uint32_t)2000000U) /*!< Value of the Internal oscillator in Hz*/
Kojto 170:19eb464bc2be 72 #endif /* MSI_VALUE */
Kojto 170:19eb464bc2be 73
Kojto 170:19eb464bc2be 74 #if !defined (HSI_VALUE)
Kojto 170:19eb464bc2be 75 #define HSI_VALUE ((uint32_t)16000000U) /*!< Value of the Internal oscillator in Hz*/
Kojto 170:19eb464bc2be 76 #endif /* HSI_VALUE */
Kojto 170:19eb464bc2be 77
Kojto 170:19eb464bc2be 78
Kojto 170:19eb464bc2be 79 /**
Kojto 170:19eb464bc2be 80 * @}
Kojto 170:19eb464bc2be 81 */
Kojto 170:19eb464bc2be 82
Kojto 170:19eb464bc2be 83 /** @addtogroup STM32L0xx_System_Private_TypesDefinitions
Kojto 170:19eb464bc2be 84 * @{
Kojto 170:19eb464bc2be 85 */
Kojto 170:19eb464bc2be 86
Kojto 170:19eb464bc2be 87 /**
Kojto 170:19eb464bc2be 88 * @}
Kojto 170:19eb464bc2be 89 */
Kojto 170:19eb464bc2be 90
Kojto 170:19eb464bc2be 91 /** @addtogroup STM32L0xx_System_Private_Defines
Kojto 170:19eb464bc2be 92 * @{
Kojto 170:19eb464bc2be 93 */
Kojto 170:19eb464bc2be 94 /************************* Miscellaneous Configuration ************************/
Kojto 170:19eb464bc2be 95
Kojto 170:19eb464bc2be 96 /*!< Uncomment the following line if you need to relocate your vector Table in
Kojto 170:19eb464bc2be 97 Internal SRAM. */
Kojto 170:19eb464bc2be 98 /* #define VECT_TAB_SRAM */
Kojto 170:19eb464bc2be 99 #define VECT_TAB_OFFSET 0x00U /*!< Vector Table base offset field.
Anna Bridge 186:707f6e361f3e 100 This value must be a multiple of 0x200. */
Kojto 170:19eb464bc2be 101 /******************************************************************************/
Kojto 170:19eb464bc2be 102 /**
Kojto 170:19eb464bc2be 103 * @}
Kojto 170:19eb464bc2be 104 */
Kojto 170:19eb464bc2be 105
Kojto 170:19eb464bc2be 106 /** @addtogroup STM32L0xx_System_Private_Macros
Kojto 170:19eb464bc2be 107 * @{
Kojto 170:19eb464bc2be 108 */
Kojto 170:19eb464bc2be 109
Kojto 170:19eb464bc2be 110 /**
Kojto 170:19eb464bc2be 111 * @}
Kojto 170:19eb464bc2be 112 */
Kojto 170:19eb464bc2be 113
Kojto 170:19eb464bc2be 114 /** @addtogroup STM32L0xx_System_Private_Variables
Kojto 170:19eb464bc2be 115 * @{
Kojto 170:19eb464bc2be 116 */
Kojto 170:19eb464bc2be 117 /* This variable is updated in three ways:
Kojto 170:19eb464bc2be 118 1) by calling CMSIS function SystemCoreClockUpdate()
Kojto 170:19eb464bc2be 119 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
Kojto 170:19eb464bc2be 120 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Kojto 170:19eb464bc2be 121 Note: If you use this function to configure the system clock; then there
Kojto 170:19eb464bc2be 122 is no need to call the 2 first functions listed above, since SystemCoreClock
Kojto 170:19eb464bc2be 123 variable is updated automatically.
Kojto 170:19eb464bc2be 124 */
Kojto 170:19eb464bc2be 125 uint32_t SystemCoreClock = 2000000U;
Kojto 170:19eb464bc2be 126 const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
Kojto 170:19eb464bc2be 127 const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
Kojto 170:19eb464bc2be 128 const uint8_t PLLMulTable[9] = {3U, 4U, 6U, 8U, 12U, 16U, 24U, 32U, 48U};
Kojto 170:19eb464bc2be 129
Kojto 170:19eb464bc2be 130 /**
Kojto 170:19eb464bc2be 131 * @}
Kojto 170:19eb464bc2be 132 */
Kojto 170:19eb464bc2be 133
Kojto 170:19eb464bc2be 134 /** @addtogroup STM32L0xx_System_Private_FunctionPrototypes
Kojto 170:19eb464bc2be 135 * @{
Kojto 170:19eb464bc2be 136 */
Kojto 170:19eb464bc2be 137
Kojto 170:19eb464bc2be 138 /**
Kojto 170:19eb464bc2be 139 * @}
Kojto 170:19eb464bc2be 140 */
Kojto 170:19eb464bc2be 141
Kojto 170:19eb464bc2be 142 /** @addtogroup STM32L0xx_System_Private_Functions
Kojto 170:19eb464bc2be 143 * @{
Kojto 170:19eb464bc2be 144 */
Kojto 170:19eb464bc2be 145
Kojto 170:19eb464bc2be 146 /*+ MBED */
Kojto 170:19eb464bc2be 147 #if 0
Kojto 170:19eb464bc2be 148 /*- MBED */
Kojto 170:19eb464bc2be 149
Kojto 170:19eb464bc2be 150 /**
Kojto 170:19eb464bc2be 151 * @brief Setup the microcontroller system.
Kojto 170:19eb464bc2be 152 * @param None
Kojto 170:19eb464bc2be 153 * @retval None
Kojto 170:19eb464bc2be 154 */
Kojto 170:19eb464bc2be 155 void SystemInit (void)
Kojto 170:19eb464bc2be 156 {
Kojto 170:19eb464bc2be 157 /*!< Set MSION bit */
Kojto 170:19eb464bc2be 158 RCC->CR |= (uint32_t)0x00000100U;
Kojto 170:19eb464bc2be 159
Kojto 170:19eb464bc2be 160 /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */
Kojto 170:19eb464bc2be 161 RCC->CFGR &= (uint32_t) 0x88FF400CU;
Kojto 170:19eb464bc2be 162
Kojto 170:19eb464bc2be 163 /*!< Reset HSION, HSIDIVEN, HSEON, CSSON and PLLON bits */
Kojto 170:19eb464bc2be 164 RCC->CR &= (uint32_t)0xFEF6FFF6U;
Kojto 170:19eb464bc2be 165
Kojto 170:19eb464bc2be 166 /*!< Reset HSI48ON bit */
Kojto 170:19eb464bc2be 167 RCC->CRRCR &= (uint32_t)0xFFFFFFFEU;
Kojto 170:19eb464bc2be 168
Kojto 170:19eb464bc2be 169 /*!< Reset HSEBYP bit */
Kojto 170:19eb464bc2be 170 RCC->CR &= (uint32_t)0xFFFBFFFFU;
Kojto 170:19eb464bc2be 171
Kojto 170:19eb464bc2be 172 /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */
Kojto 170:19eb464bc2be 173 RCC->CFGR &= (uint32_t)0xFF02FFFFU;
Kojto 170:19eb464bc2be 174
Kojto 170:19eb464bc2be 175 /*!< Disable all interrupts */
Kojto 170:19eb464bc2be 176 RCC->CIER = 0x00000000U;
Kojto 170:19eb464bc2be 177
Kojto 170:19eb464bc2be 178 /* Configure the Vector Table location add offset address ------------------*/
Kojto 170:19eb464bc2be 179 #ifdef VECT_TAB_SRAM
Kojto 170:19eb464bc2be 180 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
Kojto 170:19eb464bc2be 181 #else
Kojto 170:19eb464bc2be 182 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
Kojto 170:19eb464bc2be 183 #endif
Kojto 170:19eb464bc2be 184 }
Kojto 170:19eb464bc2be 185
Kojto 170:19eb464bc2be 186 /*+ MBED */
Kojto 170:19eb464bc2be 187 #endif
Kojto 170:19eb464bc2be 188 /*- MBED */
Kojto 170:19eb464bc2be 189
Kojto 170:19eb464bc2be 190 /**
Kojto 170:19eb464bc2be 191 * @brief Update SystemCoreClock according to Clock Register Values
Kojto 170:19eb464bc2be 192 * The SystemCoreClock variable contains the core clock (HCLK), it can
Kojto 170:19eb464bc2be 193 * be used by the user application to setup the SysTick timer or configure
Kojto 170:19eb464bc2be 194 * other parameters.
Kojto 170:19eb464bc2be 195 *
Kojto 170:19eb464bc2be 196 * @note Each time the core clock (HCLK) changes, this function must be called
Kojto 170:19eb464bc2be 197 * to update SystemCoreClock variable value. Otherwise, any configuration
Kojto 170:19eb464bc2be 198 * based on this variable will be incorrect.
Kojto 170:19eb464bc2be 199 *
Kojto 170:19eb464bc2be 200 * @note - The system frequency computed by this function is not the real
Kojto 170:19eb464bc2be 201 * frequency in the chip. It is calculated based on the predefined
Kojto 170:19eb464bc2be 202 * constant and the selected clock source:
Kojto 170:19eb464bc2be 203 *
Kojto 170:19eb464bc2be 204 * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI
Kojto 170:19eb464bc2be 205 * value as defined by the MSI range.
Kojto 170:19eb464bc2be 206 *
Kojto 170:19eb464bc2be 207 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
Kojto 170:19eb464bc2be 208 *
Kojto 170:19eb464bc2be 209 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
Kojto 170:19eb464bc2be 210 *
Kojto 170:19eb464bc2be 211 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
Kojto 170:19eb464bc2be 212 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
Kojto 170:19eb464bc2be 213 *
Kojto 170:19eb464bc2be 214 * (*) HSI_VALUE is a constant defined in stm32l0xx_hal.h file (default value
Kojto 170:19eb464bc2be 215 * 16 MHz) but the real value may vary depending on the variations
Kojto 170:19eb464bc2be 216 * in voltage and temperature.
Kojto 170:19eb464bc2be 217 *
Kojto 170:19eb464bc2be 218 * (**) HSE_VALUE is a constant defined in stm32l0xx_hal.h file (default value
Kojto 170:19eb464bc2be 219 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
Kojto 170:19eb464bc2be 220 * frequency of the crystal used. Otherwise, this function may
Kojto 170:19eb464bc2be 221 * have wrong result.
Kojto 170:19eb464bc2be 222 *
Kojto 170:19eb464bc2be 223 * - The result of this function could be not correct when using fractional
Kojto 170:19eb464bc2be 224 * value for HSE crystal.
Kojto 170:19eb464bc2be 225 * @param None
Kojto 170:19eb464bc2be 226 * @retval None
Kojto 170:19eb464bc2be 227 */
Kojto 170:19eb464bc2be 228 void SystemCoreClockUpdate (void)
Kojto 170:19eb464bc2be 229 {
Kojto 170:19eb464bc2be 230 uint32_t tmp = 0U, pllmul = 0U, plldiv = 0U, pllsource = 0U, msirange = 0U;
Kojto 170:19eb464bc2be 231
Kojto 170:19eb464bc2be 232 /* Get SYSCLK source -------------------------------------------------------*/
Kojto 170:19eb464bc2be 233 tmp = RCC->CFGR & RCC_CFGR_SWS;
Kojto 170:19eb464bc2be 234
Kojto 170:19eb464bc2be 235 switch (tmp)
Kojto 170:19eb464bc2be 236 {
Kojto 170:19eb464bc2be 237 case 0x00U: /* MSI used as system clock */
Kojto 170:19eb464bc2be 238 msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13U;
Kojto 170:19eb464bc2be 239 SystemCoreClock = (32768U * (1U << (msirange + 1U)));
Kojto 170:19eb464bc2be 240 break;
Kojto 170:19eb464bc2be 241 case 0x04U: /* HSI used as system clock */
Kojto 170:19eb464bc2be 242 SystemCoreClock = HSI_VALUE;
Kojto 170:19eb464bc2be 243 break;
Kojto 170:19eb464bc2be 244 case 0x08U: /* HSE used as system clock */
Kojto 170:19eb464bc2be 245 SystemCoreClock = HSE_VALUE;
Kojto 170:19eb464bc2be 246 break;
Kojto 170:19eb464bc2be 247 case 0x0CU: /* PLL used as system clock */
Kojto 170:19eb464bc2be 248 /* Get PLL clock source and multiplication factor ----------------------*/
Kojto 170:19eb464bc2be 249 pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
Kojto 170:19eb464bc2be 250 plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
Kojto 170:19eb464bc2be 251 pllmul = PLLMulTable[(pllmul >> 18U)];
Kojto 170:19eb464bc2be 252 plldiv = (plldiv >> 22U) + 1U;
Kojto 170:19eb464bc2be 253
Kojto 170:19eb464bc2be 254 pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
Kojto 170:19eb464bc2be 255
Kojto 170:19eb464bc2be 256 if (pllsource == 0x00U)
Kojto 170:19eb464bc2be 257 {
Kojto 170:19eb464bc2be 258 /* HSI oscillator clock selected as PLL clock entry */
Kojto 170:19eb464bc2be 259 SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
Kojto 170:19eb464bc2be 260 }
Kojto 170:19eb464bc2be 261 else
Kojto 170:19eb464bc2be 262 {
Kojto 170:19eb464bc2be 263 /* HSE selected as PLL clock entry */
Kojto 170:19eb464bc2be 264 SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
Kojto 170:19eb464bc2be 265 }
Kojto 170:19eb464bc2be 266 break;
Kojto 170:19eb464bc2be 267 default: /* MSI used as system clock */
Kojto 170:19eb464bc2be 268 msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13U;
Kojto 170:19eb464bc2be 269 SystemCoreClock = (32768U * (1U << (msirange + 1U)));
Kojto 170:19eb464bc2be 270 break;
Kojto 170:19eb464bc2be 271 }
Kojto 170:19eb464bc2be 272 /* Compute HCLK clock frequency --------------------------------------------*/
Kojto 170:19eb464bc2be 273 /* Get HCLK prescaler */
Kojto 170:19eb464bc2be 274 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
Kojto 170:19eb464bc2be 275 /* HCLK clock frequency */
Kojto 170:19eb464bc2be 276 SystemCoreClock >>= tmp;
Kojto 170:19eb464bc2be 277 }
Kojto 170:19eb464bc2be 278
Kojto 170:19eb464bc2be 279
Kojto 170:19eb464bc2be 280
Kojto 170:19eb464bc2be 281 /**
Kojto 170:19eb464bc2be 282 * @}
Kojto 170:19eb464bc2be 283 */
Kojto 170:19eb464bc2be 284
Kojto 170:19eb464bc2be 285 /**
Kojto 170:19eb464bc2be 286 * @}
Kojto 170:19eb464bc2be 287 */
Kojto 170:19eb464bc2be 288
Kojto 170:19eb464bc2be 289 /**
Kojto 170:19eb464bc2be 290 * @}
Kojto 170:19eb464bc2be 291 */
Kojto 170:19eb464bc2be 292
Kojto 170:19eb464bc2be 293 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/