diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cf1f798..e60c646 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,6 +34,7 @@ jobs: ch32x035, ch641, ] + embassy: [true, false] include: - mcu_family: ch32l003 target: riscv32imac-unknown-none-elf @@ -60,7 +61,7 @@ jobs: - uses: cargo-generate/cargo-generate-action@latest with: name: ${{ env.PROJECT_NAME }} - arguments: "--define mcu_family=${{ matrix.mcu_family }}" + arguments: "--define mcu_family=${{ matrix.mcu_family }} --define embassy=${{ matrix.embassy }}" # we need to move the generated project to a temp folder, away from the template project # otherwise `cargo` runs would fail # see https://github.com/rust-lang/cargo/issues/9922 diff --git a/Cargo.toml b/Cargo.toml index 5b919e5..66d26f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,13 +10,19 @@ name = "{{ project-name }}" harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors [dependencies] +{% if embassy_dep -%} ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal.git", features = [ "{{ mcu }}", "embassy", +{%- else -%} +ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal.git", default-features = false, features = [ + "{{ mcu }}", +{%- endif %} "time-driver-tim2", "rt", "memory-x", ] } +{% if embassy_dep -%} embassy-executor = { version = "0.6.1", features = [ "integrated-timers", "arch-spin", @@ -24,6 +30,7 @@ embassy-executor = { version = "0.6.1", features = [ "task-arena-size-128", # or better use nightly, but fails on recent Rust versions ] } embassy-time = { version = "0.3.0" } +{%- endif %} qingke-rt = "0.5.0" qingke = "0.5.0" embedded-hal = "1.0.0" diff --git a/cargo-generate.toml b/cargo-generate.toml index 345a03b..273cc6b 100644 --- a/cargo-generate.toml +++ b/cargo-generate.toml @@ -6,6 +6,12 @@ ignore = [".git", ".github", "README.md"] pre = ["pre-script.rhai"] post = ["post-script.rhai"] +# Embassy option +[placeholders.embassy] +type = "bool" +prompt = "Enable embassy (async) support?" +default = true + [placeholders.mcu_family] type = "string" prompt = "Which MCU family to target?" diff --git a/pre-script.rhai b/pre-script.rhai index 2e44e0d..e1d9477 100644 --- a/pre-script.rhai +++ b/pre-script.rhai @@ -12,6 +12,7 @@ let targets = #{ ch641: "riscv32ec-unknown-none-elf", }; +let embassy = variable::get("embassy"); let mcu_family = variable::get("mcu_family"); let target = targets.get(mcu_family); variable::set("target", `${target}`); @@ -22,3 +23,14 @@ if mcu_family == "ch641" { } else if mcu_family == "ch32x033" { variable::set("mcu", "ch32x033f8p6"); } + +if embassy { + variable::set("embassy_dep", true); +} else { + // Even if embassy not selected, add embassy dependency for the V2/V3 because USB support depends on Embassy + if mcu_family == "ch32v203" || mcu_family == "ch32v208" || mcu_family == "ch32v303" || mcu_family == "ch32v305" || mcu_family == "ch32v307" { + variable::set("embassy_dep", true); + } else { + variable::set("embassy_dep", false); + } +} diff --git a/src/main.rs b/src/main.rs index 07f4b0a..8191fdb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ #![no_main] #![feature(type_alias_impl_trait)] +{% if embassy -%} use embassy_executor::Spawner; use embassy_time::Timer; use ch32_hal::gpio::{AnyPin, Level, Output, Pin}; @@ -29,9 +30,28 @@ async fn main(spawner: Spawner) -> ! { // Adjust the LED GPIO according to your board spawner.spawn(blink(p.PA0.degrade(), 1000)).unwrap(); - loop { Timer::after_millis(1000).await; println!("tick"); } } +{%- else -%} +use hal::delay::Delay; +use hal::gpio::{Level, Output}; +use {ch32_hal as hal, panic_halt as _}; + +#[qingke_rt::entry] +fn main() -> ! { + hal::debug::SDIPrint::enable(); + let p = hal::init(hal::Config::default()); + let mut delay = Delay; + + // Adjust the LED GPIO according to your board + let mut led = Output::new(p.PA0, Level::Low, Default::default()); + loop { + led.toggle(); + delay.delay_ms(1000); + hal::println!("toggle!"); + } +} +{%- endif %}