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 
106 #include "stm32f30x.h"
107 
125 /* #define VECT_TAB_SRAM */
126 #define VECT_TAB_OFFSET 0x0
144  uint32_t SystemCoreClock = 72000000;
146  __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
156 static void SetSysClock(void);
157 
172 void SystemInit(void)
173 {
174  /* FPU settings ------------------------------------------------------------*/
175  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
176  SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
177  #endif
178 
179  /* Reset the RCC clock configuration to the default reset state ------------*/
180  /* Set HSION bit */
181  RCC->CR |= (uint32_t)0x00000001;
182 
183  /* Reset CFGR register */
184  RCC->CFGR &= 0xF87FC00C;
185 
186  /* Reset HSEON, CSSON and PLLON bits */
187  RCC->CR &= (uint32_t)0xFEF6FFFF;
188 
189  /* Reset HSEBYP bit */
190  RCC->CR &= (uint32_t)0xFFFBFFFF;
191 
192  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE bits */
193  RCC->CFGR &= (uint32_t)0xFF80FFFF;
194 
195  /* Reset PREDIV1[3:0] bits */
196  RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
197 
198  /* Reset USARTSW[1:0], I2CSW and TIMs bits */
199  RCC->CFGR3 &= (uint32_t)0xFF00FCCC;
200 
201  /* Disable all interrupts */
202  RCC->CIR = 0x00000000;
203 
204  /* Configure the System clock source, PLL Multiplier and Divider factors,
205  AHB/APBx prescalers and Flash settings ----------------------------------*/
206  SetSysClock();
207 
208 #ifdef VECT_TAB_SRAM
209  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
210 #else
211  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
212 #endif
213 }
214 
250 void SystemCoreClockUpdate (void)
251 {
252  uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;
253 
254  /* Get SYSCLK source -------------------------------------------------------*/
255  tmp = RCC->CFGR & RCC_CFGR_SWS;
256 
257  switch (tmp)
258  {
259  case 0x00: /* HSI used as system clock */
260  SystemCoreClock = HSI_VALUE;
261  break;
262  case 0x04: /* HSE used as system clock */
263  SystemCoreClock = HSE_VALUE;
264  break;
265  case 0x08: /* PLL used as system clock */
266  /* Get PLL clock source and multiplication factor ----------------------*/
267  pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
268  pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
269  pllmull = ( pllmull >> 18) + 2;
270 
271  if (pllsource == 0x00)
272  {
273  /* HSI oscillator clock divided by 2 selected as PLL clock entry */
274  SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
275  }
276  else
277  {
278  prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
279  /* HSE oscillator clock selected as PREDIV1 clock entry */
280  SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
281  }
282  break;
283  default: /* HSI used as system clock */
284  SystemCoreClock = HSI_VALUE;
285  break;
286  }
287  /* Compute HCLK clock frequency ----------------*/
288  /* Get HCLK prescaler */
289  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
290  /* HCLK clock frequency */
291  SystemCoreClock >>= tmp;
292 }
293 
301 static void SetSysClock(void)
302 {
303  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
304 
305 /******************************************************************************/
306 /* PLL (clocked by HSE) used as System clock source */
307 /******************************************************************************/
308 
309  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------*/
310  /* Enable HSE */
311  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
312 
313  /* Wait till HSE is ready and if Time out is reached exit */
314  do
315  {
316  HSEStatus = RCC->CR & RCC_CR_HSERDY;
317  StartUpCounter++;
318  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
319 
320  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
321  {
322  HSEStatus = (uint32_t)0x01;
323  }
324  else
325  {
326  HSEStatus = (uint32_t)0x00;
327  }
328 
329  if (HSEStatus == (uint32_t)0x01)
330  {
331  /* Enable Prefetch Buffer and set Flash Latency */
332  FLASH->ACR = FLASH_ACR_PRFTBE | (uint32_t)FLASH_ACR_LATENCY_1;
333 
334  /* HCLK = SYSCLK / 1 */
335  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
336 
337  /* PCLK2 = HCLK / 1 */
338  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
339 
340  /* PCLK1 = HCLK / 2 */
341  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
342 
343  /* PLL configuration */
344  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
345  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL9);
346 
347  /* Enable PLL */
348  RCC->CR |= RCC_CR_PLLON;
349 
350  /* Wait till PLL is ready */
351  while((RCC->CR & RCC_CR_PLLRDY) == 0)
352  {
353  }
354 
355  /* Select PLL as system clock source */
356  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
357  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
358 
359  /* Wait till PLL is used as system clock source */
360  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
361  {
362  }
363  }
364  else
365  { /* If HSE fails to start-up, the application will have wrong clock
366  configuration. User can add here some code to deal with this error */
367 
368  /* Go to infinite loop */
369  while (1)
370  {
371  }
372  }
373 }
374 
392 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
393 
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
__I uint8_t AHBPrescTable[16]
Definition: cmsis_system.c:184
#define VECT_TAB_OFFSET
Definition: cmsis_system.c:126
uint32_t SystemCoreClock
Definition: cmsis_system.c:182
static void SetSysClock(void)
Configures the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash...
Definition: cmsis_system.c:302