Skip to content

Commit 68e1b2b

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

File tree

8 files changed

+748
-1338
lines changed

8 files changed

+748
-1338
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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- PLL_HSE_EXTC | 3- PLL_HSI
21+
* | (external 8 MHz clock) | (internal 8 MHz)
22+
* | 2- 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 (48 MHz precise clock) | NO | NO
34+
*-----------------------------------------------------------------------------
35+
******************************************************************************
36+
*/
37+
38+
#include "stm32f1xx.h"
39+
40+
/*!< Uncomment the following line if you need to relocate your vector Table in
41+
Internal SRAM. */
42+
/* #define VECT_TAB_SRAM */
43+
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
44+
This value must be a multiple of 0x200. */
45+
46+
47+
/* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */
48+
#define USE_PLL_HSE_EXTC (1) /* Use external clock */
49+
#define USE_PLL_HSE_XTAL (1) /* Use external xtal */
50+
51+
52+
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
53+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
54+
#endif
55+
56+
uint8_t SetSysClock_PLL_HSI(void);
57+
58+
59+
/**
60+
* @brief Setup the microcontroller system
61+
* Initialize the Embedded Flash Interface, the PLL and update the
62+
* SystemCoreClock variable.
63+
* @note This function should be used only after reset.
64+
* @param None
65+
* @retval None
66+
*/
67+
void SystemInit (void)
68+
{
69+
/* Reset the RCC clock configuration to the default reset state(for debug purpose) */
70+
/* Set HSION bit */
71+
RCC->CR |= 0x00000001U;
72+
73+
/* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
74+
#if !defined(STM32F105xC) && !defined(STM32F107xC)
75+
RCC->CFGR &= 0xF8FF0000U;
76+
#else
77+
RCC->CFGR &= 0xF0FF0000U;
78+
#endif /* STM32F105xC */
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/OTGFSPRE bits */
87+
RCC->CFGR &= 0xFF80FFFFU;
88+
89+
#if defined(STM32F105xC) || defined(STM32F107xC)
90+
/* Reset PLL2ON and PLL3ON bits */
91+
RCC->CR &= 0xEBFFFFFFU;
92+
93+
/* Disable all interrupts and clear pending bits */
94+
RCC->CIR = 0x00FF0000U;
95+
96+
/* Reset CFGR2 register */
97+
RCC->CFGR2 = 0x00000000U;
98+
#elif defined(STM32F100xB) || defined(STM32F100xE)
99+
/* Disable all interrupts and clear pending bits */
100+
RCC->CIR = 0x009F0000U;
101+
102+
/* Reset CFGR2 register */
103+
RCC->CFGR2 = 0x00000000U;
104+
#else
105+
/* Disable all interrupts and clear pending bits */
106+
RCC->CIR = 0x009F0000U;
107+
#endif /* STM32F105xC */
108+
109+
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
110+
#ifdef DATA_IN_ExtSRAM
111+
SystemInit_ExtMemCtl();
112+
#endif /* DATA_IN_ExtSRAM */
113+
#endif
114+
115+
#ifdef VECT_TAB_SRAM
116+
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
117+
#else
118+
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
119+
#endif
120+
121+
}
122+
123+
/**
124+
* @brief Configures the System clock source, PLL Multiplier and Divider factors,
125+
* AHB/APBx prescalers and Flash settings
126+
* @note This function should be called only once the RCC clock configuration
127+
* is reset to the default reset state (done in SystemInit() function).
128+
* @param None
129+
* @retval None
130+
*/
131+
void SetSysClock(void)
132+
{
133+
/* 1- Try to start with HSE and external clock */
134+
#if USE_PLL_HSE_EXTC != 0
135+
if (SetSysClock_PLL_HSE(1) == 0)
136+
#endif
137+
{
138+
/* 2- If fail try to start with HSE and external xtal */
139+
#if USE_PLL_HSE_XTAL != 0
140+
if (SetSysClock_PLL_HSE(0) == 0)
141+
#endif
142+
{
143+
/* 3- If fail start with HSI clock */
144+
if (SetSysClock_PLL_HSI() == 0) {
145+
while(1) {
146+
// [TODO] Put something here to tell the user that a problem occured...
147+
}
148+
}
149+
}
150+
}
151+
152+
/* Output clock on MCO1 pin(PA8) for debugging purpose */
153+
//HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); // 72 MHz or 64 MHz
154+
}
155+
156+
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
157+
/******************************************************************************/
158+
/* PLL (clocked by HSE) used as System clock source */
159+
/******************************************************************************/
160+
uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
161+
{
162+
RCC_ClkInitTypeDef RCC_ClkInitStruct;
163+
RCC_OscInitTypeDef RCC_OscInitStruct;
164+
165+
/* Enable HSE oscillator and activate PLL with HSE as source */
166+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
167+
if (bypass == 0) {
168+
RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */
169+
} else {
170+
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */
171+
}
172+
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
173+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
174+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
175+
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 72 MHz (8 MHz * 9)
176+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
177+
return 0; // FAIL
178+
}
179+
180+
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
181+
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
182+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 72 MHz
183+
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 72 MHz
184+
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 36 MHz
185+
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 72 MHz
186+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
187+
return 0; // FAIL
188+
}
189+
190+
/* Output clock on MCO1 pin(PA8) for debugging purpose */
191+
//HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz
192+
193+
return 1; // OK
194+
}
195+
#endif
196+
197+
/******************************************************************************/
198+
/* PLL (clocked by HSI) used as System clock source */
199+
/******************************************************************************/
200+
uint8_t SetSysClock_PLL_HSI(void)
201+
{
202+
RCC_ClkInitTypeDef RCC_ClkInitStruct;
203+
RCC_OscInitTypeDef RCC_OscInitStruct;
204+
205+
/* Enable HSI oscillator and activate PLL with HSI as source */
206+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
207+
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
208+
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
209+
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
210+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
211+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
212+
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64 MHz (8 MHz/2 * 16)
213+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
214+
return 0; // FAIL
215+
}
216+
217+
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
218+
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
219+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 64 MHz
220+
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 64 MHz
221+
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 32 MHz
222+
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 64 MHz
223+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
224+
return 0; // FAIL
225+
}
226+
227+
/* Output clock on MCO1 pin(PA8) for debugging purpose */
228+
//HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 8 MHz
229+
230+
return 1; // OK
231+
}
232+

0 commit comments

Comments
 (0)