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