Transcription of 嵌入式系統建構:開發運作於STM32的韌體程式
1 STM32 . : 599882 1 : . : < . >. STM32 .. GNU Toolchain OpenOCD . STM32 Flash QEMU .. C . CPU Flash (fetch instruction) . (object file) data section 0 . RAM data section data section . data section RAM .. (memory map) . Flash (program image) (stack) . RAM . Reset .. Reset 3 stage pipeline PC. (program counter PC ) Reset The Definitive Guide To ARM cortex M3 . ( ) Reset ARM. cortex M3 32 . 0x00000000 MSP (Main Stack Point) .. 0x00000004 . PC .. 0x00000004 PC LSB 1 ( ) . (exception handler) LSB 1 . Thumb (Thumb mode) ARM cortex M3 Thumb 2. ARM ( ARM code ARM state) Thumb . The Definitive Guide To ARM cortex M3 .. With the introduction of the Thumb 2 instruction set, it is now possible to handle all processing requirements in one operation state. There is no need to switch between the two. In fact, the cortex M3 does not support the ARM code.
2 Even interrupts are now handled with the Thumb state. (Previously, the ARM core entered interrupt handlers in the ARM state.) Since there is no need to switch between states, the cortex m3 processor has a number of advantages over traditional ARM processors, such as: No state switching overhead, saving both execution time and instruction space No need to separate ARM code and Thumb code source files, making software development and maintenance easier It's easier to get the best efficiency and performance, in turn making it easier to write software, because there is no need to worry about switching code between ARM and Thumb to try to get the best density/performance ARM cortex M3 STMicroelectronics .. STMicroelectronics Reference Manual STM32 (BOOT) . Flash BOOT0=0 .. Flash cortex M3 Flash . 0x08000000 . Reset MSP PC STM32 (alias) . STM32 Flash 0x08000000.
3 ( . 0x00000000 . ) .. MSP . PC (LSB 1). text section data section .. sm (".word 0x20001000") . a asm (".word main") . main() { }. MSP 0x20001000 0x1000 ( : 0x20001000 . 0x20000000 = 0x1000 4KB) Hello World 4KB . GNU Toolchain PC LSB 1 . main C GNU/Linux Microsoft Windows . main C main .. GNU Toolchain (startup) . toolchain . ( OS ABI [Application Binary Interface] calling convention ) . bare metal .. Hello World .. STM32 GPIO (General Purpose I/O) .. GPIO ST . ? D GPIO 9 LED . LED(DS4) .. 1. (enable) D GPIO port . 2. D GPIO port 9 (PD9) (output push pull). 3. PD9 0 1 LED . STMicroelectronics . APB2 peripheral clock enable register ( ). RCC_APB2 ENR . : 0x40021000 + 0x18. Reset : 0x00000000. ( ) 1 GPIOD . IOPD EN . Port configuration register high ( ) (x=D). GPIOx_CRH . : 0x40011400 + 0x4. Reset : 0x44444444.. CNF9[1:0] = 00 (General purpose output push pull).
4 MODE[1:0 ] = 01 ( 10 MHz). Port bit set/reset register ( ) (x=D). GPIOx_BSRR . : 0x40011400 + 0x10. Reset : 0x00000000. GPIOD9 BR9 = 1. GPIOD9 BS9 = 1.. [ ]. #define GPIOD_CRH (*((volatile unsigned long *) (0x40011400 + 0x4))). #define GPIOD_BSRR (*((volatile unsigned long *)(0x40011400 + 0x10))). #define RCC_APB2 ENR (*((volatile unsigned long *)(0x40021000 + 0x18))). asm(".word 0x20001000") . asm(".word main") . int main(). {. unsigned int c = 0 . RCC_APB2 ENR = (1 << 5) /* IOPDEN = 1 */. GPIOD_CRH = 0x44444414 . while (1) {. GPIOD_BSRR = (1 << 25) /* ON */. for (c = 0 c < 100000 c++) . GPIOD_BSRR = (1 << 9) /* OFF */. for (c = 0 c < 100000 c++) . }. }. [ ]. SECTIONS {.. = 0x0 ..text : {. *(.text). }. }. [ Makefile ]. CROSS_COMPILE ?= arm none eabi ..PHONY: all all: : $(CROSS_COMPILE)gcc mcpu= cortex m3 mthumb nostartfiles c o : $(CROSS_COMPILE)ld T o : $(CROSS_COMPILE)objcopy j.
5 Text O binary clean: rm f *.o *.out *.bin 3 . for STM32 . LED ( ) . LED . (linker script) .. = 0x0 .. 0x0 (relocate) 0x0 . Linker and Loader . STM32 Flash . 0x08000000 . Makefile GNU make Managing Projects with GNU Make . Makefile . Makefile mcpu= cortex m3. mthumb nostartfiles . mcpu: gcc ARM cortex M3 . mthumb: Thumb ARM ARM cortex M3/M4 . Thumb2 . nostartfile: (starup file) . C main() . Makefile . The Definitive Guide To ARM cortex M3 .. (gcc) (as) (ld) . objcopy . (debugger) . make make .. objdump . # arm none eabi objdump D . CROSS_COMPILE ?= arm none eabi . : file format elf32 littlearm Disassembly of section .text: 00000000 <main 0x8>: 0: 20001000 andcs r1, r0, r0. 4: 00000009 andeq r0, r0, r9. 00000008 <main>: 8: b480 push {r7}. a: b083 sub sp, #12.. ( . 0x00000000 ARM exception table . <main 0x8> . andcs andeq ) MSP PC . 0x20001000.
6 0x00000009 0x8 0x9 GNU Toolchain ARM . LSB 1 . ( ELF Executable and Linkable Format) section . objdump .text ..comment 3 section( . ) ..text section .rodata ..data ..bss .. section section section . (flash write) Flash od . # od t x1 0000000 00 10 00 20 09 00 00 00 80 b4 83 b0 00 af 00 23. 0000020 7b 60 11 4b 20 22 1a 60 10 4b 11 4a 1a 60 11 4b 0000040 4f f0 00 72 1a 60 00 23 7b 60 02 e0 7b 68 01 33. 0000060 7b 60 7a 68 0c 4b 9a 42 f8 d9 0a 4b 4f f4 00 72. 0000100 1a 60 00 23 7b 60 02 e0 7b 68 01 33 7b 60 7a 68. 0000120 05 4b 9a 42 f8 d9 e2 e7 18 10 02 40 04 14 01 40. 0000140 14 44 44 44 10 14 01 40 9f 86 01 00. 0000154. little endian .. Flash . OpenOCD OpenJTAG .. 1. OpenOCD. # openocd f f 2. Flash (erase) . # telnet localhost 4444. Trying Trying Connected to localhost. Escape character is '^]'. Open On Chip Debugger > halt target state: halted target halted due to debug request, current mode: Thread xPSR: 0x81000000 pc: 0x0800002a msp: 0x200000d8.
7 > poll background polling: on TAP: (enabled) target state: halted target halted due to debug request, current mode: Thread xPSR: 0x81000000 pc: 0x0800002a msp: 0x200000d8. > flash protect_check 0. device id = 0x10016414 flash size = 512kbytes successfully checked protect state > stm32x mass_erase 0. stm32x mass erase complete > flash write_bank 0 / 0. not enough working area available(requested 16384, free 16336). wrote 144 bytes from file / to flash bank 0 at offset 0x00000000 in ( kb/s). > reset JTAG tap: tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3). JTAG tap: tap/device found: 0x06414041 (mfg: 0x020, part: 0x6414, ver: 0x0). 3.. LED LED . data section data section ( ) Flash . RAM . (loader) .. PC .. (*.ld) . ELF section section . (relocation) . section section . section section . (loadable) section . (allocatable).
8 ARM ELF .text ..data ..rodata ..bss ..comment .. section ..comment .. section . section . ELF section ..text . (text section) section text ARM..rodata ( data section) C const ..data . ( data section) C . section ..bss . ( data section) section data section section . ( { } . ) section . section . section ..text ..rodata ..data . section ..bss . section ( memset() .. 0) text section data section data section (loadable section) ".bss data section (allocatable section) . (*.o) ( . *.out) section .. 1..text ..rodata ..data . section ..bss . section ..text .rodata . section . section section 4 bytes (aligned) . section (padding) . section ELF section section input section output section . GNU linker ld .. section . section . 2.. ( C extern . ) . printf() .. section (offset) section .. ELF section . section .. data section ELF . data section . data section.
9 ( virtual memory) GNU linker ld .. LMA (Load Memory Address): section . VMA (Virtual Memory Address): . ( section ) GNU/Linux . data section Flash data section Flash SRAM data section LMA VMA text section . Flash LMA SRAM VMA . section LMA VMA .. [ ]. MEMORY. {. FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 512k SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64k }. /* Section Definitions */ SECTIONS. {..text : {. KEEP(*(.isr_vector)). *(.text). *(.rodata).. = ALIGN(4) . _etext = .. } > FLASH..data : AT (_etext). {. _data = .. *(.data).. = ALIGN(4) . _edata = .. } > SRAM. /* .bss section which is used for uninitialized data */..bss (NOLOAD) : {. _bss = .. *(.bss).. = ALIGN(4) . _ebss = .. } > SRAM. _end = .. }. ( : ) . MEMORY . section section .. SECTIONS GNU linker ld .. SECTIONS .. SECTIONS. {. section command section command .. }. sections command.
10 ENTRY .. section .. ENTRY . section . ( ) section section . _etext = .. (romfs ) .. C . extern int romfs_start /* start address of builtin romfs */. extern int romfs_size . extern int romfs_end /* ending address of romfs section */. romfs section . romfs . " ( ) .. SECTIONS . section (LMA) section . section (VMA) . section . section [address] [(type)] : [AT(lma)] [ALIGN(section_align)] [SUBALIGN(subsection_align)]. {. output section comm and output section command . } [>region] [AT>lma_region] [:phdr :phdr ] [=fillexp]. section LMA .. AT .. Output section command .. section . ( section .. section . Section . section . section . section .. section . file name [(sections_name1 section_name2 ..)]..text .. *.(.text). section .. section . (.text).. 1.. 2.. 1.. 1.. 2.. > . 1.. 2.. > . n .. section section ..text ..text .isr_vector .text .rodata.)