Rust 嵌入式 - STM32|embassy 初体验
参考教程:
- Rust嵌入式教程(8)- Embassy简介 & 点灯

MCU:STM32H750VBT6(成都模组星球开发版)
Debugger:DAP-Link(SWD)
  • .cargo/config.toml
[target.thumbv7em-none-eabihf]
runner = 'probe-rs run --chip STM32H750VBTx'

[build]
target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)

[env]
DEFMT_LOG = "trace"
  • .vscode/settings.json
{
    "rust-analyzer.check.allTargets": false,
}
  • src/main.rs
#![no_main]
#![no_std]

use defmt::info;
use embassy_executor::Spawner;
use embassy_stm32::{gpio::{Level, Output, Speed}};
use panic_probe as _;
use defmt_rtt as _;

#[embassy_executor::main]
async fn main(spawner: Spawner){
    let p = embassy_stm32::init(Default::default());

    let led = Output::new(p.PE13,Level::Low, Speed::High);
    
    spawner.spawn(blink(led).unwrap());    // 这里和教程中写法不同
 
    loop {
        embassy_time::Timer::after_secs(1).await;
    }
}

#[embassy_executor::task]
async fn blink(mut led : Output<'static>) {
    loop {
        led.set_high();
        info!("LED set high!");
        embassy_time::Timer::after_millis(500).await;
        led.set_low();
        info!("LED set low!");
        embassy_time::Timer::after_millis(500).await;
    }
}
  • build.rs
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.

fn main() {
    // // Put `memory.x` in our output directory and ensure it's
    // // on the linker search path.
    // let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
    // File::create(out.join("memory.x"))
    //     .unwrap()
    //     .write_all(include_bytes!("memory.x"))
    //     .unwrap();
    // println!("cargo:rustc-link-search={}", out.display());

    // // By default, Cargo will re-run a build script whenever
    // // any file in the project changes. By specifying `memory.x`
    // // here, we ensure the build script is only re-run when
    // // `memory.x` is changed.
    // println!("cargo:rerun-if-changed=memory.x");

    println!("cargo:rustc-link-arg-bins=--nmagic");
    println!("cargo:rustc-link-arg-bins=-Tlink.x");
    println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
}
  • Cargo.toml
[package]
name = "embassy-led-h7"
version = "0.1.0"
edition = "2024"

[dependencies]
# Change stm32h743bi to your chip name, if necessary.
embassy-stm32 = { version = "0.4.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "stm32h750vb", "time-driver-tim2", "exti", "memory-x", "unstable-pac", "chrono"] }
embassy-sync = { version = "0.7.2", git = "https://github.com/embassy-rs/embassy", features = ["defmt"] }
embassy-embedded-hal = { version = "0.5.0", git = "https://github.com/embassy-rs/embassy" }
embassy-executor = { version = "0.9.0", git = "https://github.com/embassy-rs/embassy", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] }
embassy-time = { version = "0.5.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
embassy-net = { version = "0.7.1", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "proto-ipv6", "dns"] }
embassy-usb = { version = "0.5.1", git = "https://github.com/embassy-rs/embassy", features = ["defmt"] }
embassy-futures = { version = "0.1.2", git = "https://github.com/embassy-rs/embassy" }

defmt = "1.0.1"
defmt-rtt = "1.0.0"

cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] }
cortex-m-rt = "0.7.0"
embedded-hal = "1.0.0"
# embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
embedded-hal-async = { version = "1.0" }
embedded-nal-async = "0.8.0"
embedded-io-async = { version = "0.6.1" }
panic-probe = { version = "1.0.0", features = ["print-defmt"] }
heapless = { version = "0.9.1", default-features = false }
critical-section = "1.1"
micromath = "2.0.0"
stm32-fmc = "0.4.0"
embedded-storage = "0.3.1"
static_cell = "2"
chrono = { version = "^0.4", default-features = false }
grounded = "0.2.0"

转载声明:

除特殊声明外,本站所有文章均由 debussy 原创,均采用 CC BY-NC-SA 4.0 协议,转载请注明出处:Include Everything 的博客
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