Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename the ADC constructor to new, make it infallible #1133

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `ADC` and `DAC` drivers now take virtual peripherals in their constructors, instead of splitting `APB_SARADC`/`SENS` (#1100)
- The `DAC` driver's constructor is now `new` instead of `dac`, to be more consistent with other APIs (#1100)
- The DMA peripheral is now called `Dma` for devices with both PDMA and GDMA controllers (#1125)
- The `ADC` driver's constructor is now `new` instead of `adc`, to be more consistent with other APIs (#1133)

## [0.15.0] - 2024-01-19

Expand Down
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
Loading