Skip to content

Commit bc40b73

Browse files
committed
STM32F3 : json clock source configuration
- default value is the same as before patch - system_stm32f3xx.c file is copied to family level with all other ST cube files - specific clock configuration is now in a new file: system_clock.c (target level)
1 parent df6a570 commit bc40b73

File tree

14 files changed

+1649
-2375
lines changed

14 files changed

+1649
-2375
lines changed
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This file configures the system clock as follows:
19+
*-----------------------------------------------------------------------------
20+
* System clock source | 1- USE_PLL_HSE_EXTC | 3- USE_PLL_HSI
21+
* | (external 8 MHz clock) | (internal 8 MHz)
22+
* | 2- USE_PLL_HSE_XTAL |
23+
* | (external 8 MHz xtal) |
24+
*-----------------------------------------------------------------------------
25+
* SYSCLK(MHz) | 72 | 64
26+
*-----------------------------------------------------------------------------
27+
* AHBCLK (MHz) | 72 | 64
28+
*-----------------------------------------------------------------------------
29+
* APB1CLK (MHz) | 36 | 32
30+
*-----------------------------------------------------------------------------
31+
* APB2CLK (MHz) | 72 | 64
32+
*-----------------------------------------------------------------------------
33+
* USB capable | YES | NO
34+
*-----------------------------------------------------------------------------
35+
*/
36+
37+
38+
#include "stm32f3xx.h"
39+
#include "mbed_assert.h"
40+
41+
/*!< Uncomment the following line if you need to relocate your vector Table in
42+
Internal SRAM. */
43+
/* #define VECT_TAB_SRAM */
44+
#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
45+
This value must be a multiple of 0x200. */
46+
47+
// clock source is selected with CLOCK_SOURCE in json config
48+
#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
49+
#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default)
50+
#define USE_PLL_HSI 0x2 // Use HSI internal clock
51+
52+
#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
53+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
54+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
55+
56+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
57+
uint8_t SetSysClock_PLL_HSI(void);
58+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
59+
60+
/**
61+
* @brief Setup the microcontroller system
62+
* Initialize the FPU setting, vector table location and the PLL configuration is reset.
63+
* @param None
64+
* @retval None
65+
*/
66+
void SystemInit(void)
67+
{
68+
/* FPU settings ------------------------------------------------------------*/
69+
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
70+
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
71+
#endif
72+
73+
/* Reset the RCC clock configuration to the default reset state ------------*/
74+
/* Set HSION bit */
75+
RCC->CR |= 0x00000001U;
76+
77+
/* Reset CFGR register */
78+
RCC->CFGR &= 0xF87FC00CU;
79+
80+
/* Reset HSEON, CSSON and PLLON bits */
81+
RCC->CR &= 0xFEF6FFFFU;
82+
83+
/* Reset HSEBYP bit */
84+
RCC->CR &= 0xFFFBFFFFU;
85+
86+
/* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE bits */
87+
RCC->CFGR &= 0xFF80FFFFU;
88+
89+
/* Reset PREDIV1[3:0] bits */
90+
RCC->CFGR2 &= 0xFFFFFFF0U;
91+
92+
/* Reset USARTSW[1:0], I2CSW and TIMs bits */
93+
RCC->CFGR3 &= 0xFF00FCCCU;
94+
95+
/* Disable all interrupts */
96+
RCC->CIR = 0x00000000U;
97+
98+
#ifdef VECT_TAB_SRAM
99+
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
100+
#else
101+
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
102+
#endif
103+
104+
}
105+
106+
107+
/**
108+
* @brief Configures the System clock source, PLL Multiplier and Divider factors,
109+
* AHB/APBx prescalers and Flash settings
110+
* @note This function should be called only once the RCC clock configuration
111+
* is reset to the default reset state (done in SystemInit() function).
112+
* @param None
113+
* @retval None
114+
*/
115+
116+
void SetSysClock(void)
117+
{
118+
#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC)
119+
/* 1- Try to start with HSE and external clock */
120+
if (SetSysClock_PLL_HSE(1) == 0)
121+
#endif
122+
{
123+
#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL)
124+
/* 2- If fail try to start with HSE and external xtal */
125+
if (SetSysClock_PLL_HSE(0) == 0)
126+
#endif
127+
{
128+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
129+
/* 3- If fail start with HSI clock */
130+
if (SetSysClock_PLL_HSI() == 0)
131+
#endif
132+
{
133+
while(1) {
134+
MBED_ASSERT(1);
135+
}
136+
}
137+
}
138+
}
139+
140+
/* Output clock on MCO1 pin(PA8) for debugging purpose */
141+
//HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_SYSCLK, RCC_MCO_DIV1); // 72 MHz or 64 MHz
142+
}
143+
144+
#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
145+
/******************************************************************************/
146+
/* PLL (clocked by HSE) used as System clock source */
147+
/******************************************************************************/
148+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
149+
{
150+
RCC_ClkInitTypeDef RCC_ClkInitStruct;
151+
RCC_OscInitTypeDef RCC_OscInitStruct;
152+
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
153+
154+
/* Enable HSE oscillator and activate PLL with HSE as source */
155+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
156+
if (bypass == 0) {
157+
RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */
158+
} else {
159+
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */
160+
}
161+
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
162+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
163+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
164+
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 72 MHz (8 MHz * 9)
165+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
166+
return 0; // FAIL
167+
}
168+
169+
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
170+
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
171+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 72 MHz
172+
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 72 MHz
173+
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 36 MHz
174+
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 72 MHz
175+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
176+
return 0; // FAIL
177+
}
178+
179+
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
180+
RCC_PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
181+
if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
182+
return 0; // FAIL
183+
}
184+
185+
/* Output clock on MCO1 pin(PA8) for debugging purpose */
186+
//if (bypass == 0)
187+
// HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV2); // 4 MHz with xtal
188+
//else
189+
// HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV1); // 8 MHz with ext clock
190+
191+
return 1; // OK
192+
}
193+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
194+
195+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
196+
/******************************************************************************/
197+
/* PLL (clocked by HSI) used as System clock source */
198+
/******************************************************************************/
199+
uint8_t SetSysClock_PLL_HSI(void)
200+
{
201+
RCC_ClkInitTypeDef RCC_ClkInitStruct;
202+
RCC_OscInitTypeDef RCC_OscInitStruct;
203+
204+
/* Enable HSI oscillator and activate PLL with HSI as source */
205+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
206+
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
207+
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
208+
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
209+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
210+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
211+
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64 MHz (8 MHz/2 * 16)
212+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
213+
return 0; // FAIL
214+
}
215+
216+
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
217+
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
218+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 64 MHz
219+
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 64 MHz
220+
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 32 MHz
221+
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 64 MHz
222+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
223+
return 0; // FAIL
224+
}
225+
226+
/* Output clock on MCO1 pin(PA8) for debugging purpose */
227+
//HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSI, RCC_MCO_DIV1); // 8 MHz
228+
229+
return 1; // OK
230+
}
231+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */

0 commit comments

Comments
 (0)