From 4c3c8498e997b36438264e08839a7c17a9511d72 Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 20 Mar 2020 00:45:40 +0100 Subject: [PATCH] ad7172: retry corrupt transfers --- src/ad7172/adc.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/ad7172/adc.rs b/src/ad7172/adc.rs index edd308b..8b92ae6 100644 --- a/src/ad7172/adc.rs +++ b/src/ad7172/adc.rs @@ -1,7 +1,7 @@ use core::fmt; use embedded_hal::digital::v2::OutputPin; use embedded_hal::blocking::spi::Transfer; -use log::info; +use log::{info, warn}; use super::checksum::{ChecksumMode, Checksum}; use super::AdcError; use super::{ @@ -145,12 +145,16 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc checksum.feed(&[address]); let checksum_out = checksum.result(); - let checksum_in = self.transfer(address, reg_data.as_mut(), checksum_out)?; + loop { + let checksum_in = self.transfer(address, reg_data.as_mut(), checksum_out)?; - checksum.feed(®_data); - let checksum_expected = checksum.result(); - if checksum_expected != checksum_in { - return Err(AdcError::ChecksumMismatch(checksum_expected, checksum_in)); + checksum.feed(®_data); + let checksum_expected = checksum.result(); + if checksum_expected == checksum_in { + break; + } + // Retry + warn!("read_reg checksum error, retrying"); } Ok(reg_data) } @@ -166,7 +170,13 @@ impl, NSS: OutputPin, E: fmt::Debug> Adc checksum.feed(&[address]); checksum.feed(®_data); let checksum_out = checksum.result(); - self.transfer(address, reg_data.as_mut(), checksum_out)?; + loop { + let checksum_in = self.transfer(address, reg_data.as_mut(), checksum_out)?; + if checksum_in.unwrap_or(0) == 0 { + break; + } + warn!("write_reg: checksum={:02X}, retrying", checksum_in.unwrap_or(0)); + } Ok(()) }