STM32启动后系统初始化SystemInit()

来源:互联网 发布:sql注入的防护 编辑:程序博客网 时间:2024/06/05 01:10

启动文件中复位异常相应函数如下:

; Reset handlerReset_Handler   PROC                EXPORT  Reset_Handler             [WEAK]                IMPORT  __main                IMPORT  SystemInit                LDR     R0, =SystemInit                BLX     R0                               LDR     R0, =__main                BX      R0                ENDP

SystemInit()这个函数出现在main()函数的第一行,可以看出它的重要性。以前关于SystemInit()这个函数从来没有关心过,只知道这是进行STM32系统初始化的一个函数。今天决定仔细看看,重新开始STM32的学习。这个函数在system_stm32f10x.c中,此C文件主要就是干具体硬件配置相关的工作。


[cpp] view plain copy
 print?
  1. /** @addtogroup STM32F10x_System_Private_Functions 
  2.   * @{ 
  3.   */  
  4.   
  5. /** 
  6.   * @brief  Setup the microcontroller system 
  7.   *         Initialize the Embedded Flash Interface, the PLL and update the  
  8.   *         SystemCoreClock variable. 
  9.   * @note   This function should be used only after reset. 
  10.   * @param  None 
  11.   * @retval None 
  12.   */  
  13. void SystemInit (void)  
  14. {  
  15.   /* Reset the RCC clock configuration to the default reset state(for debug purpose) */  
  16.   /* Set HSION bit */  
  17.   RCC->CR |= (uint32_t)0x00000001;  
  18.   
  19.   /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */  
  20. #ifndef STM32F10X_CL  
  21.   RCC->CFGR &= (uint32_t)0xF8FF0000;  
  22. #else  
  23.   RCC->CFGR &= (uint32_t)0xF0FF0000;  
  24. #endif /* STM32F10X_CL */     
  25.     
  26.   /* Reset HSEON, CSSON and PLLON bits */  
  27.   RCC->CR &= (uint32_t)0xFEF6FFFF;  
  28.   
  29.   /* Reset HSEBYP bit */  
  30.   RCC->CR &= (uint32_t)0xFFFBFFFF;  
  31.   
  32.   /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */  
  33.   RCC->CFGR &= (uint32_t)0xFF80FFFF;  
  34.   
  35. #ifdef STM32F10X_CL  
  36.   /* Reset PLL2ON and PLL3ON bits */  
  37.   RCC->CR &= (uint32_t)0xEBFFFFFF;  
  38.   
  39.   /* Disable all interrupts and clear pending bits  */  
  40.   RCC->CIR = 0x00FF0000;  
  41.   
  42.   /* Reset CFGR2 register */  
  43.   RCC->CFGR2 = 0x00000000;  
  44. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)  
  45.   /* Disable all interrupts and clear pending bits  */  
  46.   RCC->CIR = 0x009F0000;  
  47.   
  48.   /* Reset CFGR2 register */  
  49.   RCC->CFGR2 = 0x00000000;        
  50. #else  
  51.   /* Disable all interrupts and clear pending bits  */  
  52.   RCC->CIR = 0x009F0000;  
  53. #endif /* STM32F10X_CL */  
  54.       
  55. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)  
  56.   #ifdef DATA_IN_ExtSRAM  
  57.     SystemInit_ExtMemCtl();   
  58.   #endif /* DATA_IN_ExtSRAM */  
  59. #endif   
  60.   
  61.   /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */  
  62.   /* Configure the Flash Latency cycles and enable prefetch buffer */  
  63.   SetSysClock();  
  64.   
  65. #ifdef VECT_TAB_SRAM  
  66.   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */  
  67. #else  
  68.   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */  
  69. #endif   
  70. }  

从函数说明来看,此函数功能就是初始化内部FALSH,PLL并且更新系统时钟。此函数需在复位启动后调用。

[cpp] view plain copy
 print?
  1. RCC->CR |= (uint32_t)0x00000001;  

第一行代码操作时钟控制寄存器,将内部8M高速时钟使能,从这里可以看出系统启动后是首先依靠内部时钟源而工作的

[cpp] view plain copy
 print?
  1. #ifndef STM32F10X_CL  
  2.   RCC->CFGR &= (uint32_t)0xF8FF0000;  
  3. #else  
  4.   RCC->CFGR &= (uint32_t)0xF0FF0000;  

