ENع
→Startup code
الدرس 34 · Registers and the memory map

Reset, the vector table and startup code

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة8 خطوات5 أسئلةIntermediate
عن هذا الدرس

What happens between pressing RESET and the first line of main()? Follow an STM32F407VG (on the STM32F4DISCOVERY board) through it. The CPU reads two words from the vector table at the start of flash: 0x2002 0000 becomes the stack pointer and 0x0800 0189 sends it to Reset_Handler. That startup code copies initialized globals (.data) from flash into SRAM, zeroes the rest (.bss), calls SystemInit() and then main(). The same table later routes every interrupt. Pointers are covered in the C++ lesson “Pointers”, and flash versus SRAM in the Hardware lessons “ROM and flash” and “SRAM”; the lesson “Blink with registers, no library” comes just before this one.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 34 · تفاعلي
01 / 08

Reset: start over

Press RESET, or switch the power on, and the STM32 stops whatever it was doing. Its registers go back to fixed reset values. When the button is released, the CPU knows only one thing: where to look first, address 0.

reset     RESET pin low, or power on
registers back to their reset values
SP, PC    not yet loaded
SRAM      nobody has written your values yet
released  -> the CPU reads address 0
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Reset: start over

    Press RESET, or switch the power on, and the STM32 stops whatever it was doing. Its registers go back to fixed reset values. When the button is released, the CPU knows only one thing: where to look first, address 0.

    reset     RESET pin low, or power on
    registers back to their reset values
    SP, PC    not yet loaded
    SRAM      nobody has written your values yet
    released  -> the CPU reads address 0
  2. The vector table

    With the BOOT0 pin low, the STM32 shows its flash, which really starts at 0x0800 0000, at address 0 as well. The first 98 words there are the vector table: a list of addresses the CPU reads, not code it runs.

    BOOT0 = 0  flash is seen at 0x0000 0000 and at 0x0800 0000
    vector table  98 words = 392 bytes (16 system + 82 interrupts)
      0x0800 0000  2002 0000  Word 0: initial SP
      0x0800 0004  0800 0189  Word 1: reset vector
      0x0800 003C  &SysTick   Word 15: SysTick
    each entry is an address, not an instruction
  3. Word 0: the stack pointer

    First the CPU copies word 0, 0x2002 0000, into SP, the stack pointer. That is the address just past the top of 128 KB of SRAM. The stack grows down from there, so function calls can now save things on it.

    word 0    0x2002 0000  ->  SP
    linker    _estack = ORIGIN(RAM) + LENGTH(RAM)
              = 0x2000 0000 + 128 KB = 0x2002 0000
    first push writes 0x2001 FFFC: the stack grows down
  4. Word 1: where to start

    Next it copies word 1, 0x0800 0189, into PC, the program counter. The odd last bit only marks Thumb code, the instruction set Cortex-M runs, so PC becomes 0x0800 0188: the first instruction of Reset_Handler.

    word 1    0x0800 0189  (bit 0 = 1: Thumb)
    PC        0x0800 0188  = Reset_Handler
    here      right after the 392-byte table (our build)
    no C has run yet: globals are not set
  5. Copy .data into SRAM

    A global with a start value, like int count = 5;, must live in SRAM so it can change. But SRAM loses everything without power. So the linker keeps the 5 in flash, and this loop copies it into SRAM, word by word.

    int count = 5;   lives in .data
    _sidata   where the start values sit in flash
    _sdata .. _edata   where .data lives in SRAM
    lines 2-4 copy one 32-bit word per pass
    count     ??  ->  5
  6. Zero the .bss

    A global with no start value, like int ticks;, must read 0: the C standard says so. All of them sit together in the .bss section, and this loop writes 0 into each word. Nothing is copied, so they cost no flash.

    volatile int ticks;   lives in .bss
    C: a global with no initializer starts at 0
    _sbss .. _ebss   the .bss words in SRAM
    lines 5-6 write 0 into each word
    ticks     ??  ->  0
  7. SystemInit(), then main()

    SystemInit() switches on the floating-point unit and points the CPU at the vector table. Then Reset_Handler calls main(). Only now does your program run, with a stack and every global set. On an Uno, avr-libc does this before main() calls setup().

    SystemInit()  FPU on (CPACR), VTOR = 0x0800 0000
                  the clock stays 16 MHz HSI until main sets the PLL
    main()        your program, SP and globals ready
    Arduino Uno   avr-libc .init4 copies .data, zeroes .bss,
                  .init9 calls main(): init(), setup(), loop() ...
  8. Same table, for interrupts

    The table is not only for reset. SysTick is exception 15, so its entry sits at 15 × 4 = 0x3C. When the timer fires, the CPU saves 8 registers on the stack, reads that entry and runs SysTick_Handler: ticks becomes 1.

    SysTick   exception 15  ->  entry at 15 x 4 = 0x3C
              (0x0800 003C in flash)
    stacking  8 words pushed: R0-R3, R12, LR, PC, xPSR
    vector    fetched from the table at the same time
    handler   ticks++   ->  ticks = 1
    return    the 8 words come back off the stack

اختبار يتاح بعد الخطوة الأخيرة.