APM:Libraries
system_stm32f4xx.c
Go to the documentation of this file.
1 
107 /*
108  M N Q P MHz
109  4 168 7 2 168
110  4 360 15 4 180
111  4 192 8 2 192
112  4 216 9 2 216
113  4 240 10 2 240
114  4 264 11 2 264
115 
116 
117 */
118 
131 #include "stm32f4xx.h"
132 
133 void systemInit(uint8_t oc);
134 
154 /* #define VECT_TAB_SRAM */
155 #define VECT_TAB_OFFSET FLASH_OFFSET
159 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
160 #define PLL_M 4
161 #define PLL_N 168
163 /* SYSCLK = PLL_VCO / PLL_P */
164 #define PLL_P 2
166 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
167 #define PLL_Q 7
169 /* PLLI2S_VCO = (HSE_VALUE Or HSI_VALUE / PLL_M) * PLLI2S_N
170  I2SCLK = PLLI2S_VCO / PLLI2S_R */
171 #define PLLI2S_N 192
172 #define PLLI2S_R 5
190  uint32_t SystemCoreClock = 168000000;
192  __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
205 void SetSysClock(uint8_t oc);
206 
222 void systemInit(uint8_t oc)
223 {
224  //SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
225  /* Reset the RCC clock configuration to the default reset state ------------*/
226  /* Set HSION bit */
227  RCC->CR |= (uint32_t)0x00000001;
228 
229  /* Reset CFGR register */
230  RCC->CFGR = 0x00000000;
231 
232  /* Reset HSEON, CSSON and PLLON bits */
233  RCC->CR &= (uint32_t)0xFEF6FFFF;
234 
235  /* Reset PLLCFGR register */
236  RCC->PLLCFGR = 0x24003010;
237 
238  /* Reset HSEBYP bit */
239  RCC->CR &= (uint32_t)0xFFFBFFFF;
240 
241  /* Disable all interrupts */
242  RCC->CIR = 0x00000000;
243 
244 
245  /* Configure the System clock source, PLL Multiplier and Divider factors,
246  AHB/APBx prescalers and Flash settings ----------------------------------*/
247  SetSysClock(oc);
248 
249  // Configure the Vector Table location add offset address ------------------
250  extern unsigned __isr_vector_start; // from linker
251  SCB->VTOR = (uint32_t)&__isr_vector_start;
252 }
253 
290 void SystemCoreClockUpdate(void)
291 {
292  uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
293 
294  /* Get SYSCLK source -------------------------------------------------------*/
295  tmp = RCC->CFGR & RCC_CFGR_SWS;
296 
297  switch (tmp)
298  {
299  case 0x00: /* HSI used as system clock source */
300  SystemCoreClock = HSI_VALUE;
301  break;
302  case 0x04: /* HSE used as system clock source */
304  break;
305  case 0x08: /* PLL used as system clock source */
306 
307  /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
308  SYSCLK = PLL_VCO / PLL_P
309  */
310  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
311  pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
312 
313  if (pllsource != 0)
314  {
315  /* HSE used as PLL clock source */
316  pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
317  }
318  else
319  {
320  /* HSI used as PLL clock source */
321  pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
322  }
323 
324  pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
325  SystemCoreClock = pllvco/pllp;
326  break;
327  default:
328  SystemCoreClock = HSI_VALUE;
329  break;
330  }
331  /* Compute HCLK frequency --------------------------------------------------*/
332  /* Get HCLK prescaler */
333  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
334  /* HCLK frequency */
335  SystemCoreClock >>= tmp;
336 }
337 
338 extern void __error(uint32_t num, uint32_t pc, uint32_t lr, uint32_t flag);
339 
348 void SetSysClock(uint8_t oc)
349 {
350 /******************************************************************************/
351 /* PLL (clocked by HSE) used as System clock source */
352 /******************************************************************************/
353  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
354 
355  /* Enable HSE */
356  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
357 
358  /* Wait till HSE is ready and if Time out is reached exit */
359  do
360  {
361  HSEStatus = RCC->CR & RCC_CR_HSERDY;
362  StartUpCounter++;
363  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
364 
365  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
366  {
367  HSEStatus = (uint32_t)0x01;
368  }
369  else
370  {
371  HSEStatus = (uint32_t)0x00;
372  }
373 
374  if (HSEStatus == (uint32_t)0x01)
375  {
376  /* Enable high performance mode, System frequency up to 168 MHz */
377  RCC->APB1ENR |= RCC_APB1ENR_PWREN;
378  PWR->CR |= PWR_CR_PMODE;
379 
380  /* HCLK = SYSCLK / 1*/
381  RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
382 
383  /* PCLK2 = HCLK / 2*/
384  RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
385 
386  /* PCLK1 = HCLK / 4*/
387  RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
388 
389 /*
390  M N Q P MHz
391 0 4 168 7 2 168
392 1 4 360 15 4 180
393 2 4 192 8 2 192
394 3 4 216 9 2 216
395 4 4 240 10 2 240
396 5 4 264 11 2 264
397 
398 */
399  uint8_t pll_m=4, pll_q, pll_p=2;
400  uint16_t pll_n;
401  uint8_t flash_latency;
402  uint32_t cr_flags = RCC_CR_CSSON;
403  switch(oc) {
404  case 0:
405  default:
406  pll_n=168; pll_q=7;
407  flash_latency = FLASH_ACR_LATENCY_5WS;
408  SystemCoreClock = 168000000;
409  break;
410  case 1:
411  pll_n=360; pll_q=15; pll_p=4;
412  flash_latency = FLASH_ACR_LATENCY_5WS;
413  SystemCoreClock = 180000000;
414 // cr_flags = 0; // CSS don't support this mode
415  break;
416  case 2:
417  pll_n=192; pll_q=8;
418  flash_latency = FLASH_ACR_LATENCY_6WS;
419  SystemCoreClock = 192000000;
420  break;
421  case 3:
422  pll_n=216; pll_q=9;
423  flash_latency = FLASH_ACR_LATENCY_6WS;
424  SystemCoreClock = 216000000;
425  break;
426  case 4:
427  pll_n=240; pll_q=10;
428  flash_latency = FLASH_ACR_LATENCY_7WS;
429  SystemCoreClock = 240000000;
430  break;
431  case 5:
432  pll_n=264; pll_q=11;
433  flash_latency = FLASH_ACR_LATENCY_7WS;
434  SystemCoreClock = 264000000;
435  break;
436  }
437 
438  /* Configure the main PLL */
439 /* RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
440  (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24); */
441 
442  RCC->PLLCFGR = pll_m | (pll_n << 6) | (((pll_p >> 1) -1) << 16) |
443  (RCC_PLLCFGR_PLLSRC_HSE) | (pll_q << 24);
444 
445  /* Enable the main PLL */
446  RCC->CR |= RCC_CR_PLLON | cr_flags;
447 
448 
449  /* Wait till the main PLL is ready */
450  while((RCC->CR & RCC_CR_PLLRDY) == 0) { }
451 
452 
453  /* Configure Flash no-prefetch, Instruction cache, Data cache and wait state */
454  FLASH->ACR = FLASH_ACR_ICEN | FLASH_ACR_DCEN | flash_latency;
455 
456  /* Select the main PLL as system clock source */
457  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
458  RCC->CFGR |= RCC_CFGR_SW_PLL;
459 
460  /* Wait till the main PLL is used as system clock source */
461  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL) {}
462 
463  FLASH->ACR |= FLASH_ACR_PRFTEN; // enable prefetch. this greatly increases both noice and speed
464 
465  // also see http://radiokot.ru/forum/viewtopic.php?f=59&t=117260
466 
467  }
468  else
469  { /* If HSE fails to start-up, the application will have wrong clock
470  configuration. User can add here some code to deal with this error */
471  __error(12,0,0,0);
472  }
473 
474 /******************************************************************************/
475 /* I2S clock configuration */
476 /******************************************************************************/
477  /* PLLI2S clock used as I2S clock source */
478  RCC->CFGR &= ~RCC_CFGR_I2SSRC;
479 
480  /* Configure PLLI2S */
481  RCC->PLLI2SCFGR = (PLLI2S_N << 6) | (PLLI2S_R << 28);
482 #if 0 // we don't use I2S
483  /* Enable PLLI2S */
484  RCC->CR |= ((uint32_t)RCC_CR_PLLI2SON);
485 
486  /* Wait till PLLI2S is ready */
487  while((RCC->CR & RCC_CR_PLLI2SRDY) == 0) { }
488 #endif
489 }
490 
491 
uint32_t SystemCoreClock
#define PLLI2S_R
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
#define HSE_VALUE
Definition: board.h:18
void SetSysClock(uint8_t oc)
Configures the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash...
void __error(uint32_t num, uint32_t pc, uint32_t lr, uint32_t flag)
void uint32_t uint32_t lr
Definition: systick.h:80
__I uint8_t AHBPrescTable[16]
#define PLLI2S_N
unsigned __isr_vector_start
void uint32_t uint32_t uint32_t flag
Definition: systick.h:80
void uint32_t num
Definition: systick.h:80
void systemInit(uint8_t oc)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...