|
| 1 | +/* mbed Microcontroller Library |
| 2 | +* Copyright (c) 2006-2017 ARM Limited |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/** |
| 18 | + * This file configures the system clock as follows: |
| 19 | + *----------------------------------------------------------------- |
| 20 | + * System clock source | 1- USE_PLL_HSE_EXTC (external 8 MHz clock) |
| 21 | + * | 2- USE_PLL_HSE_XTAL (external 8 MHz xtal) |
| 22 | + * | 3- USE_PLL_HSI (internal 8 MHz) |
| 23 | + *----------------------------------------------------------------- |
| 24 | + * SYSCLK(MHz) | 48 |
| 25 | + * AHBCLK (MHz) | 48 |
| 26 | + * APB1CLK (MHz) | 48 |
| 27 | + * USB capable | NO |
| 28 | + *----------------------------------------------------------------- |
| 29 | + */ |
| 30 | + |
| 31 | +#include "stm32f0xx.h" |
| 32 | +#include "mbed_assert.h" |
| 33 | + |
| 34 | +#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) |
| 35 | +#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board) |
| 36 | +#define USE_PLL_HSI 0x2 // Use HSI internal clock |
| 37 | + |
| 38 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 39 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 40 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 41 | + |
| 42 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 43 | +uint8_t SetSysClock_PLL_HSI(void); |
| 44 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 45 | + |
| 46 | + |
| 47 | +/** |
| 48 | + * @brief Setup the microcontroller system. |
| 49 | + * Initialize the default HSI clock source, vector table location and the PLL configuration is reset. |
| 50 | + * @param None |
| 51 | + * @retval None |
| 52 | + */ |
| 53 | +void SystemInit(void) |
| 54 | +{ |
| 55 | + /* Reset the RCC clock configuration to the default reset state ------------*/ |
| 56 | + /* Set HSION bit */ |
| 57 | + RCC->CR |= (uint32_t)0x00000001U; |
| 58 | + |
| 59 | +#if defined (STM32F051x8) || defined (STM32F058x8) |
| 60 | + /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */ |
| 61 | + RCC->CFGR &= (uint32_t)0xF8FFB80CU; |
| 62 | +#else |
| 63 | + /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE, MCOSEL[2:0], MCOPRE[2:0] and PLLNODIV bits */ |
| 64 | + RCC->CFGR &= (uint32_t)0x08FFB80CU; |
| 65 | +#endif /* STM32F051x8 or STM32F058x8 */ |
| 66 | + |
| 67 | + /* Reset HSEON, CSSON and PLLON bits */ |
| 68 | + RCC->CR &= (uint32_t)0xFEF6FFFFU; |
| 69 | + |
| 70 | + /* Reset HSEBYP bit */ |
| 71 | + RCC->CR &= (uint32_t)0xFFFBFFFFU; |
| 72 | + |
| 73 | + /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */ |
| 74 | + RCC->CFGR &= (uint32_t)0xFFC0FFFFU; |
| 75 | + |
| 76 | + /* Reset PREDIV[3:0] bits */ |
| 77 | + RCC->CFGR2 &= (uint32_t)0xFFFFFFF0U; |
| 78 | + |
| 79 | +#if defined (STM32F072xB) || defined (STM32F078xx) |
| 80 | + /* Reset USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW, USBSW and ADCSW bits */ |
| 81 | + RCC->CFGR3 &= (uint32_t)0xFFFCFE2CU; |
| 82 | +#elif defined (STM32F071xB) |
| 83 | + /* Reset USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */ |
| 84 | + RCC->CFGR3 &= (uint32_t)0xFFFFCEACU; |
| 85 | +#elif defined (STM32F091xC) || defined (STM32F098xx) |
| 86 | + /* Reset USART3SW[1:0], USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */ |
| 87 | + RCC->CFGR3 &= (uint32_t)0xFFF0FEACU; |
| 88 | +#elif defined (STM32F030x6) || defined (STM32F030x8) || defined (STM32F031x6) || defined (STM32F038xx) || defined (STM32F030xC) |
| 89 | + /* Reset USART1SW[1:0], I2C1SW and ADCSW bits */ |
| 90 | + RCC->CFGR3 &= (uint32_t)0xFFFFFEECU; |
| 91 | +#elif defined (STM32F051x8) || defined (STM32F058xx) |
| 92 | + /* Reset USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */ |
| 93 | + RCC->CFGR3 &= (uint32_t)0xFFFFFEACU; |
| 94 | +#elif defined (STM32F042x6) || defined (STM32F048xx) |
| 95 | + /* Reset USART1SW[1:0], I2C1SW, CECSW, USBSW and ADCSW bits */ |
| 96 | + RCC->CFGR3 &= (uint32_t)0xFFFFFE2CU; |
| 97 | +#elif defined (STM32F070x6) || defined (STM32F070xB) |
| 98 | + /* Reset USART1SW[1:0], I2C1SW, USBSW and ADCSW bits */ |
| 99 | + RCC->CFGR3 &= (uint32_t)0xFFFFFE6CU; |
| 100 | + /* Set default USB clock to PLLCLK, since there is no HSI48 */ |
| 101 | + RCC->CFGR3 |= (uint32_t)0x00000080U; |
| 102 | +#else |
| 103 | +#warning "No target selected" |
| 104 | +#endif |
| 105 | + |
| 106 | + /* Reset HSI14 bit */ |
| 107 | + RCC->CR2 &= (uint32_t)0xFFFFFFFEU; |
| 108 | + |
| 109 | + /* Disable all interrupts */ |
| 110 | + RCC->CIR = 0x00000000U; |
| 111 | + |
| 112 | + /* Enable SYSCFGENR in APB2EN, needed for 1st call of NVIC_SetVector, to copy vectors from flash to ram */ |
| 113 | + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 118 | + * AHB/APBx prescalers and Flash settings |
| 119 | + * @note This function should be called only once the RCC clock configuration |
| 120 | + * is reset to the default reset state (done in SystemInit() function). |
| 121 | + * @param None |
| 122 | + * @retval None |
| 123 | + */ |
| 124 | +void SetSysClock(void) |
| 125 | +{ |
| 126 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) |
| 127 | + /* 1- Try to start with HSE and external clock */ |
| 128 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 129 | +#endif |
| 130 | + { |
| 131 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) |
| 132 | + /* 2- If fail try to start with HSE and external xtal */ |
| 133 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 134 | +#endif |
| 135 | + { |
| 136 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 137 | + /* 3- If fail start with HSI clock */ |
| 138 | + if (SetSysClock_PLL_HSI() == 0) |
| 139 | +#endif |
| 140 | + { |
| 141 | + while(1) { |
| 142 | + MBED_ASSERT(1); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Output clock on MCO pin(PA8) for debugging purpose |
| 149 | + //HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_SYSCLK, RCC_MCO_NODIV); // 48 MHz |
| 150 | +} |
| 151 | + |
| 152 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 153 | +/******************************************************************************/ |
| 154 | +/* PLL (clocked by HSE) used as System clock source */ |
| 155 | +/******************************************************************************/ |
| 156 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 157 | +{ |
| 158 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 159 | + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 160 | + |
| 161 | + //Select HSI as system clock source to allow modification of the PLL configuration |
| 162 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; |
| 163 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; |
| 164 | + if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
| 165 | + return 0; // FAIL |
| 166 | + } |
| 167 | + |
| 168 | + // Select HSE oscillator as PLL source |
| 169 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 170 | + if (bypass == 0) { |
| 171 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; // External 8 MHz xtal on OSC_IN/OSC_OUT |
| 172 | + } else { |
| 173 | + RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; // External 8 MHz clock on OSC_IN only |
| 174 | + } |
| 175 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 176 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 177 | + RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2; |
| 178 | + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; |
| 179 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 180 | + return 0; // FAIL |
| 181 | + } |
| 182 | + |
| 183 | + // Select PLL as system clock source and configure the HCLK and PCLK1 clocks dividers |
| 184 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1); |
| 185 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 48 MHz |
| 186 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 48 MHz |
| 187 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 48 MHz |
| 188 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
| 189 | + return 0; // FAIL |
| 190 | + } |
| 191 | + |
| 192 | +// HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV2); // 8/2 = 4 MHz |
| 193 | + |
| 194 | + return 1; // OK |
| 195 | +} |
| 196 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 197 | + |
| 198 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 199 | +/******************************************************************************/ |
| 200 | +/* PLL (clocked by HSI) used as System clock source */ |
| 201 | +/******************************************************************************/ |
| 202 | +uint8_t SetSysClock_PLL_HSI(void) |
| 203 | +{ |
| 204 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 205 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 206 | + |
| 207 | + // Select PLLCLK = 48 MHz ((HSI 8 MHz / 2) * 12) |
| 208 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
| 209 | + RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
| 210 | + RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
| 211 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 212 | + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
| 213 | + RCC_OscInitStruct.HSI14State = RCC_HSI_OFF; |
| 214 | + RCC_OscInitStruct.HSI14CalibrationValue = RCC_HSI14CALIBRATION_DEFAULT; |
| 215 | + RCC_OscInitStruct.HSI48State = RCC_HSI_ON; |
| 216 | + RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
| 217 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 218 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; // HSI div 2 |
| 219 | + RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1; |
| 220 | + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; |
| 221 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 222 | + return 0; // FAIL |
| 223 | + } |
| 224 | + |
| 225 | + // Select PLL as system clock source and configure the HCLK and PCLK1 clocks dividers |
| 226 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1); |
| 227 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 48 MHz |
| 228 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 48 MHz |
| 229 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 48 MHz |
| 230 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
| 231 | + return 0; // FAIL |
| 232 | + } |
| 233 | + |
| 234 | + //HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSI, RCC_MCO_DIV4); // 8/4 = 2 MHz |
| 235 | + |
| 236 | + return 1; // OK |
| 237 | +} |
| 238 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
0 commit comments