STM32單片機應用非常廣泛,官方提供了標準的接口庫,用戶可以不用直接操作寄存器,只需要調用接口函數就可以了。在官方庫中有一個非常重要的函數void SystemInit (void),
該函數用戶可能不會直接調用,而在啟動文件中一定會調用。函數原型如下:
函數原型
void SystemInit (void) { /* Reset the RCC clock configuration to the default reset state(for debug purpose) */ /* Set HSION bit */ RCC->CR |= (uint32_t)0x00000001; /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */ #ifndef STM32F10X_CL RCC->CFGR = (uint32_t)0xF8FF0000; #else RCC->CFGR = (uint32_t)0xF0FF0000; #endif /* STM32F10X_CL */ /* Reset HSEON, CSSON and PLLON bits */ RCC->CR = (uint32_t)0xFEF6FFFF; /* Reset HSEBYP bit */ RCC->CR = (uint32_t)0xFFFBFFFF; /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */ 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 */ 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 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ #endif }
可以看到函數體中幾乎全是條件編譯。
(1)先看第一行代碼:RCC->CR |= (uint32_t)0x00000001;顯然這是給CR寄存器的最低一位賦值為1.官方寄存器配置詳解截圖:
編譯條件宏定義
#ifndef STM32F10X_CL RCC->CFGR = (uint32_t)0xF8FF0000; #else RCC->CFGR = (uint32_t)0xF0FF0000; #endif /* STM32F10X_CL */
這個條件編譯是根據芯片容量不同默認初始化CFGR寄存器(Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits )。
RCC->CR = (uint32_t)0xFEF6FFFF; RCC->CR = (uint32_t)0xFFFBFFFF;
顯然是把CR寄存器的某些位賦值,其作用為:Reset HSEON, CSSON and PLLON ,HSEBYPbits即將HSEON,CSSON,PLLON,HSEBYP位置為零。
/* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */ RCC->CFGR = (uint32_t)0xFF80FFFF;
作用為把CFGR寄存器的PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE位置0。
#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 */ 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
這個條件編譯塊的作用為初始化Memory控制。
static void SetSysClock(void) { #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); #elif defined SYSCLK_FREQ_24MHz SetSysClockTo24(); #elif defined SYSCLK_FREQ_36MHz SetSysClockTo36(); #elif defined SYSCLK_FREQ_48MHz SetSysClockTo48(); #elif defined SYSCLK_FREQ_56MHz SetSysClockTo56(); #elif defined SYSCLK_FREQ_72MHz SetSysClockTo72(); #endif /* If none of the define above is enabled, the HSI is used as System clock source (default after reset) */ }
我們可以看到該函數就是通過判斷定義了哪個宏定義標志符而調用不同的設置sys時鐘頻率的函數,官方固件庫默認定義了SYSCLK_FREQ_72MHz,所以會調用SetSysClockTo72這個函數。
如果要使用其它頻率,那就解開相應注釋(只保留一個不被注釋)。
SetSysClockTo72()函數如下:
static void SetSysClockTo72(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0; /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/ /* Enable HSE */ RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ do { HSEStatus = RCC->CR RCC_CR_HSERDY; StartUpCounter++; } while((HSEStatus == 0) (StartUpCounter != HSE_STARTUP_TIMEOUT)); if ((RCC->CR RCC_CR_HSERDY) != RESET) { HSEStatus = (uint32_t)0x01; } else { HSEStatus = (uint32_t)0x00; } if (HSEStatus == (uint32_t)0x01) { /* Enable Prefetch Buffer */ FLASH->ACR |= FLASH_ACR_PRFTBE; /* Flash 2 wait state */ FLASH->ACR = (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2; /* HCLK = SYSCLK */ RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; /* PCLK2 = HCLK */ RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; /* PCLK1 = HCLK */ RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2; #ifdef STM32F10X_CL /* Configure PLLs ------------------------------------------------------*/ /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */ RCC->CFGR2 = (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC); RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5); /* Enable PLL2 */ RCC->CR |= RCC_CR_PLL2ON; /* Wait till PLL2 is ready */ while((RCC->CR RCC_CR_PLL2RDY) == 0) { } /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */ RCC->CFGR = (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL); RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLMULL9); #else /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */ RCC->CFGR = (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9); #endif /* STM32F10X_CL */ /* Enable PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ while((RCC->CR RCC_CR_PLLRDY) == 0) { } /* Select PLL as system clock source */ RCC->CFGR = (uint32_t)((uint32_t)~(RCC_CFGR_SW)); RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ while ((RCC->CFGR (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08) { } } else { /* If HSE fails to start-up, the application will have wrong clock configuration. User can add here some code to deal with this error */ } }
這個函數體比較長,但仔細看會發現這個函數就是在配置CR,CFGR,ACR(設置FLASH)寄存器的某些位(使能,判斷是否就緒,設置相應位,設置FLASH,設置AHB,APB預分頻系數,設置HCLK,PCLK等等外設時鐘,設置PLL鎖相環倍頻系數最終確定系統時鐘),結合官方注釋和官方寄存器的說明很容易理解。
至此,SystemInit函數就能大概理解了。但是還有一個問題需要注意:那就是雖然我們在main函數中并沒有調用SystemInit函數,但它在start up啟動文件中被調用了:
可以看到SystemInit函數是在main函數之前執行的,要是自定義該函數,那這里也要修改名稱,建議不要隨意修改或者重構該函數。
審核編輯:彭菁
-
接口
+關注
關注
33文章
8496瀏覽量
150834 -
STM32
+關注
關注
2266文章
10871瀏覽量
354786 -
函數
+關注
關注
3文章
4304瀏覽量
62427 -
代碼
+關注
關注
30文章
4744瀏覽量
68345
發布評論請先 登錄
相關推薦
評論