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:
187:0387e8f68319
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 170:19eb464bc2be 1 /* mbed Microcontroller Library
Kojto 170:19eb464bc2be 2 * Copyright (c) 2006-2017 ARM Limited
Kojto 170:19eb464bc2be 3 *
Kojto 170:19eb464bc2be 4 * Licensed under the Apache License, Version 2.0 (the "License");
Kojto 170:19eb464bc2be 5 * you may not use this file except in compliance with the License.
Kojto 170:19eb464bc2be 6 * You may obtain a copy of the License at
Kojto 170:19eb464bc2be 7 *
Kojto 170:19eb464bc2be 8 * http://www.apache.org/licenses/LICENSE-2.0
Kojto 170:19eb464bc2be 9 *
Kojto 170:19eb464bc2be 10 * Unless required by applicable law or agreed to in writing, software
Kojto 170:19eb464bc2be 11 * distributed under the License is distributed on an "AS IS" BASIS,
Kojto 170:19eb464bc2be 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kojto 170:19eb464bc2be 13 * See the License for the specific language governing permissions and
Kojto 170:19eb464bc2be 14 * limitations under the License.
Kojto 170:19eb464bc2be 15 */
Kojto 170:19eb464bc2be 16
Kojto 170:19eb464bc2be 17 /**
Kojto 170:19eb464bc2be 18 * This file configures the system clock as follows:
Kojto 170:19eb464bc2be 19 *-----------------------------------------------------------------
Kojto 170:19eb464bc2be 20 * System clock source | 1- USE_PLL_HSE_EXTC (external 8 MHz clock)
Kojto 170:19eb464bc2be 21 * | 2- USE_PLL_HSE_XTAL (external 8 MHz xtal)
Kojto 170:19eb464bc2be 22 * | 3- USE_PLL_HSI (internal 16 MHz)
Kojto 170:19eb464bc2be 23 *-----------------------------------------------------------------
Kojto 170:19eb464bc2be 24 * SYSCLK(MHz) | 32
Kojto 170:19eb464bc2be 25 * AHBCLK (MHz) | 32
Kojto 170:19eb464bc2be 26 * APB1CLK (MHz) | 32
Kojto 170:19eb464bc2be 27 * USB capable | YES
Kojto 170:19eb464bc2be 28 *-----------------------------------------------------------------
Kojto 170:19eb464bc2be 29 */
Kojto 170:19eb464bc2be 30
Kojto 170:19eb464bc2be 31 #include "stm32l1xx.h"
AnnaBridge 187:0387e8f68319 32 #include "mbed_error.h"
Kojto 170:19eb464bc2be 33
Kojto 170:19eb464bc2be 34 /*!< Uncomment the following line if you need to relocate your vector Table in
Kojto 170:19eb464bc2be 35 Internal SRAM. */
Kojto 170:19eb464bc2be 36 /* #define VECT_TAB_SRAM */
Kojto 170:19eb464bc2be 37 #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
Kojto 170:19eb464bc2be 38 This value must be a multiple of 0x200. */
Kojto 170:19eb464bc2be 39
Kojto 170:19eb464bc2be 40 #define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
Kojto 170:19eb464bc2be 41 #define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default)
Kojto 170:19eb464bc2be 42 #define USE_PLL_HSI 0x2 // Use HSI internal clock
Kojto 170:19eb464bc2be 43
Kojto 170:19eb464bc2be 44 #if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
Kojto 170:19eb464bc2be 45 uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
Kojto 170:19eb464bc2be 46 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
Kojto 170:19eb464bc2be 47
Kojto 170:19eb464bc2be 48 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
Kojto 170:19eb464bc2be 49 uint8_t SetSysClock_PLL_HSI(void);
Kojto 170:19eb464bc2be 50 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
Kojto 170:19eb464bc2be 51
Kojto 170:19eb464bc2be 52
Kojto 170:19eb464bc2be 53 /**
Kojto 170:19eb464bc2be 54 * @brief Setup the microcontroller system.
Kojto 170:19eb464bc2be 55 * Initialize the Embedded Flash Interface, the PLL and update the
Kojto 170:19eb464bc2be 56 * SystemCoreClock variable.
Kojto 170:19eb464bc2be 57 * @param None
Kojto 170:19eb464bc2be 58 * @retval None
Kojto 170:19eb464bc2be 59 */
Kojto 170:19eb464bc2be 60 void SystemInit (void)
Kojto 170:19eb464bc2be 61 {
Kojto 170:19eb464bc2be 62 /*!< Set MSION bit */
Kojto 170:19eb464bc2be 63 RCC->CR |= (uint32_t)0x00000100;
Kojto 170:19eb464bc2be 64
Kojto 170:19eb464bc2be 65 /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */
Kojto 170:19eb464bc2be 66 RCC->CFGR &= (uint32_t)0x88FFC00C;
Kojto 170:19eb464bc2be 67
Kojto 170:19eb464bc2be 68 /*!< Reset HSION, HSEON, CSSON and PLLON bits */
Kojto 170:19eb464bc2be 69 RCC->CR &= (uint32_t)0xEEFEFFFE;
Kojto 170:19eb464bc2be 70
Kojto 170:19eb464bc2be 71 /*!< Reset HSEBYP bit */
Kojto 170:19eb464bc2be 72 RCC->CR &= (uint32_t)0xFFFBFFFF;
Kojto 170:19eb464bc2be 73
Kojto 170:19eb464bc2be 74 /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */
Kojto 170:19eb464bc2be 75 RCC->CFGR &= (uint32_t)0xFF02FFFF;
Kojto 170:19eb464bc2be 76
Kojto 170:19eb464bc2be 77 /*!< Disable all interrupts */
Kojto 170:19eb464bc2be 78 RCC->CIR = 0x00000000;
Kojto 170:19eb464bc2be 79
Kojto 170:19eb464bc2be 80 #ifdef DATA_IN_ExtSRAM
Kojto 170:19eb464bc2be 81 SystemInit_ExtMemCtl();
Kojto 170:19eb464bc2be 82 #endif /* DATA_IN_ExtSRAM */
Kojto 170:19eb464bc2be 83
Kojto 170:19eb464bc2be 84 #ifdef VECT_TAB_SRAM
Kojto 170:19eb464bc2be 85 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
Kojto 170:19eb464bc2be 86 #else
Kojto 170:19eb464bc2be 87 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
Kojto 170:19eb464bc2be 88 #endif
Kojto 170:19eb464bc2be 89
Kojto 170:19eb464bc2be 90 }
Kojto 170:19eb464bc2be 91
Kojto 170:19eb464bc2be 92 /**
Kojto 170:19eb464bc2be 93 * @brief Configures the System clock source, PLL Multiplier and Divider factors,
Kojto 170:19eb464bc2be 94 * AHB/APBx prescalers and Flash settings
Kojto 170:19eb464bc2be 95 * @note This function should be called only once the RCC clock configuration
Kojto 170:19eb464bc2be 96 * is reset to the default reset state (done in SystemInit() function).
Kojto 170:19eb464bc2be 97 * @param None
Kojto 170:19eb464bc2be 98 * @retval None
Kojto 170:19eb464bc2be 99 */
Kojto 170:19eb464bc2be 100 void SetSysClock(void)
Kojto 170:19eb464bc2be 101 {
Kojto 170:19eb464bc2be 102 #if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC)
Kojto 170:19eb464bc2be 103 /* 1- Try to start with HSE and external clock */
Kojto 170:19eb464bc2be 104 if (SetSysClock_PLL_HSE(1) == 0)
Kojto 170:19eb464bc2be 105 #endif
Kojto 170:19eb464bc2be 106 {
Kojto 170:19eb464bc2be 107 #if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL)
Kojto 170:19eb464bc2be 108 /* 2- If fail try to start with HSE and external xtal */
Kojto 170:19eb464bc2be 109 if (SetSysClock_PLL_HSE(0) == 0)
Kojto 170:19eb464bc2be 110 #endif
Kojto 170:19eb464bc2be 111 {
Kojto 170:19eb464bc2be 112 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
Kojto 170:19eb464bc2be 113 /* 3- If fail start with HSI clock */
Kojto 170:19eb464bc2be 114 if (SetSysClock_PLL_HSI() == 0)
Kojto 170:19eb464bc2be 115 #endif
Kojto 170:19eb464bc2be 116 {
AnnaBridge 187:0387e8f68319 117 {
AnnaBridge 187:0387e8f68319 118 error("SetSysClock failed\n");
Kojto 170:19eb464bc2be 119 }
Kojto 170:19eb464bc2be 120 }
Kojto 170:19eb464bc2be 121 }
Kojto 170:19eb464bc2be 122 }
Kojto 170:19eb464bc2be 123
Kojto 170:19eb464bc2be 124 /* Output clock on MCO1 pin(PA8) for debugging purpose */
Kojto 170:19eb464bc2be 125 //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);
Kojto 170:19eb464bc2be 126 }
Kojto 170:19eb464bc2be 127
Kojto 170:19eb464bc2be 128 #if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
Kojto 170:19eb464bc2be 129 /******************************************************************************/
Kojto 170:19eb464bc2be 130 /* PLL (clocked by HSE) used as System clock source */
Kojto 170:19eb464bc2be 131 /******************************************************************************/
Kojto 170:19eb464bc2be 132 uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
Kojto 170:19eb464bc2be 133 {
Kojto 170:19eb464bc2be 134 RCC_ClkInitTypeDef RCC_ClkInitStruct;
Kojto 170:19eb464bc2be 135 RCC_OscInitTypeDef RCC_OscInitStruct;
Kojto 170:19eb464bc2be 136
Kojto 170:19eb464bc2be 137 /* Used to gain time after DeepSleep in case HSI is used */
Kojto 170:19eb464bc2be 138 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) {
Kojto 170:19eb464bc2be 139 return 0;
Kojto 170:19eb464bc2be 140 }
Kojto 170:19eb464bc2be 141
Kojto 170:19eb464bc2be 142 /* The voltage scaling allows optimizing the power consumption when the device is
Kojto 170:19eb464bc2be 143 clocked below the maximum system frequency, to update the voltage scaling value
Kojto 170:19eb464bc2be 144 regarding system frequency refer to product datasheet. */
Kojto 170:19eb464bc2be 145 __PWR_CLK_ENABLE();
Kojto 170:19eb464bc2be 146 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Kojto 170:19eb464bc2be 147
Kojto 170:19eb464bc2be 148 /* Enable HSE oscillator and activate PLL with HSE as source */
Kojto 170:19eb464bc2be 149 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
Kojto 170:19eb464bc2be 150 if (bypass == 0) {
Kojto 170:19eb464bc2be 151 RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */
Kojto 170:19eb464bc2be 152 } else {
Kojto 170:19eb464bc2be 153 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */
Kojto 170:19eb464bc2be 154 }
Kojto 170:19eb464bc2be 155 RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
Kojto 170:19eb464bc2be 156 // SYSCLK = 32 MHz (8 MHz *12 /3)
Kojto 170:19eb464bc2be 157 // USBCLK = 48 MHz (8 MHz *12 /2)
Kojto 170:19eb464bc2be 158 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Kojto 170:19eb464bc2be 159 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
Kojto 170:19eb464bc2be 160 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
Kojto 170:19eb464bc2be 161 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
Kojto 170:19eb464bc2be 162 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Kojto 170:19eb464bc2be 163 return 0; // FAIL
Kojto 170:19eb464bc2be 164 }
Kojto 170:19eb464bc2be 165
Kojto 170:19eb464bc2be 166 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
Kojto 170:19eb464bc2be 167 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
Kojto 170:19eb464bc2be 168 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz
Kojto 170:19eb464bc2be 169 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz
Kojto 170:19eb464bc2be 170 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz
Kojto 170:19eb464bc2be 171 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz
Kojto 170:19eb464bc2be 172 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
Kojto 170:19eb464bc2be 173 return 0; // FAIL
Kojto 170:19eb464bc2be 174 }
Kojto 170:19eb464bc2be 175
Kojto 170:19eb464bc2be 176 /* Output clock on MCO1 pin(PA8) for debugging purpose */
Kojto 170:19eb464bc2be 177 //if (bypass == 0)
Kojto 170:19eb464bc2be 178 //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_2); // 4 MHz
Kojto 170:19eb464bc2be 179 //else
Kojto 170:19eb464bc2be 180 //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz
Kojto 170:19eb464bc2be 181
Kojto 170:19eb464bc2be 182 return 1; // OK
Kojto 170:19eb464bc2be 183 }
Kojto 170:19eb464bc2be 184 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
Kojto 170:19eb464bc2be 185
Kojto 170:19eb464bc2be 186 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
Kojto 170:19eb464bc2be 187 /******************************************************************************/
Kojto 170:19eb464bc2be 188 /* PLL (clocked by HSI) used as System clock source */
Kojto 170:19eb464bc2be 189 /******************************************************************************/
Kojto 170:19eb464bc2be 190 uint8_t SetSysClock_PLL_HSI(void)
Kojto 170:19eb464bc2be 191 {
Kojto 170:19eb464bc2be 192 RCC_ClkInitTypeDef RCC_ClkInitStruct;
Kojto 170:19eb464bc2be 193 RCC_OscInitTypeDef RCC_OscInitStruct;
Kojto 170:19eb464bc2be 194
Kojto 170:19eb464bc2be 195 /* The voltage scaling allows optimizing the power consumption when the device is
Kojto 170:19eb464bc2be 196 clocked below the maximum system frequency, to update the voltage scaling value
Kojto 170:19eb464bc2be 197 regarding system frequency refer to product datasheet. */
Kojto 170:19eb464bc2be 198 __PWR_CLK_ENABLE();
Kojto 170:19eb464bc2be 199 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Kojto 170:19eb464bc2be 200
Kojto 170:19eb464bc2be 201 /* Enable HSI oscillator and activate PLL with HSI as source */
Kojto 170:19eb464bc2be 202 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Kojto 170:19eb464bc2be 203 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
Kojto 170:19eb464bc2be 204 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Kojto 170:19eb464bc2be 205 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Kojto 170:19eb464bc2be 206 // SYSCLK = 32 MHz (16 MHz *6 /3)
Kojto 170:19eb464bc2be 207 // USBCLK = 48 MHz (16 MHz *6 /2)
Kojto 170:19eb464bc2be 208 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Kojto 170:19eb464bc2be 209 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Kojto 170:19eb464bc2be 210 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
Kojto 170:19eb464bc2be 211 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
Kojto 170:19eb464bc2be 212 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Kojto 170:19eb464bc2be 213 return 0; // FAIL
Kojto 170:19eb464bc2be 214 }
Kojto 170:19eb464bc2be 215
Kojto 170:19eb464bc2be 216 /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
Kojto 170:19eb464bc2be 217 while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};
Kojto 170:19eb464bc2be 218
Kojto 170:19eb464bc2be 219 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
Kojto 170:19eb464bc2be 220 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
Kojto 170:19eb464bc2be 221 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz
Kojto 170:19eb464bc2be 222 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz
Kojto 170:19eb464bc2be 223 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz
Kojto 170:19eb464bc2be 224 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz
Kojto 170:19eb464bc2be 225 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
Kojto 170:19eb464bc2be 226 return 0; // FAIL
Kojto 170:19eb464bc2be 227 }
Kojto 170:19eb464bc2be 228
Kojto 170:19eb464bc2be 229 /* Output clock on MCO1 pin(PA8) for debugging purpose */
Kojto 170:19eb464bc2be 230 //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz
Kojto 170:19eb464bc2be 231
Kojto 170:19eb464bc2be 232 return 1; // OK
Kojto 170:19eb464bc2be 233 }
Kojto 170:19eb464bc2be 234 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */