gd32-rust / gd32f1x0-hal

Embedded Rust HAL for GD32F1x0 microcontrollers
Other
6 stars 2 forks source link

Embassy compatibility #49

Open Serhii-the-Dev opened 5 months ago

Serhii-the-Dev commented 5 months ago

The Embassy project imposes first-class async support in the Rust embedded world including wake-on-interrupt scenarios which may be extremely helpful in certain scenarios. Alas, this HAL crate is not compatible with embassy as it's not providing a generic timer driver.

My build.rs:

use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

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");
}

and the main file:

#![no_std]
#![no_main]

use defmt::*;
use embassy_executor::Spawner;
use embassy_time::Timer;
use gd32f1x0_hal::{pac, prelude::*};
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    loop {
        Timer::after_millis(1000).await;
        info!("one more second")
    }
}

Error on cargo build:

  = note: rust-lld: error: undefined symbol: _embassy_time_now
          >>> referenced by lib.rs:146 (src/lib.rs:146)
          >>>               embassy_time_driver-faf69ddeeee7d3fa.embassy_time_driver.71181d876a0d80ab-cgu.0.rcgu.o:(embassy_time_driver::now::h00fe3dbe3ce1d7f3) in archive /home/serhii/work/rusty-gd32/target/thumbv7m-none-eabi/debug/deps/libembassy_time_driver-faf69ddeeee7d3fa.rlib

          rust-lld: error: undefined symbol: _embassy_time_allocate_alarm
          >>> referenced by lib.rs:153 (src/lib.rs:153)
          >>>               embassy_time_driver-faf69ddeeee7d3fa.embassy_time_driver.71181d876a0d80ab-cgu.0.rcgu.o:(embassy_time_driver::allocate_alarm::h65b3cb5669a83b80) in archive /home/serhii/work/rusty-gd32/target/thumbv7m-none-eabi/debug/deps/libembassy_time_driver-faf69ddeeee7d3fa.rlib

          rust-lld: error: undefined symbol: _embassy_time_set_alarm_callback
          >>> referenced by lib.rs:158 (src/lib.rs:158)
          >>>               embassy_time_driver-faf69ddeeee7d3fa.embassy_time_driver.71181d876a0d80ab-cgu.0.rcgu.o:(embassy_time_driver::set_alarm_callback::h61450a8a8b58bacf) in archive /home/serhii/work/rusty-gd32/target/thumbv7m-none-eabi/debug/deps/libembassy_time_driver-faf69ddeeee7d3fa.rlib

          rust-lld: error: undefined symbol: _embassy_time_set_alarm
          >>> referenced by lib.rs:163 (src/lib.rs:163)
          >>>               embassy_time_driver-faf69ddeeee7d3fa.embassy_time_driver.71181d876a0d80ab-cgu.0.rcgu.o:(embassy_time_driver::set_alarm::h3bb1783fe163909a) in archive /home/serhii/work/rusty-gd32/target/thumbv7m-none-eabi/debug/deps/libembassy_time_driver-faf69ddeeee7d3fa.rlib
qwandor commented 5 months ago

Hello! I'm really not familiar with Embassy as I haven't used it myself yet, but would you like to send a PR to add an appropriate Embassy timer driver behind a feature flag, or would it make more sense to have a separate HAL crate specifically for Embassy?

Serhii-the-Dev commented 5 months ago

Hello! I'm really not familiar with Embassy as I haven't used it myself yet, but would you like to send a PR to add an appropriate Embassy timer driver behind a feature flag, or would it make more sense to have a separate HAL crate specifically for Embassy?

Hi! Sorry, I'm new to the Rust embedded solutions world and it's hard for me to tell what a proper architecture would be...I was using the esp-hal repository as a reference and they implemented embassy support within their own crate so I was thinking it's a right place to ask, sorry if I made a mistake.

qwandor commented 5 months ago

That sounds reasonable, please go ahead and send a PR to add it.

Serhii-the-Dev commented 4 weeks ago

Sorry it took so long, it was hard to find some spare time to learn all the stuff required for the time driver implementation 😅 I've created a PR, please let me know if some clarification or changes are required.