1. 程式人生 > 其它 >STM32 CM3/CM4 ------ startup.s 檔案分析 ------ GCC RT-Thread Studio 版本

STM32 CM3/CM4 ------ startup.s 檔案分析 ------ GCC RT-Thread Studio 版本

startup.s 功能

* This module performs:
* - Set the initial SP
* - Set the initial PC == Reset_Handler,
* - Set the vector table entries with the exceptions ISR address
* - Branches to main in the C library (which eventually
* calls main()).
* After Reset the Cortex-M4 processor is in Thread mode,
* priority is Privileged, and the Stack is set to Main.

函式Reset_Handler 內呼叫函式 entry(),entry() 函式新增一些我們要在 main() 函式之前執行的程式碼,entry() 函式的最後呼叫 main()。

向量表

在 startup.s 內,定義了一個向量表,從地址0開始(對於STM32,就是 flash 的起始地址 0x08000000)存每個向量,向量指向的位置就是對應的中斷服務函式

向量表的起始處都必須包含以下向量:   主堆疊指標(MSP)的初始值   復位向量(根據CM3權威指南,復位向量是PC初始值,也就是說程式是從中斷服務函式 Reset_Handler 開始執行的)   NMI   硬 fault 服務例程 後兩者也是必需的,因為有可能在引導過程中發生這兩種異常。

所以 0x20000D84 就是主堆疊指標的初始值,0x08059545-1就是復位向量,指向中斷服務函式Reset_Handler

可以看 rtthread.map 得到驗證

startup.s 檔案內容

以下是 GCC RT-Thread 版本的 startup.s

    .section  .text.Reset_Handler
  .weak  Reset_Handler
  .type  Reset_Handler, %function
Reset_Handler:  
  ldr   sp, =_estack     /* set stack pointer */

/* Copy the data segment initializers from flash to SRAM */  
  movs  r1, #
0 b LoopCopyDataInit CopyDataInit: ldr r3, =_sidata ldr r3, [r3, r1] str r3, [r0, r1] adds r1, r1, #4 LoopCopyDataInit: ldr r0, =_sdata ldr r3, =_edata adds r2, r0, r1 cmp r2, r3 bcc CopyDataInit ldr r2, =_sbss b LoopFillZerobss /* Zero fill the bss segment. */ FillZerobss: movs r3, #0 str r3, [r2], #4 LoopFillZerobss: ldr r3, = _ebss cmp r2, r3 bcc FillZerobss /* Call the clock system intitialization function.*/ bl SystemInit /* Call static constructors */ /* bl __libc_init_array */ /* Call the application's entry point.*/ bl entry bx lr .size Reset_Handler, .-Reset_Handler /** * @brief This is the code that gets called when the processor receives an * unexpected interrupt. This simply enters an infinite loop, preserving * the system state for examination by a debugger. * @param None * @retval None */ .section .text.Default_Handler,"ax",%progbits Default_Handler: Infinite_Loop: b Infinite_Loop .size Default_Handler, .-Default_Handler /****************************************************************************** * * The minimal vector table for a Cortex M3. Note that the proper constructs * must be placed on this to ensure that it ends up at physical address * 0x0000.0000. * *******************************************************************************/ .section .isr_vector,"a",%progbits .type g_pfnVectors, %object .size g_pfnVectors, .-g_pfnVectors g_pfnVectors: .word _estack .word Reset_Handler .word NMI_Handler .word HardFault_Handler .word MemManage_Handler .word BusFault_Handler .word UsageFault_Handler .word 0 .word 0 .word 0 .word 0 .word SVC_Handler .word DebugMon_Handler .word 0 .word PendSV_Handler .word SysTick_Handler /* External Interrupts */ .word WWDG_IRQHandler /* Window WatchDog */ .word PVD_IRQHandler /* PVD through EXTI Line detection */ .word TAMP_STAMP_IRQHandler /* Tamper and TimeStamps through the EXTI line */ .word RTC_WKUP_IRQHandler /* RTC Wakeup through the EXTI line */ .word FLASH_IRQHandler /* FLASH */ .word RCC_IRQHandler /* RCC */ .word EXTI0_IRQHandler /* EXTI Line0 */ .word EXTI1_IRQHandler /* EXTI Line1 */ .word EXTI2_IRQHandler /* EXTI Line2 */ .word EXTI3_IRQHandler /* EXTI Line3 */ ...... /******************************************************************************* * * Provide weak aliases for each Exception handler to the Default_Handler. * As they are weak aliases, any function with the same name will override * this definition. * *******************************************************************************/ .weak NMI_Handler .thumb_set NMI_Handler,Default_Handler .weak HardFault_Handler .thumb_set HardFault_Handler,Default_Handler .weak MemManage_Handler .thumb_set MemManage_Handler,Default_Handler .weak BusFault_Handler .thumb_set BusFault_Handler,Default_Handler .weak UsageFault_Handler .thumb_set UsageFault_Handler,Default_Handler ......

程式從Reset_Handler 開始執行,第一條語句就是對 SP 進行初始化。

不同於 IAR 版本,這個版本除了中斷服務函式Reset_Handler 外,其他中斷服務函式都是呼叫函式 Default_Handler,Default_Handler是一個死迴圈。功能上和 IAR 版本一樣都是死迴圈。