Skip to content

Commit df6a570

Browse files
committed
STM32F2 : json clock source configuration
- default value is the same as before patch - system_stm32f2xx.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 68e1b2b commit df6a570

File tree

3 files changed

+251
-156
lines changed

3 files changed

+251
-156
lines changed
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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 (external 8 MHz clock)
21+
* | 2- USE_PLL_HSE_XTAL (external 8 MHz xtal)
22+
* | 3- USE_PLL_HSI (internal 16 MHz)
23+
*-----------------------------------------------------------------------------
24+
* SYSCLK(MHz) | 120
25+
* AHBCLK (MHz) | 120
26+
* APB1CLK (MHz) | 30
27+
* APB2CLK (MHz) | 60
28+
* USB capable | YES
29+
*-----------------------------------------------------------------------------
30+
**/
31+
32+
#include "stm32f2xx.h"
33+
#include "mbed_assert.h"
34+
35+
/*!< Uncomment the following line if you need to relocate your vector Table in
36+
Internal SRAM. */
37+
/* #define VECT_TAB_SRAM */
38+
#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
39+
This value must be a multiple of 0x200. */
40+
41+
42+
// clock source is selected with CLOCK_SOURCE in json config
43+
#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
44+
#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default)
45+
#define USE_PLL_HSI 0x2 // Use HSI internal clock
46+
47+
#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
48+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
49+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
50+
51+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
52+
uint8_t SetSysClock_PLL_HSI(void);
53+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
54+
55+
/**
56+
* @brief Setup the microcontroller system
57+
* Initialize the Embedded Flash Interface, the PLL and update the
58+
* SystemFrequency variable.
59+
* @param None
60+
* @retval None
61+
*/
62+
void SystemInit(void)
63+
{
64+
/* Reset the RCC clock configuration to the default reset state ------------*/
65+
/* Set HSION bit */
66+
RCC->CR |= (uint32_t)0x00000001;
67+
68+
/* Reset CFGR register */
69+
RCC->CFGR = 0x00000000;
70+
71+
/* Reset HSEON, CSSON and PLLON bits */
72+
RCC->CR &= (uint32_t)0xFEF6FFFF;
73+
74+
/* Reset PLLCFGR register */
75+
RCC->PLLCFGR = 0x24003010;
76+
77+
/* Reset HSEBYP bit */
78+
RCC->CR &= (uint32_t)0xFFFBFFFF;
79+
80+
/* Disable all interrupts */
81+
RCC->CIR = 0x00000000;
82+
83+
#ifdef DATA_IN_ExtSRAM
84+
SystemInit_ExtMemCtl();
85+
#endif /* DATA_IN_ExtSRAM */
86+
87+
/* Configure the Vector Table location add offset address ------------------*/
88+
#ifdef VECT_TAB_SRAM
89+
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
90+
#else
91+
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
92+
#endif
93+
94+
}
95+
96+
97+
/**
98+
* @brief Configures the System clock source, PLL Multiplier and Divider factors,
99+
* AHB/APBx prescalers and Flash settings
100+
* @note This function should be called only once the RCC clock configuration
101+
* is reset to the default reset state (done in SystemInit() function).
102+
* @param None
103+
* @retval None
104+
*/
105+
106+
void SetSysClock(void)
107+
{
108+
#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC)
109+
/* 1- Try to start with HSE and external clock */
110+
if (SetSysClock_PLL_HSE(1) == 0)
111+
#endif
112+
{
113+
#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL)
114+
/* 2- If fail try to start with HSE and external xtal */
115+
if (SetSysClock_PLL_HSE(0) == 0)
116+
#endif
117+
{
118+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
119+
/* 3- If fail start with HSI clock */
120+
if (SetSysClock_PLL_HSI() == 0)
121+
#endif
122+
{
123+
while(1) {
124+
MBED_ASSERT(1);
125+
}
126+
}
127+
}
128+
}
129+
130+
#if 0 // SYSCLK can be map to PC_9
131+
HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_2);
132+
#endif
133+
}
134+
135+
#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
136+
/******************************************************************************/
137+
/* PLL (clocked by HSE) used as System clock source */
138+
/******************************************************************************/
139+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
140+
{
141+
RCC_OscInitTypeDef RCC_OscInitStruct;
142+
RCC_ClkInitTypeDef RCC_ClkInitStruct;
143+
144+
/* The voltage scaling allows optimizing the power consumption when the device is
145+
clocked below the maximum system frequency, to update the voltage scaling value
146+
regarding system frequency refer to product datasheet. */
147+
__HAL_RCC_PWR_CLK_ENABLE();
148+
149+
// Enable HSE oscillator and activate PLL with HSE as source
150+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
151+
if (bypass == 0) {
152+
RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External xtal on OSC_IN/OSC_OUT */
153+
} else {
154+
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External clock on OSC_IN */
155+
}
156+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
157+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
158+
RCC_OscInitStruct.PLL.PLLM = 8;
159+
RCC_OscInitStruct.PLL.PLLN = 240;
160+
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
161+
RCC_OscInitStruct.PLL.PLLQ = 5;
162+
163+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
164+
return 0; // FAIL
165+
}
166+
167+
// Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers
168+
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
169+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
170+
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
171+
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
172+
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
173+
174+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
175+
return 0; // FAIL
176+
}
177+
178+
return 1; // OK
179+
}
180+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
181+
182+
#if ((CLOCK_SOURCE) & USE_PLL_HSI)
183+
/******************************************************************************/
184+
/* PLL (clocked by HSI) used as System clock source */
185+
/******************************************************************************/
186+
uint8_t SetSysClock_PLL_HSI(void)
187+
{
188+
RCC_ClkInitTypeDef RCC_ClkInitStruct;
189+
RCC_OscInitTypeDef RCC_OscInitStruct;
190+
191+
/* The voltage scaling allows optimizing the power consumption when the device is
192+
clocked below the maximum system frequency, to update the voltage scaling value
193+
regarding system frequency refer to product datasheet. */
194+
__HAL_RCC_PWR_CLK_ENABLE();
195+
196+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
197+
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
198+
RCC_OscInitStruct.HSICalibrationValue = 16;
199+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
200+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
201+
RCC_OscInitStruct.PLL.PLLM = 16;
202+
RCC_OscInitStruct.PLL.PLLN = 240;
203+
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
204+
RCC_OscInitStruct.PLL.PLLQ = 5;
205+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
206+
return 0; // FAIL
207+
}
208+
209+
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
210+
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
211+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
212+
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
213+
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
214+
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
215+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
216+
return 0; // FAIL
217+
}
218+
219+
220+
return 1; // OK
221+
}
222+
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */

0 commit comments

Comments
 (0)