@@ -126,12 +126,13 @@ static uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
126
126
{
127
127
RCC_OscInitTypeDef RCC_OscInitStruct;
128
128
RCC_ClkInitTypeDef RCC_ClkInitStruct;
129
+ RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
129
130
130
131
/* The voltage scaling allows optimizing the power consumption when the device is
131
132
clocked below the maximum system frequency, to update the voltage scaling value
132
133
regarding system frequency refer to product datasheet. */
133
134
__HAL_RCC_PWR_CLK_ENABLE ();
134
- __HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE2 );
135
+ __HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE1 );
135
136
136
137
// Enable HSE oscillator and activate PLL with HSE as source
137
138
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
@@ -143,21 +144,35 @@ static uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
143
144
144
145
RCC_OscInitStruct.PLL .PLLState = RCC_PLL_ON;
145
146
RCC_OscInitStruct.PLL .PLLSource = RCC_PLLSOURCE_HSE;
146
- RCC_OscInitStruct.PLL .PLLM = 8 ; // VCO input clock = 1 MHz (8 MHz / 8)
147
- RCC_OscInitStruct.PLL .PLLN = 336 ; // VCO output clock = 336 MHz (1 MHz * 336)
148
- RCC_OscInitStruct.PLL .PLLP = RCC_PLLP_DIV4; // PLLCLK = 84 MHz (336 MHz / 4)
149
- RCC_OscInitStruct.PLL .PLLQ = 7 ; // USB clock = 48 MHz (336 MHz / 7) --> OK for USB
147
+ RCC_OscInitStruct.PLL .PLLM = HSE_VALUE / 1000000L ; // Expects an 8 MHz external clock by default. Redefine HSE_VALUE if not
148
+ RCC_OscInitStruct.PLL .PLLN = 360 ; // VCO output clock = 360 MHz (1 MHz * 360)
149
+ RCC_OscInitStruct.PLL .PLLP = RCC_PLLP_DIV2; // PLLCLK = 180 MHz (360 MHz / 2)
150
+ RCC_OscInitStruct.PLL .PLLQ = 7 ;
151
+ RCC_OscInitStruct.PLL .PLLR = 2 ;
150
152
if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK) {
151
153
return 0 ; // FAIL
152
154
}
153
155
156
+ // Activate the OverDrive to reach the 180 MHz Frequency
157
+ if (HAL_PWREx_EnableOverDrive () != HAL_OK) {
158
+ return 0 ; // FAIL
159
+ }
160
+
161
+ // Select PLLSAI output as USB clock source
162
+ PeriphClkInitStruct.PLLSAI .PLLSAIM = 8 ;
163
+ PeriphClkInitStruct.PLLSAI .PLLSAIN = 384 ;
164
+ PeriphClkInitStruct.PLLSAI .PLLSAIP = RCC_PLLSAIP_DIV8;
165
+ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CLK48;
166
+ PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48CLKSOURCE_PLLSAIP;
167
+ HAL_RCCEx_PeriphCLKConfig (&PeriphClkInitStruct);
168
+
154
169
// Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers
155
170
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
156
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 84 MHz
157
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 84 MHz
158
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2 ; // 42 MHz
159
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1 ; // 84 MHz
160
- if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_2 ) != HAL_OK) {
171
+ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
172
+ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 180 MHz
173
+ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4 ; // 45 MHz
174
+ RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2 ; // 90 MHz
175
+ if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_5 ) != HAL_OK) {
161
176
return 0 ; // FAIL
162
177
}
163
178
@@ -179,12 +194,13 @@ uint8_t SetSysClock_PLL_HSI(void)
179
194
{
180
195
RCC_OscInitTypeDef RCC_OscInitStruct;
181
196
RCC_ClkInitTypeDef RCC_ClkInitStruct;
197
+ RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
182
198
183
199
/* The voltage scaling allows optimizing the power consumption when the device is
184
200
clocked below the maximum system frequency, to update the voltage scaling value
185
201
regarding system frequency refer to product datasheet. */
186
202
__HAL_RCC_PWR_CLK_ENABLE ();
187
- __HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE2 );
203
+ __HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE1 );
188
204
189
205
// Enable HSI oscillator and activate PLL with HSI as source
190
206
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
@@ -194,20 +210,33 @@ uint8_t SetSysClock_PLL_HSI(void)
194
210
RCC_OscInitStruct.PLL .PLLState = RCC_PLL_ON;
195
211
RCC_OscInitStruct.PLL .PLLSource = RCC_PLLSOURCE_HSI;
196
212
RCC_OscInitStruct.PLL .PLLM = 16 ; // VCO input clock = 1 MHz (16 MHz / 16)
197
- RCC_OscInitStruct.PLL .PLLN = 336 ; // VCO output clock = 336 MHz (1 MHz * 336 )
198
- RCC_OscInitStruct.PLL .PLLP = RCC_PLLP_DIV4 ; // PLLCLK = 84 MHz (336 MHz / 4 )
199
- RCC_OscInitStruct.PLL .PLLQ = 7 ; // USB clock = 48 MHz (336 MHz / 7) --> freq is ok but not precise enough
213
+ RCC_OscInitStruct.PLL .PLLN = 360 ; // VCO output clock = 360 MHz (1 MHz * 360 )
214
+ RCC_OscInitStruct.PLL .PLLP = RCC_PLLP_DIV2 ; // PLLCLK = 180 MHz (360 MHz / 2 )
215
+ RCC_OscInitStruct.PLL .PLLQ = 7 ;
200
216
if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK) {
201
217
return 0 ; // FAIL
202
218
}
203
219
220
+ // Activate the OverDrive to reach the 180 MHz Frequency
221
+ if (HAL_PWREx_EnableOverDrive () != HAL_OK) {
222
+ return 0 ; // FAIL
223
+ }
224
+
225
+ // Select PLLSAI output as USB clock source
226
+ PeriphClkInitStruct.PLLSAI .PLLSAIM = 8 ;
227
+ PeriphClkInitStruct.PLLSAI .PLLSAIN = 192 ;
228
+ PeriphClkInitStruct.PLLSAI .PLLSAIP = RCC_PLLSAIP_DIV8;
229
+ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CLK48;
230
+ PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48CLKSOURCE_PLLSAIP;
231
+ HAL_RCCEx_PeriphCLKConfig (&PeriphClkInitStruct);
232
+
204
233
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
205
234
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
206
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 84 MHz
207
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 84 MHz
208
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2 ; // 42 MHz
209
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1 ; // 84 MHz
210
- if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_2 ) != HAL_OK) {
235
+ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
236
+ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 180 MHz
237
+ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4 ; // 45 MHz
238
+ RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2 ; // 90 MHz
239
+ if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_5 ) != HAL_OK) {
211
240
return 0 ; // FAIL
212
241
}
213
242
0 commit comments