diff --git a/Cargo.toml b/Cargo.toml index e29411bd663e1797c0a5704b38658ae6d14ac85f..fa3ea64551284ee698cb9da33eda234946090ba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,10 +11,10 @@ crc = "3.0.1" cfg-if = "1.0.0" bl-soc = { git = "https://gitee.com/rustsbi/bl-soc", branch = "main", optional = true } base-address = { version = "0.0.0", optional = true } +embedded-time = { version = "0.12.1", optional = true } [dev-dependencies] memoffset = "0.8.0" -embedded-hal = "1.0.0-alpha.10" [features] default = ["rom-peripherals"] @@ -27,7 +27,7 @@ bl808-lp = ["bl-soc/bl808"] # BL702, BL704 and BL706 chip series. bl702 = ["bl-soc/bl702"] # Provide ROM peripherals on #[entry] function. -rom-peripherals = ["bl-rom-rt-macros/rom-peripherals", "dep:bl-soc", "dep:base-address"] +rom-peripherals = ["bl-rom-rt-macros/rom-peripherals", "dep:bl-soc", "dep:base-address", "dep:embedded-time"] [workspace] members = [ diff --git a/examples/blinky-bl616/Cargo.toml b/examples/blinky-bl616/Cargo.toml index c377eefb04715fff553237fd0d70126442e3dc05..6a5c738fc29643bc94429da359e0502c61b3bf8e 100644 --- a/examples/blinky-bl616/Cargo.toml +++ b/examples/blinky-bl616/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] bl-rom-rt = { path = "../.." } -embedded-hal = "1.0.0-alpha.10" +embedded-hal = "1.0.0-rc.1" panic-halt = "0.2.0" [features] diff --git a/examples/blinky-bl616/src/main.rs b/examples/blinky-bl616/src/main.rs index b34a6efa42f7ce9c8e89cd3ea88c569a93e22537..11e6b6649dbc6db08b47bfd4bf2fcec5fe7c917d 100644 --- a/examples/blinky-bl616/src/main.rs +++ b/examples/blinky-bl616/src/main.rs @@ -5,13 +5,13 @@ #![no_std] #![no_main] -use bl_rom_rt::{entry, Peripherals}; +use bl_rom_rt::{entry, Clocks, Peripherals}; use core::arch::asm; use embedded_hal::digital::OutputPin; use panic_halt as _; #[entry] -fn main(p: Peripherals) -> ! { +fn main(p: Peripherals, _c: Clocks) -> ! { p.gpio.io10.into_jtag_m0(); p.gpio.io11.into_jtag_m0(); p.gpio.io12.into_jtag_m0(); diff --git a/examples/blinky-bl808/Cargo.toml b/examples/blinky-bl808/Cargo.toml index e03390be7526d70a2b49e7236184c7d9aea73598..7dfd8cda247580e7bff86339251389a78412543b 100644 --- a/examples/blinky-bl808/Cargo.toml +++ b/examples/blinky-bl808/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] bl-rom-rt = { path = "../.." } -embedded-hal = "1.0.0-alpha.10" +embedded-hal = "1.0.0-rc.1" panic-halt = "0.2.0" [features] diff --git a/examples/blinky-bl808/src/main.rs b/examples/blinky-bl808/src/main.rs index 99b116c3b0eac303b13e7a9ddf277d556c2e0284..3acac520118250387866981bf015e8cf53dd4a01 100644 --- a/examples/blinky-bl808/src/main.rs +++ b/examples/blinky-bl808/src/main.rs @@ -9,12 +9,12 @@ #![no_std] #![no_main] -use bl_rom_rt::{entry, Peripherals}; +use bl_rom_rt::{entry, Clocks, Peripherals}; use embedded_hal::digital::OutputPin; use panic_halt as _; #[entry] -fn main(p: Peripherals) -> ! { +fn main(p: Peripherals, _c: Clocks) -> ! { let mut led = p.gpio.io8.into_floating_output(); loop { led.set_low().ok(); diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 009bc16985c30c38d0aea63768707c5a31c21e66..1a761fa50803bd2eb93a917afd65096d99729934 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -19,3 +19,8 @@ features = ["extra-traits", "full"] [features] default = [] rom-peripherals = [] +bl616 = [] +bl808-m0 = [] +bl808-d0 = [] +bl808-lp = [] +bl702 = [] diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 279d944422b56e73007145a1fd77d0b51991f356..c843aa54d2146755e4397a8760cd7fd1aba6dab5 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -26,10 +26,10 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { } #[cfg(feature = "rom-peripherals")] - if f.sig.inputs.len() > 1 { + if f.sig.inputs.len() != 2 { return parse::Error::new( f.sig.inputs.span(), - "`#[entry]` function with rom peripherlas should include zero or one parameter", + "`#[entry]` function with rom peripherlas should include exactly two parameters", ) .to_compile_error() .into(); @@ -55,7 +55,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { #[cfg(feature = "rom-peripherals")] return parse::Error::new( f.sig.span(), - "`#[entry]` function must have signature `[unsafe] fn([p: Peripherals]) -> !`", + "`#[entry]` function must have signature `[unsafe] fn(p: Peripherals, c: Clocks) -> !`", ) .to_compile_error() .into(); @@ -73,16 +73,22 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { #[allow(non_snake_case)] #[export_name = "main"] #(#attrs)* - pub #unsafety fn __bl_rom_rt__main() -> ! { + pub #unsafety fn main() -> ! { #(#stmts)* } ), #[cfg(feature = "rom-peripherals")] () => quote!( - #[allow(non_snake_case)] #[export_name = "main"] + pub extern "C" fn main() -> ! { + let p = unsafe { core::mem::transmute(()) }; + let c = bl_rom_rt::__new_clocks(40_000_000); + unsafe { __bl_rom_rt_macros__main(p, c) } + } + #[allow(non_snake_case)] + #[inline(always)] #(#attrs)* - pub #unsafety fn __bl_rom_rt__main(#inputs) -> ! { + #unsafety fn __bl_rom_rt_macros__main(#inputs) -> ! { #(#stmts)* } ), diff --git a/src/lib.rs b/src/lib.rs index d5fadfc6afbfcefdb5511ad7285f00950bdf5d81..ba5f07e18fdbf563472c752e99eab4c4b81c8d2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,11 +9,11 @@ pub mod soc; #[cfg(feature = "rom-peripherals")] cfg_if::cfg_if! { if #[cfg(feature = "bl616")] { - pub use soc::bl616::Peripherals; + pub use soc::bl616::{Peripherals, Clocks, __new_clocks}; } else if #[cfg(feature = "bl702")] { - pub use soc::bl702::Peripherals; + pub use soc::bl702::{Peripherals, Clocks, __new_clocks}; } else if #[cfg(any(feature = "bl808-m0", feature = "bl808-d0"))] { - pub use soc::bl808::Peripherals; + pub use soc::bl808::{Peripherals, Clocks, __new_clocks}; } } diff --git a/src/soc/bl616.rs b/src/soc/bl616.rs index 4188b74caec540988a17d00243650b514d2295cf..3110113f99b175c2170769d86674b4a84b0bb5bb 100644 --- a/src/soc/bl616.rs +++ b/src/soc/bl616.rs @@ -6,10 +6,7 @@ use crate::{HalBasicConfig, HalFlashConfig, HalPatchCfg}; use base_address::Static; #[cfg(feature = "bl616")] -use crate::Stack; - -#[cfg(feature = "bl616")] -use core::arch::asm; +use {crate::Stack, core::arch::asm}; #[cfg(feature = "bl616")] const LEN_STACK: usize = 1 * 1024; @@ -51,7 +48,6 @@ unsafe extern "C" fn start() -> ! { } #[cfg(feature = "bl616")] -#[rustfmt::skip] extern "Rust" { // This symbol is generated by `#[entry]` macro fn main() -> !; @@ -246,6 +242,20 @@ pub struct Peripherals { pub hbn: bl_soc::HBN>, } +#[cfg(feature = "rom-peripherals")] +pub use bl_soc::clocks::Clocks; + +// Used by macros only. +#[cfg(feature = "rom-peripherals")] +#[doc(hidden)] +#[inline(always)] +pub fn __new_clocks(xtal_hz: u32) -> Clocks { + use embedded_time::rate::Hertz; + Clocks { + xtal: Hertz(xtal_hz), + } +} + #[cfg(test)] mod tests { use super::{HalBootheader, HalPllConfig, HalSysClkConfig}; diff --git a/src/soc/bl702.rs b/src/soc/bl702.rs index d64bf1e90e576bf145eb70110f7303a4c32443ba..f4e21c36fc8b160920166b3178d0573abfa5b12d 100644 --- a/src/soc/bl702.rs +++ b/src/soc/bl702.rs @@ -174,6 +174,21 @@ pub struct Peripherals { // TODO: BL702 peripherals. } +#[cfg(feature = "rom-peripherals")] +pub use bl_soc::clocks::Clocks; + +// TODO: BL702 clock tree configuration. +// Used by macros only. +#[cfg(feature = "rom-peripherals")] +#[doc(hidden)] +#[inline(always)] +pub fn __new_clocks(xtal_hz: u32) -> Clocks { + use embedded_time::rate::Hertz; + Clocks { + xtal: Hertz(xtal_hz), + } +} + #[cfg(test)] mod tests { use super::{HalBasicConfig, HalBootheader, HalPllConfig, HalSysClkConfig}; diff --git a/src/soc/bl808.rs b/src/soc/bl808.rs index e90dc5aff05e53092a3a46eb8effd1352b09cefd..473112b488546b97ff4a0003072225cdda1c0ba1 100644 --- a/src/soc/bl808.rs +++ b/src/soc/bl808.rs @@ -3,10 +3,7 @@ use crate::{HalBasicConfig, HalFlashConfig, HalPatchCfg}; #[cfg(any(feature = "bl808-m0", feature = "bl808-d0"))] -use core::arch::asm; - -#[cfg(any(feature = "bl808-m0", feature = "bl808-d0"))] -use crate::Stack; +use {crate::Stack, core::arch::asm}; #[cfg(feature = "rom-peripherals")] use base_address::Static; @@ -90,7 +87,6 @@ unsafe extern "C" fn start() -> ! { } #[cfg(any(feature = "bl808-m0", feature = "bl808-d0"))] -#[rustfmt::skip] extern "Rust" { // This symbol is generated by `#[entry]` macro fn main() -> !; @@ -385,6 +381,20 @@ pub struct Peripherals { pub i2c3: bl_soc::I2C>, } +#[cfg(feature = "rom-peripherals")] +pub use bl_soc::clocks::Clocks; + +// Used by macros only. +#[cfg(feature = "rom-peripherals")] +#[doc(hidden)] +#[inline(always)] +pub fn __new_clocks(xtal_hz: u32) -> Clocks { + use embedded_time::rate::Hertz; + Clocks { + xtal: Hertz(xtal_hz), + } +} + #[cfg(test)] mod tests { use super::{HalBootheader, HalCpuCfg, HalPllConfig, HalSysClkConfig};