diff --git a/micro-rdk/src/esp32/analog.rs b/micro-rdk/src/esp32/analog.rs index de3516586..cbbb11cd7 100755 --- a/micro-rdk/src/esp32/analog.rs +++ b/micro-rdk/src/esp32/analog.rs @@ -1,34 +1,23 @@ #![allow(dead_code)] use crate::common::analog::{AnalogError, AnalogReader, AnalogResolution}; -use crate::esp32::esp_idf_svc::hal::adc::{AdcChannelDriver, AdcDriver}; +use crate::esp32::esp_idf_svc::hal::{ + adc::oneshot::{AdcChannelDriver, AdcDriver}, + gpio::ADCPin, +}; +use std::borrow::Borrow; -// TODO(RSDK-10188): Update to ESP-IDF 5 ADC API -use crate::esp32::esp_idf_svc::hal::gpio::ADCPin; -use std::sync::{Arc, Mutex}; - -pub struct Esp32AnalogReader<'a, const A: u32, T: ADCPin> { - channel: AdcChannelDriver<'a, A, T>, - driver: Arc>>, +pub struct Esp32AnalogReader<'a, T: ADCPin, M: Borrow>> { + channel: AdcChannelDriver<'a, T, M>, name: String, } -impl<'a, const A: u32, T: ADCPin> Esp32AnalogReader<'a, A, T> { - pub fn new( - name: String, - channel: AdcChannelDriver<'a, A, T>, - driver: Arc>>, - ) -> Self { - Self { - name, - channel, - driver, - } +impl<'a, T: ADCPin, M: Borrow>> Esp32AnalogReader<'a, T, M> { + pub fn new(name: String, channel: AdcChannelDriver<'a, T, M>) -> Self { + Self { name, channel } } fn inner_read(&mut self) -> Result { - self.driver - .lock() - .unwrap() - .read(&mut self.channel) + self.channel + .read() .map_err(|e| AnalogError::AnalogReadError(e.code())) } fn inner_name(&self) -> String { @@ -36,7 +25,9 @@ impl<'a, const A: u32, T: ADCPin> Esp32AnalogReader<'a, A, T> { } } -impl AnalogReader for Esp32AnalogReader<'_, A, T> { +impl<'a, T: ADCPin, M: Borrow>> AnalogReader + for Esp32AnalogReader<'a, T, M> +{ type Error = AnalogError; fn read(&mut self) -> Result { self.inner_read() diff --git a/micro-rdk/src/esp32/board.rs b/micro-rdk/src/esp32/board.rs index cc8267dd8..0a896c031 100755 --- a/micro-rdk/src/esp32/board.rs +++ b/micro-rdk/src/esp32/board.rs @@ -34,8 +34,12 @@ use super::analog::Esp32AnalogReader; // TODO(RSDK-10188): Update to ESP-IDF ADC API #[cfg(esp32)] use crate::esp32::esp_idf_svc::hal::adc::{ - attenuation::adc_atten_t_ADC_ATTEN_DB_11 as Atten11dB, config::Config, AdcChannelDriver, - AdcDriver, ADC1, + attenuation::DB_11, + oneshot::{ + config::{AdcChannelConfig, Calibration}, + AdcChannelDriver, AdcDriver, + }, + ADC1, }; use crate::esp32::esp_idf_svc::hal::gpio::InterruptType; @@ -81,23 +85,24 @@ impl EspBoard { #[cfg(esp32)] let analogs = if let Ok(analogs) = cfg.get_attribute::>("analogs") { + let adc1 = Arc::new(AdcDriver::new(unsafe { ADC1::new() })?); let analogs: Result>, BoardError> = analogs .iter() .map(|v| { - let adc1 = Arc::new(Mutex::new(AdcDriver::new( - unsafe { ADC1::new() }, - &Config::new().calibration(true), - )?)); + let config = AdcChannelConfig { + attenuation: DB_11, + calibration: Calibration::Line, + ..Default::default() + }; let chan: Result, BoardError> = match v.pin { 32 => { let p: AnalogReaderType = Arc::new(Mutex::new(Esp32AnalogReader::new( v.name.to_string(), - AdcChannelDriver::::new(unsafe { + AdcChannelDriver::new(adc1.clone(), unsafe { crate::esp32::esp_idf_svc::hal::gpio::Gpio32::new() - }) - .map_err(BoardError::EspError)?, - adc1, + }, &config) + .map_err(BoardError::EspError)? ))); Ok(p) } @@ -105,11 +110,10 @@ impl EspBoard { let p: AnalogReaderType = Arc::new(Mutex::new(Esp32AnalogReader::new( v.name.to_string(), - AdcChannelDriver::::new(unsafe { + AdcChannelDriver::new(adc1.clone(), unsafe { crate::esp32::esp_idf_svc::hal::gpio::Gpio33::new() - }) - .map_err(BoardError::EspError)?, - adc1, + }, &config) + .map_err(BoardError::EspError)? ))); Ok(p) } @@ -117,11 +121,10 @@ impl EspBoard { let p: AnalogReaderType = Arc::new(Mutex::new(Esp32AnalogReader::new( v.name.to_string(), - AdcChannelDriver::::new(unsafe { + AdcChannelDriver::new(adc1.clone(), unsafe { crate::esp32::esp_idf_svc::hal::gpio::Gpio34::new() - }) - .map_err(BoardError::EspError)?, - adc1, + }, &config) + .map_err(BoardError::EspError)? ))); Ok(p) } @@ -129,11 +132,10 @@ impl EspBoard { let p: AnalogReaderType = Arc::new(Mutex::new(Esp32AnalogReader::new( v.name.to_string(), - AdcChannelDriver::::new(unsafe { + AdcChannelDriver::new(adc1.clone(), unsafe { crate::esp32::esp_idf_svc::hal::gpio::Gpio35::new() - }) - .map_err(BoardError::EspError)?, - adc1, + }, &config) + .map_err(BoardError::EspError)? ))); Ok(p) } @@ -141,11 +143,10 @@ impl EspBoard { let p: AnalogReaderType = Arc::new(Mutex::new(Esp32AnalogReader::new( v.name.to_string(), - AdcChannelDriver::::new(unsafe { + AdcChannelDriver::new(adc1.clone(), unsafe { crate::esp32::esp_idf_svc::hal::gpio::Gpio36::new() - }) - .map_err(BoardError::EspError)?, - adc1, + }, &config) + .map_err(BoardError::EspError)? ))); Ok(p) } @@ -153,11 +154,10 @@ impl EspBoard { let p: AnalogReaderType = Arc::new(Mutex::new(Esp32AnalogReader::new( v.name.to_string(), - AdcChannelDriver::::new(unsafe { + AdcChannelDriver::new(adc1.clone(), unsafe { crate::esp32::esp_idf_svc::hal::gpio::Gpio37::new() - }) - .map_err(BoardError::EspError)?, - adc1, + }, &config) + .map_err(BoardError::EspError)? ))); Ok(p) } @@ -165,11 +165,10 @@ impl EspBoard { let p: AnalogReaderType = Arc::new(Mutex::new(Esp32AnalogReader::new( v.name.to_string(), - AdcChannelDriver::::new(unsafe { + AdcChannelDriver::new(adc1.clone(), unsafe { crate::esp32::esp_idf_svc::hal::gpio::Gpio38::new() - }) - .map_err(BoardError::EspError)?, - adc1, + }, &config) + .map_err(BoardError::EspError)? ))); Ok(p) } @@ -177,11 +176,10 @@ impl EspBoard { let p: AnalogReaderType = Arc::new(Mutex::new(Esp32AnalogReader::new( v.name.to_string(), - AdcChannelDriver::::new(unsafe { + AdcChannelDriver::new(adc1.clone(), unsafe { crate::esp32::esp_idf_svc::hal::gpio::Gpio39::new() - }) - .map_err(BoardError::EspError)?, - adc1, + }, &config) + .map_err(BoardError::EspError)? ))); Ok(p) }