Siphon Regulator 1.0
Nanosattelite attitude determination and control system.
Loading...
Searching...
No Matches
system_stm32f1xx.c
Go to the documentation of this file.
1
58#include "stm32f1xx.h"
59
76#if !defined (HSE_VALUE)
77 #define HSE_VALUE 8000000U
79#endif /* HSE_VALUE */
80
81#if !defined (HSI_VALUE)
82 #define HSI_VALUE 8000000U
84#endif /* HSI_VALUE */
85
87#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
88/* #define DATA_IN_ExtSRAM */
89#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
90
91/* Note: Following vector table addresses must be defined in line with linker
92 configuration. */
96/* #define USER_VECT_TAB_ADDRESS */
97
98#if defined(USER_VECT_TAB_ADDRESS)
101/* #define VECT_TAB_SRAM */
102#if defined(VECT_TAB_SRAM)
103#define VECT_TAB_BASE_ADDRESS SRAM_BASE
105#define VECT_TAB_OFFSET 0x00000000U
107#else
108#define VECT_TAB_BASE_ADDRESS FLASH_BASE
110#define VECT_TAB_OFFSET 0x00000000U
112#endif /* VECT_TAB_SRAM */
113#endif /* USER_VECT_TAB_ADDRESS */
114
115/******************************************************************************/
116
133 /* This variable is updated in three ways:
134 1) by calling CMSIS function SystemCoreClockUpdate()
135 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
136 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
137 Note: If you use this function to configure the system clock; then there
138 is no need to call the 2 first functions listed above, since SystemCoreClock
139 variable is updated automatically.
140 */
141uint32_t SystemCoreClock = 8000000;
142const uint8_t AHBPrescTable[16U] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
143const uint8_t APBPrescTable[8U] = {0, 0, 0, 0, 1, 2, 3, 4};
144
153#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
154#ifdef DATA_IN_ExtSRAM
155 static void SystemInit_ExtMemCtl(void);
156#endif /* DATA_IN_ExtSRAM */
157#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
158
175void SystemInit (void)
176{
177#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
178 #ifdef DATA_IN_ExtSRAM
179 SystemInit_ExtMemCtl();
180 #endif /* DATA_IN_ExtSRAM */
181#endif
182
183 /* Configure the Vector Table location -------------------------------------*/
184#if defined(USER_VECT_TAB_ADDRESS)
185 SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
186#endif /* USER_VECT_TAB_ADDRESS */
187}
188
225{
226 uint32_t tmp = 0U, pllmull = 0U, pllsource = 0U;
227
228#if defined(STM32F105xC) || defined(STM32F107xC)
229 uint32_t prediv1source = 0U, prediv1factor = 0U, prediv2factor = 0U, pll2mull = 0U;
230#endif /* STM32F105xC */
231
232#if defined(STM32F100xB) || defined(STM32F100xE)
233 uint32_t prediv1factor = 0U;
234#endif /* STM32F100xB or STM32F100xE */
235
236 /* Get SYSCLK source -------------------------------------------------------*/
237 tmp = RCC->CFGR & RCC_CFGR_SWS;
238
239 switch (tmp)
240 {
241 case 0x00U: /* HSI used as system clock */
243 break;
244 case 0x04U: /* HSE used as system clock */
246 break;
247 case 0x08U: /* PLL used as system clock */
248
249 /* Get PLL clock source and multiplication factor ----------------------*/
250 pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
251 pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
252
253#if !defined(STM32F105xC) && !defined(STM32F107xC)
254 pllmull = ( pllmull >> 18U) + 2U;
255
256 if (pllsource == 0x00U)
257 {
258 /* HSI oscillator clock divided by 2 selected as PLL clock entry */
259 SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
260 }
261 else
262 {
263 #if defined(STM32F100xB) || defined(STM32F100xE)
264 prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
265 /* HSE oscillator clock selected as PREDIV1 clock entry */
266 SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
267 #else
268 /* HSE selected as PLL clock entry */
269 if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
270 {/* HSE oscillator clock divided by 2 */
271 SystemCoreClock = (HSE_VALUE >> 1U) * pllmull;
272 }
273 else
274 {
275 SystemCoreClock = HSE_VALUE * pllmull;
276 }
277 #endif
278 }
279#else
280 pllmull = pllmull >> 18U;
281
282 if (pllmull != 0x0DU)
283 {
284 pllmull += 2U;
285 }
286 else
287 { /* PLL multiplication factor = PLL input clock * 6.5 */
288 pllmull = 13U / 2U;
289 }
290
291 if (pllsource == 0x00U)
292 {
293 /* HSI oscillator clock divided by 2 selected as PLL clock entry */
294 SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
295 }
296 else
297 {/* PREDIV1 selected as PLL clock entry */
298
299 /* Get PREDIV1 clock source and division factor */
300 prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
301 prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
302
303 if (prediv1source == 0U)
304 {
305 /* HSE oscillator clock selected as PREDIV1 clock entry */
306 SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
307 }
308 else
309 {/* PLL2 clock selected as PREDIV1 clock entry */
310
311 /* Get PREDIV2 division factor and PLL2 multiplication factor */
312 prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4U) + 1U;
313 pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8U) + 2U;
314 SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
315 }
316 }
317#endif /* STM32F105xC */
318 break;
319
320 default:
322 break;
323 }
324
325 /* Compute HCLK clock frequency ----------------*/
326 /* Get HCLK prescaler */
327 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
328 /* HCLK clock frequency */
329 SystemCoreClock >>= tmp;
330}
331
332#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
339#ifdef DATA_IN_ExtSRAM
349void SystemInit_ExtMemCtl(void)
350{
351 __IO uint32_t tmpreg;
355 /* Enable FSMC clock */
356 RCC->AHBENR = 0x00000114U;
357
358 /* Delay after an RCC peripheral clock enabling */
359 tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
360
361 /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
362 RCC->APB2ENR = 0x000001E0U;
363
364 /* Delay after an RCC peripheral clock enabling */
365 tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN);
366
367 (void)(tmpreg);
368
369/* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
370/*---------------- SRAM Address lines configuration -------------------------*/
371/*---------------- NOE and NWE configuration --------------------------------*/
372/*---------------- NE3 configuration ----------------------------------------*/
373/*---------------- NBL0, NBL1 configuration ---------------------------------*/
374
375 GPIOD->CRL = 0x44BB44BBU;
376 GPIOD->CRH = 0xBBBBBBBBU;
377
378 GPIOE->CRL = 0xB44444BBU;
379 GPIOE->CRH = 0xBBBBBBBBU;
380
381 GPIOF->CRL = 0x44BBBBBBU;
382 GPIOF->CRH = 0xBBBB4444U;
383
384 GPIOG->CRL = 0x44BBBBBBU;
385 GPIOG->CRH = 0x444B4B44U;
386
387/*---------------- FSMC Configuration ---------------------------------------*/
388/*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
389
390 FSMC_Bank1->BTCR[4U] = 0x00001091U;
391 FSMC_Bank1->BTCR[5U] = 0x00110212U;
392}
393#endif /* DATA_IN_ExtSRAM */
394#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
395
#define HSI_VALUE
#define HSE_VALUE
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
const uint8_t AHBPrescTable[16U]
uint32_t SystemCoreClock
const uint8_t APBPrescTable[8U]