From 6869e856914c0e00e8de444f2d465b12ed8a6a35 Mon Sep 17 00:00:00 2001 From: Zhouqi Jiang Date: Wed, 25 Sep 2024 09:40:46 +0800 Subject: [PATCH] [rust] refactor, move common rust parts from 100ask-d1-h to rust module Signed-off-by: Zhouqi Jiang --- .cargo/config.toml | 2 +- Cargo.lock | 42 +++++++++++-------- Cargo.toml | 7 ++-- board/100ask-d1-h-rs/Cargo.toml | 20 ++------- board/100ask-d1-h-rs/src/bin/hello-world.rs | 2 +- board/100ask-d1-h-rs/src/bin/init-dram.rs | 3 +- board/100ask-d1-h-rs/src/bin/led-lightup.rs | 2 +- board/100ask-d1-h-rs/src/main.rs | 11 +++++ rust/Cargo.toml | 28 +++++++++++++ .../100ask-d1-h-rs => rust}/macros/Cargo.toml | 0 .../100ask-d1-h-rs => rust}/macros/src/lib.rs | 14 +++---- {board/100ask-d1-h-rs => rust}/src/lib.rs | 4 ++ {board/100ask-d1-h-rs => rust}/src/mctl.rs | 0 .../100ask-d1-h-rs => rust}/xtask/Cargo.toml | 2 +- .../100ask-d1-h-rs => rust}/xtask/src/main.rs | 3 +- 15 files changed, 91 insertions(+), 49 deletions(-) create mode 100644 board/100ask-d1-h-rs/src/main.rs create mode 100644 rust/Cargo.toml rename {board/100ask-d1-h-rs => rust}/macros/Cargo.toml (100%) rename {board/100ask-d1-h-rs => rust}/macros/src/lib.rs (80%) rename {board/100ask-d1-h-rs => rust}/src/lib.rs (99%) rename {board/100ask-d1-h-rs => rust}/src/mctl.rs (100%) rename {board/100ask-d1-h-rs => rust}/xtask/Cargo.toml (89%) rename {board/100ask-d1-h-rs => rust}/xtask/src/main.rs (99%) diff --git a/.cargo/config.toml b/.cargo/config.toml index 5d67ddc9..0d5c04ca 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,4 +1,4 @@ [alias] -xtask = "run --package xtask --release --" +xtask = "run --package syterkit-xtask --release --" make = "xtask make" flash = "xtask flash" diff --git a/Cargo.lock b/Cargo.lock index a93e39c4..035b3f32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,9 +226,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.158" +version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" [[package]] name = "lock_api" @@ -492,19 +492,27 @@ dependencies = [ ] [[package]] -name = "syterkit-100ask-d1-h" -version = "0.1.0" +name = "syterkit" +version = "0.0.0" dependencies = [ "allwinner-hal", "allwinner-rt", "embedded-hal", "embedded-io", "naked-function", - "panic-halt", "spin", "syterkit-macros", ] +[[package]] +name = "syterkit-100ask-d1-h" +version = "0.0.0" +dependencies = [ + "embedded-hal", + "panic-halt", + "syterkit", +] + [[package]] name = "syterkit-macros" version = "0.1.0" @@ -514,6 +522,18 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "syterkit-xtask" +version = "0.1.0" +dependencies = [ + "byteorder", + "clap", + "clap-verbosity-flag", + "ctrlc", + "env_logger", + "log", +] + [[package]] name = "termcolor" version = "1.4.1" @@ -665,15 +685,3 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "xtask" -version = "0.1.0" -dependencies = [ - "byteorder", - "clap", - "clap-verbosity-flag", - "ctrlc", - "env_logger", - "log", -] diff --git a/Cargo.toml b/Cargo.toml index 197b15c8..3c32010a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,9 @@ [workspace] resolver = "2" members = [ + "rust", + "rust/macros", + "rust/xtask", "board/100ask-d1-h-rs", - "board/100ask-d1-h-rs/macros", - "board/100ask-d1-h-rs/xtask", ] -default-members = ["board/100ask-d1-h-rs/xtask"] +default-members = ["rust/xtask"] diff --git a/board/100ask-d1-h-rs/Cargo.toml b/board/100ask-d1-h-rs/Cargo.toml index 36b11b72..9900fcd7 100644 --- a/board/100ask-d1-h-rs/Cargo.toml +++ b/board/100ask-d1-h-rs/Cargo.toml @@ -2,24 +2,17 @@ cargo-features = ["per-package-target"] [package] name = "syterkit-100ask-d1-h" -version = "0.1.0" +version = "0.0.0" edition = "2021" default-target = "riscv64imac-unknown-none-elf" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -naked-function = "0.1.5" panic-halt = "0.2.0" -allwinner-hal = { git = "https://github.com/rustsbi/allwinner-hal" } -allwinner-rt = { git = "https://github.com/rustsbi/allwinner-hal" } embedded-hal = "1.0.0" -embedded-io = "0.6.1" -spin = "0.9" -syterkit-macros = { path = "macros" } +syterkit = { path = "../../rust" } -[lib] -name = "syterkit_100ask_d1_h" +[[bin]] +name = "syterkit-100ask-d1-h" test = false bench = false @@ -37,8 +30,3 @@ bench = false name = "led-lightup" test = false bench = false - -[features] -default = ["nezha"] -nezha = [] -lichee = [] diff --git a/board/100ask-d1-h-rs/src/bin/hello-world.rs b/board/100ask-d1-h-rs/src/bin/hello-world.rs index 6a43e412..afef07b6 100644 --- a/board/100ask-d1-h-rs/src/bin/hello-world.rs +++ b/board/100ask-d1-h-rs/src/bin/hello-world.rs @@ -2,7 +2,7 @@ #![no_main] use panic_halt as _; -use syterkit_100ask_d1_h::{entry, println, Clocks, Peripherals}; +use syterkit::{entry, println, Clocks, Peripherals}; #[entry] fn main(p: Peripherals, c: Clocks) { diff --git a/board/100ask-d1-h-rs/src/bin/init-dram.rs b/board/100ask-d1-h-rs/src/bin/init-dram.rs index 0066a745..00d7dca5 100644 --- a/board/100ask-d1-h-rs/src/bin/init-dram.rs +++ b/board/100ask-d1-h-rs/src/bin/init-dram.rs @@ -1,7 +1,8 @@ #![no_std] #![no_main] + use panic_halt as _; -use syterkit_100ask_d1_h::{entry, mctl, println, Clocks, Peripherals}; +use syterkit::{entry, mctl, println, Clocks, Peripherals}; #[entry] fn main(p: Peripherals, c: Clocks) { diff --git a/board/100ask-d1-h-rs/src/bin/led-lightup.rs b/board/100ask-d1-h-rs/src/bin/led-lightup.rs index 63af4083..c7cb40d3 100644 --- a/board/100ask-d1-h-rs/src/bin/led-lightup.rs +++ b/board/100ask-d1-h-rs/src/bin/led-lightup.rs @@ -3,7 +3,7 @@ use embedded_hal::digital::{InputPin, OutputPin}; use panic_halt as _; -use syterkit_100ask_d1_h::{entry, Clocks, Peripherals}; +use syterkit::{entry, Clocks, Peripherals}; #[entry] fn main(p: Peripherals, c: Clocks) { diff --git a/board/100ask-d1-h-rs/src/main.rs b/board/100ask-d1-h-rs/src/main.rs new file mode 100644 index 00000000..ef927a06 --- /dev/null +++ b/board/100ask-d1-h-rs/src/main.rs @@ -0,0 +1,11 @@ +#![no_std] +#![no_main] + +use panic_halt as _; +use syterkit::{entry, println, Clocks, Peripherals}; + +#[entry] +fn main(p: Peripherals, c: Clocks) { + println!("Welcome to SyterKit 100ask-d1-h package!"); + println!("Please refer to each files in `bin` path for examples."); +} diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 00000000..2f02dd26 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,28 @@ +cargo-features = ["per-package-target"] + +[package] +name = "syterkit" +version = "0.0.0" +edition = "2021" +default-target = "riscv64imac-unknown-none-elf" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +naked-function = "0.1.5" +allwinner-hal = { git = "https://github.com/rustsbi/allwinner-hal" } +allwinner-rt = { git = "https://github.com/rustsbi/allwinner-hal" } +embedded-hal = "1.0.0" +embedded-io = "0.6.1" +spin = "0.9" +syterkit-macros = { path = "macros" } + +[lib] +name = "syterkit" +test = false +bench = false + +[features] +default = ["nezha"] +nezha = [] +lichee = [] diff --git a/board/100ask-d1-h-rs/macros/Cargo.toml b/rust/macros/Cargo.toml similarity index 100% rename from board/100ask-d1-h-rs/macros/Cargo.toml rename to rust/macros/Cargo.toml diff --git a/board/100ask-d1-h-rs/macros/src/lib.rs b/rust/macros/src/lib.rs similarity index 80% rename from board/100ask-d1-h-rs/macros/src/lib.rs rename to rust/macros/src/lib.rs index d76e5087..332551f7 100644 --- a/board/100ask-d1-h-rs/macros/src/lib.rs +++ b/rust/macros/src/lib.rs @@ -77,8 +77,8 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { quote!( #[export_name = "main"] pub fn main() { - let (p, c) = ::allwinner_rt::__rom_init_params(); - let (__p, __uart0, __tx, __rx) = ::syterkit_100ask_d1_h::Peripherals::configure_uart0(p); + let (p, c) = ::syterkit::allwinner_rt::__rom_init_params(); + let (__p, __uart0, __tx, __rx) = ::syterkit::Peripherals::configure_uart0(p); unsafe { __syterkit_macros__main(__p, c, __uart0, __tx, __rx) } } #[allow(non_snake_case)] @@ -86,13 +86,13 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { #(#attrs)* #unsafety fn __syterkit_macros__main( #args, - __uart0: ::allwinner_rt::soc::d1::UART0, - __tx: ::allwinner_hal::gpio::Function<'static, 'B', 8, 6>, - __rx: ::allwinner_hal::gpio::Function<'static, 'B', 9, 6> + __uart0: ::syterkit::allwinner_rt::soc::d1::UART0, + __tx: ::syterkit::allwinner_hal::gpio::Function<'static, 'B', 8, 6>, + __rx: ::syterkit::allwinner_hal::gpio::Function<'static, 'B', 9, 6> ) #ret { - let mut __serial = ::allwinner_hal::uart::Serial::new(__uart0, (__tx, __rx), ::allwinner_hal::uart::Config::default(), &c, &p.ccu); + let mut __serial = ::syterkit::allwinner_hal::uart::Serial::new(__uart0, (__tx, __rx), ::syterkit::allwinner_hal::uart::Config::default(), &c, &p.ccu); unsafe { - *::syterkit_100ask_d1_h::CONSOLE.lock() = Some(::syterkit_100ask_d1_h::SyterKitConsole { inner: __serial }) + *::syterkit::CONSOLE.lock() = Some(::syterkit::SyterKitConsole { inner: __serial }) }; #(#stmts)* } diff --git a/board/100ask-d1-h-rs/src/lib.rs b/rust/src/lib.rs similarity index 99% rename from board/100ask-d1-h-rs/src/lib.rs rename to rust/src/lib.rs index 3f74d104..4db64940 100644 --- a/board/100ask-d1-h-rs/src/lib.rs +++ b/rust/src/lib.rs @@ -283,3 +283,7 @@ impl_gpio_pins! { pg17: ('G', 17, Disabled); pg18: ('G', 18, Disabled); } + +// macro internal code. +#[doc(hidden)] +pub use {allwinner_hal, allwinner_rt}; diff --git a/board/100ask-d1-h-rs/src/mctl.rs b/rust/src/mctl.rs similarity index 100% rename from board/100ask-d1-h-rs/src/mctl.rs rename to rust/src/mctl.rs diff --git a/board/100ask-d1-h-rs/xtask/Cargo.toml b/rust/xtask/Cargo.toml similarity index 89% rename from board/100ask-d1-h-rs/xtask/Cargo.toml rename to rust/xtask/Cargo.toml index bdb6bb53..a7859534 100644 --- a/board/100ask-d1-h-rs/xtask/Cargo.toml +++ b/rust/xtask/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "xtask" +name = "syterkit-xtask" version = "0.1.0" edition = "2021" diff --git a/board/100ask-d1-h-rs/xtask/src/main.rs b/rust/xtask/src/main.rs similarity index 99% rename from board/100ask-d1-h-rs/xtask/src/main.rs rename to rust/xtask/src/main.rs index 31c4cff5..6d821943 100644 --- a/board/100ask-d1-h-rs/xtask/src/main.rs +++ b/rust/xtask/src/main.rs @@ -95,6 +95,7 @@ fn xtask_build_d1_flash_bt0(env: &Env, bin: &str) { trace!("found cargo at {}", cargo); let mut command = Command::new(cargo); command.current_dir(project_root()); + trace!("cargo current dir: {:?}", project_root()); command.arg("build"); command.arg("-p"); command.arg("syterkit-100ask-d1-h"); @@ -265,7 +266,7 @@ fn find_binutils_prefix_or_fail() -> &'static str { fn project_root() -> PathBuf { Path::new(&env!("CARGO_MANIFEST_DIR")) .ancestors() - .nth(3) + .nth(2) .unwrap() .to_path_buf() }