From 4d4b4897a0d56a72252d37179ee3d28db810c3e7 Mon Sep 17 00:00:00 2001 From: plucky Date: Fri, 8 Sep 2023 15:33:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?macros:=20=E5=88=9D=E6=AD=A5=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0#[interrupt]=E5=AE=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 目前宏只有检查中断处理函数的功能,还没有将中断注册到硬件中。 Signed-off-by: plucky --- examples/blinky-bl808/src/main.rs | 7 +++- macros/src/lib.rs | 65 ++++++++++++++++++++++++++++++- src/lib.rs | 2 +- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/examples/blinky-bl808/src/main.rs b/examples/blinky-bl808/src/main.rs index 3acac52..504b45a 100644 --- a/examples/blinky-bl808/src/main.rs +++ b/examples/blinky-bl808/src/main.rs @@ -9,7 +9,7 @@ #![no_std] #![no_main] -use bl_rom_rt::{entry, Clocks, Peripherals}; +use bl_rom_rt::{entry, interrupt, Clocks, Peripherals}; use embedded_hal::digital::OutputPin; use panic_halt as _; @@ -27,3 +27,8 @@ fn main(p: Peripherals, _c: Clocks) -> ! { } } } + +#[interrupt] +fn uart0() { + // TODO: interrupt handler content +} diff --git a/macros/src/lib.rs b/macros/src/lib.rs index c843aa5..f8bd0de 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -29,7 +29,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { if f.sig.inputs.len() != 2 { return parse::Error::new( f.sig.inputs.span(), - "`#[entry]` function with rom peripherlas should include exactly two parameters", + "`#[entry]` function with rom peripherals should include exactly two parameters", ) .to_compile_error() .into(); @@ -95,3 +95,66 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { } .into() } + +/// Interrupt handler function. +#[proc_macro_attribute] +pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { + if !args.is_empty() { + return parse::Error::new( + Span::call_site(), + "#[interrupt] attribute accepts no arguments", + ) + .to_compile_error() + .into(); + } + + let f = parse_macro_input!(input as ItemFn); + + if f.sig.inputs.len() != 0 { + return parse::Error::new( + f.sig.inputs.span(), + "`#[interrupt]` function should not include any parameter", + ) + .to_compile_error() + .into(); + } + + let valid_signature = f.sig.constness.is_none() + && f.vis == Visibility::Inherited + && f.sig.abi.is_none() + && f.sig.inputs.is_empty() + && f.sig.generics.params.is_empty() + && f.sig.generics.where_clause.is_none() + && f.sig.variadic.is_none() + && match f.sig.output { + ReturnType::Default => true, + ReturnType::Type(_, ref ty) => match **ty { + Type::Tuple(ref tuple) => tuple.elems.is_empty(), + Type::Never(..) => true, + _ => false, + }, + }; + + if !valid_signature { + return parse::Error::new( + f.sig.span(), + "`#[interrupt]` handlers must have signature `[unsafe] fn() [-> !]`", + ) + .to_compile_error() + .into(); + } + + let attrs = f.attrs; + let unsafety = f.sig.unsafety; + let stmts = f.block.stmts; + let ident = f.sig.ident; + let output = f.sig.output; + + quote!( + #(#attrs)* + pub #unsafety extern "C" fn #ident() #output { + #(#stmts)* + } + ) + .into() +} diff --git a/src/lib.rs b/src/lib.rs index ba5f07e..4c0623a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ #![feature(naked_functions, asm_const)] #![no_std] -pub use bl_rom_rt_macros::entry; +pub use bl_rom_rt_macros::{entry, interrupt}; pub mod soc; -- Gitee From 95f5649eb989899807683ebd4f90f407e48d1936 Mon Sep 17 00:00:00 2001 From: plucky Date: Fri, 8 Sep 2023 15:40:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?examples:=20=E7=94=A8riscv=E5=8C=85?= =?UTF-8?q?=E7=9A=84delay=E5=87=BD=E6=95=B0=E4=BB=A3=E6=9B=BFfor=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: plucky --- examples/blinky-bl616/Cargo.toml | 1 + examples/blinky-bl616/src/main.rs | 8 ++------ examples/blinky-bl808/Cargo.toml | 1 + examples/blinky-bl808/src/main.rs | 8 ++------ 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/examples/blinky-bl616/Cargo.toml b/examples/blinky-bl616/Cargo.toml index 6a5c738..3af114e 100644 --- a/examples/blinky-bl616/Cargo.toml +++ b/examples/blinky-bl616/Cargo.toml @@ -10,6 +10,7 @@ publish = false bl-rom-rt = { path = "../.." } embedded-hal = "1.0.0-rc.1" panic-halt = "0.2.0" +riscv = "0.10.1" [features] default = ["bl616"] diff --git a/examples/blinky-bl616/src/main.rs b/examples/blinky-bl616/src/main.rs index 11e6b66..7c87684 100644 --- a/examples/blinky-bl616/src/main.rs +++ b/examples/blinky-bl616/src/main.rs @@ -23,13 +23,9 @@ fn main(p: Peripherals, _c: Clocks) -> ! { loop { io27.set_low().ok(); io28.set_low().ok(); - for _ in 0..=100_000 { - unsafe { asm!("nop") } - } + unsafe { riscv::asm::delay(100_000) }; io27.set_high().ok(); io28.set_high().ok(); - for _ in 0..=100_000 { - unsafe { asm!("nop") } - } + unsafe { riscv::asm::delay(100_000) }; } } diff --git a/examples/blinky-bl808/Cargo.toml b/examples/blinky-bl808/Cargo.toml index 7dfd8cd..46c565a 100644 --- a/examples/blinky-bl808/Cargo.toml +++ b/examples/blinky-bl808/Cargo.toml @@ -10,6 +10,7 @@ publish = false bl-rom-rt = { path = "../.." } embedded-hal = "1.0.0-rc.1" panic-halt = "0.2.0" +riscv = "0.10.1" [features] default = ["bl808-d0"] diff --git a/examples/blinky-bl808/src/main.rs b/examples/blinky-bl808/src/main.rs index 504b45a..782ab6f 100644 --- a/examples/blinky-bl808/src/main.rs +++ b/examples/blinky-bl808/src/main.rs @@ -18,13 +18,9 @@ fn main(p: Peripherals, _c: Clocks) -> ! { let mut led = p.gpio.io8.into_floating_output(); loop { led.set_low().ok(); - for _ in 0..100_000 { - unsafe { core::arch::asm!("nop") } - } + unsafe { riscv::asm::delay(100_000) }; led.set_high().ok(); - for _ in 0..100_000 { - unsafe { core::arch::asm!("nop") } - } + unsafe { riscv::asm::delay(100_000) }; } } -- Gitee