Added RelocateProgram class for adjusting PIO-programs for different origins.
This commit is contained in:
@ -17,6 +17,8 @@ pub mod interrupt;
|
||||
pub mod pio;
|
||||
#[cfg(feature = "pio")]
|
||||
pub mod pio_instr_util;
|
||||
#[cfg(feature = "pio")]
|
||||
pub mod relocate;
|
||||
|
||||
pub mod rom_data;
|
||||
pub mod rtc;
|
||||
|
@ -941,7 +941,10 @@ pub trait PioStateMachine: Sized + Unpin {
|
||||
}
|
||||
}
|
||||
|
||||
fn write_instr(&mut self, start: usize, instrs: &[u16]) {
|
||||
fn write_instr<I>(&mut self, start: usize, instrs: I)
|
||||
where
|
||||
I: Iterator<Item = u16>,
|
||||
{
|
||||
let _ = self;
|
||||
write_instr(
|
||||
Self::Pio::PIO_NO,
|
||||
@ -1098,8 +1101,11 @@ impl<PIO: PioInstance> PioCommon for PioCommonInstance<PIO> {
|
||||
type Pio = PIO;
|
||||
}
|
||||
|
||||
fn write_instr(pio_no: u8, start: usize, instrs: &[u16], mem_user: u32) {
|
||||
for (i, instr) in instrs.iter().enumerate() {
|
||||
fn write_instr<I>(pio_no: u8, start: usize, instrs: I, mem_user: u32)
|
||||
where
|
||||
I: Iterator<Item = u16>,
|
||||
{
|
||||
for (i, instr) in instrs.enumerate() {
|
||||
let addr = (i + start) as u8;
|
||||
assert!(
|
||||
instr_mem_is_free(pio_no, addr),
|
||||
@ -1108,7 +1114,7 @@ fn write_instr(pio_no: u8, start: usize, instrs: &[u16], mem_user: u32) {
|
||||
);
|
||||
unsafe {
|
||||
PIOS[pio_no as usize].instr_mem(addr as usize).write(|w| {
|
||||
w.set_instr_mem(*instr);
|
||||
w.set_instr_mem(instr);
|
||||
});
|
||||
instr_mem_set_status(pio_no, addr, mem_user);
|
||||
}
|
||||
@ -1118,7 +1124,10 @@ fn write_instr(pio_no: u8, start: usize, instrs: &[u16], mem_user: u32) {
|
||||
pub trait PioCommon: Sized {
|
||||
type Pio: PioInstance;
|
||||
|
||||
fn write_instr(&mut self, start: usize, instrs: &[u16]) {
|
||||
fn write_instr<I>(&mut self, start: usize, instrs: I)
|
||||
where
|
||||
I: Iterator<Item = u16>,
|
||||
{
|
||||
let _ = self;
|
||||
write_instr(Self::Pio::PIO_NO, start, instrs, MEM_USED_BY_COMMON);
|
||||
}
|
||||
|
77
embassy-rp/src/relocate.rs
Normal file
77
embassy-rp/src/relocate.rs
Normal file
@ -0,0 +1,77 @@
|
||||
use core::iter::Iterator;
|
||||
|
||||
use pio::{Program, SideSet, Wrap};
|
||||
|
||||
pub struct CodeIterator<'a, I>
|
||||
where
|
||||
I: Iterator<Item = &'a u16>,
|
||||
{
|
||||
iter: I,
|
||||
offset: u8,
|
||||
}
|
||||
|
||||
impl<'a, I: Iterator<Item = &'a u16>> CodeIterator<'a, I> {
|
||||
pub fn new(iter: I, offset: u8) -> CodeIterator<'a, I> {
|
||||
CodeIterator { iter, offset }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I> Iterator for CodeIterator<'a, I>
|
||||
where
|
||||
I: Iterator<Item = &'a u16>,
|
||||
{
|
||||
type Item = u16;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.iter.next().and_then(|&instr| {
|
||||
Some(if instr & 0b1110_0000_0000_0000 == 0 {
|
||||
// this is a JMP instruction -> add offset to address
|
||||
let address = (instr & 0b1_1111) as u8;
|
||||
let address = address + self.offset;
|
||||
assert!(
|
||||
address < pio::RP2040_MAX_PROGRAM_SIZE as u8,
|
||||
"Invalid JMP out of the program after offset addition"
|
||||
);
|
||||
instr & (!0b11111) | address as u16
|
||||
} else {
|
||||
instr
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RelocatedProgram<'a, const PROGRAM_SIZE: usize> {
|
||||
program: &'a Program<PROGRAM_SIZE>,
|
||||
origin: u8,
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
pub fn code(&'a self) -> CodeIterator<'a, core::slice::Iter<'a, u16>> {
|
||||
CodeIterator::new(self.program.code.iter(), self.origin)
|
||||
}
|
||||
|
||||
pub fn wrap(&self) -> Wrap {
|
||||
let wrap = self.program.wrap;
|
||||
let origin = self.origin;
|
||||
Wrap {
|
||||
source: wrap.source + origin,
|
||||
target: wrap.target + origin,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn side_set(&self) -> SideSet {
|
||||
self.program.side_set
|
||||
}
|
||||
|
||||
pub fn origin(&self) -> u8 {
|
||||
self.origin
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user