|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * SPDX-License-Identifier: BSD-3-Clause |
| 3 | + ****************************************************************************** |
| 4 | + * |
| 5 | + * Copyright (c) 2015-2021 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) | DEVICE_USBDEVICE=1 |
| 20 | + * | 2- USE_PLL_HSE_XTAL (external 8 MHz xtal) | |
| 21 | + * | 3- USE_PLL_HSI (internal 16 MHz) | |
| 22 | + *----------------------------------------------------------------------------- |
| 23 | + * SYSCLK(MHz) | 100 | 96 |
| 24 | + * AHBCLK (MHz) | 100 | 96 |
| 25 | + * APB1CLK (MHz) | 50 | 48 |
| 26 | + * APB2CLK (MHz) | 100 | 96 |
| 27 | + * USB capable | NO | YES |
| 28 | + *----------------------------------------------------------------------------- |
| 29 | +**/ |
| 30 | + |
| 31 | +#include "stm32f4xx.h" |
| 32 | +#include "mbed_error.h" |
| 33 | + |
| 34 | +// clock source is selected with CLOCK_SOURCE in json config |
| 35 | +#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) |
| 36 | +#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default) |
| 37 | +#define USE_PLL_HSI 0x2 // Use HSI internal clock |
| 38 | + |
| 39 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 40 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 41 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 42 | + |
| 43 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 44 | +uint8_t SetSysClock_PLL_HSI(void); |
| 45 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | +* @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 50 | +* AHB/APBx prescalers and Flash settings |
| 51 | +* @note This function should be called only once the RCC clock configuration |
| 52 | +* is reset to the default reset state (done in SystemInit() function). |
| 53 | + * @param None |
| 54 | + * @retval None |
| 55 | + */ |
| 56 | + |
| 57 | +MBED_WEAK void SetSysClock(void) |
| 58 | +{ |
| 59 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) |
| 60 | + /* 1- Try to start with HSE and external clock */ |
| 61 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 62 | +#endif |
| 63 | + { |
| 64 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) |
| 65 | + /* 2- If fail try to start with HSE and external xtal */ |
| 66 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 67 | +#endif |
| 68 | + { |
| 69 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 70 | + /* 3- If fail start with HSI clock */ |
| 71 | + if (SetSysClock_PLL_HSI() == 0) |
| 72 | +#endif |
| 73 | + { |
| 74 | + { |
| 75 | + error("SetSysClock failed\n"); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /* Output clock on MCO2 pin(PC9) for debugging purpose */ |
| 82 | + //HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_4); |
| 83 | +} |
| 84 | + |
| 85 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 86 | +/******************************************************************************/ |
| 87 | +/* PLL (clocked by HSE) used as System clock source */ |
| 88 | +/******************************************************************************/ |
| 89 | +MBED_WEAK uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 90 | +{ |
| 91 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 92 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 93 | + |
| 94 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 95 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 96 | + regarding system frequency refer to product datasheet. */ |
| 97 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 98 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); |
| 99 | + |
| 100 | + /* Get the Clocks configuration according to the internal RCC registers */ |
| 101 | + HAL_RCC_GetOscConfig(&RCC_OscInitStruct); |
| 102 | + |
| 103 | + /* PLL could be already configured by bootlader */ |
| 104 | + if (RCC_OscInitStruct.PLL.PLLState != RCC_PLL_ON) { |
| 105 | + |
| 106 | + // Enable HSE oscillator and activate PLL with HSE as source |
| 107 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 108 | + if (bypass == 0) { |
| 109 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; // External 8 MHz xtal on OSC_IN/OSC_OUT |
| 110 | + } else { |
| 111 | + RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; // External 8 MHz clock on OSC_IN |
| 112 | + } |
| 113 | + |
| 114 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 115 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 116 | + RCC_OscInitStruct.PLL.PLLM = HSE_VALUE / 1000000; // VCO input clock = 2 MHz (8 MHz / 4) |
| 117 | +#if (DEVICE_USBDEVICE) |
| 118 | + RCC_OscInitStruct.PLL.PLLN = 192; // VCO output clock = 384 MHz (2 MHz * 192) |
| 119 | +#else /* DEVICE_USBDEVICE */ |
| 120 | + RCC_OscInitStruct.PLL.PLLN = 200; // VCO output clock = 400 MHz (2 MHz * 200) |
| 121 | +#endif /* DEVICE_USBDEVICE */ |
| 122 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLLCLK = 100 MHz or 96 MHz (depending on DEVICE_USBDEVICE) |
| 123 | + RCC_OscInitStruct.PLL.PLLQ = 8; // USB clock = 48 MHz (DEVICE_USBDEVICE=1) |
| 124 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 125 | + return 0; // FAIL |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers |
| 130 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
| 131 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 100/96 MHz |
| 132 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 100/96 MHz |
| 133 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 50/48 MHz |
| 134 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 100/96 MHz |
| 135 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { |
| 136 | + return 0; // FAIL |
| 137 | + } |
| 138 | + |
| 139 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 140 | + //if (bypass == 0) |
| 141 | + // HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_2); // 4 MHz with xtal |
| 142 | + //else |
| 143 | + // HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz with external clock |
| 144 | + |
| 145 | + return 1; // OK |
| 146 | +} |
| 147 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 148 | + |
| 149 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 150 | +/******************************************************************************/ |
| 151 | +/* PLL (clocked by HSI) used as System clock source */ |
| 152 | +/******************************************************************************/ |
| 153 | +uint8_t SetSysClock_PLL_HSI(void) |
| 154 | +{ |
| 155 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 156 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 157 | + |
| 158 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 159 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 160 | + regarding system frequency refer to product datasheet. */ |
| 161 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 162 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); |
| 163 | + |
| 164 | + // Enable HSI oscillator and activate PLL with HSI as source |
| 165 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE; |
| 166 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 167 | + RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
| 168 | + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
| 169 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 170 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 171 | + RCC_OscInitStruct.PLL.PLLM = 8; // VCO input clock = 2 MHz (16 MHz / 8) |
| 172 | +#if (DEVICE_USBDEVICE) |
| 173 | + RCC_OscInitStruct.PLL.PLLN = 192; // VCO output clock = 384 MHz (2 MHz * 192) |
| 174 | +#else /* DEVICE_USBDEVICE */ |
| 175 | + RCC_OscInitStruct.PLL.PLLN = 200; // VCO output clock = 400 MHz (2 MHz * 200) |
| 176 | +#endif /* DEVICE_USBDEVICE */ |
| 177 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; // PLLCLK = 100 MHz or 96 MHz (depending on DEVICE_USBDEVICE) |
| 178 | + RCC_OscInitStruct.PLL.PLLQ = 8; // USB clock = 48 MHz (DEVICE_USBDEVICE=1) |
| 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, PCLK1 and PCLK2 clocks dividers */ |
| 184 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 185 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 186 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 187 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
| 188 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 189 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { |
| 190 | + return 0; // FAIL |
| 191 | + } |
| 192 | + |
| 193 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 194 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz |
| 195 | + |
| 196 | + return 1; // OK |
| 197 | +} |
| 198 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
0 commit comments