rp: relocate programs implicitly during load
this removed the RelocatedProgram construction step from pio uses. there's not all that much to be said for the extra step because the origin can be set on the input program itself, and the remaining information exposed by RelocatedProgram can be exposed from LoadedProgram instead (even though it's already available on the pio_asm programs, albeit perhaps less convenient). we do lose access to the relocated instruction iterator, but we also cannot think of anything this iterator would actually be useful for outside of program loading.
This commit is contained in:
@ -33,7 +33,7 @@ pub mod watchdog;
|
||||
// TODO: move `pio_instr_util` and `relocate` to inside `pio`
|
||||
pub mod pio;
|
||||
pub mod pio_instr_util;
|
||||
pub mod relocate;
|
||||
pub(crate) mod relocate;
|
||||
|
||||
// Reexports
|
||||
pub use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
|
||||
|
@ -11,7 +11,7 @@ use fixed::types::extra::U8;
|
||||
use fixed::FixedU32;
|
||||
use pac::io::vals::Gpio0ctrlFuncsel;
|
||||
use pac::pio::vals::SmExecctrlStatusSel;
|
||||
use pio::{SideSet, Wrap};
|
||||
use pio::{Program, SideSet, Wrap};
|
||||
|
||||
use crate::dma::{Channel, Transfer, Word};
|
||||
use crate::gpio::sealed::Pin as SealedPin;
|
||||
@ -734,23 +734,67 @@ pub struct InstanceMemory<'d, PIO: Instance> {
|
||||
|
||||
pub struct LoadedProgram<'d, PIO: Instance> {
|
||||
pub used_memory: InstanceMemory<'d, PIO>,
|
||||
origin: u8,
|
||||
wrap: Wrap,
|
||||
side_set: SideSet,
|
||||
pub origin: u8,
|
||||
pub wrap: Wrap,
|
||||
pub side_set: SideSet,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub enum LoadError {
|
||||
/// Insufficient consecutive free instruction space to load program.
|
||||
InsufficientSpace,
|
||||
/// Loading the program would overwrite an instruction address already
|
||||
/// used by another program.
|
||||
AddressInUse(usize),
|
||||
}
|
||||
|
||||
impl<'d, PIO: Instance> Common<'d, PIO> {
|
||||
pub fn load_program<const SIZE: usize>(&mut self, prog: &RelocatedProgram<SIZE>) -> LoadedProgram<'d, PIO> {
|
||||
/// Load a PIO program. This will automatically relocate the program to
|
||||
/// an available chunk of free instruction memory if the program origin
|
||||
/// was not explicitly specified, otherwise it will attempt to load the
|
||||
/// program only at its origin.
|
||||
pub fn load_program<const SIZE: usize>(&mut self, prog: &Program<SIZE>) -> LoadedProgram<'d, PIO> {
|
||||
match self.try_load_program(prog) {
|
||||
Ok(r) => r,
|
||||
Err(at) => panic!("Trying to write already used PIO instruction memory at {}", at),
|
||||
Err(e) => panic!("Failed to load PIO program: {:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Load a PIO program. This will automatically relocate the program to
|
||||
/// an available chunk of free instruction memory if the program origin
|
||||
/// was not explicitly specified, otherwise it will attempt to load the
|
||||
/// program only at its origin.
|
||||
pub fn try_load_program<const SIZE: usize>(
|
||||
&mut self,
|
||||
prog: &RelocatedProgram<SIZE>,
|
||||
prog: &Program<SIZE>,
|
||||
) -> Result<LoadedProgram<'d, PIO>, LoadError> {
|
||||
match prog.origin {
|
||||
Some(origin) => self
|
||||
.try_load_program_at(prog, origin)
|
||||
.map_err(|a| LoadError::AddressInUse(a)),
|
||||
None => {
|
||||
// naively search for free space, allowing wraparound since
|
||||
// PIO does support that. with only 32 instruction slots it
|
||||
// doesn't make much sense to do anything more fancy.
|
||||
let mut origin = 0;
|
||||
while origin < 32 {
|
||||
match self.try_load_program_at(prog, origin as _) {
|
||||
Ok(r) => return Ok(r),
|
||||
Err(a) => origin = a + 1,
|
||||
}
|
||||
}
|
||||
Err(LoadError::InsufficientSpace)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn try_load_program_at<const SIZE: usize>(
|
||||
&mut self,
|
||||
prog: &Program<SIZE>,
|
||||
origin: u8,
|
||||
) -> Result<LoadedProgram<'d, PIO>, usize> {
|
||||
let prog = RelocatedProgram::new_with_origin(prog, origin);
|
||||
let used_memory = self.try_write_instr(prog.origin() as _, prog.code())?;
|
||||
Ok(LoadedProgram {
|
||||
used_memory,
|
||||
@ -760,7 +804,7 @@ impl<'d, PIO: Instance> Common<'d, PIO> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn try_write_instr<I>(&mut self, start: usize, instrs: I) -> Result<InstanceMemory<'d, PIO>, usize>
|
||||
fn try_write_instr<I>(&mut self, start: usize, instrs: I) -> Result<InstanceMemory<'d, PIO>, usize>
|
||||
where
|
||||
I: Iterator<Item = u16>,
|
||||
{
|
||||
|
@ -41,11 +41,6 @@ pub struct RelocatedProgram<'a, const PROGRAM_SIZE: usize> {
|
||||
}
|
||||
|
||||
impl<'a, const PROGRAM_SIZE: usize> RelocatedProgram<'a, PROGRAM_SIZE> {
|
||||
pub fn new(program: &Program<PROGRAM_SIZE>) -> RelocatedProgram<PROGRAM_SIZE> {
|
||||
let origin = program.origin.unwrap_or(0);
|
||||
RelocatedProgram { program, origin }
|
||||
}
|
||||
|
||||
pub fn new_with_origin(program: &Program<PROGRAM_SIZE>, origin: u8) -> RelocatedProgram<PROGRAM_SIZE> {
|
||||
RelocatedProgram { program, origin }
|
||||
}
|
||||
|
Reference in New Issue
Block a user