|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * SPDX-License-Identifier: BSD-3-Clause |
| 3 | + ****************************************************************************** |
| 4 | + * |
| 5 | + * Copyright (c) 2019 STMicroelectronics. |
| 6 | + * All rights reserved. |
| 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 16 MHz) |
| 22 | + *----------------------------------------------------------------- |
| 23 | + * SYSCLK(MHz) | 64 |
| 24 | + * AHBCLK (MHz) | 64 |
| 25 | + * APB1CLK (MHz) | 64 |
| 26 | + * USB capable | NO |
| 27 | + *----------------------------------------------------------------- |
| 28 | + */ |
| 29 | + |
| 30 | +#include "stm32g4xx.h" |
| 31 | +#include "mbed_error.h" |
| 32 | + |
| 33 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 34 | + Internal SRAM. */ |
| 35 | +/* #define VECT_TAB_SRAM */ |
| 36 | +#define VECT_TAB_OFFSET 0x00U /*!< Vector Table base offset field. |
| 37 | + This value must be a multiple of 0x100. */ |
| 38 | + |
| 39 | +#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) |
| 40 | +#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board) |
| 41 | +#define USE_PLL_HSI 0x2 // Use HSI internal clock |
| 42 | + |
| 43 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 44 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 45 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 46 | + |
| 47 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 48 | +uint8_t SetSysClock_PLL_HSI(void); |
| 49 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 54 | + * AHB/APBx prescalers and Flash settings |
| 55 | + * @note This function should be called only once the RCC clock configuration |
| 56 | + * is reset to the default reset state (done in SystemInit() function). |
| 57 | + * @param None |
| 58 | + * @retval None |
| 59 | + */ |
| 60 | +void SetSysClock(void) |
| 61 | +{ |
| 62 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) |
| 63 | + /* 1- Try to start with HSE and external clock */ |
| 64 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 65 | +#endif |
| 66 | + { |
| 67 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) |
| 68 | + /* 2- If fail try to start with HSE and external xtal */ |
| 69 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 70 | +#endif |
| 71 | + { |
| 72 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 73 | + /* 3- If fail start with HSI clock */ |
| 74 | + if (SetSysClock_PLL_HSI() == 0) |
| 75 | +#endif |
| 76 | + { |
| 77 | + { |
| 78 | + error("SetSysClock failed\n"); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 85 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); |
| 86 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI48, RCC_MCODIV_1); |
| 87 | +} |
| 88 | + |
| 89 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 90 | +/******************************************************************************/ |
| 91 | +/* PLL (clocked by HSE) used as System clock source */ |
| 92 | +/******************************************************************************/ |
| 93 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 94 | +{ |
| 95 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 96 | + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 97 | + RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = { 0 }; |
| 98 | + |
| 99 | + /** Configure the main internal regulator output voltage |
| 100 | + */ |
| 101 | + HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST); |
| 102 | + /** Initializes the CPU, AHB and APB busses clocks |
| 103 | + */ |
| 104 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 105 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
| 106 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 107 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 108 | + RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6; |
| 109 | + //! 170MHz as a core frequency for FDCAN is not suitable for many frequencies, |
| 110 | + //! as it provides low accuracy. When no FDCAN is used, the full capacity of 170 MHz |
| 111 | + //! should be standard. |
| 112 | +#if DEVICE_CAN |
| 113 | + RCC_OscInitStruct.PLL.PLLN = 80; |
| 114 | +#else |
| 115 | + RCC_OscInitStruct.PLL.PLLN = 85; |
| 116 | +#endif |
| 117 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
| 118 | + RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; |
| 119 | + RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; |
| 120 | + |
| 121 | + // For USB: Default to USB driven by HSI48 Clock. (USB needs a 48 MHz Clock). |
| 122 | + // Code below enables the HSI48 Clock |
| 123 | + // (Sidenote: RNG is also driven (and according to STM specs) verified with the 48MHz HSI48) |
| 124 | + RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_HSI48; |
| 125 | + RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
| 126 | + |
| 127 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 128 | + return 0; // FAIL |
| 129 | + } |
| 130 | + |
| 131 | + // For USB: Default to USB driven by HSI48 Clock. |
| 132 | + // Code below connects USB (and RNG) to the HSI48 Clock. |
| 133 | + RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; |
| 134 | + RCC_PeriphCLKInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 135 | + if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct) != HAL_OK) { |
| 136 | + return 0; // FAIL |
| 137 | + } |
| 138 | + |
| 139 | + /** Initializes the CPU, AHB and APB busses clocks |
| 140 | + */ |
| 141 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
| 142 | + | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
| 143 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 144 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 145 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 146 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 147 | + |
| 148 | + // This is an assumption: Using same Flash Latency as for G474RE |
| 149 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8) != HAL_OK) { |
| 150 | + return 0; // FAIL |
| 151 | + } |
| 152 | + |
| 153 | + return 1; // OK |
| 154 | +} |
| 155 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 156 | + |
| 157 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 158 | +/******************************************************************************/ |
| 159 | +/* PLL (clocked by HSI) used as System clock source */ |
| 160 | +/******************************************************************************/ |
| 161 | +uint8_t SetSysClock_PLL_HSI(void) |
| 162 | +{ |
| 163 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 164 | + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 165 | + RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = { 0 }; |
| 166 | + |
| 167 | + /** Configure the main internal regulator output voltage |
| 168 | + */ |
| 169 | + HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 170 | + /** Initializes the CPU, AHB and APB busses clocks |
| 171 | + */ |
| 172 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
| 173 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 174 | + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
| 175 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 176 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 177 | + RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4; |
| 178 | + RCC_OscInitStruct.PLL.PLLN = 85; |
| 179 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
| 180 | + RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; |
| 181 | + RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; |
| 182 | + RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_HSI48; // Enable HSI48 and feed it to USB/RNG. |
| 183 | + RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; // USB needs - and RNG is verified with - the 48Mhz HSI48. |
| 184 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 185 | + return 0; // FAIL |
| 186 | + } |
| 187 | + RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; // Connect HSI48 clock to USB (and RNG) |
| 188 | + RCC_PeriphCLKInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 189 | + if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct) != HAL_OK) { |
| 190 | + return 0; // FAIL |
| 191 | + } |
| 192 | + |
| 193 | + /** Initializes the CPU, AHB and APB busses clocks |
| 194 | + */ |
| 195 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
| 196 | + | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
| 197 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 198 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 199 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 200 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 201 | + |
| 202 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8) != HAL_OK) { |
| 203 | + return 0; // FAIL |
| 204 | + } |
| 205 | + |
| 206 | + return 1; // OK |
| 207 | +} |
| 208 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
0 commit comments