问题描述
我正在为 RaspBerry Pi 3 进行裸机编程。我已经能够使用 a tutorial 使灯闪烁,现在我正在尝试在 Rust 中执行同样的操作。
我的项目包含以下文件:
main.rs
#![no_main]
#![no_std]
#![feature(global_asm)]
global_asm!(include_str!("start.s"));
#[panic_handler]
fn on_panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
start.s
.section .init
.global _start
.equ BASE,0x3f200000 //Base address
.equ GPFSEL2,0x08 //FSEL2 register offset
.equ GPSET0,0x1c //GPSET0 register offset
.equ GPCLR0,0x28 //GPCLR0 register offset
.equ SET_BIT3,0x08 //sets bit three b1000
.equ SET_BIT21,0x200000 //sets bit 21
.equ COUNTER,0xf0000
_start:
ldr x0,=BASE
ldr x1,=SET_BIT3
str x1,[x0,#GPFSEL2]
ldr x1,=SET_BIT21
str x1,#GPSET0]
当我为 aarch64-unkNown-none 编译它时,我得到以下输出:
0000000000008000 <_start>:
8000: d2a7e400 mov x0,#0x3f200000 // #1059061760
8004: d2800101 mov x1,#0x8 // #8
8008: f9000401 str x1,#8]
800c: d2a00401 mov x1,#0x200000 // #2097152
8010: f801c001 stur x1,#28]
disassembly of section .comment:
0000000000000000 <.comment>:
0: 6b6e694c .inst 0x6b6e694c ; undefined
4: 203a7265 .inst 0x203a7265 ; undefined
8: 20444c4c .inst 0x20444c4c ; undefined
c: 302e3231 adr x17,5c651 <_start+0x54651>
10: Address 0x0000000000000010 is out of bounds.
然后我使用 aarch64-none-elf-objcopy --strip-all -O binary $(KERNEL_ELF) kernel.img
命令制作我复制到 SD 卡上的内核文件。
LED 不闪烁。是否有一个原因?我该如何排查问题?
解决方法
事实证明问题出在我的 arch.json
文件中,我用它来指定编译器的架构和工具。将链接器和 llvm 目标更改为 arm-none-eabihf
解决了此问题:
{
"llvm-target": "arm-none-eabihf","target-endian": "little","target-pointer-width": "32","target-c-int-width": "32","os": "none","env": "eabi","vendor": "unknown","arch": "arm","linker-flavor": "ld","linker": "arm-none-eabi-ld","data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64","executables": true,"relocation-model": "static","no-compiler-rt": true
}