Skip to content

Commit e0e6e5a

Browse files
committed
DISCO_F303VC : Align clock configuration with STM32 family
1 parent e8e4af3 commit e0e6e5a

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/TARGET_DISCO_F303VC/system_clock.c

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
/**
1818
* This file configures the system clock as follows:
1919
*-----------------------------------------------------------------------------
20-
* System clock source | 1- PLL_HSE_EXTC | 3- PLL_HSI
20+
* System clock source | 1- USE_PLL_HSE_EXTC | 3- USE_PLL_HSI
2121
* | (external 8 MHz clock) | (internal 8 MHz)
22-
* | 2- PLL_HSE_XTAL |
22+
* | 2- USE_PLL_HSE_XTAL |
2323
* | (external 8 MHz xtal) |
2424
*-----------------------------------------------------------------------------
2525
* SYSCLK(MHz) | 72 | 64
@@ -35,23 +35,26 @@
3535
**/
3636

3737
#include "stm32f3xx.h"
38+
#include "mbed_assert.h"
3839

3940
/*!< Uncomment the following line if you need to relocate your vector Table in
4041
Internal SRAM. */
4142
/* #define VECT_TAB_SRAM */
4243
#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
4344
This value must be a multiple of 0x200. */
4445

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
4850

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) )
5152
uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
52-
#endif
53+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
5354

55+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
5456
uint8_t SetSysClock_PLL_HSI(void);
57+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
5558

5659
/**
5760
* @brief Setup the microcontroller system
@@ -108,22 +111,26 @@ void SystemInit(void)
108111
* @param None
109112
* @retval None
110113
*/
114+
111115
void SetSysClock(void)
112116
{
117+
#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC)
113118
/* 1- Try to start with HSE and external clock */
114-
#if USE_PLL_HSE_EXTC != 0
115119
if (SetSysClock_PLL_HSE(1) == 0)
116120
#endif
117121
{
122+
#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL)
118123
/* 2- If fail try to start with HSE and external xtal */
119-
#if USE_PLL_HSE_XTAL != 0
120124
if (SetSysClock_PLL_HSE(0) == 0)
121125
#endif
122126
{
127+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
123128
/* 3- If fail start with HSI clock */
124-
if (SetSysClock_PLL_HSI() == 0) {
129+
if (SetSysClock_PLL_HSI() == 0)
130+
#endif
131+
{
125132
while(1) {
126-
// [TODO] Put something here to tell the user that a problem occured...
133+
MBED_ASSERT(1);
127134
}
128135
}
129136
}
@@ -133,14 +140,15 @@ void SetSysClock(void)
133140
//HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_SYSCLK, RCC_MCO_DIV1); // 72 MHz or 64 MHz
134141
}
135142

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) )
137144
/******************************************************************************/
138145
/* PLL (clocked by HSE) used as System clock source */
139146
/******************************************************************************/
140147
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
141148
{
142149
RCC_ClkInitTypeDef RCC_ClkInitStruct;
143150
RCC_OscInitTypeDef RCC_OscInitStruct;
151+
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
144152

145153
/* Enable HSE oscillator and activate PLL with HSE as source */
146154
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
@@ -167,6 +175,12 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
167175
return 0; // FAIL
168176
}
169177

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+
170184
/* Output clock on MCO1 pin(PA8) for debugging purpose */
171185
//if (bypass == 0)
172186
// 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)
175189

176190
return 1; // OK
177191
}
178-
#endif
192+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
179193

194+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
180195
/******************************************************************************/
181196
/* PLL (clocked by HSI) used as System clock source */
182197
/******************************************************************************/
@@ -189,7 +204,7 @@ uint8_t SetSysClock_PLL_HSI(void)
189204
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
190205
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
191206
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
192-
RCC_OscInitStruct.HSICalibrationValue = 16;
207+
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
193208
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
194209
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
195210
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64 MHz (8 MHz/2 * 16)
@@ -212,4 +227,4 @@ uint8_t SetSysClock_PLL_HSI(void)
212227

213228
return 1; // OK
214229
}
215-
230+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */

targets/targets.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,15 @@
16551655
"core": "Cortex-M4F",
16561656
"extra_labels_add": ["STM32F3", "STM32F303", "STM32F303xC", "STM32F303VC"],
16571657
"overrides": {"lse_available": 0},
1658-
"supported_toolchains": ["GCC_ARM"],
1658+
"config": {
1659+
"clock_source": {
1660+
"help": "Mask value : USE_PLL_HSE_EXTC | USE_PLL_HSI",
1661+
"value": "USE_PLL_HSE_EXTC|USE_PLL_HSI",
1662+
"macro_name": "CLOCK_SOURCE"
1663+
}
1664+
},
1665+
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
1666+
"release_versions": ["2", "5"],
16591667
"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "SERIAL_FC"],
16601668
"device_name": "STM32F303VC"
16611669
},

0 commit comments

Comments
 (0)