|
| 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 64 MHz clock) |
| 23 | + *-------------------------------------------------------------------- |
| 24 | + * SYSCLK(MHz) | 400 |
| 25 | + * AHBCLK (MHz) | 200 |
| 26 | + * APB1CLK (MHz) | 100 |
| 27 | + * APB2CLK (MHz) | 100 |
| 28 | + * APB3CLK (MHz) | 100 |
| 29 | + * APB4CLK (MHz) | 100 |
| 30 | + * USB capable (48 MHz) | YES |
| 31 | + *-------------------------------------------------------------------- |
| 32 | +**/ |
| 33 | + |
| 34 | +#include "stm32h7xx.h" |
| 35 | +#include "nvic_addr.h" |
| 36 | +#include "mbed_error.h" |
| 37 | + |
| 38 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 39 | + Internal SRAM. */ |
| 40 | +/* #define VECT_TAB_SRAM */ |
| 41 | +#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. |
| 42 | + This value must be a multiple of 0x200. */ |
| 43 | + |
| 44 | +// clock source is selected with CLOCK_SOURCE in json config |
| 45 | +#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) |
| 46 | +#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default) |
| 47 | +#define USE_PLL_HSI 0x2 // Use HSI internal clock |
| 48 | + |
| 49 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 50 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 51 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 52 | + |
| 53 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 54 | +uint8_t SetSysClock_PLL_HSI(void); |
| 55 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 56 | + |
| 57 | +/** |
| 58 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 59 | + * AHB/APBx prescalers and Flash settings |
| 60 | + * @note This function should be called only once the RCC clock configuration |
| 61 | + * is reset to the default reset state (done in SystemInit() function). |
| 62 | + * @param None |
| 63 | + * @retval None |
| 64 | + */ |
| 65 | + |
| 66 | +void SetSysClock(void) |
| 67 | +{ |
| 68 | + |
| 69 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) |
| 70 | + /* 1- Try to start with HSE and external clock (MCO from STLink PCB part) */ |
| 71 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 72 | +#endif |
| 73 | + { |
| 74 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) |
| 75 | + /* 2- If fail try to start with HSE and external xtal */ |
| 76 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 77 | +#endif |
| 78 | + { |
| 79 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 80 | + /* 3- If fail start with HSI clock */ |
| 81 | + if (SetSysClock_PLL_HSI() == 0) |
| 82 | +#endif |
| 83 | + { |
| 84 | + error("SetSysClock failed\n"); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 91 | +/******************************************************************************/ |
| 92 | +/* PLL (clocked by HSE) used as System clock source */ |
| 93 | +/******************************************************************************/ |
| 94 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 95 | +{ |
| 96 | + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 97 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 98 | + RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
| 99 | + |
| 100 | + HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY); |
| 101 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 102 | + while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {} |
| 103 | + |
| 104 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 105 | + if (bypass) { |
| 106 | + RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; |
| 107 | + } else { |
| 108 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
| 109 | + } |
| 110 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 111 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 112 | + RCC_OscInitStruct.PLL.PLLM = 5; // 5 MHz |
| 113 | + RCC_OscInitStruct.PLL.PLLN = 192; // 960 MHz |
| 114 | + RCC_OscInitStruct.PLL.PLLP = 2; // PLLCLK = SYSCLK = 480 MHz |
| 115 | + RCC_OscInitStruct.PLL.PLLQ = 116; // PLL1Q used for FDCAN = 10 MHz |
| 116 | + RCC_OscInitStruct.PLL.PLLR = 2; |
| 117 | + RCC_OscInitStruct.PLL.PLLFRACN = 0; |
| 118 | + RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE; |
| 119 | + RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2; |
| 120 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 121 | + return 0; // FAIL |
| 122 | + } |
| 123 | + |
| 124 | + /* Select PLL as system clock source and configure bus clocks dividers */ |
| 125 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | |
| 126 | + RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | |
| 127 | + RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_D3PCLK1; |
| 128 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 129 | + RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; |
| 130 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2; |
| 131 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2; |
| 132 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2; |
| 133 | + RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2; |
| 134 | + RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2; |
| 135 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { |
| 136 | + return 0; // FAIL |
| 137 | + } |
| 138 | + |
| 139 | + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_USB; |
| 140 | + PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1; |
| 141 | + PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 142 | + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
| 143 | + return 0; // FAIL |
| 144 | + } |
| 145 | + |
| 146 | + __HAL_RCC_CSI_ENABLE() ; |
| 147 | + |
| 148 | + __HAL_RCC_SYSCFG_CLK_ENABLE() ; |
| 149 | + |
| 150 | + HAL_EnableCompensationCell(); |
| 151 | + |
| 152 | + return 1; // OK |
| 153 | +} |
| 154 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 155 | + |
| 156 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 157 | +/******************************************************************************/ |
| 158 | +/* PLL (clocked by HSI) used as System clock source */ |
| 159 | +/******************************************************************************/ |
| 160 | +uint8_t SetSysClock_PLL_HSI(void) |
| 161 | +{ |
| 162 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 163 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 164 | + |
| 165 | + /*!< Supply configuration update enable */ |
| 166 | + HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY); |
| 167 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 168 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 169 | + regarding system frequency refer to product datasheet. */ |
| 170 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 171 | + while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {} |
| 172 | + |
| 173 | + // Enable HSI oscillator and activate PLL with HSI as source |
| 174 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_CSI; |
| 175 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 176 | + RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
| 177 | + RCC_OscInitStruct.CSIState = RCC_CSI_OFF; |
| 178 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 179 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 180 | + RCC_OscInitStruct.PLL.PLLM = 8; |
| 181 | + RCC_OscInitStruct.PLL.PLLN = 100; |
| 182 | + RCC_OscInitStruct.PLL.PLLP = 2; |
| 183 | + RCC_OscInitStruct.PLL.PLLQ = 2; |
| 184 | + RCC_OscInitStruct.PLL.PLLR = 2; |
| 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 | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | \ |
| 193 | + RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1); |
| 194 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 195 | + RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; |
| 196 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2; |
| 197 | + RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2; |
| 198 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2; |
| 199 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2; |
| 200 | + RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2; |
| 201 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { |
| 202 | + return 0; // FAIL |
| 203 | + } |
| 204 | + |
| 205 | + return 1; // OK |
| 206 | +} |
| 207 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
0 commit comments