Rust编程2026-03-04 01:00

Pi Pico 使用 Rust 嵌入式系统,每次使用 elf2uf2-r 都会产生错误:"未识别的 ABI"。

rustfan
rustfan
作者

我一直在尝试,但未能成功地在 Pi Pico 上构建和运行一个简单的嵌入式 Rust 程序。

以下是我的项目:

.cargo/config.toml:

[build]
target = "thumbv6m-none-eabi"

[target.thumbv6m-none-eabi]
rustflags = [
    "-C", "link-arg=-Tlink.x",
    "-C", "link-arg=--nmagic",
    # 这行代码是关键:
    "-C", "link-arg=-z", "-C", "link-arg=keep-section=.boot2",
]

src/main.rs:

”` #![no_std] #![no_main]

// 强制将引导加载程序包含在二进制文件中 #[link_section = “.boot2”] #[used] pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;

use panic_halt as _;

// 使用 HAL 中的 entry 宏,而不是 cortex_m_rt // 这确保引导加载程序正确链接 use rp_pico::entry; use core::fmt::Write; use embedded_hal::digital::OutputPin;

use rp_pico::hal::{

clocks::{init_clocks_and_plls, Clock},
fugit::RateExtU32,
pac,
sio::Sio,
uart::{DataBits, StopBits, UartConfig, UartPeripheral},
watchdog::Watchdog,

};

const XTAL_FREQ_HZ: u32 = 12_000_000;

#[entry] fn main() -> ! {

let mut pac = pac::Peripherals::take().unwrap();
let core = pac::CorePeripherals::take().unwrap();

let mut watchdog = Watchdog::new(pac.WATCHDOG);
let sio = Sio::new(pac.SIO);

let clocks = init_clocks_and_plls(
    XTAL_FREQ_HZ,
    pac.XOSC,
    pac.CLOCKS,
    pac.PLL_SYS,
    pac.PLL_USB,
    &mut pac.RESETS,
    &mut watchdog,
)
.ok()
.unwrap();

let pins = rp_pico::Pins::new(
    pac.IO_BANK0,
    pac.PADS_BANK0,
    sio.gpio_bank0,
    &mut pac.RESETS,
);

let uart_pins = (
    pins.gpio0.into_function(),
    pins.gpio1.into_function(),
);

let mut uart = UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
    .enable(
        UartConfig::new(
            115200.Hz(),
            DataBits::Eight,
            None,
            StopBits::One,
        ),
        clocks.peripheral_clock.f
51

评论 (0)

登录后评论

立即登录

加载评论中...