Skip to content

Commit 66759fc

Browse files
committed
Add system_stm32f2xx.c
Only one time Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 8f3f63d commit 66759fc

File tree

1 file changed

+364
-0
lines changed

1 file changed

+364
-0
lines changed

system/STM32F2xx/system_stm32f2xx.c

+364
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
1+
/**
2+
******************************************************************************
3+
* @file system_stm32f2xx.c
4+
* @author MCD Application Team
5+
* @version V2.2.0
6+
* @date 17-March-2017
7+
* @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
8+
*
9+
* This file provides two functions and one global variable to be called from
10+
* user application:
11+
* - SystemInit(): This function is called at startup just after reset and
12+
* before branch to main program. This call is made inside
13+
* the "startup_stm32f2xx.s" file.
14+
*
15+
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
16+
* by the user application to setup the SysTick
17+
* timer or configure other parameters.
18+
*
19+
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
20+
* be called whenever the core clock is changed
21+
* during program execution.
22+
*
23+
******************************************************************************
24+
* @attention
25+
*
26+
* <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
27+
*
28+
* Redistribution and use in source and binary forms, with or without modification,
29+
* are permitted provided that the following conditions are met:
30+
* 1. Redistributions of source code must retain the above copyright notice,
31+
* this list of conditions and the following disclaimer.
32+
* 2. Redistributions in binary form must reproduce the above copyright notice,
33+
* this list of conditions and the following disclaimer in the documentation
34+
* and/or other materials provided with the distribution.
35+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
36+
* may be used to endorse or promote products derived from this software
37+
* without specific prior written permission.
38+
*
39+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
43+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49+
*
50+
******************************************************************************
51+
*/
52+
53+
/** @addtogroup CMSIS
54+
* @{
55+
*/
56+
57+
/** @addtogroup stm32f2xx_system
58+
* @{
59+
*/
60+
61+
/** @addtogroup STM32F2xx_System_Private_Includes
62+
* @{
63+
*/
64+
65+
#include "stm32f2xx.h"
66+
67+
#if !defined (HSE_VALUE)
68+
#define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
69+
#endif /* HSE_VALUE */
70+
71+
#if !defined (HSI_VALUE)
72+
#define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
73+
#endif /* HSI_VALUE */
74+
75+
/**
76+
* @}
77+
*/
78+
79+
/** @addtogroup STM32F2xx_System_Private_TypesDefinitions
80+
* @{
81+
*/
82+
83+
/**
84+
* @}
85+
*/
86+
87+
/** @addtogroup STM32F2xx_System_Private_Defines
88+
* @{
89+
*/
90+
/************************* Miscellaneous Configuration ************************/
91+
/*!< Uncomment the following line if you need to use external SRAM mounted
92+
on STM322xG_EVAL board as data memory */
93+
/* #define DATA_IN_ExtSRAM */
94+
95+
/*!< Uncomment the following line if you need to relocate your vector Table in
96+
Internal SRAM. */
97+
/* #define VECT_TAB_SRAM */
98+
#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
99+
This value must be a multiple of 0x200. */
100+
/******************************************************************************/
101+
102+
/**
103+
* @}
104+
*/
105+
106+
/** @addtogroup STM32F2xx_System_Private_Macros
107+
* @{
108+
*/
109+
110+
/**
111+
* @}
112+
*/
113+
114+
/** @addtogroup STM32F2xx_System_Private_Variables
115+
* @{
116+
*/
117+
118+
/* This variable can be updated in Three ways :
119+
1) by calling CMSIS function SystemCoreClockUpdate()
120+
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
121+
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
122+
Note: If you use this function to configure the system clock; then there
123+
is no need to call the 2 first functions listed above, since SystemCoreClock
124+
variable is updated automatically.
125+
*/
126+
uint32_t SystemCoreClock = F_CPU;
127+
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
128+
const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
129+
/**
130+
* @}
131+
*/
132+
133+
/** @addtogroup STM32F2xx_System_Private_FunctionPrototypes
134+
* @{
135+
*/
136+
137+
#ifdef DATA_IN_ExtSRAM
138+
static void SystemInit_ExtMemCtl(void);
139+
#endif /* DATA_IN_ExtSRAM */
140+
141+
/**
142+
* @}
143+
*/
144+
145+
/** @addtogroup STM32F2xx_System_Private_Functions
146+
* @{
147+
*/
148+
149+
/**
150+
* @brief Setup the microcontroller system
151+
* Initialize the Embedded Flash Interface, the PLL and update the
152+
* SystemFrequency variable.
153+
* @param None
154+
* @retval None
155+
*/
156+
void SystemInit(void)
157+
{
158+
/* Reset the RCC clock configuration to the default reset state ------------*/
159+
/* Set HSION bit */
160+
RCC->CR |= (uint32_t)0x00000001;
161+
162+
/* Reset CFGR register */
163+
RCC->CFGR = 0x00000000;
164+
165+
/* Reset HSEON, CSSON and PLLON bits */
166+
RCC->CR &= (uint32_t)0xFEF6FFFF;
167+
168+
/* Reset PLLCFGR register */
169+
RCC->PLLCFGR = 0x24003010;
170+
171+
/* Reset HSEBYP bit */
172+
RCC->CR &= (uint32_t)0xFFFBFFFF;
173+
174+
/* Disable all interrupts */
175+
RCC->CIR = 0x00000000;
176+
177+
#ifdef DATA_IN_ExtSRAM
178+
SystemInit_ExtMemCtl();
179+
#endif /* DATA_IN_ExtSRAM */
180+
181+
/* Configure the Vector Table location add offset address ------------------*/
182+
#ifdef VECT_TAB_SRAM
183+
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
184+
#else
185+
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
186+
#endif
187+
}
188+
189+
/**
190+
* @brief Update SystemCoreClock variable according to Clock Register Values.
191+
* The SystemCoreClock variable contains the core clock (HCLK), it can
192+
* be used by the user application to setup the SysTick timer or configure
193+
* other parameters.
194+
*
195+
* @note Each time the core clock (HCLK) changes, this function must be called
196+
* to update SystemCoreClock variable value. Otherwise, any configuration
197+
* based on this variable will be incorrect.
198+
*
199+
* @note - The system frequency computed by this function is not the real
200+
* frequency in the chip. It is calculated based on the predefined
201+
* constant and the selected clock source:
202+
*
203+
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
204+
*
205+
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
206+
*
207+
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
208+
* or HSI_VALUE(*) multiplied/divided by the PLL factors.
209+
*
210+
* (*) HSI_VALUE is a constant defined in stm32f2xx_hal_conf.h file (default value
211+
* 16 MHz) but the real value may vary depending on the variations
212+
* in voltage and temperature.
213+
*
214+
* (**) HSE_VALUE is a constant defined in stm32f2xx_hal_conf.h file (its value
215+
* depends on the application requirements), user has to ensure that HSE_VALUE
216+
* is same as the real frequency of the crystal used. Otherwise, this function
217+
* may have wrong result.
218+
*
219+
* - The result of this function could be not correct when using fractional
220+
* value for HSE crystal.
221+
*
222+
* @param None
223+
* @retval None
224+
*/
225+
void SystemCoreClockUpdate(void)
226+
{
227+
uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
228+
229+
/* Get SYSCLK source -------------------------------------------------------*/
230+
tmp = RCC->CFGR & RCC_CFGR_SWS;
231+
232+
switch (tmp)
233+
{
234+
case 0x00: /* HSI used as system clock source */
235+
SystemCoreClock = HSI_VALUE;
236+
break;
237+
case 0x04: /* HSE used as system clock source */
238+
SystemCoreClock = HSE_VALUE;
239+
break;
240+
case 0x08: /* PLL used as system clock source */
241+
242+
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
243+
SYSCLK = PLL_VCO / PLL_P
244+
*/
245+
pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
246+
pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
247+
248+
if (pllsource != 0)
249+
{
250+
/* HSE used as PLL clock source */
251+
pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
252+
}
253+
else
254+
{
255+
/* HSI used as PLL clock source */
256+
pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
257+
}
258+
259+
pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
260+
SystemCoreClock = pllvco/pllp;
261+
break;
262+
default:
263+
SystemCoreClock = HSI_VALUE;
264+
break;
265+
}
266+
/* Compute HCLK frequency --------------------------------------------------*/
267+
/* Get HCLK prescaler */
268+
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
269+
/* HCLK frequency */
270+
SystemCoreClock >>= tmp;
271+
}
272+
273+
#ifdef DATA_IN_ExtSRAM
274+
/**
275+
* @brief Setup the external memory controller.
276+
* Called in startup_stm32f2xx.s before jump to main.
277+
* This function configures the external SRAM mounted on STM322xG_EVAL board
278+
* This SRAM will be used as program data memory (including heap and stack).
279+
* @param None
280+
* @retval None
281+
*/
282+
void SystemInit_ExtMemCtl(void)
283+
{
284+
__IO uint32_t tmp = 0x00;
285+
286+
/*-- GPIOs Configuration -----------------------------------------------------*/
287+
/* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
288+
RCC->AHB1ENR |= 0x00000078;
289+
/* Delay after an RCC peripheral clock enabling */
290+
tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
291+
(void)(tmp);
292+
293+
/* Connect PDx pins to FSMC Alternate function */
294+
GPIOD->AFR[0] = 0x00CCC0CC;
295+
GPIOD->AFR[1] = 0xCCCCCCCC;
296+
/* Configure PDx pins in Alternate function mode */
297+
GPIOD->MODER = 0xAAAA0A8A;
298+
/* Configure PDx pins speed to 100 MHz */
299+
GPIOD->OSPEEDR = 0xFFFF0FCF;
300+
/* Configure PDx pins Output type to push-pull */
301+
GPIOD->OTYPER = 0x00000000;
302+
/* No pull-up, pull-down for PDx pins */
303+
GPIOD->PUPDR = 0x00000000;
304+
305+
/* Connect PEx pins to FSMC Alternate function */
306+
GPIOE->AFR[0] = 0xC00CC0CC;
307+
GPIOE->AFR[1] = 0xCCCCCCCC;
308+
/* Configure PEx pins in Alternate function mode */
309+
GPIOE->MODER = 0xAAAA828A;
310+
/* Configure PEx pins speed to 100 MHz */
311+
GPIOE->OSPEEDR = 0xFFFFC3CF;
312+
/* Configure PEx pins Output type to push-pull */
313+
GPIOE->OTYPER = 0x00000000;
314+
/* No pull-up, pull-down for PEx pins */
315+
GPIOE->PUPDR = 0x00000000;
316+
317+
/* Connect PFx pins to FSMC Alternate function */
318+
GPIOF->AFR[0] = 0x00CCCCCC;
319+
GPIOF->AFR[1] = 0xCCCC0000;
320+
/* Configure PFx pins in Alternate function mode */
321+
GPIOF->MODER = 0xAA000AAA;
322+
/* Configure PFx pins speed to 100 MHz */
323+
GPIOF->OSPEEDR = 0xFF000FFF;
324+
/* Configure PFx pins Output type to push-pull */
325+
GPIOF->OTYPER = 0x00000000;
326+
/* No pull-up, pull-down for PFx pins */
327+
GPIOF->PUPDR = 0x00000000;
328+
329+
/* Connect PGx pins to FSMC Alternate function */
330+
GPIOG->AFR[0] = 0x00CCCCCC;
331+
GPIOG->AFR[1] = 0x000000C0;
332+
/* Configure PGx pins in Alternate function mode */
333+
GPIOG->MODER = 0x00085AAA;
334+
/* Configure PGx pins speed to 100 MHz */
335+
GPIOG->OSPEEDR = 0x000CAFFF;
336+
/* Configure PGx pins Output type to push-pull */
337+
GPIOG->OTYPER = 0x00000000;
338+
/* No pull-up, pull-down for PGx pins */
339+
GPIOG->PUPDR = 0x00000000;
340+
341+
/*--FSMC Configuration -------------------------------------------------------*/
342+
/* Enable the FSMC interface clock */
343+
RCC->AHB3ENR |= 0x00000001;
344+
345+
/* Configure and enable Bank1_SRAM2 */
346+
FSMC_Bank1->BTCR[2] = 0x00001011;
347+
FSMC_Bank1->BTCR[3] = 0x00000201;
348+
FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
349+
}
350+
#endif /* DATA_IN_ExtSRAM */
351+
352+
353+
/**
354+
* @}
355+
*/
356+
357+
/**
358+
* @}
359+
*/
360+
361+
/**
362+
* @}
363+
*/
364+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)