Skip to content

Commit decc6d3

Browse files
committed
Added common system files from H747 targets
1 parent a1bb4b1 commit decc6d3

File tree

2 files changed

+583
-0
lines changed

2 files changed

+583
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/*
2+
******************************************************************************
3+
* @attention
4+
*
5+
* <h2><center>&copy; 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);
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+
#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC)
68+
/* 1- Try to start with HSE and external clock (MCO from STLink PCB part) */
69+
if (SetSysClock_PLL_HSE(1) == 0)
70+
#endif
71+
{
72+
#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL)
73+
/* 2- If fail try to start with HSE and external xtal */
74+
if (SetSysClock_PLL_HSE(0) == 0)
75+
#endif
76+
{
77+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
78+
/* 3- If fail start with HSI clock */
79+
if (SetSysClock_PLL_HSI() == 0)
80+
#endif
81+
{
82+
error("SetSysClock failed\n");
83+
}
84+
}
85+
}
86+
}
87+
88+
#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
89+
/******************************************************************************/
90+
/* PLL (clocked by HSE) used as System clock source */
91+
/******************************************************************************/
92+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
93+
{
94+
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
95+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
96+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
97+
98+
/* Supply configuration update enable */
99+
HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
100+
/* Configure the main internal regulator output voltage */
101+
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
102+
103+
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
104+
105+
/* Enable HSE Oscillator and activate PLL with HSE as source */
106+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI48;
107+
if (bypass) {
108+
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
109+
} else {
110+
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
111+
}
112+
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
113+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
114+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
115+
RCC_OscInitStruct.PLL.PLLM = 5; // 5 MHz
116+
RCC_OscInitStruct.PLL.PLLN = 192; // 960 MHz
117+
RCC_OscInitStruct.PLL.PLLP = 2; // PLLCLK = SYSCLK = 480 MHz
118+
RCC_OscInitStruct.PLL.PLLQ = 116; // PLL1Q used for FDCAN = 10 MHz
119+
RCC_OscInitStruct.PLL.PLLR = 2;
120+
RCC_OscInitStruct.PLL.PLLFRACN = 0;
121+
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
122+
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
123+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
124+
return 0; // FAIL
125+
}
126+
127+
/* Select PLL as system clock source and configure bus clocks dividers */
128+
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |
129+
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 |
130+
RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_D3PCLK1;
131+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
132+
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
133+
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
134+
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
135+
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
136+
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
137+
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
138+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) {
139+
return 0; // FAIL
140+
}
141+
142+
#if DEVICE_USBDEVICE
143+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB;
144+
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
145+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
146+
return 0; // FAIL
147+
}
148+
149+
HAL_PWREx_EnableUSBVoltageDetector();
150+
#endif /* DEVICE_USBDEVICE */
151+
152+
__HAL_RCC_CSI_ENABLE() ;
153+
154+
__HAL_RCC_SYSCFG_CLK_ENABLE() ;
155+
156+
HAL_EnableCompensationCell();
157+
158+
return 1; // OK
159+
}
160+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
161+
162+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
163+
/******************************************************************************/
164+
/* PLL (clocked by HSI) used as System clock source */
165+
/******************************************************************************/
166+
uint8_t SetSysClock_PLL_HSI(void)
167+
{
168+
RCC_ClkInitTypeDef RCC_ClkInitStruct;
169+
RCC_OscInitTypeDef RCC_OscInitStruct;
170+
171+
/*!< Supply configuration update enable */
172+
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
173+
/* The voltage scaling allows optimizing the power consumption when the device is
174+
clocked below the maximum system frequency, to update the voltage scaling value
175+
regarding system frequency refer to product datasheet. */
176+
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
177+
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
178+
179+
// Enable HSI oscillator and activate PLL with HSI as source
180+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_CSI;
181+
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
182+
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
183+
RCC_OscInitStruct.CSIState = RCC_CSI_OFF;
184+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
185+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
186+
RCC_OscInitStruct.PLL.PLLM = 8;
187+
RCC_OscInitStruct.PLL.PLLN = 100;
188+
RCC_OscInitStruct.PLL.PLLP = 2;
189+
RCC_OscInitStruct.PLL.PLLQ = 2;
190+
RCC_OscInitStruct.PLL.PLLR = 2;
191+
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
192+
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
193+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
194+
return 0; // FAIL
195+
}
196+
197+
/* Select PLL as system clock source and configure bus clocks dividers */
198+
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | \
199+
RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1);
200+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
201+
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
202+
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
203+
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
204+
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
205+
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
206+
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
207+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) {
208+
return 0; // FAIL
209+
}
210+
211+
return 1; // OK
212+
}
213+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */

0 commit comments

Comments
 (0)