From 055c92c6845472dd851eaf1979f78d73a2e7f18f Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 17 Nov 2020 13:09:45 +0100 Subject: [PATCH] Adding WIP refactor --- ad9959/src/lib.rs | 61 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 21 ++++++-------- src/pounder/dds_output.rs | 1 + 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/ad9959/src/lib.rs b/ad9959/src/lib.rs index 99f1072..b1a4001 100644 --- a/ad9959/src/lib.rs +++ b/ad9959/src/lib.rs @@ -549,3 +549,64 @@ impl Ad9959 { Ok(serialized) } } + +struct ProfileSerializer { + data: [u8; 16], + index: usize, +} + +impl ProfileSerializer { + fn new() -> Self { + Self { + data: [0; 16], + index: 0, + } + } + + fn add_write(&mut self, register: Register, value: &[u8]) { + let data = &mut self.data[self.index..]; + assert!(value.len() + 1 <= data.len()); + + data[0] = register as u8; + data[1..][..value.len()].copy_from_slice(value); + self.index += value.len() + 1; + } + + fn finalize(self) -> [u32; 4] { + assert!(self.index == self.data.len()); + unsafe { core::mem::transmute(self.data) } + } +} + +pub fn serialize_profile(channel: Channel, ftw: u32, pow: u16, acr: Option) -> [u32; 4] { + let mut serializer = ProfileSerializer::new(); + + let csr: u8 = *0x00_u8 + .set_bits(1..=2, Mode::FourBitSerial as u8) + .set_bit(4 + channel as usize, true); + + let acr: [u8; 3] = { + let mut data = [0u8; 3]; + if acr.is_some() { + data[2].set_bit(0, acr.is_some()); + data[0..2].copy_from_slice(&acr.unwrap().to_be_bytes()); + } + + data + }; + + // 4 bytes + serializer.add_write(Register::CSR, &[csr]); + serializer.add_write(Register::CSR, &[csr]); + + // 5 bytes + serializer.add_write(Register::CFTW0, &ftw.to_be_bytes()); + + // 3 bytes + serializer.add_write(Register::CPOW0, &pow.to_be_bytes()); + + // 4 bytes + serializer.add_write(Register::ACR, &acr); + + serializer.finalize() +} diff --git a/src/main.rs b/src/main.rs index 704a03e..b0666fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -821,18 +821,15 @@ const APP: () = { y1 as i16 as u16 ^ 0x8000 }; - let dds_output = &mut c.resources.dds_output; - if let Some(pounder) = c.resources.pounder { - let profile = pounder - .ad9959 - .serialize_profile( - pounder::Channel::Out0.into(), - 100_000_000_f32, - 0.0_f32, - 1.0_f32, - ) - .unwrap(); - dds_output.lock(|dds_output| { + if c.resources.pounder.is_some() { + let profile = ad9959::serialize_profile( + pounder::Channel::Out0.into(), + u32::MAX / 4, + 0, + None, + ); + + c.resources.dds_output.lock(|dds_output| { dds_output.push(profile); }); } diff --git a/src/pounder/dds_output.rs b/src/pounder/dds_output.rs index 61bd590..b847d28 100644 --- a/src/pounder/dds_output.rs +++ b/src/pounder/dds_output.rs @@ -14,6 +14,7 @@ impl DdsOutput { ) -> Self { timer.pause(); timer.reset_counter(); + timer.clear_uif_bit(); timer.listen(hal::timer::Event::TimeOut); Self {