diff options
Diffstat (limited to 'usbh/src/pipe.rs')
-rw-r--r-- | usbh/src/pipe.rs | 80 |
1 files changed, 76 insertions, 4 deletions
diff --git a/usbh/src/pipe.rs b/usbh/src/pipe.rs index c33a3a2..cd89eb3 100644 --- a/usbh/src/pipe.rs +++ b/usbh/src/pipe.rs @@ -608,17 +608,17 @@ impl PipeDesc { } #[derive(Clone, Copy, Debug)] -#[repr(C)] +#[repr(C, packed)] // 16 bytes per bank. pub(crate) struct BankDesc { pub addr: Addr, pub pcksize: PckSize, pub extreg: ExtReg, pub status_bk: StatusBk, + _reserved0: u8, pub ctrl_pipe: CtrlPipe, pub status_pipe: StatusPipe, - - _reserved: u8, + _reserved1: u8, } impl BankDesc { @@ -628,9 +628,81 @@ impl BankDesc { pcksize: PckSize::from(0), extreg: ExtReg::from(0), status_bk: StatusBk::from(0), + _reserved0: 0, ctrl_pipe: CtrlPipe::from(0), status_pipe: StatusPipe::from(0), - _reserved: 0, + _reserved1: 0, } } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn bank_desc_sizes() { + assert_eq!(core::mem::size_of::<Addr>(), 4, "Addr register size."); + assert_eq!(core::mem::size_of::<PckSize>(), 4, "PckSize register size."); + assert_eq!(core::mem::size_of::<ExtReg>(), 2, "ExtReg register size."); + assert_eq!( + core::mem::size_of::<StatusBk>(), + 1, + "StatusBk register size." + ); + assert_eq!( + core::mem::size_of::<CtrlPipe>(), + 2, + "CtrlPipe register size." + ); + assert_eq!( + core::mem::size_of::<StatusPipe>(), + 1, + "StatusPipe register size." + ); + + // addr at 0x00 for 4 + // pcksize at 0x04 for 4 + // extreg at 0x08 for 2 + // status_bk at 0x0a for 2 + // ctrl_pipe at 0x0c for 2 + // status_pipe at 0x0e for 1 + assert_eq!( + core::mem::size_of::<BankDesc>(), + 16, + "Bank descriptor size." + ); + } + + #[test] + fn bank_desc_offsets() { + let bd = BankDesc::new(); + let base = &bd as *const _ as usize; + + assert_offset("Addr", &bd.addr, base, 0x00); + assert_offset("PckSize", &bd.pcksize, base, 0x04); + assert_offset("ExtReg", &bd.extreg, base, 0x08); + assert_offset("StatusBk", &bd.status_bk, base, 0x0a); + assert_offset("CtrlPipe", &bd.ctrl_pipe, base, 0x0c); + assert_offset("StatusPipe", &bd.status_pipe, base, 0x0e); + } + + #[test] + fn pipe_desc_size() { + assert_eq!(core::mem::size_of::<PipeDesc>(), 32); + } + + #[test] + fn pipe_desc_offsets() { + let pd = PipeDesc::new(); + let base = &pd as *const _ as usize; + + assert_offset("Bank0", &pd.bank0, base, 0x00); + assert_offset("Bank1", &pd.bank1, base, 0x10); + } + + fn assert_offset<T>(name: &str, field: &T, base: usize, offset: usize) { + let ptr = field as *const _ as usize; + assert_eq!(ptr - base, offset, "{} register offset.", name); + } +} |