|
| 1 | +/* |
| 2 | + ****************************************************************************** |
| 3 | + * @attention |
| 4 | + * |
| 5 | + * <h2><center>© Copyright (c) 2019 STMicroelectronics. |
| 6 | + * All rights reserved.</center></h2> |
| 7 | + * |
| 8 | + * This software component is licensed by ST under BSD 3-Clause license, |
| 9 | + * the "License"; You may not use this file except in compliance with the |
| 10 | + * License. You may obtain a copy of the License at: |
| 11 | + * opensource.org/licenses/BSD-3-Clause |
| 12 | + * |
| 13 | + ****************************************************************************** |
| 14 | +*/ |
| 15 | + |
| 16 | +/** |
| 17 | + * This file configures the system clock as follows: |
| 18 | + *-------------------------------------------------------------------- |
| 19 | + * System clock source | 1- USE_PLL_HSE_EXTC (external 8 MHz clock) |
| 20 | + * | 2- USE_PLL_HSE_XTAL (external 8 MHz xtal) |
| 21 | + * | 3- USE_PLL_HSI (internal 64 MHz clock) |
| 22 | + *-------------------------------------------------------------------- |
| 23 | + * SYSCLK(MHz) | 480 |
| 24 | + * AHBCLK (MHz) | 240 |
| 25 | + * APB1CLK (MHz) | 120 |
| 26 | + * APB2CLK (MHz) | 120 |
| 27 | + * APB3CLK (MHz) | 120 |
| 28 | + * APB4CLK (MHz) | 120 |
| 29 | + * USB capable (48 MHz) | YES |
| 30 | + *-------------------------------------------------------------------- |
| 31 | +**/ |
| 32 | + |
| 33 | +#include "stm32h7xx.h" |
| 34 | +#include "nvic_addr.h" |
| 35 | +#include "mbed_error.h" |
| 36 | + |
| 37 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 38 | + Internal SRAM. */ |
| 39 | +/* #define VECT_TAB_SRAM */ |
| 40 | +#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. |
| 41 | + This value must be a multiple of 0x200. */ |
| 42 | + |
| 43 | +// clock source is selected with CLOCK_SOURCE in json config |
| 44 | +#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) |
| 45 | +#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default) |
| 46 | +#define USE_PLL_HSI 0x2 // Use HSI internal clock |
| 47 | + |
| 48 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 49 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass, bool lowspeed); |
| 50 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 51 | + |
| 52 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 53 | +uint8_t SetSysClock_PLL_HSI(void); |
| 54 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 55 | + |
| 56 | +/** |
| 57 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 58 | + * AHB/APBx prescalers and Flash settings |
| 59 | + * @note This function should be called only once the RCC clock configuration |
| 60 | + * is reset to the default reset state (done in SystemInit() function). |
| 61 | + * @param None |
| 62 | + * @retval None |
| 63 | + */ |
| 64 | + |
| 65 | +void SetSysClock(void) |
| 66 | +{ |
| 67 | + |
| 68 | + bool lowspeed = false; |
| 69 | +#if defined(LOWSPEED) && (LOWSPEED == 1) |
| 70 | + lowspeed = true; |
| 71 | +#endif |
| 72 | + |
| 73 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) |
| 74 | + /* 1- Try to start with HSE and external clock (MCO from STLink PCB part) */ |
| 75 | + if (SetSysClock_PLL_HSE(1, lowspeed) == 0) |
| 76 | +#endif |
| 77 | + { |
| 78 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) |
| 79 | + /* 2- If fail try to start with HSE and external xtal */ |
| 80 | + if (SetSysClock_PLL_HSE(0, lowspeed) == 0) |
| 81 | +#endif |
| 82 | + { |
| 83 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 84 | + /* 3- If fail start with HSI clock */ |
| 85 | + if (SetSysClock_PLL_HSI() == 0) |
| 86 | +#endif |
| 87 | + { |
| 88 | + error("SetSysClock failed\n"); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +static const uint32_t _keep; |
| 95 | +bool isBootloader() { |
| 96 | + return ((uint32_t)&_keep < 0x8040000); |
| 97 | +} |
| 98 | + |
| 99 | +bool isBetaBoard() { |
| 100 | + uint8_t* bootloader_data = (uint8_t*)(0x801F000); |
| 101 | + if (bootloader_data[0] != 0xA0 || bootloader_data[1] < 14) { |
| 102 | + return true; |
| 103 | + } else { |
| 104 | + return (bootloader_data[10] == 27); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 109 | +/******************************************************************************/ |
| 110 | +/* PLL (clocked by HSE) used as System clock source */ |
| 111 | +/******************************************************************************/ |
| 112 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass, bool lowspeed) |
| 113 | +{ |
| 114 | + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 115 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 116 | + RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
| 117 | + |
| 118 | + // If we are reconfiguring the clock, select CSI as system clock source to allow modification of the PLL configuration |
| 119 | + if (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) { |
| 120 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; |
| 121 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_CSI; |
| 122 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
| 123 | + { |
| 124 | + return 0; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /* Enable oscillator pin */ |
| 129 | + __HAL_RCC_GPIOH_CLK_ENABLE(); |
| 130 | + GPIO_InitTypeDef gpio_osc_init_structure; |
| 131 | + gpio_osc_init_structure.Pin = GPIO_PIN_1; |
| 132 | + gpio_osc_init_structure.Mode = GPIO_MODE_OUTPUT_PP; |
| 133 | + gpio_osc_init_structure.Pull = GPIO_PULLUP; |
| 134 | + gpio_osc_init_structure.Speed = GPIO_SPEED_FREQ_LOW; |
| 135 | + HAL_GPIO_Init(GPIOH, &gpio_osc_init_structure); |
| 136 | + HAL_Delay(10); |
| 137 | + HAL_GPIO_WritePin(GPIOH, GPIO_PIN_1, 1); |
| 138 | + |
| 139 | + /* Supply configuration update enable */ |
| 140 | +#if HSE_VALUE == 27000000 |
| 141 | + HAL_PWREx_ConfigSupply(PWR_SMPS_1V8_SUPPLIES_EXT); |
| 142 | +#else |
| 143 | + HAL_PWREx_ConfigSupply(PWR_SMPS_1V8_SUPPLIES_LDO); |
| 144 | +#endif |
| 145 | + /* Configure the main internal regulator output voltage */ |
| 146 | + |
| 147 | + if (lowspeed) { |
| 148 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3); |
| 149 | + } else { |
| 150 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 151 | + } |
| 152 | + |
| 153 | + while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {} |
| 154 | + |
| 155 | + /* Enable HSE Oscillator and activate PLL with HSE as source */ |
| 156 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI48; |
| 157 | + if (bypass) { |
| 158 | + RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; |
| 159 | + } else { |
| 160 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
| 161 | + } |
| 162 | + RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
| 163 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 164 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 165 | + RCC_OscInitStruct.PLL.PLLM = 5; |
| 166 | + if (lowspeed) { |
| 167 | + RCC_OscInitStruct.PLL.PLLN = 40; |
| 168 | + } else { |
| 169 | + RCC_OscInitStruct.PLL.PLLN = 160; |
| 170 | + } |
| 171 | + |
| 172 | +#if HSE_VALUE == 27000000 |
| 173 | + RCC_OscInitStruct.PLL.PLLM = 9; |
| 174 | + if (lowspeed) { |
| 175 | + RCC_OscInitStruct.PLL.PLLN = 80; |
| 176 | + } else { |
| 177 | + RCC_OscInitStruct.PLL.PLLN = 300; |
| 178 | + } |
| 179 | +#endif |
| 180 | + |
| 181 | + RCC_OscInitStruct.PLL.PLLFRACN = 0; |
| 182 | + RCC_OscInitStruct.PLL.PLLP = 2; |
| 183 | + RCC_OscInitStruct.PLL.PLLR = 2; |
| 184 | + RCC_OscInitStruct.PLL.PLLQ = 10; |
| 185 | + RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE; |
| 186 | + RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2; |
| 187 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 188 | + return 0; // FAIL |
| 189 | + } |
| 190 | + |
| 191 | + /* Select PLL as system clock source and configure bus clocks dividers */ |
| 192 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | |
| 193 | + RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | |
| 194 | + RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_D3PCLK1; |
| 195 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 196 | + RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; |
| 197 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2; |
| 198 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2; |
| 199 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2; |
| 200 | + RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2; |
| 201 | + RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2; |
| 202 | + if (lowspeed) { |
| 203 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) |
| 204 | + return 0; // FAIL |
| 205 | + } else { |
| 206 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) |
| 207 | + return 0; // FAIL |
| 208 | + } |
| 209 | + |
| 210 | + // HAL_RCCEx_EnableBootCore(RCC_BOOT_C2); |
| 211 | + |
| 212 | +#if DEVICE_USBDEVICE |
| 213 | + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; |
| 214 | + PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 215 | + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
| 216 | + return 0; // FAIL |
| 217 | + } |
| 218 | + |
| 219 | + HAL_PWREx_EnableUSBVoltageDetector(); |
| 220 | +#endif /* DEVICE_USBDEVICE */ |
| 221 | + |
| 222 | + __HAL_RCC_CSI_ENABLE() ; |
| 223 | + |
| 224 | + __HAL_RCC_SYSCFG_CLK_ENABLE() ; |
| 225 | + |
| 226 | + HAL_EnableCompensationCell(); |
| 227 | + |
| 228 | + return 1; // OK |
| 229 | +} |
| 230 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 231 | + |
| 232 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 233 | +/******************************************************************************/ |
| 234 | +/* PLL (clocked by HSI) used as System clock source */ |
| 235 | +/******************************************************************************/ |
| 236 | +uint8_t SetSysClock_PLL_HSI(void) |
| 237 | +{ |
| 238 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 239 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 240 | + |
| 241 | + /* Supply configuration update enable */ |
| 242 | +#if HSE_VALUE == 27000000 |
| 243 | + HAL_PWREx_ConfigSupply(PWR_SMPS_1V8_SUPPLIES_EXT); |
| 244 | +#else |
| 245 | + HAL_PWREx_ConfigSupply(PWR_SMPS_1V8_SUPPLIES_LDO); |
| 246 | +#endif |
| 247 | + |
| 248 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 249 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 250 | + regarding system frequency refer to product datasheet. */ |
| 251 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 252 | + while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {} |
| 253 | + |
| 254 | + // Enable HSI oscillator and activate PLL with HSI as source |
| 255 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_CSI; |
| 256 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 257 | + RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
| 258 | + RCC_OscInitStruct.CSIState = RCC_CSI_OFF; |
| 259 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 260 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 261 | + RCC_OscInitStruct.PLL.PLLM = 8; |
| 262 | + RCC_OscInitStruct.PLL.PLLN = 100; |
| 263 | + RCC_OscInitStruct.PLL.PLLP = 2; |
| 264 | + RCC_OscInitStruct.PLL.PLLQ = 10; |
| 265 | + RCC_OscInitStruct.PLL.PLLR = 2; |
| 266 | + RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE; |
| 267 | + RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3; |
| 268 | + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
| 269 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 270 | + return 0; // FAIL |
| 271 | + } |
| 272 | + |
| 273 | + /* Select PLL as system clock source and configure bus clocks dividers */ |
| 274 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | \ |
| 275 | + RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1); |
| 276 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 277 | + RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; |
| 278 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2; |
| 279 | + RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2; |
| 280 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2; |
| 281 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2; |
| 282 | + RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2; |
| 283 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { |
| 284 | + return 0; // FAIL |
| 285 | + } |
| 286 | + |
| 287 | + return 1; // OK |
| 288 | +} |
| 289 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 290 | + |
| 291 | +#if defined (CORE_CM4) |
| 292 | +void HSEM2_IRQHandler(void) |
| 293 | +{ |
| 294 | + HAL_HSEM_IRQHandler(); |
| 295 | +} |
| 296 | +#endif |
| 297 | + |
| 298 | +#if defined (CORE_CM7) |
| 299 | +void HSEM1_IRQHandler(void) |
| 300 | +{ |
| 301 | + HAL_HSEM_IRQHandler(); |
| 302 | +} |
| 303 | +#endif |
0 commit comments