Skip to content

Commit

Permalink
Fix the direct-vectoring examples on all RISCV chips (esp-rs#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev authored Feb 26, 2024
1 parent f5ba872 commit 7763036
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
8 changes: 2 additions & 6 deletions esp-hal/src/interrupt/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,7 @@ mod classic {
#[no_mangle]
#[link_section = ".trap"]
pub(super) unsafe extern "C" fn _restore_priority(stored_prio: u32) {
unsafe {
riscv::interrupt::disable();
}
riscv::interrupt::disable();
let intr = &*crate::peripherals::INTERRUPT_CORE0::PTR;
intr.cpu_int_thresh().write(|w| w.bits(stored_prio));
}
Expand Down Expand Up @@ -903,9 +901,7 @@ mod plic {
#[no_mangle]
#[link_section = ".trap"]
pub(super) unsafe extern "C" fn _restore_priority(stored_prio: u32) {
unsafe {
riscv::interrupt::disable();
}
riscv::interrupt::disable();
let thresh_reg = PLIC_MXINT_THRESH_REG as *mut u32;
thresh_reg.write_volatile(stored_prio);
}
Expand Down
49 changes: 28 additions & 21 deletions examples/src/bin/direct_vectoring.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![no_main]
#![no_std]
#![feature(naked_functions)]

//% CHIPS: esp32c2 esp32c3 esp32c6 esp32h2
//% FEATURES: direct-vectoring
Expand All @@ -21,6 +20,15 @@ static SWINT: Mutex<RefCell<Option<SoftwareInterruptControl>>> = Mutex::new(RefC
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
cfg_if::cfg_if! {
if #[cfg(any(feature = "esp32c6", feature = "esp32h2"))] {
let cpu_intr = &peripherals.INTPRI;
} else {
let cpu_intr = &peripherals.SYSTEM;
}
}
let sw0_trigger_addr = cpu_intr.cpu_intr_from_cpu_0() as *const _ as u32;

let system = peripherals.SYSTEM.split();
let sw_int = system.software_interrupt_control;

Expand All @@ -42,17 +50,17 @@ fn main() -> ! {
);
}

esp_println::println!("MPC:{}", unsafe { fetch_performance_timer() });

// interrupt is raised from assembly for max timer granularity.
unsafe {
asm!(
"
li t0, 0x600C5090 #FROM_CPU_INTR0 address
li t1, 1 #Flip flag
csrrwi x0, 0x7e1, 1 #enable timer
sw t1, 0(t0) #trigger FROM_CPU_INTR0
"
li {bit}, 1 # Flip flag (bit 0)
csrrwi x0, 0x7e1, 1 # enable timer
sw {bit}, 0({addr}) # trigger FROM_CPU_INTR0
",
options(nostack),
addr = in(reg) sw0_trigger_addr,
bit = out(reg) _,
)
}
esp_println::println!("Returned");
Expand All @@ -70,17 +78,16 @@ fn cpu_int_1_handler() {
.unwrap()
.reset(SoftwareInterrupt::SoftwareInterrupt0);
});
esp_println::println!("Performance counter:{}", unsafe {
fetch_performance_timer()
});
}
#[naked]
unsafe extern "C" fn fetch_performance_timer() -> i32 {
asm!(
"
csrr a0, 0x7e2
jr ra
",
options(noreturn)
);

let mut perf_counter: u32 = 0;
unsafe {
asm!(
"
csrr {x}, 0x7e2
",
options(nostack),
x = inout(reg) perf_counter,
)
};
esp_println::println!("Performance counter:{}", perf_counter);
}

0 comments on commit 7763036

Please sign in to comment.