STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers
system_stm32l4xx.c
Go to the documentation of this file.
1 
63 /* Includes ------------------------------------------------------------------*/
64 #include "stm32l4xx.h"
65 
66 #if !defined(HSE_VALUE)
68 #define HSE_VALUE 8000000U
69 #endif /* HSE_VALUE */
70 
71 #if !defined(MSI_VALUE)
73 #define MSI_VALUE 4000000U
74 #endif /* MSI_VALUE */
75 
76 #if !defined(HSI_VALUE)
78 #define HSI_VALUE 16000000U
79 #endif /* HSI_VALUE */
80 
91 // #define USER_VECT_TAB_ADDRESS
92 
93 #if defined(USER_VECT_TAB_ADDRESS)
103 /* #define VECT_TAB_SRAM */
104 
105 #if defined(VECT_TAB_SRAM)
107 #define VECT_TAB_BASE_ADDRESS SRAM1_BASE
108 #else
110 #define VECT_TAB_BASE_ADDRESS FLASH_BASE
111 #endif /* VECT_TAB_SRAM */
112 
113 #if !defined(VECT_TAB_OFFSET)
115 #define VECT_TAB_OFFSET 0x00000000U
116 #endif /* VECT_TAB_OFFSET */
117 
118 #endif /* USER_VECT_TAB_ADDRESS */
119 
133 uint32_t SystemCoreClock = 4000000U;
134 
136 const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U,
137  1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
138 
140 const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
141 
143 const uint32_t MSIRangeTable[12] = {100000U, 200000U, 400000U, 800000U,
144  1000000U, 2000000U, 4000000U, 8000000U,
145  16000000U, 24000000U, 32000000U, 48000000U};
146 
150 void SystemInit(void)
151 {
152 #if defined(USER_VECT_TAB_ADDRESS)
153  // Configure the vector table location
154  SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
155 #endif
156 
157  // Configure the FPU settings
158 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
159  SCB->CPACR |=
160  ((3UL << 20U) | (3UL << 22U)); // Set CP10 and CP11 full access
161 #endif
162 }
163 
201 {
202  uint32_t tmp;
203  uint32_t msirange;
204  uint32_t pllvco;
205  uint32_t pllsource;
206  uint32_t pllm;
207  uint32_t pllr;
208 
209  // Get MSI range
210  if ((RCC->CR & RCC_CR_MSIRGSEL) == 0U)
211  {
212  // MSISRANGE from RCC_CSR applies
213  msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> 8U;
214  }
215  else
216  {
217  // MSIRANGE from RCC_CR applies
218  msirange = (RCC->CR & RCC_CR_MSIRANGE) >> 4U;
219  }
220  // MSI frequency range in Hz
221  msirange = MSIRangeTable[msirange];
222 
223  // Get SYSCLK source
224  switch (RCC->CFGR & RCC_CFGR_SWS)
225  {
226  case 0x00:
227  // MSI used as system clock source
228  SystemCoreClock = msirange;
229  break;
230 
231  case 0x04:
232  // HSI used as system clock source
234  break;
235 
236  case 0x08:
237  // HSE used as system clock source
239  break;
240 
241  case 0x0C:
242  // PLL used as system clock source
243  // PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE) / PLLM * PLLN
244  // SYSCLK = PLL_VCO / PLLR
245  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
246  pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4U) + 1U;
247 
248  switch (pllsource)
249  {
250  case 0x02:
251  // HSI used as PLL clock source
252  pllvco = (HSI_VALUE / pllm);
253  break;
254 
255  case 0x03:
256  // HSE used as PLL clock source
257  pllvco = (HSE_VALUE / pllm);
258  break;
259 
260  default:
261  // MSI used as PLL clock source
262  pllvco = (msirange / pllm);
263  break;
264  }
265  pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8U);
266  pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25U) + 1U) * 2U;
267  SystemCoreClock = pllvco / pllr;
268  break;
269 
270  default:
271  SystemCoreClock = msirange;
272  break;
273  }
274  // Compute HCLK clock frequency
275  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
276  SystemCoreClock >>= tmp;
277 }
278 
const uint32_t MSIRangeTable[12]
const uint8_t APBPrescTable[8]
const uint8_t AHBPrescTable[16]
void SystemInit(void)
Initialize the system before calling the main() function.
uint32_t SystemCoreClock
Define to enable user-defined vector table address setting.
#define HSI_VALUE
void SystemCoreClockUpdate(void)
Update the SystemCoreClock variable according to the clock register values.
#define HSE_VALUE