ad7172: implement crc checksumming
This commit is contained in:
parent
baab9b2d3f
commit
3bf1010969
@ -132,9 +132,8 @@ impl<SPI> From<SPI> for AdcError<SPI> {
|
|||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum ChecksumMode {
|
pub enum ChecksumMode {
|
||||||
Off = 0b00,
|
Off = 0b00,
|
||||||
|
/// Seems much less reliable than `Crc`
|
||||||
Xor = 0b01,
|
Xor = 0b01,
|
||||||
/// Not implemented
|
|
||||||
#[allow(unused)]
|
|
||||||
Crc = 0b10,
|
Crc = 0b10,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +151,15 @@ impl Checksum {
|
|||||||
ChecksumMode::Off => {},
|
ChecksumMode::Off => {},
|
||||||
ChecksumMode::Xor => self.state ^= input,
|
ChecksumMode::Xor => self.state ^= input,
|
||||||
ChecksumMode::Crc => {
|
ChecksumMode::Crc => {
|
||||||
// TODO
|
for i in 0..8 {
|
||||||
|
let input_mask = 0x80 >> i;
|
||||||
|
self.state = (self.state << 1) ^
|
||||||
|
if ((self.state & 0x80) != 0) != ((input & input_mask) != 0) {
|
||||||
|
0x07 /* x8 + x2 + x + 1 */
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -247,17 +254,17 @@ impl<SPI: Transfer<u8>, NSS: OutputPin> Adc<SPI, NSS> {
|
|||||||
|
|
||||||
fn write_reg<R: Register>(&mut self, reg: &R, reg_data: &mut R::Data) -> Result<(), AdcError<SPI::Error>> {
|
fn write_reg<R: Register>(&mut self, reg: &R, reg_data: &mut R::Data) -> Result<(), AdcError<SPI::Error>> {
|
||||||
let address = reg.address();
|
let address = reg.address();
|
||||||
let checksum_out = match self.checksum_mode {
|
let mut checksum = Checksum::new(match self.checksum_mode {
|
||||||
ChecksumMode::Off => None,
|
ChecksumMode::Off => ChecksumMode::Off,
|
||||||
ChecksumMode::Xor => {
|
// write checksums are always crc
|
||||||
let mut xor = address;
|
ChecksumMode::Xor => ChecksumMode::Crc,
|
||||||
for b in reg_data.as_mut() {
|
ChecksumMode::Crc => ChecksumMode::Crc,
|
||||||
xor ^= *b;
|
});
|
||||||
}
|
checksum.feed(address);
|
||||||
Some(xor)
|
for &mut b in reg_data.as_mut() {
|
||||||
}
|
checksum.feed(b);
|
||||||
ChecksumMode::Crc => panic!("Not implemented"),
|
}
|
||||||
};
|
let checksum_out = checksum.result();
|
||||||
self.transfer(address, reg_data.as_mut(), checksum_out)?;
|
self.transfer(address, reg_data.as_mut(), checksum_out)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -116,8 +116,8 @@ fn main() -> ! {
|
|||||||
writeln!(stdout, "Corrupt ADC id: {:04X}", id).unwrap(),
|
writeln!(stdout, "Corrupt ADC id: {:04X}", id).unwrap(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
writeln!(stdout, "AD7172: setting checksum mode to XOR").unwrap();
|
writeln!(stdout, "AD7172: setting checksum mode").unwrap();
|
||||||
adc.set_checksum_mode(ad7172::ChecksumMode::Xor).unwrap();
|
adc.set_checksum_mode(ad7172::ChecksumMode::Crc).unwrap();
|
||||||
loop {
|
loop {
|
||||||
let r = adc.identify();
|
let r = adc.identify();
|
||||||
match r {
|
match r {
|
||||||
|
Loading…
Reference in New Issue
Block a user