|
| 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 25 MHz clock) |
| 21 | + * | 2- USE_PLL_HSE_XTAL (external 25 MHz xtal) |
| 22 | + * | 3- USE_PLL_HSI (internal 16 MHz clock) |
| 23 | + *-------------------------------------------------------------------- |
| 24 | + * SYSCLK(MHz) | 216 |
| 25 | + * AHBCLK (MHz) | 216 |
| 26 | + * APB1CLK (MHz) | 54 |
| 27 | + * APB2CLK (MHz) | 108 |
| 28 | + * USB capable (48 MHz) | YES |
| 29 | + *-------------------------------------------------------------------- |
| 30 | +**/ |
| 31 | + |
| 32 | +#include "stm32f7xx.h" |
| 33 | +#include "mbed_assert.h" |
| 34 | + |
| 35 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 36 | + Internal SRAM. */ |
| 37 | +/* #define VECT_TAB_SRAM */ |
| 38 | +#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. |
| 39 | + This value must be a multiple of 0x200. */ |
| 40 | + |
| 41 | +// clock source is selected with CLOCK_SOURCE in json config |
| 42 | +#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) |
| 43 | +#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default) |
| 44 | +#define USE_PLL_HSI 0x2 // Use HSI internal clock |
| 45 | + |
| 46 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 47 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 48 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 49 | + |
| 50 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 51 | +uint8_t SetSysClock_PLL_HSI(void); |
| 52 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 53 | + |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief Setup the microcontroller system |
| 57 | + * Initialize the Embedded Flash Interface, the PLL and update the |
| 58 | + * SystemFrequency variable. |
| 59 | + * @param None |
| 60 | + * @retval None |
| 61 | + */ |
| 62 | +void SystemInit(void) |
| 63 | +{ |
| 64 | + /* FPU settings ------------------------------------------------------------*/ |
| 65 | +#if (__FPU_PRESENT == 1) && (__FPU_USED == 1) |
| 66 | + SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ |
| 67 | +#endif |
| 68 | + /* Reset the RCC clock configuration to the default reset state ------------*/ |
| 69 | + /* Set HSION bit */ |
| 70 | + RCC->CR |= (uint32_t)0x00000001; |
| 71 | + |
| 72 | + /* Reset CFGR register */ |
| 73 | + RCC->CFGR = 0x00000000; |
| 74 | + |
| 75 | + /* Reset HSEON, CSSON and PLLON bits */ |
| 76 | + RCC->CR &= (uint32_t)0xFEF6FFFF; |
| 77 | + |
| 78 | + /* Reset PLLCFGR register */ |
| 79 | + RCC->PLLCFGR = 0x24003010; |
| 80 | + |
| 81 | + /* Reset HSEBYP bit */ |
| 82 | + RCC->CR &= (uint32_t)0xFFFBFFFF; |
| 83 | + |
| 84 | + /* Disable all interrupts */ |
| 85 | + RCC->CIR = 0x00000000; |
| 86 | + |
| 87 | +#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) |
| 88 | + SystemInit_ExtMemCtl(); |
| 89 | +#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ |
| 90 | + |
| 91 | + /* Configure the Vector Table location add offset address ------------------*/ |
| 92 | +#ifdef VECT_TAB_SRAM |
| 93 | + SCB->VTOR = RAMDTCM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ |
| 94 | +#else |
| 95 | + SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ |
| 96 | +#endif |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 102 | + * AHB/APBx prescalers and Flash settings |
| 103 | + * @note This function should be called only once the RCC clock configuration |
| 104 | + * is reset to the default reset state (done in SystemInit() function). |
| 105 | + * @param None |
| 106 | + * @retval None |
| 107 | + */ |
| 108 | + |
| 109 | +void SetSysClock(void) |
| 110 | +{ |
| 111 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) |
| 112 | + /* 1- Try to start with HSE and external clock */ |
| 113 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 114 | +#endif |
| 115 | + { |
| 116 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) |
| 117 | + /* 2- If fail try to start with HSE and external xtal */ |
| 118 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 119 | +#endif |
| 120 | + { |
| 121 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 122 | + /* 3- If fail start with HSI clock */ |
| 123 | + if (SetSysClock_PLL_HSI() == 0) |
| 124 | +#endif |
| 125 | + { |
| 126 | + while(1) { |
| 127 | + MBED_ASSERT(1); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Output clock on MCO2 pin(PC9) for debugging purpose |
| 134 | + // Can be visualized on uSD card CN3 connector pin 8 |
| 135 | + //HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_4); // 216 MHz / 4 = 54 MHz |
| 136 | +} |
| 137 | + |
| 138 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 139 | +/******************************************************************************/ |
| 140 | +/* PLL (clocked by HSE) used as System clock source */ |
| 141 | +/******************************************************************************/ |
| 142 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 143 | +{ |
| 144 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 145 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 146 | + RCC_PeriphCLKInitTypeDef RCC_PeriphClkInitStruct; |
| 147 | + |
| 148 | + // Enable power clock |
| 149 | + __PWR_CLK_ENABLE(); |
| 150 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 151 | + |
| 152 | + // Enable HSE oscillator and activate PLL with HSE as source |
| 153 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 154 | + if (bypass == 0) { |
| 155 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External xtal on OSC_IN/OSC_OUT */ |
| 156 | + } else { |
| 157 | + RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External clock on OSC_IN */ |
| 158 | + } |
| 159 | + // Warning: this configuration is for a 25 MHz xtal clock only |
| 160 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 161 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 162 | + RCC_OscInitStruct.PLL.PLLM = 25; // VCO input clock = 1 MHz (25 MHz / 25) |
| 163 | + RCC_OscInitStruct.PLL.PLLN = 432; // VCO output clock = 432 MHz (1 MHz * 432) |
| 164 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLLCLK = 216 MHz (432 MHz / 2) |
| 165 | + RCC_OscInitStruct.PLL.PLLQ = 9; // USB clock = 48 MHz (432 MHz / 9) --> OK for USB |
| 166 | + |
| 167 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 168 | + return 0; // FAIL |
| 169 | + } |
| 170 | + |
| 171 | + // Activate the OverDrive to reach the 216 MHz Frequency |
| 172 | + if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
| 173 | + return 0; // FAIL |
| 174 | + } |
| 175 | + |
| 176 | + // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers |
| 177 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 178 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 216 MHz |
| 179 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 216 MHz |
| 180 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; // 54 MHz |
| 181 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; // 108 MHz |
| 182 | + |
| 183 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK) { |
| 184 | + return 0; // FAIL |
| 185 | + } |
| 186 | + |
| 187 | + RCC_PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CLK48; |
| 188 | + RCC_PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL; |
| 189 | + if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInitStruct) != HAL_OK) { |
| 190 | + return 0; // FAIL |
| 191 | + } |
| 192 | + |
| 193 | + return 1; // OK |
| 194 | +} |
| 195 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 196 | + |
| 197 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 198 | +/******************************************************************************/ |
| 199 | +/* PLL (clocked by HSI) used as System clock source */ |
| 200 | +/******************************************************************************/ |
| 201 | +uint8_t SetSysClock_PLL_HSI(void) |
| 202 | +{ |
| 203 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 204 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 205 | + RCC_PeriphCLKInitTypeDef RCC_PeriphClkInitStruct; |
| 206 | + |
| 207 | + // Enable power clock |
| 208 | + __PWR_CLK_ENABLE(); |
| 209 | + |
| 210 | + // Enable HSI oscillator and activate PLL with HSI as source |
| 211 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
| 212 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 213 | + RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
| 214 | + RCC_OscInitStruct.HSICalibrationValue = 16; |
| 215 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 216 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 217 | + RCC_OscInitStruct.PLL.PLLM = 8; // VCO input clock = 2 MHz (16 MHz / 8) |
| 218 | + RCC_OscInitStruct.PLL.PLLN = 216; // VCO output clock = 432 MHz (2 MHz * 216) |
| 219 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLLCLK = 216 MHz (432 MHz / 2) |
| 220 | + RCC_OscInitStruct.PLL.PLLQ = 9; |
| 221 | + |
| 222 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 223 | + return 0; // FAIL |
| 224 | + } |
| 225 | + |
| 226 | + // Activate the OverDrive to reach the 216 MHz Frequency |
| 227 | + if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
| 228 | + return 0; // FAIL |
| 229 | + } |
| 230 | + |
| 231 | + // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers |
| 232 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 233 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 216 MHz |
| 234 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 216 MHz |
| 235 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; // 54 MHz |
| 236 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; // 108 MHz |
| 237 | + |
| 238 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK) { |
| 239 | + return 0; // FAIL |
| 240 | + } |
| 241 | + |
| 242 | + RCC_PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CLK48; |
| 243 | + RCC_PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL; |
| 244 | + if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInitStruct) != HAL_OK) { |
| 245 | + return 0; // FAIL |
| 246 | + } |
| 247 | + |
| 248 | + return 1; // OK |
| 249 | +} |
| 250 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
0 commit comments