dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
system_stm32f0xx.c
Go to the documentation of this file.
1 
77 #include "stm32f0xx.h"
78 
109 uint32_t SystemCoreClock = 40000000;
110 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
111 
120 static void SetSysClock(void);
121 
136 void SystemInit (void)
137 {
138  /* Set HSION bit */
139  RCC->CR |= (uint32_t)0x00000001;
140 
141 #if defined(STM32F051)
142  /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */
143  RCC->CFGR &= (uint32_t)0xF8FFB80C;
144 #else
145  /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE, MCOSEL[2:0], MCOPRE[2:0] and PLLNODIV bits */
146  RCC->CFGR &= (uint32_t)0x08FFB80C;
147 #endif /* STM32F051 */
148 
149  /* Reset HSEON, CSSON and PLLON bits */
150  RCC->CR &= (uint32_t)0xFEF6FFFF;
151 
152  /* Reset HSEBYP bit */
153  RCC->CR &= (uint32_t)0xFFFBFFFF;
154 
155  /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
156  RCC->CFGR &= (uint32_t)0xFFC0FFFF;
157 
158  /* Reset PREDIV1[3:0] bits */
159  RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
160 
161  /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
162  RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;
163 
164  /* Reset HSI14 bit */
165  RCC->CR2 &= (uint32_t)0xFFFFFFFE;
166 
167  /* Disable all interrupts */
168  RCC->CIR = 0x00000000;
169 
170  /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
171  SetSysClock();
172 }
173 
209 {
210  uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;
211 
212  /* Get SYSCLK source -------------------------------------------------------*/
213  tmp = RCC->CFGR & RCC_CFGR_SWS;
214 
215  switch (tmp)
216  {
217  case 0x00: /* HSI used as system clock */
218  SystemCoreClock = HSI_VALUE;
219  break;
220  case 0x04: /* HSE used as system clock */
221  SystemCoreClock = HSE_VALUE;
222  break;
223  case 0x08: /* PLL used as system clock */
224  /* Get PLL clock source and multiplication factor ----------------------*/
225  pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
226  pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
227  pllmull = ( pllmull >> 18) + 2;
228 
229  if (pllsource == 0x00)
230  {
231  /* HSI oscillator clock divided by 2 selected as PLL clock entry */
232  SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
233  }
234  else
235  {
236  prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
237  /* HSE oscillator clock selected as PREDIV1 clock entry */
238  SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
239  }
240  break;
241  default: /* HSI used as system clock */
242  SystemCoreClock = HSI_VALUE;
243  break;
244  }
245  /* Compute HCLK clock frequency ----------------*/
246  /* Get HCLK prescaler */
247  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
248  /* HCLK clock frequency */
249  SystemCoreClock >>= tmp;
250 }
251 
259 static void SetSysClock(void)
260 {
261  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
262 
263 /******************************************************************************/
264 /* PLL (clocked by HSE) used as System clock source */
265 /******************************************************************************/
266 
267  /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
268  /* Enable HSE */
269  RCC->CR |= ((uint32_t)RCC_CR_HSEON);
270 
271  /* Wait till HSE is ready and if Time out is reached exit */
272  do
273  {
274  HSEStatus = RCC->CR & RCC_CR_HSERDY;
275  StartUpCounter++;
276  } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
277 
278  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
279  {
280  HSEStatus = (uint32_t)0x01;
281  }
282  else
283  {
284  HSEStatus = (uint32_t)0x00;
285  }
286 
287  if (HSEStatus == (uint32_t)0x01)
288  {
289  /* Enable Prefetch Buffer and set Flash Latency */
290  FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
291 
292  /* HCLK = SYSCLK */
293  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
294 
295  /* PCLK = HCLK */
296  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;
297 
298  /* PLL configuration */
299  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
300  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL5);
301 
302  /* Enable PLL */
303  RCC->CR |= RCC_CR_PLLON;
304 
305  /* Wait till PLL is ready */
306  while((RCC->CR & RCC_CR_PLLRDY) == 0)
307  {
308  }
309 
310  /* Select PLL as system clock source */
311  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
312  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
313 
314  /* Wait till PLL is used as system clock source */
315  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
316  {
317  }
318  }
319  else
320  { /* If HSE fails to start-up, the application will have wrong clock
321  configuration. User can add here some code to deal with this error */
322  }
323 }
324 
342 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void SystemCoreClockUpdate(void)
Update SystemCoreClock according to Clock Register Values The SystemCoreClock variable contains the c...
__I uint8_t AHBPrescTable[16]
void SystemInit(void)
Setup the microcontroller system. Initialize the Embedded Flash Interface, the PLL and update the Sys...
static void SetSysClock(void)
Configures the System clock frequency, AHB/APBx prescalers and Flash settings.
uint32_t SystemCoreClock