在“startup_stm32f429xx.s”文件中,系統復位后會首先調用SystemInit函數
1 ; Reset handler 2 Reset_Handler PROC 3 EXPORT Reset_Handler [WEAK] 4 IMPORT SystemInit 5 IMPORT __main 6 7 LDR R0, =SystemInit 8 BLX R0 9 LDR R0, =__main 10 BX R0 11 ENDP
SystemInit函數定義:
1 void SystemInit(void) 2 { 3 /* FPU settings ------------------------------------------------------------*/ 4 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) 5 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ 6 #endif 7 /* Reset the RCC clock configuration to the default reset state ------------*/ 8 /* Set HSION bit */ 9 RCC->CR |= (uint32_t)0x00000001; 10 11 /* Reset CFGR register */ 12 RCC->CFGR = 0x00000000; 13 14 /* Reset HSEON, CSSON and PLLON bits */ 15 RCC->CR &= (uint32_t)0xFEF6FFFF; 16 17 /* Reset PLLCFGR register */ 18 RCC->PLLCFGR = 0x24003010; 19 20 /* Reset HSEBYP bit */ 21 RCC->CR &= (uint32_t)0xFFFBFFFF; 22 23 /* Disable all interrupts */ 24 RCC->CIR = 0x00000000; 25 26 /* Configure the Vector Table location add offset address ------------------*/ 27 #ifdef VECT_TAB_SRAM 28 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ 29 #else 30 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ 31 #endif 32 }
1. FPU設置
1 /* FPU settings ------------------------------------------------------------*/ 2 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) 3 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ 4 #endif
__FPU_PRESENT 用來確定處理器是否帶 FPU 功能
__FPU_USED 用來確定是否開啟 FPU 功能
如果處理器帶有FPU功能且確定開啟FPU功能,則設置“SCB->CPACR”(協處理器訪問控制)寄存器的 20~23 位為 1 。
利用CPACR寄存器,可以使能或禁止FPU。可通過SCB->CPACR來訪問。第0~19位及第24~31位未實現,為保留位,如圖:
對於Cortex-M4處理器,FPU被定義為協處理器10和11。由於其他協處理器不存在,只有CP10和CP11可用且都用於FPU。在設置這個寄存器時,CP10和CP11的設置必須相同。
CP10和CP11在復位后為0。在這種配置下,FPU禁止且允許低功耗,在使用之前,需要使能FPU,本步驟一般在SystemInit函數內執行。
2. RCC時鍾配置復位
1 /* Reset the RCC clock configuration to the default reset state ------------*/ 2 /* Set HSION bit */ 3 RCC->CR |= (uint32_t)0x00000001; 4 5 /* Reset CFGR register */ 6 RCC->CFGR = 0x00000000; 7 8 /* Reset HSEON, CSSON and PLLON bits */ 9 RCC->CR &= (uint32_t)0xFEF6FFFF; 10 11 /* Reset PLLCFGR register */ 12 RCC->PLLCFGR = 0x24003010; 13 14 /* Reset HSEBYP bit */ 15 RCC->CR &= (uint32_t)0xFFFBFFFF; 16 17 /* Disable all interrupts */ 18 RCC->CIR = 0x00000000;
2.1 將RCC->CR(RCC 時鍾控制寄存器)的HSION置位,使能內部高速時鍾
2.2 將RCC->CFGR(RCC 時鍾配置寄存器 )清零
2.3 復位RCC->CR(RCC 時鍾控制寄存器)的HSEON, CSSON 和 PLLON位
2.4 復位RCC->PLLCFGR(RCC PLL 配置寄存器 )
2.5 將RCC->CR(RCC 時鍾控制寄存器)的HSEBYP復位
2.6 禁用所有中斷
3. 配置中斷向量表地址
1 /* Configure the Vector Table location add offset address ------------------*/ 2 #ifdef VECT_TAB_SRAM 3 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ 4 #else 5 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ 6 #endif
HAL 庫的 SystemInit 函數除了打開 HSI 之外,沒有任何時鍾相關配置,所以使用 HAL 庫我們必須編寫自己的時鍾配置函數。