From 0e5d582363ae7d7f335269708f729064f40d199c Mon Sep 17 00:00:00 2001 From: Jonathan Nilsson Date: Thu, 9 Sep 2021 21:08:46 +0200 Subject: [PATCH] Fix adc giving wrong values on sequential reads of multiple channels --- rp2040-hal/src/adc.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/rp2040-hal/src/adc.rs b/rp2040-hal/src/adc.rs index 477ffae..eac89fd 100644 --- a/rp2040-hal/src/adc.rs +++ b/rp2040-hal/src/adc.rs @@ -138,15 +138,17 @@ where self.device.cs.modify(|_, w| w.ts_en().set_bit()) } - if self.device.cs.read().ready().bit_is_set() { - self.device - .cs - .modify(|_, w| unsafe { w.ainsel().bits(chan).start_once().set_bit() }); - }; - if !self.device.cs.read().ready().bit_is_set() { - // Can't return WouldBlock here since that would take to long and next call conversion would be over + while !self.device.cs.read().ready().bit_is_set() { cortex_m::asm::nop(); - }; + } + + self.device + .cs + .modify(|_, w| unsafe { w.ainsel().bits(chan).start_once().set_bit() }); + + while !self.device.cs.read().ready().bit_is_set() { + cortex_m::asm::nop(); + } Ok(self.device.result.read().result().bits().into()) }