Skip to content

Commit f871eff

Browse files
author
Tauno Magnusson
committed
Reverted to a pure copy of 474RE system_clock.c since it was updated with RNG clock fixes. Removing USB Clock enablement (USB will not work out-of-the box but will require users to modify system_clock.c to enable its clock) - this inline with changes to targets.json where I removed the USBDEVICE define
1 parent bfd1f09 commit f871eff

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

targets/TARGET_STM/TARGET_STM32G4/TARGET_STM32G431xB/system_clock.c

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,14 @@
2020
* | 2- USE_PLL_HSE_XTAL (external 8 MHz xtal)
2121
* | 3- USE_PLL_HSI (internal 16 MHz)
2222
*-----------------------------------------------------------------
23-
* SYSCLK(MHz) | 64
24-
* AHBCLK (MHz) | 64
25-
* APB1CLK (MHz) | 64
23+
* SYSCLK(MHz) | 160 (default configuration) / 170 (CAN disabled)
2624
* USB capable | NO
2725
*-----------------------------------------------------------------
2826
*/
2927

3028
#include "stm32g4xx.h"
3129
#include "mbed_error.h"
3230

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-
3931
#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
4032
#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board)
4133
#define USE_PLL_HSI 0x2 // Use HSI internal clock
@@ -90,17 +82,19 @@ void SetSysClock(void)
9082
/******************************************************************************/
9183
/* PLL (clocked by HSE) used as System clock source */
9284
/******************************************************************************/
93-
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
85+
MBED_WEAK uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
9486
{
9587
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
9688
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
97-
RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = { 0 };
9889

99-
/** Configure the main internal regulator output voltage
100-
*/
90+
#if HSE_VALUE != 24000000
91+
#error Unsupported externall clock value, check HSE_VALUE define
92+
#endif
93+
94+
/* Configure the main internal regulator output voltage */
95+
__HAL_RCC_PWR_CLK_ENABLE();
10196
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
102-
/** Initializes the CPU, AHB and APB busses clocks
103-
*/
97+
10498
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
10599
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
106100
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
@@ -117,35 +111,20 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
117111
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
118112
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
119113
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)
114+
#if defined(DEVICE_TRNG)
124115
RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_HSI48;
125116
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
126-
117+
#endif
127118
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
128119
return 0; // FAIL
129120
}
130121

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-
*/
141122
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
142123
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
143124
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
144125
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
145126
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
146127
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
147-
148-
// This is an assumption: Using same Flash Latency as for G474RE
149128
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8) != HAL_OK) {
150129
return 0; // FAIL
151130
}
@@ -162,43 +141,42 @@ uint8_t SetSysClock_PLL_HSI(void)
162141
{
163142
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
164143
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
165-
RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = { 0 };
166144

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-
*/
145+
/* Configure the main internal regulator output voltage */
146+
__HAL_RCC_PWR_CLK_ENABLE();
147+
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
148+
172149
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
173150
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
174151
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
175152
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
176153
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
177154
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
155+
//! 170MHz as a core frequency for FDCAN is not suitable for many frequencies,
156+
//! as it provides low accuracy. When no FDCAN is used, the full capacity of 170 MHz
157+
//! should be standard.
158+
#if DEVICE_CAN
159+
RCC_OscInitStruct.PLL.PLLN = 80;
160+
#else
178161
RCC_OscInitStruct.PLL.PLLN = 85;
162+
#endif
179163
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
180164
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
181165
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.
166+
#if defined(DEVICE_TRNG)
167+
RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_HSI48;
168+
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
169+
#endif
184170
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
185171
return 0; // FAIL
186172
}
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-
*/
173+
195174
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
196175
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
197176
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
198177
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
199178
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
200179
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
201-
202180
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8) != HAL_OK) {
203181
return 0; // FAIL
204182
}

0 commit comments

Comments
 (0)