46
46
#define USE_PLL_HSI 0x2 // Use HSI internal clock
47
47
48
48
#if ( ((CLOCK_SOURCE ) & USE_PLL_HSE_XTAL ) || ((CLOCK_SOURCE ) & USE_PLL_HSE_EXTC ) )
49
- uint8_t SetSysClock_PLL_HSE (uint8_t bypass );
49
+ uint8_t SetSysClock_PLL_HSE (uint8_t bypass , bool lowspeed );
50
50
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
51
51
52
52
#if ((CLOCK_SOURCE ) & USE_PLL_HSI )
@@ -64,14 +64,20 @@ uint8_t SetSysClock_PLL_HSI(void);
64
64
65
65
void SetSysClock (void )
66
66
{
67
+
68
+ bool lowspeed = false;
69
+ #if defined(LOWSPEED ) && (LOWSPEED == 1 )
70
+ lowspeed = true;
71
+ #endif
72
+
67
73
#if ((CLOCK_SOURCE ) & USE_PLL_HSE_EXTC )
68
74
/* 1- Try to start with HSE and external clock (MCO from STLink PCB part) */
69
- if (SetSysClock_PLL_HSE (1 ) == 0 )
75
+ if (SetSysClock_PLL_HSE (1 , lowspeed ) == 0 )
70
76
#endif
71
77
{
72
78
#if ((CLOCK_SOURCE ) & USE_PLL_HSE_XTAL )
73
79
/* 2- If fail try to start with HSE and external xtal */
74
- if (SetSysClock_PLL_HSE (0 ) == 0 )
80
+ if (SetSysClock_PLL_HSE (0 , lowspeed ) == 0 )
75
81
#endif
76
82
{
77
83
#if ((CLOCK_SOURCE ) & USE_PLL_HSI )
@@ -103,12 +109,22 @@ bool isBetaBoard() {
103
109
/******************************************************************************/
104
110
/* PLL (clocked by HSE) used as System clock source */
105
111
/******************************************************************************/
106
- uint8_t SetSysClock_PLL_HSE (uint8_t bypass )
112
+ uint8_t SetSysClock_PLL_HSE (uint8_t bypass , bool lowspeed )
107
113
{
108
114
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0 };
109
115
RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
110
116
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0 };
111
117
118
+ // If we are reconfiguring the clock, select CSI as system clock source to allow modification of the PLL configuration
119
+ if (__HAL_RCC_GET_PLL_OSCSOURCE () == RCC_PLLSOURCE_HSE ) {
120
+ RCC_ClkInitStruct .ClockType = RCC_CLOCKTYPE_SYSCLK ;
121
+ RCC_ClkInitStruct .SYSCLKSource = RCC_SYSCLKSOURCE_CSI ;
122
+ if (HAL_RCC_ClockConfig (& RCC_ClkInitStruct , FLASH_LATENCY_1 ) != HAL_OK )
123
+ {
124
+ return 0 ;
125
+ }
126
+ }
127
+
112
128
/* Enable oscillator pin */
113
129
__HAL_RCC_GPIOH_CLK_ENABLE ();
114
130
GPIO_InitTypeDef gpio_osc_init_structure ;
@@ -128,7 +144,7 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
128
144
}
129
145
/* Configure the main internal regulator output voltage */
130
146
131
- if (isBootloader () ) {
147
+ if (lowspeed ) {
132
148
__HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE3 );
133
149
} else {
134
150
__HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE1 );
@@ -147,14 +163,15 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
147
163
RCC_OscInitStruct .PLL .PLLState = RCC_PLL_ON ;
148
164
RCC_OscInitStruct .PLL .PLLSource = RCC_PLLSOURCE_HSE ;
149
165
RCC_OscInitStruct .PLL .PLLM = 5 ;
150
- if (isBootloader () ) {
166
+ if (lowspeed ) {
151
167
RCC_OscInitStruct .PLL .PLLN = 40 ;
152
168
} else {
153
169
RCC_OscInitStruct .PLL .PLLN = 160 ;
154
170
}
171
+
155
172
if (isBetaBoard ()) {
156
173
RCC_OscInitStruct .PLL .PLLM = 9 ;
157
- if (isBootloader () ) {
174
+ if (lowspeed ) {
158
175
RCC_OscInitStruct .PLL .PLLN = 80 ;
159
176
} else {
160
177
RCC_OscInitStruct .PLL .PLLN = 300 ;
@@ -182,7 +199,11 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
182
199
RCC_ClkInitStruct .APB2CLKDivider = RCC_APB2_DIV2 ;
183
200
RCC_ClkInitStruct .APB3CLKDivider = RCC_APB3_DIV2 ;
184
201
RCC_ClkInitStruct .APB4CLKDivider = RCC_APB4_DIV2 ;
185
- if (HAL_RCC_ClockConfig (& RCC_ClkInitStruct , FLASH_LATENCY_4 ) != HAL_OK ) {
202
+ if (lowspeed ) {
203
+ if (HAL_RCC_ClockConfig (& RCC_ClkInitStruct , FLASH_LATENCY_0 ) != HAL_OK )
204
+ return 0 ; // FAIL
205
+ } else {
206
+ if (HAL_RCC_ClockConfig (& RCC_ClkInitStruct , FLASH_LATENCY_4 ) != HAL_OK )
186
207
return 0 ; // FAIL
187
208
}
188
209
0 commit comments