From 194a98ff6b40fde4e7b91608c810b4d42837bc9a Mon Sep 17 00:00:00 2001 From: "K." Date: Mon, 21 Oct 2024 03:00:21 +0500 Subject: [PATCH] Add example for sigma-delta modulation Issue #2370 --- examples/src/bin/sdm.rs | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/src/bin/sdm.rs diff --git a/examples/src/bin/sdm.rs b/examples/src/bin/sdm.rs new file mode 100644 index 00000000000..c79ca8baa80 --- /dev/null +++ b/examples/src/bin/sdm.rs @@ -0,0 +1,47 @@ +//! Connect an oscilloscope to an IO pin and see the modulation sine wave. +//! +//! Also you may connect low-pass filter to SDM output. +//! +//! The following wiring is assumed for ESP32: +//! - SDM pin => GPIO32 +//! The following wiring is assumed for ESP32S2/S3: +//! - SDM pin => GPIO3 +//! The following wiring is assumed for others: +//! - SDM pin => GPIO2 + +//% CHIPS: esp32 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3 + +#![no_std] +#![no_main] + +use esp_backtrace as _; +use esp_hal::{delay::Delay, gpio::Io, prelude::*, sdm::Sdm}; +use esp_println::println; + +#[entry] +fn main() -> ! { + let peripherals = esp_hal::init(esp_hal::Config::default()); + + let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let modulation_pin = io.pins.gpio32; + } else if #[cfg(any(feature = "esp32s2", feature = "esp32s3"))] { + let modulation_pin = io.pins.gpio3; + } else { + let modulation_pin = io.pins.gpio2; + } + } + + let mut sdm = Sdm::new(peripherals.GPIO_SD); + let mut sdm_ch = sdm.enable_pin(modulation_pin, 100.kHz()).unwrap(); + + let delay = Delay::new(); + + println!("Sigma-delta modulation is on"); + + loop { + sdm_ch.set_pulse_density(0); + delay.delay_millis(1500); + } +}