pounder: simplify attenuator spi interface

This commit is contained in:
Robert Jördens 2021-05-25 15:21:09 +00:00
parent 9587088de2
commit f1a58b7811
2 changed files with 10 additions and 30 deletions

View File

@ -30,16 +30,16 @@ pub trait AttenuatorInterface {
// Read all the channels, modify the channel of interest, and write all the channels back. // 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. // This ensures the staging register and the output register are always in sync.
let mut channels = [0_u8; 4]; 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 // 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 // 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. // treats inputs as active-low, so the code is inverted before writing.
channels[channel as usize] = (!attenuation_code) << 2; 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. // 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) 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 // 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 // duplicate write-back to ensure the staging register is always equal to the output
// register. // register.
self.read_all_attenuators(&mut channels)?; self.transfer_attenuators(&mut channels)?;
self.write_all_attenuators(&channels)?; self.transfer_attenuators(&mut channels)?;
// The attenuation code is stored in the upper 6 bits of the register, where each LSB // 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 // 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 reset_attenuators(&mut self) -> Result<(), Error>;
fn latch_attenuators(&mut self, channel: Channel) -> Result<(), Error>; fn latch_attenuator(&mut self, channel: Channel) -> Result<(), Error>;
fn read_all_attenuators(
fn transfer_attenuators(
&mut self, &mut self,
channels: &mut [u8; 4], channels: &mut [u8; 4],
) -> Result<(), Error>; ) -> Result<(), Error>;
fn write_all_attenuators(
&mut self,
channels: &[u8; 4],
) -> Result<(), Error>;
} }

View File

@ -344,7 +344,7 @@ impl AttenuatorInterface for PounderDevices {
/// ///
/// Args: /// Args:
/// * `channel` - The attenuator channel to latch. /// * `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 { let pin = match channel {
Channel::In0 => ATT_LE0_PIN, Channel::In0 => ATT_LE0_PIN,
Channel::In1 => ATT_LE2_PIN, Channel::In1 => ATT_LE2_PIN,
@ -368,7 +368,7 @@ impl AttenuatorInterface for PounderDevices {
/// ///
/// Args: /// Args:
/// * `channels` - A slice to store the channel readings into. /// * `channels` - A slice to store the channel readings into.
fn read_all_attenuators( fn transfer_attenuators(
&mut self, &mut self,
channels: &mut [u8; 4], channels: &mut [u8; 4],
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -378,23 +378,6 @@ impl AttenuatorInterface for PounderDevices {
Ok(()) 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 { impl PowerMeasurementInterface for PounderDevices {