Skip to content

Commit

Permalink
Rename the ADC constructor to new, make it infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Jan 31, 2024
1 parent f36e2e7 commit 7e80f4e
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 32 deletions.
10 changes: 4 additions & 6 deletions esp-hal/src/analog/adc/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Result<Self, ()> {
) -> Self {
let sensors = unsafe { &*SENS::ptr() };

// Set reading and sampling resolution
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/analog/adc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//!
//! ```no_run
//! let mut adc1_config = AdcConfig::new();
//! let mut adc1 = ADC::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
//! let mut adc1 = ADC::<ADC1>::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);
Expand Down
14 changes: 6 additions & 8 deletions esp-hal/src/analog/adc/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Result<Self, ()> {
) -> 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()
Expand All @@ -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)
}
}
}

Expand Down
10 changes: 4 additions & 6 deletions esp-hal/src/analog/adc/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Result<Self, ()> {
) -> Self {
let sensors = unsafe { &*SENS::ptr() };

// Set attenuation for pins
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion esp32-hal/examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ADC2>::adc(peripherals.ADC2, adc2_config).unwrap();
let mut adc2 = ADC::<ADC2>::new(peripherals.ADC2, adc2_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32c2-hal/examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32c2-hal/examples/adc_cal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> ! {
io.pins.gpio2.into_analog(),
Attenuation::Attenuation11dB,
);
let mut adc1 = ADC::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32c3-hal/examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32c3-hal/examples/adc_cal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() -> ! {
io.pins.gpio2.into_analog(),
Attenuation::Attenuation11dB,
);
let mut adc1 = ADC::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32c6-hal/examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32c6-hal/examples/adc_cal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() -> ! {
io.pins.gpio2.into_analog(),
Attenuation::Attenuation11dB,
);
let mut adc1 = ADC::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32h2-hal/examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32s2-hal/examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32s3-hal/examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion esp32s3-hal/examples/adc_cal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> ! {
io.pins.gpio3.into_analog(),
Attenuation::Attenuation11dB,
);
let mut adc1 = ADC::<ADC1>::adc(peripherals.ADC1, adc1_config).unwrap();
let mut adc1 = ADC::<ADC1>::new(peripherals.ADC1, adc1_config);

let mut delay = Delay::new(&clocks);

Expand Down

0 comments on commit 7e80f4e

Please sign in to comment.