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