07-31-2023, 04:26 AM
Code:
pub const I2S_PCM0 = IRQ(45);
pub const I2S_PCM1 = IRQ(46);
pub const I2S_PCM2 = IRQ(47);
pub const PG_EINT = IRQ(49);
pub const TIMER0 = IRQ(50);
pub const TIMER1 = IRQ(51);
fn IRQ(comptime irq: u8) type {
const cache = struct {
var lookup: [IRQ_LIMIT]*const fn () void = undefined;
pub export fn zigInterruptHandler(id: c_int) void {
const handler = lookup[@intCast(u8, id)];
handler();
debug.green(true);
}
};
return struct {
const irqNo: u8 = irq;
var enabled: bool = false;
pub fn enable(handler: *const fn () void) void {
if (!enabled) {
c.enableIRQ(irqNo);
cache.lookup[irqNo] = handler;
enabled = true;
}
}
};
}
Oh and the code if you are interested. It works if I move the exported fn and lookup table up to the file level, but since that is also just a 'struct' in Zig, I thought this might work.