Skip to content

Commit f063946

Browse files
committed
Add example for sigma-delta modulation
Issue esp-rs#2370
1 parent 0ce7ff1 commit f063946

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

examples/src/bin/sdm.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! Connect an oscilloscope to an IO pin and see the modulation sine wave.
2+
//!
3+
//! Also you may connect low-pass filter to SDM output.
4+
//!
5+
//! The following wiring is assumed for ESP32:
6+
//! - SDM pin => GPIO32
7+
//! The following wiring is assumed for ESP32S2/S3:
8+
//! - SDM pin => GPIO3
9+
//! The following wiring is assumed for others:
10+
//! - SDM pin => GPIO2
11+
12+
//% CHIPS: esp32 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
13+
14+
#![no_std]
15+
#![no_main]
16+
17+
use esp_backtrace as _;
18+
use esp_hal::{
19+
sdm::Sdm,
20+
delay::Delay,
21+
gpio::Io,
22+
prelude::*,
23+
};
24+
use esp_println::println;
25+
26+
#[entry]
27+
fn main() -> ! {
28+
let peripherals = esp_hal::init(esp_hal::Config::default());
29+
30+
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
31+
cfg_if::cfg_if! {
32+
if #[cfg(feature = "esp32")] {
33+
let modulation_pin = io.pins.gpio32;
34+
} else if #[cfg(any(feature = "esp32s2", feature = "esp32s3"))] {
35+
let modulation_pin = io.pins.gpio3;
36+
} else {
37+
let modulation_pin = io.pins.gpio2;
38+
}
39+
}
40+
41+
let mut sdm = Sdm::new(peripherals.GPIO_SD);
42+
let mut sdm_ch = sdm.enable_pin(modulation_pin, 100.kHz()).unwrap();
43+
44+
let delay = Delay::new();
45+
46+
println!("Sigma-delta modulation is on");
47+
48+
loop {
49+
sdm_ch.set_pulse_density(0);
50+
delay.delay_millis(1500);
51+
}
52+
}

0 commit comments

Comments
 (0)