pio: Fix bugs spotted in the review.

This commit is contained in:
Mathias Gottschlag 2021-09-29 20:55:24 +02:00
parent 684f483859
commit 64fa8441d8

View file

@ -29,21 +29,21 @@ pub trait PIOExt:
_phantom: core::marker::PhantomData, _phantom: core::marker::PhantomData,
}; };
let sm1 = UninitStateMachine { let sm1 = UninitStateMachine {
id: 0, id: 1,
block: self.deref(), block: self.deref(),
sm: &self.deref().sm[0], sm: &self.deref().sm[1],
_phantom: core::marker::PhantomData, _phantom: core::marker::PhantomData,
}; };
let sm2 = UninitStateMachine { let sm2 = UninitStateMachine {
id: 0, id: 2,
block: self.deref(), block: self.deref(),
sm: &self.deref().sm[0], sm: &self.deref().sm[2],
_phantom: core::marker::PhantomData, _phantom: core::marker::PhantomData,
}; };
let sm3 = UninitStateMachine { let sm3 = UninitStateMachine {
id: 0, id: 3,
block: self.deref(), block: self.deref(),
sm: &self.deref().sm[0], sm: &self.deref().sm[3],
_phantom: core::marker::PhantomData, _phantom: core::marker::PhantomData,
}; };
( (
@ -207,7 +207,7 @@ impl<P: PIOExt> PIO<P> {
/// Removes the specified program from instruction memory, freeing the allocated space. /// Removes the specified program from instruction memory, freeing the allocated space.
pub fn uninstall(&mut self, p: InstalledProgram<P>) { pub fn uninstall(&mut self, p: InstalledProgram<P>) {
let instr_mask = (1 << p.length as u32) << p.offset as u32; let instr_mask = ((1 << p.length as u32) - 1) << p.offset as u32;
self.used_instruction_space = self.used_instruction_space & !instr_mask; self.used_instruction_space = self.used_instruction_space & !instr_mask;
} }
@ -227,10 +227,11 @@ impl<P: PIOExt> PIO<P> {
const ATOMIC_SET_OFFSET: usize = 0x2000; const ATOMIC_SET_OFFSET: usize = 0x2000;
// Safety: We only use the atomic alias of the register. // Safety: We only use the atomic alias of the register.
unsafe { unsafe {
*(*sm_set[0].sm.block) (*sm_set[0].sm.block)
.ctrl .ctrl
.as_ptr() .as_ptr()
.add(ATOMIC_SET_OFFSET / 4) = sm_mask; .add(ATOMIC_SET_OFFSET / 4)
.write_volatile(sm_mask);
} }
} }
} }
@ -344,7 +345,11 @@ impl<P: PIOExt> UninitStateMachine<P> {
const ATOMIC_SET_OFFSET: usize = 0x2000; const ATOMIC_SET_OFFSET: usize = 0x2000;
// Safety: We only use the atomic alias of the register. // Safety: We only use the atomic alias of the register.
unsafe { unsafe {
*(*self.block).ctrl.as_ptr().add(ATOMIC_SET_OFFSET / 4) = bits; (*self.block)
.ctrl
.as_ptr()
.add(ATOMIC_SET_OFFSET / 4)
.write_volatile(bits);
} }
} }
@ -352,7 +357,11 @@ impl<P: PIOExt> UninitStateMachine<P> {
const ATOMIC_CLEAR_OFFSET: usize = 0x3000; const ATOMIC_CLEAR_OFFSET: usize = 0x3000;
// Safety: We only use the atomic alias of the register. // Safety: We only use the atomic alias of the register.
unsafe { unsafe {
*(*self.block).ctrl.as_ptr().add(ATOMIC_CLEAR_OFFSET / 4) = bits; (*self.block)
.ctrl
.as_ptr()
.add(ATOMIC_CLEAR_OFFSET / 4)
.write_volatile(bits);
} }
} }
@ -496,11 +505,18 @@ impl<P: PIOExt> StateMachine<P, Stopped> {
} }
w w
}); });
let set_pindirs = pio::Instruction {
operands: pio::InstructionOperands::SET {
destination: pio::SetDestination::PINDIRS,
data: ((pindir >> pin) & 0x1) as u8,
},
delay: 0,
side_set: None,
}
.encode(SideSet::new(false, 0, false));
self.sm.sm().sm_instr.write(|w| { self.sm.sm().sm_instr.write(|w| {
unsafe { unsafe {
const SET_PINDIRS: u16 = 0xe080; w.sm0_instr().bits(set_pindirs);
w.sm0_instr()
.bits(SET_PINDIRS | ((pindir >> pin) & 0x1) as u16);
} }
w w
}); });