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