17
17
/**
18
18
* This file configures the system clock as follows:
19
19
*-----------------------------------------------------------------------------
20
- * System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI
20
+ * System clock source | 1- USE_PLL_HSE_EXTC | 3- USE_PLL_HSI
21
21
* | (external 8 MHz clock) | (internal 8 MHz)
22
- * | 2- PLL_HSE_XTAL |
22
+ * | 2- USE_PLL_HSE_XTAL |
23
23
* | (external 8 MHz xtal) |
24
24
*-----------------------------------------------------------------------------
25
25
* SYSCLK(MHz) | 72 | 64
35
35
**/
36
36
37
37
#include "stm32f3xx.h"
38
+ #include "mbed_assert.h"
38
39
39
40
/*!< Uncomment the following line if you need to relocate your vector Table in
40
41
Internal SRAM. */
41
42
/* #define VECT_TAB_SRAM */
42
43
#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
43
44
This value must be a multiple of 0x200. */
44
45
45
- /* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */
46
- #define USE_PLL_HSE_EXTC (1) /* Use external clock */
47
- #define USE_PLL_HSE_XTAL (1) /* Use external xtal */
46
+ // clock source is selected with CLOCK_SOURCE in json config
47
+ #define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
48
+ #define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default)
49
+ #define USE_PLL_HSI 0x2 // Use HSI internal clock
48
50
49
-
50
- #if (USE_PLL_HSE_XTAL != 0 ) || (USE_PLL_HSE_EXTC != 0 )
51
+ #if ( ((CLOCK_SOURCE ) & USE_PLL_HSE_XTAL ) || ((CLOCK_SOURCE ) & USE_PLL_HSE_EXTC ) )
51
52
uint8_t SetSysClock_PLL_HSE (uint8_t bypass );
52
- #endif
53
+ #endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
53
54
55
+ #if ((CLOCK_SOURCE ) & USE_PLL_HSI )
54
56
uint8_t SetSysClock_PLL_HSI (void );
57
+ #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
55
58
56
59
/**
57
60
* @brief Setup the microcontroller system
@@ -108,22 +111,26 @@ void SystemInit(void)
108
111
* @param None
109
112
* @retval None
110
113
*/
114
+
111
115
void SetSysClock (void )
112
116
{
117
+ #if ((CLOCK_SOURCE ) & USE_PLL_HSE_EXTC )
113
118
/* 1- Try to start with HSE and external clock */
114
- #if USE_PLL_HSE_EXTC != 0
115
119
if (SetSysClock_PLL_HSE (1 ) == 0 )
116
120
#endif
117
121
{
122
+ #if ((CLOCK_SOURCE ) & USE_PLL_HSE_XTAL )
118
123
/* 2- If fail try to start with HSE and external xtal */
119
- #if USE_PLL_HSE_XTAL != 0
120
124
if (SetSysClock_PLL_HSE (0 ) == 0 )
121
125
#endif
122
126
{
127
+ #if ((CLOCK_SOURCE ) & USE_PLL_HSI )
123
128
/* 3- If fail start with HSI clock */
124
- if (SetSysClock_PLL_HSI () == 0 ) {
129
+ if (SetSysClock_PLL_HSI () == 0 )
130
+ #endif
131
+ {
125
132
while (1 ) {
126
- // [TODO] Put something here to tell the user that a problem occured...
133
+ MBED_ASSERT ( 1 );
127
134
}
128
135
}
129
136
}
@@ -133,14 +140,15 @@ void SetSysClock(void)
133
140
//HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_SYSCLK, RCC_MCO_DIV1); // 72 MHz or 64 MHz
134
141
}
135
142
136
- #if (USE_PLL_HSE_XTAL != 0 ) || (USE_PLL_HSE_EXTC != 0 )
143
+ #if ( (( CLOCK_SOURCE ) & USE_PLL_HSE_XTAL ) || (( CLOCK_SOURCE ) & USE_PLL_HSE_EXTC ) )
137
144
/******************************************************************************/
138
145
/* PLL (clocked by HSE) used as System clock source */
139
146
/******************************************************************************/
140
147
uint8_t SetSysClock_PLL_HSE (uint8_t bypass )
141
148
{
142
149
RCC_ClkInitTypeDef RCC_ClkInitStruct ;
143
150
RCC_OscInitTypeDef RCC_OscInitStruct ;
151
+ RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit ;
144
152
145
153
/* Enable HSE oscillator and activate PLL with HSE as source */
146
154
RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_HSE ;
@@ -167,6 +175,12 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
167
175
return 0 ; // FAIL
168
176
}
169
177
178
+ RCC_PeriphClkInit .PeriphClockSelection = RCC_PERIPHCLK_USB ;
179
+ RCC_PeriphClkInit .USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5 ;
180
+ if (HAL_RCCEx_PeriphCLKConfig (& RCC_PeriphClkInit ) != HAL_OK ) {
181
+ return 0 ; // FAIL
182
+ }
183
+
170
184
/* Output clock on MCO1 pin(PA8) for debugging purpose */
171
185
//if (bypass == 0)
172
186
// HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV2); // 4 MHz with xtal
@@ -175,8 +189,9 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
175
189
176
190
return 1 ; // OK
177
191
}
178
- #endif
192
+ #endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
179
193
194
+ #if ((CLOCK_SOURCE ) & USE_PLL_HSI )
180
195
/******************************************************************************/
181
196
/* PLL (clocked by HSI) used as System clock source */
182
197
/******************************************************************************/
@@ -189,7 +204,7 @@ uint8_t SetSysClock_PLL_HSI(void)
189
204
RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE ;
190
205
RCC_OscInitStruct .HSIState = RCC_HSI_ON ;
191
206
RCC_OscInitStruct .HSEState = RCC_HSE_OFF ;
192
- RCC_OscInitStruct .HSICalibrationValue = 16 ;
207
+ RCC_OscInitStruct .HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT ;
193
208
RCC_OscInitStruct .PLL .PLLState = RCC_PLL_ON ;
194
209
RCC_OscInitStruct .PLL .PLLSource = RCC_PLLSOURCE_HSI ;
195
210
RCC_OscInitStruct .PLL .PLLMUL = RCC_PLL_MUL16 ; // 64 MHz (8 MHz/2 * 16)
@@ -212,4 +227,4 @@ uint8_t SetSysClock_PLL_HSI(void)
212
227
213
228
return 1 ; // OK
214
229
}
215
-
230
+ #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
0 commit comments