作業系統之前的程式 for stm32f4 - discovery (1) - 1 加到 10 , asm version

對 x86 的學習暫時告個段落, 我開始邁向另一個硬體平台 arm, 使用的是 stm32f4discovery 這塊開發板, 比較不滿意的是沒有 mmu, 192k ram 也實在小了點, 咦!前一篇提過了阿?那我就再說一次。至少給我 1M 阿!我實在是不習慣 mcu 的小資源。

重複同樣的學習方式, 先來開發第一支作業系統之前的程式, 和 x86 pc 有 bios 不同, 這支程式就是板子開機後第一支執行的程式。比起 作業系統之前的程式, 標題取為 開機後的第一支程式聽起來似乎威一點, 但是 對稱之美是很重要的, 所以就這樣囉!

和之前一樣, 先來個組合語言的版本, 而和 x86 一樣, 有不同的組合語言語法 - arm/gnu, 這裡的例子是 gnu arm 組合語言語法。這是 ARM Cortex-M3 嵌入式系統 設計入門 page 19-3 的範例 - 1 加到 10。

one2ten.S
 1 # ARM Cortex-M3 嵌入式系統 設計入門 p 19-3 
 2 .equ STACK_TOP, 0x20000800
 3 .text
 4 .global _start
 5 .code 16
 6 .syntax unified
 7 _start:
 8   .word STACK_TOP, start
 9   .type start, function 
10   # let lsb to 1
11 
12 start:
13   movs r0, #10
14   movs r1, #0
15 
16 loop:
17   adds r1, r0
18   subs r0, #1
19   bne loop
20 
21 deadloop:
22   b deadloop
23 .end

編譯指令在作業系統之前的程式是很重要的, 一定要列出來:
arm-none-eabi-as -g -mcpu=cortex-m3 -mthumb -o factorial.o factorial.S
arm-none-eabi-ld -Ttext 0x0 -o factorial.elf factorial.o
arm-none-eabi-objcopy -O binary factorial.elf factorial.bin

對於我們的第一支 cortex-m3 程式, 就簡單點, 不用 link script。

再來和 x86 pc 有點不同, 把這程式燒錄到 flash (pc 則是複製到軟碟或是硬碟), 無法使用平常的 cp/dd 指令, 得用 openocd/stlink。
flash address : 0x8000000
st-flash write factorial.bin 0x8000000
openocd 指令請參考:    Programming STM32 F2, F4 ARMs under Linux: A Tutorial from Scratch  

開機後在預設情形下, flash address 會被 map 到 0x0 上, 所以可以簡單看成這支程式是從位址 0 的地方開始。

由於沒有使用特定開發板的 IO, 所以應該可以在各家的 cortex-m 系列 的開發板執行, 也可以用〖 作業系統之前的程式 for stm32f4discovery (0)〗提到的模擬器來執行。

和 arm v6 以前的架構有點不同, 找書籍/資料時可別看到 arm 就認為是自己要看的資料。arm 開始幹不相容的事情了, 沒有 intel 的老舊包袱還真好。

讓 stm32f4discovery 一開機就執行的程式要怎麼寫呢?
L8, L9 就是重點:
  • L8 的 .word 填上兩個 4 個 byte 值, 第一個就是 stack (sp) 的位址, 所以填上什麼數字, stack 位址就從那開始。開機時, cortex-m3 系列會把 0~3 這 4 byte 的值填到 sp。
  • L8 的 start 則是一開機就會跳到該位址執行程式碼, 本範例是 start 的位址, 所以一開機之後, stack 設好了, 程式也從 start 開始執行。而這 8 個 byte 必須在執行檔的前 8 個 byte。

這是怎麼做到的?
arm-none-eabi-ld -Ttext 0x0 表示將 text section 從 0 開始算起, 所以
.word STACK_TOP, start 就會佔據執行檔的前 8 byte。

descent@debianlinux:stm32_prog$ hexdump -C factorial.bin 
00000000  00 08 00 20 09 00 00 00  0a 20 00 21 09 18 01 38  |... ..... .!...8|
00000010  7f f4 fc af ff f7 fe bf 

紅色 20000800 就是 sp 的值, 從 gdb dump register 可以得證。

(gdb) target remote localhost:1234
Remote debugging using localhost:1234
0x00000008 in ?? ()
(gdb) file ./
.factorial.S.swp factorial.S.html h.sh stm32.h
.git/ factorial.bin hello.c stm32.ld
.makefile.swp factorial.elf makefile stm32f4xx.h
README.md factorial.o mymain.c stm32f4xx_gpio.h
factorial.S g1.sh q.sh
(gdb) file ./factorial.elf
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from /home/descent/git/jserv_course/stm32_prog/factorial.elf...done.
(gdb) l
1 # ARM Cortex-M3 嵌入式系統 設計入門 p 19-3
2 .equ STACK_TOP, 0x20000800
3 .text
4 .global _start
5 .code 16
6 .syntax unified
7 _start:
8 .word STACK_TOP, start
9 .type start, function
10 # let lsb to 1
(gdb) l
11
12 start:
13 movs r0, #10
14 movs r1, #0
15
16 loop:
17 adds r1, r0
18 subs r0, #1
19 bne loop
20
(gdb) b 13
Breakpoint 1 at 0xa: file factorial.S, line 13.
(gdb) r
The "remote" target does not support "run". Try "help target" or "continue".
(gdb) c
Continuing.

Breakpoint 1, start () at factorial.S:14
14 movs r1, #0
(gdb) i r
r0 0xa 10
r1 0x0 0
r2 0x0 0
r3 0x0 0
r4 0x0 0
r5 0x0 0
r6 0x0 0
r7 0x0 0
r8 0x0 0
r9 0x0 0
r10 0x0 0
r11 0x0 0。
r12 0x0 0
sp 0x20000800 0x20000800
lr 0x0 0
pc 0xa 0xa
cpsr 0x173 371
(gdb)



藍色的 00000009 是 start 的位址, 從 objdump 看來明明就是 00000008, 怎麼多了個 1。這是 thumb 的關係, 若是 00000008 會讓 cpu 以為是在 arm 模式, 會得到一個 Hard Fault Exception。

這就是 L9 的作用, 沒有這行, 這個值就會是 00000008。
 9   .type start, function 


arm-none-eabi-objdump -d factorial.elf
 1 
 2 factorial.elf:     file format elf32-littlearm
 3 
 4 
 5 Disassembly of section .text:
 6 
 7 00000000 <_start>:
 8    0: 20000800  .word 0x20000800
 9    4: 00000009  .word 0x00000009
10 
11 00000008 <start>:
12    8: 200a       movs r0, #10
13    a: 2100       movs r1, #0
14 
15 0000000c <loop>:
16    c: 1809       adds r1, r1, r0
17    e: 3801       subs r0, #1
18   10: f47f affc  bne.w c <loop>
19 
20 00000014 <deadloop>:
21   14: f7ff bffe  b.w 14 <deadloop>

一樣不需要寫 dram controller 的 code, lucky!!因為使用的是 sram, 似乎不用寫什麼程式碼就可直接使用。好了, 解釋完畢, 組合語言的部份請自己查閱, 用 openocd/gdb 來 single step 吧!依照慣例, 應該猜得到, 下一篇會是 c 語言的版本

source code:
https://github.com/descent/stm32f4_prog

ref: ARM Cortex-M3 嵌入式系統 設計入門
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值