Skip to content
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 @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add initial support for LEDC in ESP32-H2 (#560)
- Add initial support for ASSIST_DEBUG in ESP32-H2 (#566)
- Add all `SPI` examples for the ESP32-H2 (#549)
- Add initial support for ADC in ESP32-H2 (#564)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use fugit::{
};
pub use nb;

#[cfg(any(esp32c2, esp32c3, esp32c6))]
#[cfg(any(esp32c2, esp32c3, esp32c6, esp32h2))]
pub use crate::analog::SarAdcExt as _esp_hal_analog_SarAdcExt;
#[cfg(sens)]
pub use crate::analog::SensExt as _esp_hal_analog_SensExt;
Expand Down
71 changes: 71 additions & 0 deletions esp32h2-hal/examples/adc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! Connect a potentiometer to PIN2 and see the read values change when
//! rotating the shaft. Alternatively you could also connect the PIN to GND or
//! 3V3 to see the maximum and minimum raw values read.

#![no_std]
#![no_main]

use esp32h2_hal::{
adc::{AdcConfig, Attenuation, ADC, ADC1},
clock::ClockControl,
gpio::IO,
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
Delay,
Rtc,
};
use esp_backtrace as _;
use esp_println::println;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let mut system = peripherals.PCR.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

// Disable the watchdog timers. For the ESP32-H2, this includes the Super WDT,
// and the TIMG WDTs.
let mut rtc = Rtc::new(peripherals.LP_CLKRST);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;

rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

// Create ADC instances
let analog = peripherals.APB_SARADC.split();

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(
&mut system.peripheral_clock_control,
analog.adc1,
adc1_config,
)
.unwrap();

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

loop {
let pin_value: u16 = nb::block!(adc1.read(&mut pin)).unwrap();
println!("PIN2 ADC reading = {}", pin_value);
delay.delay_ms(1500u32);
}
}