这两行代码则是操作时钟配置寄存器。其主要设置了MCO(微控制器时钟输出)PLL相关(PLL倍频系数,PLL输入时钟源),ADCPRE(ADC时钟),PPRE2(高速APB分频系数),PPRE1(低速APB分频系数),HPRE(AHB预分频系数),SW(系统时钟切换),开始时,系统时钟切换到HSI,由它作为系统初始时钟。宏STM32F10X_CL是跟具体STM32芯片相关的一个宏。

[cpp] view plain copy
 print?
  1. /* Reset HSEON, CSSON and PLLON bits */  
  2. RCC->CR &= (uint32_t)0xFEF6FFFF;  
  3.   
  4. /* Reset HSEBYP bit */  
  5. RCC->CR &= (uint32_t)0xFFFBFFFF;  
  6.   
  7. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */  
  8. RCC->CFGR &= (uint32_t)0xFF80FFFF;  

这几句话则是先在关闭HSE,CSS,,PLL等的情况下配置好与之相关参数然后开启,达到生效的目的

[cpp] view plain copy
 print?
  1. #ifdef STM32F10X_CL  
  2.   /* Reset PLL2ON and PLL3ON bits */  
  3.   RCC->CR &= (uint32_t)0xEBFFFFFF;  
  4.   
  5.   /* Disable all interrupts and clear pending bits  */  
  6.   RCC->CIR = 0x00FF0000;  
  7.   
  8.   /* Reset CFGR2 register */  
  9.   RCC->CFGR2 = 0x00000000;  
  10. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)  
  11.   /* Disable all interrupts and clear pending bits  */  
  12.   RCC->CIR = 0x009F0000;  
  13.   
  14.   /* Reset CFGR2 register */  
  15.   RCC->CFGR2 = 0x00000000;        
  16. #else  
  17.   /* Disable all interrupts and clear pending bits  */  
  18.   RCC->CIR = 0x009F0000;  
  19. #endif /* STM32F10X_CL */  

这一段主要是跟中断设置有关。开始时,我们需要禁止所有中断并且清除所有中断标志位。不同硬件有不同之处。

[cpp] view plain copy
 print?
  1. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)  
  2.   #ifdef DATA_IN_ExtSRAM  
  3.     SystemInit_ExtMemCtl();   
  4.   #endif /* DATA_IN_ExtSRAM */  
  5. #endif  

这段跟设置外部RAM有关吧,我用到的STM32F103RBT与此无关。

[cpp] view plain copy
 print?
  1. SetSysClock();  

此又是一个函数,主要是配置系统时钟频率。HCLK,PCLK2,PCLK1的分频值,分别代表AHB,APB2,和APB1。当然还干了其它的事情,配置FLASH延时周期和使能预取缓冲期。后面的这个配置具体还不了解。

[cpp] view plain copy
 print?
  1. #ifdef VECT_TAB_SRAM  
  2.   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */  
  3. #else  
  4.   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */  
  5. #endif   

这段代码主要是实现向量表的重定位。依据你想要将向量表定位在内部SRAM中还是内部FLASH中。这个SCB开始没在STM32参考手册中发现,原来它是跟Cortex-M3内核相关的东西。所以ST公司就没有把它包含进来吧。内核的东西后面再了解,这里给自己提个醒。


然后再看看SystemInit()中的那个函数SetClock()又做了什么吧。

[cpp] view plain copy
 print?
  1. static void SetSysClock(void)  
  2. {  
  3. #ifdef SYSCLK_FREQ_HSE  
  4.   SetSysClockToHSE();  
  5. #elif defined SYSCLK_FREQ_24MHz  
  6.   SetSysClockTo24();  
  7. #elif defined SYSCLK_FREQ_36MHz  
  8.   SetSysClockTo36();  
  9. #elif defined SYSCLK_FREQ_48MHz  
  10.   SetSysClockTo48();  
  11. #elif defined SYSCLK_FREQ_56MHz  
  12.   SetSysClockTo56();    
  13. #elif defined SYSCLK_FREQ_72MHz  
  14.   SetSysClockTo72();  
  15. #endif  
  16.    
  17.  /* If none of the define above is enabled, the HSI is used as System clock 
  18.     source (default after reset) */   
  19. }  


