pounder: simplify attenuator spi interface

master
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.
// 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>;
}

View File

@ -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 {