无法使用裸机 Rust 使 Raspberry Pi LED 闪烁

Unable to blink Raspberry Pi LED using bare metal Rust

我正在为 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, [x0, #GPSET0]

当我为 aarch64-unknown-none 编译它时,我得到以下输出:

0000000000008000 <_start>:
    8000:       d2a7e400        mov     x0, #0x3f200000                 // #1059061760
    8004:       d2800101        mov     x1, #0x8                        // #8
    8008:       f9000401        str     x1, [x0, #8]
    800c:       d2a00401        mov     x1, #0x200000                   // #2097152
    8010:       f801c001        stur    x1, [x0, #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
}