从中可以看出就是根据不同的宏来设置不同的系统时钟,这些宏就在跟此函数在同一个源文件里。官方很是考虑周到,我们只需要选择相应宏就能达到快速配置系统时钟的目的。
[cpp] view plain copy
 print?
  1. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)  
  2. /* #define SYSCLK_FREQ_HSE    HSE_VALUE */  
  3.  #define SYSCLK_FREQ_24MHz  24000000  
  4. #else  
  5. /* #define SYSCLK_FREQ_HSE    HSE_VALUE */  
  6. /* #define SYSCLK_FREQ_24MHz  24000000 */   
  7. /* #define SYSCLK_FREQ_36MHz  36000000 */  
  8. /* #define SYSCLK_FREQ_48MHz  48000000 */  
  9. /* #define SYSCLK_FREQ_56MHz  56000000 */  
  10. #define SYSCLK_FREQ_72MHz  72000000  
  11. #endif  


比如这里我需要配置系统时钟为72MHZ,则只需要将#define SYSCLK_FREQ_72MHz  72000000两边的注释符去掉。

这个函数里面又有SetSysClockTo72()函数,这个函数就是具体操作寄存器进行配置了。

[cpp] view plain copy
 print?
  1. #elif defined SYSCLK_FREQ_72MHz  
  2. /** 
  3.   * @brief  Sets System clock frequency to 72MHz and configure HCLK, PCLK2  
  4.   *         and PCLK1 prescalers.  
  5.   * @note   This function should be used only after reset. 
  6.   * @param  None 
  7.   * @retval None 
  8.   */  
  9. static void SetSysClockTo72(void)  
  10. {  
  11.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;  
  12.     
  13.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/      
  14.   /* Enable HSE */      
  15.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);  
  16.    
  17.   /* Wait till HSE is ready and if Time out is reached exit */  
  18.   do  
  19.   {  
  20.     HSEStatus = RCC->CR & RCC_CR_HSERDY;  
  21.     StartUpCounter++;    
  22.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));  
  23.   
  24.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)  
  25.   {  
  26.     HSEStatus = (uint32_t)0x01;  
  27.   }  
  28.   else  
  29.   {  
  30.     HSEStatus = (uint32_t)0x00;  
  31.   }    
  32.   
  33.   if (HSEStatus == (uint32_t)0x01)  
  34.   {  
  35.     /* Enable Prefetch Buffer */  
  36.     FLASH->ACR |= FLASH_ACR_PRFTBE;  
  37.   
  38.     /* Flash 2 wait state */  
  39.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);  
  40.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;      
  41.   
  42.    
  43.     /* HCLK = SYSCLK */  
  44.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;  
  45.         
  46.     /* PCLK2 = HCLK */  
  47.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;  
  48.       
  49.     /* PCLK1 = HCLK */  
  50.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;  
  51.   
  52. #ifdef STM32F10X_CL  
  53.     /* Configure PLLs ------------------------------------------------------*/  
  54.     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */  
  55.     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */  
  56.           
  57.     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |  
  58.                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);  
  59.     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |  
  60.                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);  
  61.     
  62.     /* Enable PLL2 */  
  63.     RCC->CR |= RCC_CR_PLL2ON;  
  64.     /* Wait till PLL2 is ready */  
  65.     while((RCC->CR & RCC_CR_PLL2RDY) == 0)  
  66.     {  
  67.     }  
  68.       
  69.      
  70.     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */   
  71.     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);  
  72.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |   
  73.                             RCC_CFGR_PLLMULL9);   
  74. #else      
  75.     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */  
  76.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |  
  77.                                         RCC_CFGR_PLLMULL));  
  78.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);  
  79. #endif /* STM32F10X_CL */  
  80.   
  81.     /* Enable PLL */  
  82.     RCC->CR |= RCC_CR_PLLON;  
  83.   
  84.     /* Wait till PLL is ready */  
  85.     while((RCC->CR & RCC_CR_PLLRDY) == 0)  
  86.     {  
  87.     }  
  88.       
  89.     /* Select PLL as system clock source */  
  90.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));  
  91.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;      
  92.   
  93.     /* Wait till PLL is used as system clock source */  
  94.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)  
  95.     {  
  96.     }  
  97.   }  
  98.   else  
  99.   { /* If HSE fails to start-up, the application will have wrong clock  
  100.          configuration. User can add here some code to deal with this error */  
  101.   }  
  102. }  
  103. #endif  
0 0