create a serializable version of Spim

this works by blowing apart the structure and re-assembling
it on the other side. The reason we can't simply pass it
is because the hardware pointer to the core register violates
a Rust safety rule, but Rust doesn't know that this pointer
points to a hardware register that's immutable and static versus
a regular block of memory that can go away.
This commit is contained in:
bunnie 2024-05-31 20:39:39 +08:00
parent ff3138b72b
commit a8518f7fa1
2 changed files with 67 additions and 4 deletions

View file

@ -8,6 +8,7 @@ pub enum IframBank {
}
/// `IframRange` is a range of memory that is suitable for use as a DMA target.
#[derive(Debug)]
pub struct IframRange {
pub phys_range: MemoryRange,
pub virt_range: MemoryRange,

View file

@ -203,7 +203,7 @@ impl Into<u32> for PeriphEventType {
}
#[repr(u32)]
#[derive(Copy, Clone, num_derive::FromPrimitive)]
#[derive(Debug, Copy, Clone, num_derive::FromPrimitive)]
pub enum EventChannel {
Channel0 = 0,
Channel1 = 8,
@ -799,7 +799,7 @@ pub enum SpimClkPha {
CaptureOnTrailing = 1,
}
#[repr(u32)]
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum SpimCs {
Cs0 = 0,
Cs1 = 1,
@ -807,13 +807,13 @@ pub enum SpimCs {
Cs3 = 3,
}
#[repr(u32)]
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SpimMode {
Standard = 0,
Quad = 1,
}
#[repr(u32)]
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum SpimByteAlign {
Enable = 0,
Disable = 1,
@ -935,6 +935,7 @@ pub enum SpimChannel {
Channel2,
Channel3,
}
#[derive(Debug)]
pub struct Spim {
csr: CSR<u32>,
cs: SpimCs,
@ -1113,6 +1114,67 @@ impl Spim {
spim
}
/// For creating a clone to the current SPIM handle passed through a thread.
///
/// Safety: can only be used on devices that are static for the life of the OS. Also, does nothing
/// to prevent races/contention for the underlying device. The main reason this is introduced is
/// to facilitate a panic handler for the graphics frame buffer, where we're about to kill the OS
/// anyways: we don't care about soundness guarantees after this point.
pub unsafe fn from_raw_parts(
csr: usize,
cs: SpimCs,
sot_wait: u8,
eot_wait: u8,
event_channel: Option<EventChannel>,
mode: SpimMode,
_align: SpimByteAlign,
ifram: IframRange,
tx_buf_len_bytes: usize,
rx_buf_len_bytes: usize,
dummy_cycles: u8,
) -> Self {
Spim {
csr: CSR::new(csr as *mut u32),
cs,
sot_wait,
eot_wait,
event_channel,
_align,
mode,
ifram,
tx_buf_len_bytes,
rx_buf_len_bytes,
dummy_cycles,
}
}
/// Blows a SPIM structure into parts that can be sent across a thread boundary.
///
/// Safety: this is only safe because the *mut u32 for the CSR doesn't change, because it's tied to
/// a piece of hardware, not some arbitrary block of memory.
pub unsafe fn into_raw_parts(
&self,
) -> (usize, SpimCs, u8, u8, Option<EventChannel>, SpimMode, SpimByteAlign, IframRange, usize, usize, u8)
{
(
self.csr.base() as usize,
self.cs,
self.sot_wait,
self.eot_wait,
self.event_channel,
self.mode,
self._align,
IframRange {
phys_range: self.ifram.phys_range,
virt_range: self.ifram.virt_range,
conn: self.ifram.conn,
},
self.tx_buf_len_bytes,
self.rx_buf_len_bytes,
self.dummy_cycles,
)
}
/// The command buf is *always* a `u32`; so tie the type down here.
fn cmd_buf_mut(&mut self) -> &mut [u32] {
&mut self.ifram.as_slice_mut()[(self.tx_buf_len_bytes + self.rx_buf_len_bytes) / size_of::<u32>()