STM32啓動後系統初始化SystemInit()

啓動文件中復位異常相應函數如下:

; Reset handler
Reset_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文件主要就是幹具體硬件配置相關的工作。


  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並且更新系統時鐘。此函數需在復位啓動後調用。

  1. RCC->CR |= (uint32_t)0x00000001;  

第一行代碼操作時鐘控制寄存器,將內部8M高速時鐘使能,從這裏可以看出系統啓動後是首先依靠內部時鐘源而工作的

  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芯片相關的一個宏。

  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等的情況下配置好與之相關參數然後開啓,達到生效的目的

  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 */  

這一段主要是跟中斷設置有關。開始時,我們需要禁止所有中斷並且清除所有中斷標誌位。不同硬件有不同之處。

  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與此無關。

  1. SetSysClock();  

此又是一個函數,主要是配置系統時鐘頻率。HCLK,PCLK2,PCLK1的分頻值,分別代表AHB,APB2,和APB1。當然還幹了其它的事情,配置FLASH延時週期和使能預取緩衝期。後面的這個配置具體還不瞭解。

  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()又做了什麼吧。

  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. }  


從中可以看出就是根據不同的宏來設置不同的系統時鐘,這些宏就在跟此函數在同一個源文件裏。官方很是考慮周到,我們只需要選擇相應宏就能達到快速配置系統時鐘的目的。
  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()函數,這個函數就是具體操作寄存器進行配置了。

  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  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章