2019-12-25 12:11:15 +00:00
|
|
|
use crate::definitions::XousError;
|
2019-12-24 01:33:02 +00:00
|
|
|
use crate::filled_array;
|
2020-01-09 02:45:22 +00:00
|
|
|
use vexriscv::register::{mstatus, vmim};
|
2019-12-19 04:17:07 +00:00
|
|
|
|
|
|
|
static mut IRQ_HANDLERS: [Option<fn(usize)>; 32] = filled_array![None; 32];
|
|
|
|
|
|
|
|
pub fn handle(irqs_pending: usize) {
|
|
|
|
// Unsafe is required here because we're accessing a static
|
|
|
|
// mutable value, and it could be modified from various threads.
|
|
|
|
// However, this is fine because this is run from an IRQ context
|
|
|
|
// with interrupts disabled.
|
|
|
|
// NOTE: This will become an issue when running with multiple cores,
|
|
|
|
// so this should be protected by a mutex.
|
|
|
|
unsafe {
|
|
|
|
for irq_no in 0..IRQ_HANDLERS.len() {
|
|
|
|
if irqs_pending & (1 << irq_no) != 0 {
|
|
|
|
if let Some(f) = IRQ_HANDLERS[irq_no] {
|
|
|
|
// Call the IRQ handler
|
|
|
|
f(irq_no);
|
|
|
|
} else {
|
|
|
|
// If there is no handler, mask this interrupt
|
|
|
|
// to prevent an IRQ storm. This is considered
|
|
|
|
// an error.
|
|
|
|
vmim::write(vmim::read() | (1 << irq_no));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 12:11:15 +00:00
|
|
|
pub fn sys_interrupt_claim(irq: usize, f: fn(usize)) -> Result<(), XousError> {
|
2019-12-19 04:17:07 +00:00
|
|
|
// Unsafe is required since we're accessing a static mut array.
|
|
|
|
// However, we disable interrupts to prevent contention on this array.
|
|
|
|
unsafe {
|
|
|
|
mstatus::clear_mie();
|
|
|
|
let result = if irq > IRQ_HANDLERS.len() {
|
2019-12-25 12:11:15 +00:00
|
|
|
Err(XousError::InterruptNotFound)
|
2019-12-19 04:17:07 +00:00
|
|
|
} else if IRQ_HANDLERS[irq].is_some() {
|
2019-12-25 12:11:15 +00:00
|
|
|
Err(XousError::InterruptInUse)
|
2019-12-19 04:17:07 +00:00
|
|
|
} else {
|
|
|
|
IRQ_HANDLERS[irq] = Some(f);
|
|
|
|
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
|
|
|
|
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
|
|
|
|
vmim::write(vmim::read() | (1 << irq));
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
mstatus::set_mie();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|