Skip to content

Commit

Permalink
Add an option to not use embassy
Browse files Browse the repository at this point in the history
Embassy is still set as a dependency on V2/V3 targets because it is used
for the USB support.
But on CH32V003, disabling embassy allows to save precious FLASH size.
  • Loading branch information
romainreignier committed Nov 17, 2024
1 parent c9cae8c commit b05d1a7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
ch32x035,
ch641,
]
embassy: [true, false]
include:
- mcu_family: ch32l003
target: riscv32imac-unknown-none-elf
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ 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",
"executor-thread",
"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"
Expand Down
6 changes: 6 additions & 0 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
12 changes: 12 additions & 0 deletions pre-script.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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);
}
}
22 changes: 21 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 %}

0 comments on commit b05d1a7

Please sign in to comment.