STM32F10x的系统初始化 SystemInit()

对于无操作系统的软件来说,STM32上电后的第一件事,就是系统初始化。

Libaries\CMSIS_MDK\CM3\DeviceSupport\ST\STM32F10x\system_stm32f10x.c 中,官方给出了系统初始化函数 SystemInit(),我添加了一些中文注释,便于学习、模仿。

/**
  * @brief  Setup the microcontroller system
  *         Initialize the Embedded Flash Interface, the PLL and update the 
  *         SystemCoreClock variable.
  * @note   This function should be used only after reset.
  * @param  None
  * @retval None
  */
void SystemInit (void)
{

  /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  /* Set HSION bit */
  
  RCC->CR |= (uint32_t)0x00000001;    // 打开内部的 8MHz RC 振荡器, 使能内部高速时钟(HSI)

  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
#ifndef STM32F10X_CL
  RCC->CFGR &= (uint32_t)0xF8FF0000;
#else
    // MCO         = 000:     MCO 引脚设置成无时钟输出
    // ADCPRE    = 00:        ADC 的时钟分频系数为2, ADC 的时钟频率=PCLK2/2
    // PPRE2    = 000:    APB2 的时钟分频系数为1, PCLK2 = HCLK
    // PPRE1    = 000:    APB1 的时钟分频系数为1, PCLK1 = HCLK
    // HPRE     = 0000:    AHB 的时钟分频系数为1, HCLK = SYSCLK = 8MHz
    // SW         = 00:     选择 HSI 作为系统时钟
    RCC->CFGR &= (uint32_t)0xF0FF0000;
#endif /* STM32F10X_CL */   
  
  /* Reset HSEON, CSSON and PLLON bits */
    // PLLON    = 0:    禁用 PLL
    // CSSON    = 0:    禁用时钟安全系统(CSS)
    // HSEON    = 0:    关闭外部高速时钟(HSE)振荡器
  RCC->CR &= (uint32_t)0xFEF6FFFF;

  /* Reset HSEBYP bit */
    // HSEBY    = 0:    外部高速时钟(HSE)振荡器没有准备好
  RCC->CR &= (uint32_t)0xFFFBFFFF;

  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
    // OTGFSPRE    = 0:        USB OTG FS 的时钟频率 = 2*PLLCLK/3
    // PLLMUL        = 0000:    保留
    // PLLXTPRE    = 0:        如果选择 PREDIV1 为 PLL 的输入时钟, 则不对 PREDIV1 分频
    // PLLSRC        = 0:        选择 HSI 时钟二分器为PLL 的输入时钟
    RCC->CFGR &= (uint32_t)0xFF80FFFF;

#ifdef STM32F10X_CL
  /* Reset PLL2ON and PLL3ON bits */
  RCC->CR &= (uint32_t)0xEBFFFFFF;

  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x00FF0000;

  /* Reset CFGR2 register */
  RCC->CFGR2 = 0x00000000;
#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x009F0000;

  /* Reset CFGR2 register */
  RCC->CFGR2 = 0x00000000;      
#else
  /* Disable all interrupts and clear pending bits  */
    // CSSC            = 1:    清除"时钟安全系统"中断标志(CSSF)
    // PLL3RDYC    = 0:    无效
    // PLL2RDYC    = 0:    无效
    // PLLRDYC    = 1:    清除"PLL 准备"中断标志(PLLRDYF)
    // HSERDYC    = 1:    清除"外部高速时钟准备"中断标志(HSERDYF)
    // HSIRDYC    = 1:    清除"内部高速时钟准备"中断标志(HSIRDYF)
    // LSERDYC    = 1:    清除"外部低速时钟准备"中断标志(LSERDYF)
    // LSIRDYC    = 1:    清除"内部低速时钟准备"中断标志(LSIRDYF)
    // PLL3RDYIE=    0:    禁止"PLL3 锁定"中断
    // PLL2RDYIE= 0:    禁止"PLL2 锁定"中断
    // PLLRDYIE    = 0:    禁止"PLL 锁定"中断
    // HSERDYIE    = 0:    禁止"外部高速时钟准备"中断
    // HSIRDYIE    = 0:    禁止"内部高速时钟准备"中断
    // LSERDYIE    = 0:    禁止"外部低速时钟准备"中断
    // LSIRDYIE    = 0:    禁止"内部低速时钟准备"中断
  RCC->CIR = 0x009F0000;
#endif /* STM32F10X_CL */
    
#if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  #ifdef DATA_IN_ExtSRAM
    SystemInit_ExtMemCtl(); 
  #endif /* DATA_IN_ExtSRAM */
#endif 

  /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  /* Configure the Flash Latency cycles and enable prefetch buffer */
  SetSysClock();

#ifdef VECT_TAB_SRAM
  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#else
    // 如果从内置 Flash 中开始运行程序, 则起始地址为 0x08000000
  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
#endif 
}
 

执行 SetSysClock() 之前,时钟配置图如下:

 STM32F10x的系统初始化 SystemInit()_第1张图片

你可能感兴趣的:(STM32编程)