dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cmsis_system.c
Go to the documentation of this file.
1 
44 #include "stm32f4xx.h"
45 
62 /************************* Miscellaneous Configuration ************************/
65 /* #define DATA_IN_ExtSRAM */
66 
69 /* #define VECT_TAB_SRAM */
70 #define VECT_TAB_OFFSET 0x00
72 /******************************************************************************/
73 
74 /************************* PLL Parameters *************************************/
75 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
76 #define PLL_M 16
77 #define PLL_N 336
78 
79 /* SYSCLK = PLL_VCO / PLL_P */
80 #define PLL_P 2
81 
82 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
83 #define PLL_Q 7
84 
85 /******************************************************************************/
86 
103  uint32_t SystemCoreClock = 168000000;
105  __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
115 static void SetSysClock(void);
116 #ifdef DATA_IN_ExtSRAM
117  static void SystemInit_ExtMemCtl(void);
118 #endif /* DATA_IN_ExtSRAM */
119 
134 void SystemInit(void)
135 {
136  /* FPU settings ------------------------------------------------------------*/
137  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
138  SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
139  #endif
140 
141  /* Reset the RCC clock configuration to the default reset state ------------*/
142  /* Set HSION bit */
143  RCC->CR |= (uint32_t)0x00000001;
144 
145  /* Reset CFGR register */
146  RCC->CFGR = 0x00000000;
147 
148  /* Reset HSEON, CSSON and PLLON bits */
149  RCC->CR &= (uint32_t)0xFEF6FFFF;
150 
151  /* Reset PLLCFGR register */
152  RCC->PLLCFGR = 0x24003010;
153 
154  /* Reset HSEBYP bit */
155  RCC->CR &= (uint32_t)0xFFFBFFFF;
156 
157  /* Disable all interrupts */
158  RCC->CIR = 0x00000000;
159 
160 #ifdef DATA_IN_ExtSRAM
161  SystemInit_ExtMemCtl();
162 #endif /* DATA_IN_ExtSRAM */
163 
164  /* Configure the System clock source, PLL Multiplier and Divider factors,
165  AHB/APBx prescalers and Flash settings ----------------------------------*/
166  SetSysClock();
167 
168  /* Configure the Vector Table location add offset address ------------------*/
169 #ifdef VECT_TAB_SRAM
170  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
171 #else
172  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
173 #endif
174 }
175 
211 void SystemCoreClockUpdate(void)
212 {
213  uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
214 
215  /* Get SYSCLK source -------------------------------------------------------*/
216  tmp = RCC->CFGR & RCC_CFGR_SWS;
217 
218  switch (tmp)
219  {
220  case 0x00: /* HSI used as system clock source */
221  SystemCoreClock = HSI_VALUE;
222  break;
223  case 0x04: /* HSE used as system clock source */
224  SystemCoreClock = HSE_VALUE;
225  break;
226  case 0x08: /* PLL used as system clock source */
227 
228  /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
229  SYSCLK = PLL_VCO / PLL_P
230  */
231  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
232  pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
233 
234  if (pllsource != 0)
235  {
236  /* HSE used as PLL clock source */
237  pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
238  }
239  else
240  {
241  /* HSI used as PLL clock source */
242  pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
243  }
244 
245  pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
246  SystemCoreClock = pllvco/pllp;
247  break;
248  default:
249  SystemCoreClock = HSI_VALUE;
250  break;
251  }
252  /* Compute HCLK frequency --------------------------------------------------*/
253  /* Get HCLK prescaler */
254  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
255  /* HCLK frequency */
256  SystemCoreClock >>= tmp;
257 }
258 
266 static void SetSysClock(void)
267 {
268 /******************************************************************************/
269 /* PLL (clocked by HSE) used as System clock source */
270 /******************************************************************************/
271  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
272 
273  /* Enable HSE */
274  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
275 
276  /* Wait till HSE is ready and if Time out is reached exit */
277  do
278  {
279  HSEStatus = RCC->CR & RCC_CR_HSERDY;
280  StartUpCounter++;
281  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
282 
283  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
284  {
285  HSEStatus = (uint32_t)0x01;
286  }
287  else
288  {
289  HSEStatus = (uint32_t)0x00;
290  }
291 
292  if (HSEStatus == (uint32_t)0x01)
293  {
294  /* Select regulator voltage output Scale 1 mode, System frequency up to 168 MHz */
295  RCC->APB1ENR |= RCC_APB1ENR_PWREN;
296  PWR->CR |= PWR_CR_VOS;
297 
298  /* HCLK = SYSCLK / 1*/
299  RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
300 
301  /* PCLK2 = HCLK / 2*/
302  RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
303 
304  /* PCLK1 = HCLK / 4*/
305  RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
306 
307  /* Configure the main PLL */
308  RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
309  (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
310 
311  /* Enable the main PLL */
312  RCC->CR |= RCC_CR_PLLON;
313 
314  /* Wait till the main PLL is ready */
315  while((RCC->CR & RCC_CR_PLLRDY) == 0);
316 
317  /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
318  FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
319 
320  /* Select the main PLL as system clock source */
321  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
322  RCC->CFGR |= RCC_CFGR_SW_PLL;
323 
324  /* Wait till the main PLL is used as system clock source */
325  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
326  }
327  else
328  { /* If HSE fails to start-up, the application will have wrong clock
329  configuration. User can add here some code to deal with this error */
330 
331  /* better to hang here than to start with a wrong clock */
332  while (1);
333  }
334 
335 }
336 
342 #ifdef DATA_IN_ExtSRAM
343 
350 void SystemInit_ExtMemCtl(void)
351 {
352 /*-- GPIOs Configuration -----------------------------------------------------*/
353 /*
354  +-------------------+--------------------+------------------+------------------+
355  + SRAM pins assignment +
356  +-------------------+--------------------+------------------+------------------+
357  | PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 |
358  | PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 |
359  | PD4 <-> FSMC_NOE | PE3 <-> FSMC_A19 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 |
360  | PD5 <-> FSMC_NWE | PE4 <-> FSMC_A20 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 |
361  | PD8 <-> FSMC_D13 | PE7 <-> FSMC_D4 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 |
362  | PD9 <-> FSMC_D14 | PE8 <-> FSMC_D5 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 |
363  | PD10 <-> FSMC_D15 | PE9 <-> FSMC_D6 | PF12 <-> FSMC_A6 | PG9 <-> FSMC_NE2 |
364  | PD11 <-> FSMC_A16 | PE10 <-> FSMC_D7 | PF13 <-> FSMC_A7 |------------------+
365  | PD12 <-> FSMC_A17 | PE11 <-> FSMC_D8 | PF14 <-> FSMC_A8 |
366  | PD13 <-> FSMC_A18 | PE12 <-> FSMC_D9 | PF15 <-> FSMC_A9 |
367  | PD14 <-> FSMC_D0 | PE13 <-> FSMC_D10 |------------------+
368  | PD15 <-> FSMC_D1 | PE14 <-> FSMC_D11 |
369  | | PE15 <-> FSMC_D12 |
370  +-------------------+--------------------+
371 */
372  /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
373  RCC->AHB1ENR = 0x00000078;
374 
375  /* Connect PDx pins to FSMC Alternate function */
376  GPIOD->AFR[0] = 0x00cc00cc;
377  GPIOD->AFR[1] = 0xcc0ccccc;
378  /* Configure PDx pins in Alternate function mode */
379  GPIOD->MODER = 0xaaaa0a0a;
380  /* Configure PDx pins speed to 100 MHz */
381  GPIOD->OSPEEDR = 0xffff0f0f;
382  /* Configure PDx pins Output type to push-pull */
383  GPIOD->OTYPER = 0x00000000;
384  /* No pull-up, pull-down for PDx pins */
385  GPIOD->PUPDR = 0x00000000;
386 
387  /* Connect PEx pins to FSMC Alternate function */
388  GPIOE->AFR[0] = 0xc00cc0cc;
389  GPIOE->AFR[1] = 0xcccccccc;
390  /* Configure PEx pins in Alternate function mode */
391  GPIOE->MODER = 0xaaaa828a;
392  /* Configure PEx pins speed to 100 MHz */
393  GPIOE->OSPEEDR = 0xffffc3cf;
394  /* Configure PEx pins Output type to push-pull */
395  GPIOE->OTYPER = 0x00000000;
396  /* No pull-up, pull-down for PEx pins */
397  GPIOE->PUPDR = 0x00000000;
398 
399  /* Connect PFx pins to FSMC Alternate function */
400  GPIOF->AFR[0] = 0x00cccccc;
401  GPIOF->AFR[1] = 0xcccc0000;
402  /* Configure PFx pins in Alternate function mode */
403  GPIOF->MODER = 0xaa000aaa;
404  /* Configure PFx pins speed to 100 MHz */
405  GPIOF->OSPEEDR = 0xff000fff;
406  /* Configure PFx pins Output type to push-pull */
407  GPIOF->OTYPER = 0x00000000;
408  /* No pull-up, pull-down for PFx pins */
409  GPIOF->PUPDR = 0x00000000;
410 
411  /* Connect PGx pins to FSMC Alternate function */
412  GPIOG->AFR[0] = 0x00cccccc;
413  GPIOG->AFR[1] = 0x000000c0;
414  /* Configure PGx pins in Alternate function mode */
415  GPIOG->MODER = 0x00080aaa;
416  /* Configure PGx pins speed to 100 MHz */
417  GPIOG->OSPEEDR = 0x000c0fff;
418  /* Configure PGx pins Output type to push-pull */
419  GPIOG->OTYPER = 0x00000000;
420  /* No pull-up, pull-down for PGx pins */
421  GPIOG->PUPDR = 0x00000000;
422 
423 /*-- FSMC Configuration ------------------------------------------------------*/
424  /* Enable the FSMC interface clock */
425  RCC->AHB3ENR = 0x00000001;
426 
427  /* Configure and enable Bank1_SRAM2 */
428  FSMC_Bank1->BTCR[2] = 0x00001015;
429  FSMC_Bank1->BTCR[3] = 0x00010603;
430  FSMC_Bank1E->BWTR[2] = 0x0fffffff;
431 /*
432  Bank1_SRAM2 is configured as follow:
433 
434  p.FSMC_AddressSetupTime = 3;
435  p.FSMC_AddressHoldTime = 0;
436  p.FSMC_DataSetupTime = 6;
437  p.FSMC_BusTurnAroundDuration = 1;
438  p.FSMC_CLKDivision = 0;
439  p.FSMC_DataLatency = 0;
440  p.FSMC_AccessMode = FSMC_AccessMode_A;
441 
442  FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
443  FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
444  FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_PSRAM;
445  FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
446  FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
447  FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
448  FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
449  FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
450  FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
451  FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
452  FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
453  FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
454  FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
455  FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
456  FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
457 */
458 
459 }
460 #endif /* DATA_IN_ExtSRAM */
461 
462 
479 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
Definition: cmsis_system.c:289
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
Definition: cmsis_system.c:213
#define PLL_Q
Definition: cmsis_system.c:84
#define PLL_M
Definition: cmsis_system.c:77
__I uint8_t AHBPrescTable[16]
Definition: cmsis_system.c:184
static void SetSysClock(void)
Configures the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash...
Definition: cmsis_system.c:267
#define VECT_TAB_OFFSET
Definition: cmsis_system.c:70
uint32_t SystemCoreClock
Definition: cmsis_system.c:182