Skip to content

Commit 8145971

Browse files
committed
adc: iio: hardware: communicate errors from iio thread to other threads
... via the timestamp value. Previously the consumers of iio adc values were not notified about a iio thread that exited unexpectedly, meaning they only found out via e.g. timeouts (or not at all). Give the iio thread a way to tell the other threads about issues. Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
1 parent 04d3da7 commit 8145971

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/adc/iio/hardware.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ const CHANNELS_PWR: &[ChannelDesc] = &[
102102
},
103103
];
104104

105+
const TIMESTAMP_ERROR: u64 = u64::MAX;
106+
105107
#[derive(Clone, Copy)]
106108
struct Calibration {
107109
scale: f32,
@@ -193,7 +195,11 @@ impl CalibratedChannel {
193195

194196
let ts_after = self.iio_thread.timestamp.load(Ordering::Acquire);
195197

196-
if (ts_before == ts_after) && (ts_before != 0) {
198+
if ts_before == TIMESTAMP_ERROR || ts_after == TIMESTAMP_ERROR {
199+
panic!("Failed to read from ADC");
200+
}
201+
202+
if ts_before == ts_after {
197203
let ts = self
198204
.iio_thread
199205
.ref_instant
@@ -315,7 +321,7 @@ impl IioThread {
315321
Ok((stm32_channels, stm32_buf, pwr_channels)) => {
316322
let thread = Arc::new(Self {
317323
ref_instant: Instant::now(),
318-
timestamp: AtomicU64::new(0),
324+
timestamp: AtomicU64::new(TIMESTAMP_ERROR),
319325
values: [
320326
AtomicU16::new(0),
321327
AtomicU16::new(0),
@@ -348,6 +354,8 @@ impl IioThread {
348354
// Use the buffer interface to get STM32 ADC values at a high
349355
// sampling rate to perform averaging in software.
350356
if let Err(e) = stm32_buf.refill() {
357+
thread.timestamp.store(TIMESTAMP_ERROR, Ordering::Relaxed);
358+
351359
error!("Failed to refill STM32 ADC buffer: {}", e);
352360

353361
// If the ADC has not yet produced any values we still have the

0 commit comments

Comments
 (0)