From 7e80f4ebd65113c3b99504cd9ba0639f4259b110 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Wed, 31 Jan 2024 11:35:12 -0800 Subject: [PATCH] Rename the ADC constructor to `new`, make it infallible --- esp-hal/src/analog/adc/esp32.rs | 10 ++++------ esp-hal/src/analog/adc/mod.rs | 2 +- esp-hal/src/analog/adc/riscv.rs | 14 ++++++-------- esp-hal/src/analog/adc/xtensa.rs | 10 ++++------ esp32-hal/examples/adc.rs | 2 +- esp32c2-hal/examples/adc.rs | 2 +- esp32c2-hal/examples/adc_cal.rs | 2 +- esp32c3-hal/examples/adc.rs | 2 +- esp32c3-hal/examples/adc_cal.rs | 2 +- esp32c6-hal/examples/adc.rs | 2 +- esp32c6-hal/examples/adc_cal.rs | 2 +- esp32h2-hal/examples/adc.rs | 2 +- esp32s2-hal/examples/adc.rs | 2 +- esp32s3-hal/examples/adc.rs | 2 +- esp32s3-hal/examples/adc_cal.rs | 2 +- 15 files changed, 26 insertions(+), 32 deletions(-) diff --git a/esp-hal/src/analog/adc/esp32.rs b/esp-hal/src/analog/adc/esp32.rs index d05823bf87f..db33e1b0aac 100644 --- a/esp-hal/src/analog/adc/esp32.rs +++ b/esp-hal/src/analog/adc/esp32.rs @@ -256,10 +256,10 @@ impl<'d, ADCI> ADC<'d, ADCI> where ADCI: RegisterAccess, { - pub fn adc( + pub fn new( adc_instance: impl crate::peripheral::Peripheral

+ 'd, config: AdcConfig, - ) -> Result { + ) -> Self { let sensors = unsafe { &*SENS::ptr() }; // Set reading and sampling resolution @@ -322,13 +322,11 @@ where .sar_read_ctrl2() .modify(|_, w| w.sar2_data_inv().set_bit()); - let adc = ADC { + ADC { _adc: adc_instance.into_ref(), attenuations: config.attenuations, active_channel: None, - }; - - Ok(adc) + } } } diff --git a/esp-hal/src/analog/adc/mod.rs b/esp-hal/src/analog/adc/mod.rs index 061f272a22b..2af7496b6e6 100644 --- a/esp-hal/src/analog/adc/mod.rs +++ b/esp-hal/src/analog/adc/mod.rs @@ -8,7 +8,7 @@ //! //! ```no_run //! let mut adc1_config = AdcConfig::new(); -//! let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); +//! let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); //! let mut pin = adc1_config.enable_pin(io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB); //! //! let mut delay = Delay::new(&clocks); diff --git a/esp-hal/src/analog/adc/riscv.rs b/esp-hal/src/analog/adc/riscv.rs index 7d5ba1aa0e5..424dc0ad7e6 100644 --- a/esp-hal/src/analog/adc/riscv.rs +++ b/esp-hal/src/analog/adc/riscv.rs @@ -490,14 +490,13 @@ impl<'d, ADCI> ADC<'d, ADCI> where ADCI: RegisterAccess + 'd, { - pub fn adc( + pub fn new( adc_instance: impl crate::peripheral::Peripheral

+ 'd, config: AdcConfig, - ) -> Result { + ) -> Self { PeripheralClockControl::enable(Peripheral::ApbSarAdc); - let sar_adc = unsafe { &*APB_SARADC::PTR }; - sar_adc.ctrl().modify(|_, w| unsafe { + unsafe { &*APB_SARADC::PTR }.ctrl().modify(|_, w| unsafe { w.saradc_start_force() .set_bit() .saradc_start() @@ -507,13 +506,12 @@ where .saradc_xpd_sar_force() .bits(0b11) }); - let adc = ADC { + + ADC { _adc: adc_instance.into_ref(), attenuations: config.attenuations, active_channel: None, - }; - - Ok(adc) + } } } diff --git a/esp-hal/src/analog/adc/xtensa.rs b/esp-hal/src/analog/adc/xtensa.rs index f279813f71d..e1614b74d67 100644 --- a/esp-hal/src/analog/adc/xtensa.rs +++ b/esp-hal/src/analog/adc/xtensa.rs @@ -524,10 +524,10 @@ impl<'d, ADCI> ADC<'d, ADCI> where ADCI: RegisterAccess, { - pub fn adc( + pub fn new( adc_instance: impl crate::peripheral::Peripheral

+ 'd, config: AdcConfig, - ) -> Result { + ) -> Self { let sensors = unsafe { &*SENS::ptr() }; // Set attenuation for pins @@ -592,13 +592,11 @@ where .sar_amp_ctrl2() .modify(|_, w| unsafe { w.sar_amp_wait3().bits(1) }); - let adc = ADC { + ADC { _adc: adc_instance.into_ref(), attenuations: config.attenuations, active_channel: None, - }; - - Ok(adc) + } } } diff --git a/esp32-hal/examples/adc.rs b/esp32-hal/examples/adc.rs index 088de5a7148..b1e83647a77 100644 --- a/esp32-hal/examples/adc.rs +++ b/esp32-hal/examples/adc.rs @@ -28,7 +28,7 @@ fn main() -> ! { let mut adc2_config = AdcConfig::new(); let mut pin25 = adc2_config.enable_pin(io.pins.gpio25.into_analog(), Attenuation::Attenuation11dB); - let mut adc2 = ADC::::adc(peripherals.ADC2, adc2_config).unwrap(); + let mut adc2 = ADC::::new(peripherals.ADC2, adc2_config); let mut delay = Delay::new(&clocks); diff --git a/esp32c2-hal/examples/adc.rs b/esp32c2-hal/examples/adc.rs index ee2af6fe11a..22dc195769f 100644 --- a/esp32c2-hal/examples/adc.rs +++ b/esp32c2-hal/examples/adc.rs @@ -27,7 +27,7 @@ fn main() -> ! { // Create ADC instances let mut adc1_config = AdcConfig::new(); let mut pin = adc1_config.enable_pin(io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32c2-hal/examples/adc_cal.rs b/esp32c2-hal/examples/adc_cal.rs index 05c2b584f7e..64230a6b870 100644 --- a/esp32c2-hal/examples/adc_cal.rs +++ b/esp32c2-hal/examples/adc_cal.rs @@ -39,7 +39,7 @@ fn main() -> ! { io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB, ); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32c3-hal/examples/adc.rs b/esp32c3-hal/examples/adc.rs index e004ff4dc02..a0d0622282e 100644 --- a/esp32c3-hal/examples/adc.rs +++ b/esp32c3-hal/examples/adc.rs @@ -27,7 +27,7 @@ fn main() -> ! { // Create ADC instances let mut adc1_config = AdcConfig::new(); let mut pin = adc1_config.enable_pin(io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32c3-hal/examples/adc_cal.rs b/esp32c3-hal/examples/adc_cal.rs index e83e128a44f..7ec564c3459 100644 --- a/esp32c3-hal/examples/adc_cal.rs +++ b/esp32c3-hal/examples/adc_cal.rs @@ -40,7 +40,7 @@ fn main() -> ! { io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB, ); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32c6-hal/examples/adc.rs b/esp32c6-hal/examples/adc.rs index 1c5c2229aff..7c48620fcf2 100644 --- a/esp32c6-hal/examples/adc.rs +++ b/esp32c6-hal/examples/adc.rs @@ -27,7 +27,7 @@ fn main() -> ! { // Create ADC instances let mut adc1_config = AdcConfig::new(); let mut pin = adc1_config.enable_pin(io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32c6-hal/examples/adc_cal.rs b/esp32c6-hal/examples/adc_cal.rs index 4d491a3b1ec..410f407558a 100644 --- a/esp32c6-hal/examples/adc_cal.rs +++ b/esp32c6-hal/examples/adc_cal.rs @@ -40,7 +40,7 @@ fn main() -> ! { io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB, ); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32h2-hal/examples/adc.rs b/esp32h2-hal/examples/adc.rs index 4c1a4e41089..62b1f4d004a 100644 --- a/esp32h2-hal/examples/adc.rs +++ b/esp32h2-hal/examples/adc.rs @@ -27,7 +27,7 @@ fn main() -> ! { // Create ADC instances let mut adc1_config = AdcConfig::new(); let mut pin = adc1_config.enable_pin(io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32s2-hal/examples/adc.rs b/esp32s2-hal/examples/adc.rs index cf960acb568..4baec96e660 100644 --- a/esp32s2-hal/examples/adc.rs +++ b/esp32s2-hal/examples/adc.rs @@ -28,7 +28,7 @@ fn main() -> ! { let mut adc1_config = AdcConfig::new(); let mut pin3 = adc1_config.enable_pin(io.pins.gpio3.into_analog(), Attenuation::Attenuation11dB); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32s3-hal/examples/adc.rs b/esp32s3-hal/examples/adc.rs index 8423160b751..27d5ec291be 100644 --- a/esp32s3-hal/examples/adc.rs +++ b/esp32s3-hal/examples/adc.rs @@ -28,7 +28,7 @@ fn main() -> ! { let mut adc1_config = AdcConfig::new(); let mut pin3 = adc1_config.enable_pin(io.pins.gpio3.into_analog(), Attenuation::Attenuation11dB); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks); diff --git a/esp32s3-hal/examples/adc_cal.rs b/esp32s3-hal/examples/adc_cal.rs index 6b9ca9a64ca..4e865f46468 100644 --- a/esp32s3-hal/examples/adc_cal.rs +++ b/esp32s3-hal/examples/adc_cal.rs @@ -39,7 +39,7 @@ fn main() -> ! { io.pins.gpio3.into_analog(), Attenuation::Attenuation11dB, ); - let mut adc1 = ADC::::adc(peripherals.ADC1, adc1_config).unwrap(); + let mut adc1 = ADC::::new(peripherals.ADC1, adc1_config); let mut delay = Delay::new(&clocks);