问题描述
我已经用 C 和 Rust 对我的 STM32F401 进行了编程,但在组装时遇到了问题。
- 当我最初使用 Black Magic Probe 加载代码时,它运行良好
- 当我的电路板通电或复位时,代码不会运行。
- 重置后,在 GDB 中,如果我尝试 C继续它,它抛出异常。
- 重置后,在 GDB 中,如果我尝试R取消它,它运行良好
重置处理程序中的实际代码是常年“闪烁”。 我确定代码没有问题,这是某种“初始化问题”的“气味”?
- 我是一个庞大的 GDB 菜鸟,但如果我这样做
info registers
,PC 始终是我期望的值 - 0x8000198
disassembly of section .text:
08000000 <vector_table>:
8000000: 20010000 .word 0x20010000
8000004: 08000198 .word 0x08000198
8000008: 08000194 .word 0x08000194
-- redacted a load from brevity... they're all 0x0800194
8000188: 08000194 .word 0x08000194
800018c: 08000194 .word 0x08000194
8000190: 08000194 .word 0x08000194
08000194 <Default_handler>:
8000194: f7ff bffe b.w 8000194 <Default_handler>
08000198 <Reset_handler>:
; RCC_AHB1ENR := RCC_AHB1ENR OR RCC_AHB1Periph_GPIOC
8000198: f643 0030 movw r0,#14384 ; 0x3830
800019c: f2c4 0002 movt r0,#16386 ; 0x4002
80001a0: 6802 ldr r2,[r0,#0]
80001a2: f042 0204 orr.w r2,r2,#4
80001a6: 6002 str r2,#0]
; GPIOC_MODER := GPIO_MODER_13_OUTPUT
80001a8: f640 0000 movw r0,#2048 ; 0x800
80001ac: f2c4 0002 movt r0,#16386 ; 0x4002
80001b0: f240 0200 movw r2,#0
80001b4: f2c0 4200 movt r2,#1024 ; 0x400
80001b8: 6002 str r2,#0]
80001ba: f240 0220 movw r2,#32 ; 0b00100000
80001be: f2c0 0200 movt r2,#0
80001c2: f640 0019 movw r0,#2073 ; 0x819 GPIOC_BSRR8
80001c6: f2c4 0002 movt r0,#16386 ; 0x4002
80001ca: f640 011b movw r1,#2075 ; 0x81b GPIOC_BSRR24
80001ce: f2c4 0102 movt r1,#16386 ; 0x4002
080001d2 <.loop>:
80001d2: 7002 strb r2,#0] ; set bit
80001d4: f240 0300 movw r3,#0
80001d8: f2c0 030d movt r3,#13
080001dc <.delay0>:
80001dc: 3b01 subs r3,#1
80001de: bf18 it ne
80001e0: e7fc bne.n 80001dc <.delay0>
80001e2: 700a strb r2,[r1,#0] ; reset bit
80001e4: f240 0300 movw r3,#0
80001e8: f2c0 030d movt r3,#13
080001ec <.delay1>:
80001ec: 3b01 subs r3,#1
80001ee: bf18 it ne
80001f0: e7fc bne.n 80001ec <.delay1>
80001f2: e7ee b.n 80001d2 <.loop>
解决方法
8000004: 08000198 .word 0x08000198
这需要是一个奇数才能启动
您需要使用 .thumb_func 或 .type(如果使用 gnu 汇编程序,如果使用其他汇编语言,则需要针对该语言对其进行排序)
.cpu cortex-m4
.thumb
vector_table:
.word 0x20001000
.word ResetVector
.thumb_func
.globl ResetVector
ResetVector:
b .
Disassembly of section .text:
08000000 <vector_table>:
8000000: 20001000 .word 0x20001000
8000004: 08000009 .word 0x08000009
08000008 <ResetVector>:
8000008: e7fe b.n 8000008 <ResetVector>
这将启动(或至少会尝试运行重置处理程序)
没有:
.cpu cortex-m4
.thumb
vector_table:
.word 0x20001000
.word ResetVector
.globl ResetVector
ResetVector:
b .
Disassembly of section .text:
08000000 <vector_table>:
8000000: 20001000 .word 0x20001000
8000004: 08000008 .word 0x08000008
08000008 <ResetVector>:
8000008: e7fe b.n 8000008 <ResetVector>
这不会启动
您也可以使用 .type,.type 对手臂和拇指都有效(如果执行 ARM 指令,这不能因为它是 cortex-m)。
.cpu cortex-m4
.thumb
vector_table:
.word 0x20001000
.word ResetVector
.type ResetVector,%function
.globl ResetVector
ResetVector:
b .
Disassembly of section .text:
08000000 <vector_table>:
8000000: 20001000 .word 0x20001000
8000004: 08000009 .word 0x08000009
08000008 <ResetVector>:
8000008: e7fe b.n 8000008 <ResetVector>
所有其他向量(不是 sp init 值)必须是奇数。使用工具不要做一些加一的事情。
您的调试器可能是直接启动程序而不是重置部件。