use atsamd_hal_macros::hal_cfg;
use core::sync::atomic;
use crate::{
async_hal::interrupts::{DMAC, Handler},
dmac::{TriggerSource, waker::WAKERS},
util::BitIter,
};
pub struct InterruptHandler {
_private: (),
}
impl crate::typelevel::Sealed for InterruptHandler {}
#[hal_cfg(any("dmac-d11", "dmac-d21"))]
impl Handler<DMAC> for InterruptHandler {
unsafe fn on_interrupt() {
let dmac = unsafe { crate::pac::Peripherals::steal().dmac };
critical_section::with(|_| {
let old_id = dmac.chid().read().id().bits();
let pending_interrupts = BitIter(dmac.intstatus().read().bits());
for pend_channel in pending_interrupts {
unsafe { dmac.chid().modify(|_, w| w.id().bits(pend_channel as u8)) };
let wake = if dmac.chintflag().read().tcmpl().bit_is_set() {
dmac.chintenclr().modify(|_, w| w.tcmpl().set_bit());
true
} else if dmac.chintflag().read().terr().bit_is_set() {
dmac.chintenclr().modify(|_, w| w.terr().set_bit());
true
} else {
false
};
if wake {
dmac.chctrla().modify(|_, w| w.enable().clear_bit());
dmac.chctrlb()
.modify(|_, w| w.trigsrc().variant(TriggerSource::Disable));
while dmac.chctrla().read().enable().bit_is_set() {
core::hint::spin_loop();
}
atomic::fence(atomic::Ordering::Acquire);
WAKERS[pend_channel as usize].wake();
}
}
unsafe {
dmac.chid().write(|w| w.id().bits(old_id));
}
});
}
}
#[hal_cfg("dmac-d5x")]
impl Handler<DMAC> for InterruptHandler {
unsafe fn on_interrupt() {
let dmac = unsafe { crate::pac::Peripherals::steal().dmac };
let pending_channels = BitIter(dmac.intstatus().read().bits());
for channel in pending_channels.map(|c| c as usize) {
let wake = if dmac
.channel(channel)
.chintflag()
.read()
.tcmpl()
.bit_is_set()
{
dmac.channel(channel)
.chintenclr()
.modify(|_, w| w.tcmpl().set_bit());
true
} else if dmac.channel(channel).chintflag().read().terr().bit_is_set() {
dmac.channel(channel)
.chintenclr()
.modify(|_, w| w.terr().set_bit());
true
} else {
false
};
if wake {
dmac.channel(channel).chctrla().modify(|_, w| {
w.enable().clear_bit();
w.trigsrc().variant(TriggerSource::Disable)
});
while dmac.channel(channel).chctrla().read().enable().bit_is_set() {
core::hint::spin_loop();
}
atomic::fence(atomic::Ordering::Acquire);
WAKERS[channel].wake();
}
}
}
}