Демонстрационная прошивка отладочного комплекта МТС NB-IoT
system_stm32l1xx.c
См. документацию.
1 
102 #include "stm32l1xx.h"
103 
122 /* #define VECT_TAB_SRAM */
123 #define VECT_TAB_OFFSET 0x0
140 uint32_t SystemCoreClock = 32000000;
141 __I uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48};
142 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
143 
152 static void SetSysClock(void);
153 
169 void SystemInit (void)
170 {
172  RCC->CR |= (uint32_t)0x00000100;
173 
175  RCC->CFGR &= (uint32_t)0x88FFC00C;
176 
178  RCC->CR &= (uint32_t)0xEEFEFFFE;
179 
181  RCC->CR &= (uint32_t)0xFFFBFFFF;
182 
184  RCC->CFGR &= (uint32_t)0xFF02FFFF;
185 
187  RCC->CIR = 0x00000000;
188 
189  /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
190  SetSysClock();
191 
192 #ifdef VECT_TAB_SRAM
193  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
194 #else
195  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
196 #endif
197 }
198 
237 void SystemCoreClockUpdate (void)
238 {
239  uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, msirange = 0;
240 
241  /* Get SYSCLK source -------------------------------------------------------*/
242  tmp = RCC->CFGR & RCC_CFGR_SWS;
243 
244  switch (tmp)
245  {
246  case 0x00: /* MSI used as system clock */
247  msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
248  SystemCoreClock = (32768 * (1 << (msirange + 1)));
249  break;
250  case 0x04: /* HSI used as system clock */
251  SystemCoreClock = HSI_VALUE;
252  break;
253  case 0x08: /* HSE used as system clock */
254  SystemCoreClock = HSE_VALUE;
255  break;
256  case 0x0C: /* PLL used as system clock */
257  /* Get PLL clock source and multiplication factor ----------------------*/
258  pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
259  plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
260  pllmul = PLLMulTable[(pllmul >> 18)];
261  plldiv = (plldiv >> 22) + 1;
262 
263  pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
264 
265  if (pllsource == 0x00)
266  {
267  /* HSI oscillator clock selected as PLL clock entry */
268  SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
269  }
270  else
271  {
272  /* HSE selected as PLL clock entry */
273  SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
274  }
275  break;
276  default: /* MSI used as system clock */
277  msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
278  SystemCoreClock = (32768 * (1 << (msirange + 1)));
279  break;
280  }
281  /* Compute HCLK clock frequency --------------------------------------------*/
282  /* Get HCLK prescaler */
283  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
284  /* HCLK clock frequency */
285  SystemCoreClock >>= tmp;
286 }
287 
296 static void SetSysClock(void)
297 {
298  __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
299 
300  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
301  /* Enable HSI */
302  RCC->CR |= ((uint32_t)RCC_CR_HSION);
303 
304  /* Wait till HSI is ready and if Time out is reached exit */
305  do
306  {
307  HSIStatus = RCC->CR & RCC_CR_HSIRDY;
308  } while((HSIStatus == 0) && (StartUpCounter != HSI_STARTUP_TIMEOUT));
309 
310  if ((RCC->CR & RCC_CR_HSIRDY) != RESET)
311  {
312  HSIStatus = (uint32_t)0x01;
313  }
314  else
315  {
316  HSIStatus = (uint32_t)0x00;
317  }
318 
319  if (HSIStatus == (uint32_t)0x01)
320  {
321  /* Enable 64-bit access */
322  FLASH->ACR |= FLASH_ACR_ACC64;
323 
324  /* Enable Prefetch Buffer */
325  FLASH->ACR |= FLASH_ACR_PRFTEN;
326 
327  /* Flash 1 wait state */
328  FLASH->ACR |= FLASH_ACR_LATENCY;
329 
330 
331  /* Power enable */
332  RCC->APB1ENR |= RCC_APB1ENR_PWREN;
333 
334  /* Select the Voltage Range 1 (1.8 V) */
335  PWR->CR = PWR_CR_VOS_0;
336 
337 
338  /* Wait Until the Voltage Regulator is ready */
339  while((PWR->CSR & PWR_CSR_VOSF) != RESET)
340  {
341  }
342 
343  /* HCLK = SYSCLK /1*/
344  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
345 
346  /* PCLK2 = HCLK /1*/
347  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
348 
349  /* PCLK1 = HCLK /1*/
350  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
351 
352  /* PLL configuration */
353  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL |
354  RCC_CFGR_PLLDIV));
355  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI | RCC_CFGR_PLLMUL6 | RCC_CFGR_PLLDIV3);
356 
357  /* Enable PLL */
358  RCC->CR |= RCC_CR_PLLON;
359 
360  /* Wait till PLL is ready */
361  while((RCC->CR & RCC_CR_PLLRDY) == 0)
362  {
363  }
364 
365  /* Select PLL as system clock source */
366  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
367  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
368 
369  /* Wait till PLL is used as system clock source */
370  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
371  {
372  }
373  }
374  else
375  {
376  /* If HSI fails to start-up, the application will have wrong clock
377  configuration. User can add here some code to deal with this error */
378  }
379 }
380 
393 /******************* (C) COPYRIGHT 2012 STMicroelectronics *****END OF FILE****/
SystemInit
void SystemInit(void)
Setup the microcontroller system. Initialize the Embedded Flash Interface, the PLL and update the Sys...
Definition: system_stm32l1xx.c:170
VECT_TAB_OFFSET
#define VECT_TAB_OFFSET
Definition: system_stm32l1xx.c:123
SystemCoreClockUpdate
void SystemCoreClockUpdate(void)
Update SystemCoreClock according to Clock Register Values The SystemCoreClock variable contains the c...
Definition: system_stm32l1xx.c:238