From 4f9113cb45b189791e134e20d0893a1121aaf8ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 26 May 2021 16:06:55 +0000 Subject: [PATCH] ad9959: refactor pad() --- ad9959/src/lib.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/ad9959/src/lib.rs b/ad9959/src/lib.rs index 21dfa21..4a66349 100644 --- a/ad9959/src/lib.rs +++ b/ad9959/src/lib.rs @@ -596,6 +596,22 @@ impl ProfileSerializer { self.index += value.len() + 1; } + fn pad(&mut self) { + // Pad the buffer to 32-bit (4 byte) alignment by adding dummy writes to CSR and LSRR. + match self.index & 3 { + 3 => { + // For a level of 3, we have to pad with 5 bytes to align things. + self.add_write(Register::CSR, &[(self.mode as u8) << 1]); + self.add_write(Register::LSRR, &[0, 0]); + } + 2 => self.add_write(Register::CSR, &[(self.mode as u8) << 1]), + 1 => self.add_write(Register::LSRR, &[0, 0]), + 0 => {} + + _ => unreachable!(), + } + } + /// Get the serialized profile as a slice of 32-bit words. /// /// # Note @@ -604,21 +620,8 @@ impl ProfileSerializer { /// /// # Returns /// A slice of `u32` words representing the serialized profile. - pub fn finalize<'a>(&'a mut self) -> &[u32] { - // Pad the buffer to 32-bit alignment by adding dummy writes to CSR and LSRR. - let padding = 4 - (self.index % 4); - match padding { - 1 => { - // For a pad size of 1, we have to pad with 5 bytes to align things. - self.add_write(Register::CSR, &[(self.mode as u8) << 1]); - self.add_write(Register::LSRR, &[0, 0]); - } - 2 => self.add_write(Register::CSR, &[(self.mode as u8) << 1]), - 3 => self.add_write(Register::LSRR, &[0, 0]), - 4 => {} - - _ => unreachable!(), - } + pub fn finalize<'a>(&'a mut self) -> &'a [u32] { + self.pad(); unsafe { core::slice::from_raw_parts::<'a, u32>( &self.data as *const _ as *const u32,