From c405b543a480a7afe91c9020527e0c61ce1bba94 Mon Sep 17 00:00:00 2001 From: Andelf Date: Sun, 24 Nov 2024 17:17:51 +0800 Subject: [PATCH] feat: replace riscv-rt with hpm-riscv-rt --- Cargo.toml | 30 +++---- build.rs | 10 +-- examples/hpm5300evk/.cargo/config.toml | 19 +++-- examples/hpm5300evk/Cargo.toml | 13 ++- examples/hpm5300evk/memory.x | 19 +++-- examples/hpm6300evk/Cargo.toml | 2 +- examples/hpm6750evkmini/Cargo.toml | 4 +- examples/hpm6e00evk/Cargo.toml | 14 +++- src/embassy/mod.rs | 5 ++ src/embassy/time_driver_mchtmr.rs | 6 +- src/lib.rs | 11 ++- src/rt.rs | 112 ------------------------- 12 files changed, 78 insertions(+), 167 deletions(-) delete mode 100644 src/rt.rs diff --git a/Cargo.toml b/Cargo.toml index 856d49d..e809367 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,11 @@ license = "MIT/Apache-2.0" [dependencies] # hpm-metapac = { path = "../hpm-data/build/hpm-metapac" } -hpm-metapac = { version = "0.0.5", git = "https://github.com/hpmicro-rs/hpm-metapac.git", tag = "hpm-data-71a17c4971e813a2c7151de990cbe05fe9a19a70" } +hpm-metapac = { version = "0.0.5", git = "https://github.com/hpmicro-rs/hpm-metapac.git", tag = "hpm-data-caa8fd80439f3ac44e0701c4e6cde71a87128ee6" } +hpm-riscv-rt = { version = "0.1.1", optional = true } +andes-riscv = { version = "0.1.1" } riscv = { version = "0.11", features = ["critical-section-single-hart"] } -riscv-rt = { version = "0.12.2", optional = true } -andes-riscv = { version = "0.0.3" } embedded-hal = { version = "1.0.0" } embassy-time-driver = { version = "0.1.0", features = [ @@ -49,16 +49,16 @@ rand_core = "0.6.4" # hpm-metapac = { path = "../hpm-data/build/hpm-metapac", default-features = false, features = [ # "metadata", # ] } -hpm-metapac = { git = "https://github.com/hpmicro-rs/hpm-metapac.git", tag = "hpm-data-71a17c4971e813a2c7151de990cbe05fe9a19a70", default-features = false, features = [ +hpm-metapac = { git = "https://github.com/hpmicro-rs/hpm-metapac.git", tag = "hpm-data-caa8fd80439f3ac44e0701c4e6cde71a87128ee6", default-features = false, features = [ "metadata", ] } -proc-macro2 = "1.0.86" +proc-macro2 = "1.0.92" quote = "1.0.37" [features] default = ["rt", "embassy", "defmt", "time"] -rt = ["dep:riscv-rt", "hpm-metapac/rt"] +rt = ["dep:hpm-riscv-rt", "hpm-metapac/rt"] defmt = ["dep:defmt"] time = ["dep:embassy-time"] chrono = ["dep:chrono"] @@ -83,15 +83,15 @@ hpm6340 = ["hpm-metapac/hpm6340"] hpm6350 = ["hpm-metapac/hpm6350"] hpm6360 = ["hpm-metapac/hpm6360"] hpm6364 = ["hpm-metapac/hpm6364"] -hpm6420 = ["hpm-metapac/hpm6420"] -hpm6430 = ["hpm-metapac/hpm6430"] -hpm6450 = ["hpm-metapac/hpm6450"] -hpm6454 = ["hpm-metapac/hpm6454"] -hpm64a0 = ["hpm-metapac/hpm64a0"] -hpm64g0 = ["hpm-metapac/hpm64g0"] -hpm6730 = ["hpm-metapac/hpm6730"] -hpm6750 = ["hpm-metapac/hpm6750"] -hpm6754 = ["hpm-metapac/hpm6754"] +hpm6420 = ["hpm-metapac/hpm6420", "hpm-riscv-rt/hpm67-fix"] +hpm6430 = ["hpm-metapac/hpm6430", "hpm-riscv-rt/hpm67-fix"] +hpm6450 = ["hpm-metapac/hpm6450", "hpm-riscv-rt/hpm67-fix"] +hpm6454 = ["hpm-metapac/hpm6454", "hpm-riscv-rt/hpm67-fix"] +hpm64a0 = ["hpm-metapac/hpm64a0", "hpm-riscv-rt/hpm67-fix"] +hpm64g0 = ["hpm-metapac/hpm64g0", "hpm-riscv-rt/hpm67-fix"] +hpm6730 = ["hpm-metapac/hpm6730", "hpm-riscv-rt/hpm67-fix"] +hpm6750 = ["hpm-metapac/hpm6750", "hpm-riscv-rt/hpm67-fix"] +hpm6754 = ["hpm-metapac/hpm6754", "hpm-riscv-rt/hpm67-fix"] hpm6830 = ["hpm-metapac/hpm6830", "mcan"] hpm6850 = ["hpm-metapac/hpm6850", "mcan"] hpm6880 = ["hpm-metapac/hpm6880", "mcan"] diff --git a/build.rs b/build.rs index 1d17a2d..7417e31 100644 --- a/build.rs +++ b/build.rs @@ -503,9 +503,8 @@ fn main() { quote! { #[cfg(feature = "rt")] - #[allow(non_snake_case)] - #[no_mangle] - unsafe extern "riscv-interrupt-m" fn #irq() { + #[crate::interrupt] + fn #irq() { ::on_irq(); } } @@ -553,9 +552,8 @@ fn main() { }; g.extend(quote! { - #[no_mangle] - #[link_section = ".fast"] - unsafe extern "riscv-interrupt-m" fn #irq_ident() { + #[crate::interrupt] + fn #irq_ident() { use crate::interrupt::InterruptExt; crate::gpio::input_future::on_interrupt(crate::pac::GPIO0, #port_offset); diff --git a/examples/hpm5300evk/.cargo/config.toml b/examples/hpm5300evk/.cargo/config.toml index 189833e..4c6f9dc 100644 --- a/examples/hpm5300evk/.cargo/config.toml +++ b/examples/hpm5300evk/.cargo/config.toml @@ -4,11 +4,16 @@ target = "riscv32imafc-unknown-none-elf" [target.riscv32imafc-unknown-none-elf] # runner = 'riscv64-unknown-elf-gdb -x ../../openocd.gdb' runner = [ - "probe-rs", "run", - "--chip", "HPM5361", - "--chip-description-path", "../../HPMicro.yaml", - "--protocol","jtag", - "--log-format", "{t} {L} {F}:{l} {s}", + "probe-rs", + "run", + "--chip", + "HPM5361", + "--chip-description-path", + "../../HPMicro.yaml", + "--protocol", + "jtag", + "--log-format", + "{t} {L} {F}:{l} {s}", ] rustflags = [ @@ -20,9 +25,9 @@ rustflags = [ "-C", "link-arg=-Tmemory.x", "-C", - "link-arg=-Tdevice.x", # __VECTORED_INTERRUPTS + "link-arg=-Tdevice.x", # __VECTORED_INTERRUPTS, peripheral interrupt names "-C", - "link-arg=-Tlink-fixed.x", + "link-arg=-Tlink.x", "-C", "link-arg=-Tdefmt.x", "-C", diff --git a/examples/hpm5300evk/Cargo.toml b/examples/hpm5300evk/Cargo.toml index 4e2f5b8..f0f7177 100644 --- a/examples/hpm5300evk/Cargo.toml +++ b/examples/hpm5300evk/Cargo.toml @@ -14,7 +14,7 @@ hpm-hal = { path = "../..", features = [ defmt = "0.3.8" defmt-rtt = "0.4.1" -panic-halt = "0.2.0" +panic-halt = "1.0.0" riscv = { version = "0.11.1", features = ["critical-section-single-hart"] } @@ -25,14 +25,14 @@ embedded-io = "0.6.1" embedded-hal-bus = "0.2.0" embassy-time = { version = "0.3.0", features = ["tick-hz-1_000_000"] } -embassy-executor = { version = "0.6.0", features = [ +embassy-executor = { version = "0.6.3", features = [ "nightly", "integrated-timers", "arch-riscv32", "executor-thread", ] } embassy-embedded-hal = "0.2.0" -embassy-sync = "0.6.0" +embassy-sync = "0.6.1" embassy-usb = { version = "0.3.0", features = [ "defmt", @@ -43,12 +43,17 @@ usbd-hid = "0.8" mcan = "0.5.0" assign-resources = "0.4.1" -futures-util = { version = "0.3.30", default-features = false } +futures-util = { version = "0.3.31", default-features = false } heapless = "0.8.0" micromath = "2.1.0" static_cell = "2" rand_core = "0.6.4" +tinygif = "0.0.4" +smart-leds = "0.4.0" +ws2812-spi = "0.5.0" +text-image = "0.2" +microfft = "0.6.0" [profile.release] strip = false # symbols are not flashed to the microcontroller, so don't strip them. diff --git a/examples/hpm5300evk/memory.x b/examples/hpm5300evk/memory.x index bc6efae..6e087fd 100644 --- a/examples/hpm5300evk/memory.x +++ b/examples/hpm5300evk/memory.x @@ -2,16 +2,17 @@ MEMORY { XPI0_HEADER : ORIGIN = 0x80000000, LENGTH = 0x3000 /* bootheader */ XPI0_APP : ORIGIN = 0x80003000, LENGTH = 1024K - 0x3000 /* app firmware */ - ILM0 : ORIGIN = 0x00000000, LENGTH = 128K /* instruction local memory */ - DLM0 : ORIGIN = 0x00080000, LENGTH = 128K /* data local memory */ - AHB_SRAM : ORIGIN = 0xf0400000, LENGTH = 32K + ILM : ORIGIN = 0x00000000, LENGTH = 128K /* instruction local memory */ + DLM : ORIGIN = 0x00080000, LENGTH = 128K /* data local memory */ + AHB_SRAM : ORIGIN = 0xf0400000, LENGTH = 32K } REGION_ALIAS("REGION_TEXT", XPI0_APP); +REGION_ALIAS("REGION_FASTTEXT", ILM); +REGION_ALIAS("REGION_FASTDATA", DLM); REGION_ALIAS("REGION_RODATA", XPI0_APP); -REGION_ALIAS("REGION_DATA", DLM0); -REGION_ALIAS("REGION_BSS", DLM0) -REGION_ALIAS("REGION_HEAP", DLM0); -REGION_ALIAS("REGION_STACK", DLM0); -REGION_ALIAS("REGION_FASTTEXT", ILM0); +REGION_ALIAS("REGION_DATA", DLM); +REGION_ALIAS("REGION_BSS", DLM); +REGION_ALIAS("REGION_HEAP", DLM); +REGION_ALIAS("REGION_STACK", DLM); +REGION_ALIAS("REGION_NONCACHEABLE_RAM", DLM); -REGION_ALIAS("REGION_CAN", AHB_SRAM); diff --git a/examples/hpm6300evk/Cargo.toml b/examples/hpm6300evk/Cargo.toml index 9161803..899698f 100644 --- a/examples/hpm6300evk/Cargo.toml +++ b/examples/hpm6300evk/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" # "hpm6360", # "memory-x", # "rt", -# ], git = "https://github.com/hpmicro-rs/hpm-metapac.git", tag = "hpm-data-71a17c4971e813a2c7151de990cbe05fe9a19a70" } +# ], git = "https://github.com/hpmicro-rs/hpm-metapac.git", tag = "hpm-data-caa8fd80439f3ac44e0701c4e6cde71a87128ee6" } hpm-hal = { path = "../..", features = ["rt", "embassy", "hpm6360"] } defmt = "0.3.8" defmt-rtt = "0.4.1" diff --git a/examples/hpm6750evkmini/Cargo.toml b/examples/hpm6750evkmini/Cargo.toml index 359be6d..491be5f 100644 --- a/examples/hpm6750evkmini/Cargo.toml +++ b/examples/hpm6750evkmini/Cargo.toml @@ -11,7 +11,7 @@ embedded-hal = "1.0.0" # "hpm6750", # "memory-x", # "rt", -#], git = "https://github.com/hpmicro-rs/hpm-metapac.git", tag = "hpm-data-71a17c4971e813a2c7151de990cbe05fe9a19a70" } +#], git = "https://github.com/hpmicro-rs/hpm-metapac.git", tag = "hpm-data-caa8fd80439f3ac44e0701c4e6cde71a87128ee6" } hpm-hal = { path = "../..", features = ["rt", "embassy", "hpm6750", "chrono"] } @@ -21,7 +21,7 @@ riscv-rt = { version = "0.12.2", features = ["single-hart"] } riscv-semihosting = "0.1.0" embassy-time = { version = "0.3.0", features = ["tick-hz-1_000_000"] } embassy-executor = { version = "0.6.0", features = [ - "nightly", + # "nightly", "integrated-timers", "arch-riscv32", "executor-thread", diff --git a/examples/hpm6e00evk/Cargo.toml b/examples/hpm6e00evk/Cargo.toml index e3b1d27..2edfcbf 100644 --- a/examples/hpm6e00evk/Cargo.toml +++ b/examples/hpm6e00evk/Cargo.toml @@ -29,7 +29,7 @@ embassy-usb = { version = "0.3.0", features = [ "max-handler-count-8", "max-interface-count-8", ] } -futures-util = { version = "0.3.30", default-features = false } +futures-util = { version = "0.3", default-features = false } tinygif = "0.0.4" assign-resources = "0.4.1" embedded-hal-bus = "0.2.0" @@ -37,6 +37,18 @@ embassy-sync = "0.6.0" usbd-hid = "0.8" static_cell = "2" +edrv-bh1750 = { path = "../../../../embedded-drivers/bh1750" } +edrv-bme280 = { path = "../../../../embedded-drivers/bme280" } +edrv-bmp180 = { path = "../../../../embedded-drivers/bmp180" } +edrv-bme680 = { path = "../../../../embedded-drivers/bme680" } + +edrv-mpu6050 = { path = "../../../../embedded-drivers/mpu6050" } +edrv-apds9930 = { path = "../../../../embedded-drivers/apds9930" } + +edrv-ltr390uv = { path = "../../../../embedded-drivers/ltr390uv" } + +edrv-spl06 = { path = "../../../../embedded-drivers/spl06" } + [profile.release] strip = false # symbols are not flashed to the microcontroller, so don't strip them. lto = true diff --git a/src/embassy/mod.rs b/src/embassy/mod.rs index ca0b7bb..a1e08f2 100644 --- a/src/embassy/mod.rs +++ b/src/embassy/mod.rs @@ -1,3 +1,8 @@ +//! The embassy time driver for HPMicro MCUs. +//! +//! Dev Note: Unlike STM32, GPTMR(TMR) can not be used for time driver because it lacks of channel sychronization. +//! See-also: https://github.com/hpmicro/hpm-hal/issues/9 + #[path = "time_driver_mchtmr.rs"] pub mod time_driver_impl; diff --git a/src/embassy/time_driver_mchtmr.rs b/src/embassy/time_driver_mchtmr.rs index 7c1c51a..3394e5d 100644 --- a/src/embassy/time_driver_mchtmr.rs +++ b/src/embassy/time_driver_mchtmr.rs @@ -9,8 +9,8 @@ use embassy_time_driver::AlarmHandle; use hpm_metapac::sysctl::vals; use hpm_metapac::{MCHTMR, SYSCTL}; -use crate::pac; use crate::sysctl::ClockConfig; +use crate::{interrupt, pac}; pub const ALARM_COUNT: usize = 1; @@ -184,9 +184,7 @@ impl embassy_time_driver::Driver for MachineTimerDriver { } // Core local interrupts are handled in CORE_LOCAL -#[no_mangle] -#[link_section = ".fast"] -#[allow(non_snake_case)] +#[interrupt] fn MachineTimer() { DRIVER.on_interrupt(); } diff --git a/src/lib.rs b/src/lib.rs index 9c93c8b..e35c49b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,6 +57,8 @@ pub mod usb; #[cfg(femc)] pub mod femc; +//#[cfg(i2s)] +//pub mod i2s; #[cfg(rtc)] pub mod rtc; @@ -75,9 +77,7 @@ pub mod rng; pub mod trgm; #[cfg(feature = "rt")] -pub mod rt; -#[cfg(feature = "rt")] -pub use riscv_rt::entry; +pub use hpm_riscv_rt::{entry, interrupt, pre_init}; #[cfg(feature = "embassy")] pub mod embassy; @@ -131,9 +131,8 @@ macro_rules! bind_interrupts { $vis struct $name; $( - #[allow(non_snake_case)] - #[no_mangle] - unsafe extern "riscv-interrupt-m" fn $irq() { + #[$crate::interrupt] + fn $irq() { use $crate::interrupt::InterruptExt; $( diff --git a/src/rt.rs b/src/rt.rs deleted file mode 100644 index 1f6d9c8..0000000 --- a/src/rt.rs +++ /dev/null @@ -1,112 +0,0 @@ -//! The runtime support for the MCU. - -use core::arch::asm; - -use crate::interrupt::PlicExt; -use crate::pac; - -#[no_mangle] -pub unsafe extern "Rust" fn _setup_interrupts() { - extern "C" { - // Symbol defined in hpm-metapac. - // The symbol must be in FLASH(XPI) or ILM section. - static __VECTORED_INTERRUPTS: [u32; 1]; - } - - // clean up plic, it will help while debugging - pac::PLIC.set_threshold(0); - for i in 0..128 { - pac::PLIC.targetconfig(0).claim().modify(|w| w.set_interrupt_id(i)); - } - // clear any bits left in plic enable register - for i in 0..4 { - pac::PLIC.targetint(0).inten(i).write(|w| w.0 = 0); - } - - // enable mcycle - unsafe { - riscv::register::mcounteren::set_cy(); - } - - let vector_addr = __VECTORED_INTERRUPTS.as_ptr() as u32; - // FIXME: TrapMode is ignored in mtvec, it's set in CSR_MMISC_CTL - riscv::register::mtvec::write(vector_addr as usize, riscv::register::mtvec::TrapMode::Direct); - - // Enable vectored external PLIC interrupt - // CSR_MMISC_CTL = 0x7D0 - unsafe { - asm!("csrsi 0x7D0, 2"); - pac::PLIC.feature().modify(|w| w.set_vectored(true)); - riscv::register::mstatus::set_mie(); // must enable global interrupt - riscv::register::mstatus::set_sie(); // and supervisor interrupt - riscv::register::mie::set_mext(); // and PLIC external interrupt - } -} - -#[no_mangle] -#[link_section = ".fast"] -unsafe extern "riscv-interrupt-m" fn CORE_LOCAL() { - use riscv_rt::__INTERRUPTS; - extern "C" { - fn DefaultHandler(); - } - - let cause = riscv::register::mcause::read(); - let code = cause.code(); - - if cause.is_exception() { - // Ref: HPM6700_6400_Errata_V2_0.pdf "E00001:RISC-V 处理器指令和数据本地存储器使用限制" - #[cfg(hpm67)] - if code == 2 { - // Illegal instruction - if riscv::register::mtval::read() == 0x0 { - return; - } - } - defmt::error!("Exception code: {}", code); - loop {} // dead loop - } else if code < __INTERRUPTS.len() { - let h = &__INTERRUPTS[code]; - if let Some(handler) = h { - handler(); - } else { - DefaultHandler(); - } - } else { - DefaultHandler(); - } -} - -#[riscv_rt::pre_init] -unsafe fn __pre_init() { - andes_riscv::l1c::ic_enable(); - andes_riscv::l1c::dc_enable(); - andes_riscv::l1c::dc_invalidate_all(); - - core::arch::asm!( - " - // Copy over .fast - la {start},_sfast - la {end},_efast - la {input},_sifast - - bgeu {start},{end},2f - 1: - lw {a},0({input}) - addi {input},{input},4 - sw {a},0({start}) - addi {start},{start},4 - bltu {start},{end},1b - - 2: - li {a},0 - li {input},0 - li {start},0 - li {end},0 - ", - start = out(reg) _, - end = out(reg) _, - input = out(reg) _, - a = out(reg) _, - ); -}