From f1a58b781164bf2764c264feeaa9aaed1143b69c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Tue, 25 May 2021 15:21:09 +0000 Subject: [PATCH] pounder: simplify attenuator spi interface --- src/hardware/pounder/attenuators.rs | 19 ++++++++----------- src/hardware/pounder/mod.rs | 21 ++------------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/src/hardware/pounder/attenuators.rs b/src/hardware/pounder/attenuators.rs index 156da9f..092338e 100644 --- a/src/hardware/pounder/attenuators.rs +++ b/src/hardware/pounder/attenuators.rs @@ -30,16 +30,16 @@ pub trait AttenuatorInterface { // Read all the channels, modify the channel of interest, and write all the channels back. // This ensures the staging register and the output register are always in sync. let mut channels = [0_u8; 4]; - self.read_all_attenuators(&mut channels)?; + self.transfer_attenuators(&mut channels)?; // The lowest 2 bits of the 8-bit shift register on the attenuator are ignored. Shift the // attenuator code into the upper 6 bits of the register value. Note that the attenuator // treats inputs as active-low, so the code is inverted before writing. channels[channel as usize] = (!attenuation_code) << 2; - self.write_all_attenuators(&channels)?; + self.transfer_attenuators(&mut channels)?; // Finally, latch the output of the updated channel to force it into an active state. - self.latch_attenuators(channel)?; + self.latch_attenuator(channel)?; Ok(attenuation_code as f32 / 2.0) } @@ -57,8 +57,8 @@ pub trait AttenuatorInterface { // Reading the data always shifts data out of the staging registers, so we perform a // duplicate write-back to ensure the staging register is always equal to the output // register. - self.read_all_attenuators(&mut channels)?; - self.write_all_attenuators(&channels)?; + self.transfer_attenuators(&mut channels)?; + self.transfer_attenuators(&mut channels)?; // The attenuation code is stored in the upper 6 bits of the register, where each LSB // represents 0.5 dB. The attenuator stores the code as active-low, so inverting the result @@ -74,13 +74,10 @@ pub trait AttenuatorInterface { fn reset_attenuators(&mut self) -> Result<(), Error>; - fn latch_attenuators(&mut self, channel: Channel) -> Result<(), Error>; - fn read_all_attenuators( + fn latch_attenuator(&mut self, channel: Channel) -> Result<(), Error>; + + fn transfer_attenuators( &mut self, channels: &mut [u8; 4], ) -> Result<(), Error>; - fn write_all_attenuators( - &mut self, - channels: &[u8; 4], - ) -> Result<(), Error>; } diff --git a/src/hardware/pounder/mod.rs b/src/hardware/pounder/mod.rs index 32a8e80..dd5ce95 100644 --- a/src/hardware/pounder/mod.rs +++ b/src/hardware/pounder/mod.rs @@ -344,7 +344,7 @@ impl AttenuatorInterface for PounderDevices { /// /// Args: /// * `channel` - The attenuator channel to latch. - fn latch_attenuators(&mut self, channel: Channel) -> Result<(), Error> { + fn latch_attenuator(&mut self, channel: Channel) -> Result<(), Error> { let pin = match channel { Channel::In0 => ATT_LE0_PIN, Channel::In1 => ATT_LE2_PIN, @@ -368,7 +368,7 @@ impl AttenuatorInterface for PounderDevices { /// /// Args: /// * `channels` - A slice to store the channel readings into. - fn read_all_attenuators( + fn transfer_attenuators( &mut self, channels: &mut [u8; 4], ) -> Result<(), Error> { @@ -378,23 +378,6 @@ impl AttenuatorInterface for PounderDevices { Ok(()) } - - /// Write the attenuator shift registers. - /// - /// Args: - /// * `channels` - The data to write into the attenuators. - fn write_all_attenuators( - &mut self, - channels: &[u8; 4], - ) -> Result<(), Error> { - let mut result = [0_u8; 4]; - result.clone_from_slice(channels); - self.attenuator_spi - .transfer(&mut result) - .map_err(|_| Error::Spi)?; - - Ok(()) - } } impl PowerMeasurementInterface for PounderDevices {