|
| 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 depending on config from targets.json: |
| 18 | + *----------------------------------------------------------------------------- |
| 19 | + * System clock source | 1- USE_PLL_HSE_EXTC (external clock) |
| 20 | + * | 2- USE_PLL_HSE_XTAL (external xtal) |
| 21 | + * | 3- USE_PLL_HSI (internal 16 MHz) |
| 22 | + * | 4- USE_PLL_MSI (internal 100kHz to 48 MHz) |
| 23 | + *----------------------------------------------------------------------------- |
| 24 | + * SYSCLK(MHz) | 160 |
| 25 | + * AHBCLK (MHz) | 160 |
| 26 | + * APB1CLK (MHz) | 160 |
| 27 | + * APB2CLK (MHz) | 160 |
| 28 | + * APB3CLK (MHz) | 160 |
| 29 | + * USB capable | TODO |
| 30 | + *----------------------------------------------------------------------------- |
| 31 | +**/ |
| 32 | + |
| 33 | +#include "stm32u5xx.h" |
| 34 | +#include "mbed_error.h" |
| 35 | + |
| 36 | +// clock source is selected with CLOCK_SOURCE in json config |
| 37 | +#define USE_PLL_HSE_EXTC 0x8 // Use external clock (OSC_IN) |
| 38 | +#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (OSC_IN/OSC_OUT) |
| 39 | +#define USE_PLL_HSI 0x2 // Use HSI internal clock |
| 40 | +#define USE_PLL_MSI 0x1 // Use MSI internal clock |
| 41 | + |
| 42 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 43 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 44 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 45 | + |
| 46 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 47 | +uint8_t SetSysClock_PLL_HSI(void); |
| 48 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 49 | + |
| 50 | +#if ((CLOCK_SOURCE) & USE_PLL_MSI) |
| 51 | +uint8_t SetSysClock_PLL_MSI(void); |
| 52 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_MSI) */ |
| 53 | + |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 57 | + * AHB/APBx prescalers and Flash settings |
| 58 | + * @note This function is called in mbed_sdk_init() function (targets/TARGET_STM/mbed_overrides.c) |
| 59 | + * and after each deepsleep period in hal_deepsleep() (targets/TARGET_STM/sleep.c) |
| 60 | + * @param None |
| 61 | + * @retval None |
| 62 | + */ |
| 63 | + |
| 64 | +MBED_WEAK void SetSysClock(void) |
| 65 | +{ |
| 66 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) |
| 67 | + /* 1- Try to start with HSE and external clock */ |
| 68 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 69 | +#endif |
| 70 | + { |
| 71 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) |
| 72 | + /* 2- If fail try to start with HSE and external xtal */ |
| 73 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 74 | +#endif |
| 75 | + { |
| 76 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 77 | + /* 3- If fail start with HSI clock */ |
| 78 | + if (SetSysClock_PLL_HSI() == 0) |
| 79 | +#endif |
| 80 | + { |
| 81 | +#if ((CLOCK_SOURCE) & USE_PLL_MSI) |
| 82 | + /* 4- If fail start with MSI clock */ |
| 83 | + if (SetSysClock_PLL_MSI() == 0) |
| 84 | +#endif |
| 85 | + { |
| 86 | + { |
| 87 | + error("SetSysClock failed\n"); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 96 | +/******************************************************************************/ |
| 97 | +/* PLL (clocked by HSE) used as System clock source */ |
| 98 | +/******************************************************************************/ |
| 99 | +MBED_WEAK uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 100 | +{ |
| 101 | + return 0; |
| 102 | +} |
| 103 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 104 | + |
| 105 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 106 | +/******************************************************************************/ |
| 107 | +/* PLL (clocked by HSI) used as System clock source */ |
| 108 | +/******************************************************************************/ |
| 109 | +uint8_t SetSysClock_PLL_HSI(void) |
| 110 | +{ |
| 111 | + return 0; // TODO |
| 112 | +} |
| 113 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 114 | + |
| 115 | +#if ((CLOCK_SOURCE) & USE_PLL_MSI) |
| 116 | +/******************************************************************************/ |
| 117 | +/* PLL (clocked by MSI) used as System clock source */ |
| 118 | +/******************************************************************************/ |
| 119 | +MBED_WEAK uint8_t SetSysClock_PLL_MSI(void) |
| 120 | +{ |
| 121 | + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 122 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 123 | + RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; |
| 124 | + |
| 125 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 126 | + HAL_PWREx_DisableUCPDDeadBattery(); /* Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral */ |
| 127 | + HAL_PWREx_EnableVddA(); |
| 128 | + HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 129 | + |
| 130 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSI |
| 131 | + | RCC_OSCILLATORTYPE_MSI; |
| 132 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 133 | + RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
| 134 | + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
| 135 | + RCC_OscInitStruct.MSIState = RCC_MSI_ON; |
| 136 | + RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; |
| 137 | + RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_0; |
| 138 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 139 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; |
| 140 | + RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV4; |
| 141 | + RCC_OscInitStruct.PLL.PLLM = 3; |
| 142 | + RCC_OscInitStruct.PLL.PLLN = 10; |
| 143 | + RCC_OscInitStruct.PLL.PLLP = 2; |
| 144 | + RCC_OscInitStruct.PLL.PLLQ = 2; |
| 145 | + RCC_OscInitStruct.PLL.PLLR = 1; |
| 146 | + RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_1; |
| 147 | + RCC_OscInitStruct.PLL.PLLFRACN = 0; |
| 148 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 149 | + return 0; // FAIL |
| 150 | + } |
| 151 | + |
| 152 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
| 153 | + | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 |
| 154 | + | RCC_CLOCKTYPE_PCLK3; |
| 155 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 156 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 157 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 158 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 159 | + RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1; |
| 160 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { |
| 161 | + return 0; // FAIL |
| 162 | + } |
| 163 | + |
| 164 | + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RNG; |
| 165 | + PeriphClkInit.RngClockSelection = RCC_RNGCLKSOURCE_HSI48; |
| 166 | + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { |
| 167 | + return 0; // FAIL |
| 168 | + } |
| 169 | + |
| 170 | + HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY); |
| 171 | + HAL_ICACHE_Enable(); |
| 172 | + |
| 173 | + return 1; // OK |
| 174 | +} |
| 175 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_MSI) */ |
0 commit comments