Skip to content

Commit ba31e63

Browse files
committed
Add example for sigma-delta modulation
Issue esp-rs#2370
1 parent db8bab0 commit ba31e63

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

examples/src/bin/sdm.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 esp32c2 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+
// Create SDM instances
42+
//let mut sdm_config = SdmConfig::default();
43+
//let mut sdm_pin = sdm_config.enable_pin(modulation_pin, 1.MHz());
44+
//let mut sdm = Sdm::new(peripherals.GPIO_SD, sdm_config);
45+
46+
let mut sdm = Sdm::new(peripherals.GPIO_SD);
47+
let mut sdm_ch = sdm.enable_pin(modulation_pin, 100.kHz()).unwrap();
48+
49+
let delay = Delay::new();
50+
51+
loop {
52+
sdm_ch.set_pulse_density(0);
53+
delay.delay_millis(1500);
54+
}
55+
}

0 commit comments

Comments
 (0)