STSW-STLKT01: Projects/SensorTile/Applications/AudioLoop/Src/system_stm32l4xx.c Source File

STSW-STLKT01

system_stm32l4xx.c
1 
110 #include "stm32l4xx.h"
111 
112 #if !defined (HSE_VALUE)
113  #define HSE_VALUE ((uint32_t)8000000)
114 #endif /* HSE_VALUE */
115 
116 #if !defined (MSI_VALUE)
117  #define MSI_VALUE ((uint32_t)4000000)
118 #endif /* MSI_VALUE */
119 
120 #if !defined (HSI_VALUE)
121  #define HSI_VALUE ((uint32_t)16000000)
122 #endif /* HSI_VALUE */
123 
140 /************************* Miscellaneous Configuration ************************/
143 /* #define VECT_TAB_SRAM */
144 #define VECT_TAB_OFFSET 0x000
146 /******************************************************************************/
147 
162  /* The SystemCoreClock variable is updated in three ways:
163  1) by calling CMSIS function SystemCoreClockUpdate()
164  2) by calling HAL API function HAL_RCC_GetHCLKFreq()
165  3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
166  Note: If you use this function to configure the system clock; then there
167  is no need to call the 2 first functions listed above, since SystemCoreClock
168  variable is updated automatically.
169  */
170  uint32_t SystemCoreClock = 4000000;
171 
172  const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
173  const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
174  const uint32_t MSIRangeTable[12] = {100000, 200000, 400000, 800000, 1000000, 2000000, \
175  4000000, 8000000, 16000000, 24000000, 32000000, 48000000};
198 void SystemInit(void)
199 {
200  /* FPU settings ------------------------------------------------------------*/
201  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
202  SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
203  #endif
204  /* Reset the RCC clock configuration to the default reset state ------------*/
205  /* Set MSION bit */
206  RCC->CR |= RCC_CR_MSION;
207 
208  /* Reset CFGR register */
209  RCC->CFGR = 0x00000000;
210 
211  /* Reset HSEON, CSSON , HSION, and PLLON bits */
212  RCC->CR &= (uint32_t)0xEAF6FFFF;
213 
214  /* Reset PLLCFGR register */
215  RCC->PLLCFGR = 0x00001000;
216 
217  /* Reset HSEBYP bit */
218  RCC->CR &= (uint32_t)0xFFFBFFFF;
219 
220  /* Disable all interrupts */
221  RCC->CIER = 0x00000000;
222 
223  /* Configure the Vector Table location add offset address ------------------*/
224 #ifdef VECT_TAB_SRAM
225  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
226 #else
227  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
228 #endif
229 }
230 
273 void SystemCoreClockUpdate(void)
274 {
275  uint32_t tmp = 0, msirange = 0, pllvco = 0, pllr = 2, pllsource = 0, pllm = 2;
276 
277  /* Get MSI Range frequency--------------------------------------------------*/
278  if((RCC->CR & RCC_CR_MSIRGSEL) == RESET)
279  { /* MSISRANGE from RCC_CSR applies */
280  msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> 8;
281  }
282  else
283  { /* MSIRANGE from RCC_CR applies */
284  msirange = (RCC->CR & RCC_CR_MSIRANGE) >> 4;
285  }
286  /*MSI frequency range in HZ*/
287  msirange = MSIRangeTable[msirange];
288 
289  /* Get SYSCLK source -------------------------------------------------------*/
290  switch (RCC->CFGR & RCC_CFGR_SWS)
291  {
292  case 0x00: /* MSI used as system clock source */
293  SystemCoreClock = msirange;
294  break;
295 
296  case 0x04: /* HSI used as system clock source */
297  SystemCoreClock = HSI_VALUE;
298  break;
299 
300  case 0x08: /* HSE used as system clock source */
301  SystemCoreClock = HSE_VALUE;
302  break;
303 
304  case 0x0C: /* PLL used as system clock source */
305  /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
306  SYSCLK = PLL_VCO / PLLR
307  */
308  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
309  pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1 ;
310 
311  switch (pllsource)
312  {
313  case 0x02: /* HSI used as PLL clock source */
314  pllvco = (HSI_VALUE / pllm);
315  break;
316 
317  case 0x03: /* HSE used as PLL clock source */
318  pllvco = (HSE_VALUE / pllm);
319  break;
320 
321  default: /* MSI used as PLL clock source */
322  pllvco = (msirange / pllm);
323  break;
324  }
325  pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8);
326  pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1) * 2;
327  SystemCoreClock = pllvco/pllr;
328  break;
329 
330  default:
331  SystemCoreClock = msirange;
332  break;
333  }
334  /* Compute HCLK clock frequency --------------------------------------------*/
335  /* Get HCLK prescaler */
336  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
337  /* HCLK clock frequency */
338  SystemCoreClock >>= tmp;
339 }
340 
341 
354 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
void SystemInit(void)
Setup the microcontroller system.
#define VECT_TAB_OFFSET
#define HSI_VALUE
#define HSE_VALUE
Generated by   doxygen 1.8.13