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

Fix RMT examples #11

Merged
merged 2 commits into from
Feb 26, 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
49 changes: 36 additions & 13 deletions examples/src/bin/embassy_rmt_rx.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Demonstrates decoding pulse sequences with RMT
//!
//! This uses the boot button as input - press the button a couple of
//! times to generate a pulse sequence and then wait for the idle timeout to see
//! the recorded pulse sequence
//! Connect GPIO5 to GPIO4

//% CHIPS: esp32 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
//% FEATURES: async embassy embassy-executor-thread embassy-time-timg0
Expand All @@ -12,34 +9,57 @@
#![feature(type_alias_impl_trait)]

use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
embassy::{self},
gpio::{Gpio5, Output, PushPull},
peripherals::Peripherals,
prelude::*,
rmt::{asynch::RxChannelAsync, PulseCode, RxChannelConfig, RxChannelCreator},
timer::TimerGroup,
Rmt,
IO,
};
use esp_println::{print, println};

const WIDTH: usize = 80;

#[cfg(debug_assertions)]
compile_error!("Run this example in release mode");

#[embassy_executor::task]
async fn signal_task(mut pin: Gpio5<Output<PushPull>>) {
loop {
for _ in 0..10 {
pin.toggle().unwrap();
Timer::after(Duration::from_micros(10)).await;
}
Timer::after(Duration::from_millis(1000)).await;
}
}

#[main]
async fn main(_spawner: Spawner) {
async fn main(spawner: Spawner) {
println!("Init!");
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timg0);
let timer_group0 = esp_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timer_group0);

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

let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "esp32h2")] {
let freq = 32u32.MHz();
} else {
let freq = 80u32.MHz();
}
};

let rmt = Rmt::new(peripherals.RMT, freq, &clocks).unwrap();
let rx_config = RxChannelConfig {
clk_divider: 255,
idle_threshold: 10000,
Expand All @@ -48,14 +68,18 @@ async fn main(_spawner: Spawner) {

cfg_if::cfg_if! {
if #[cfg(any(feature = "esp32", feature = "esp32s2"))] {
let mut channel = rmt.channel0.configure(io.pins.gpio0, rx_config).unwrap();
let mut channel = rmt.channel0.configure(io.pins.gpio4, rx_config).unwrap();
} else if #[cfg(feature = "esp32s3")] {
let mut channel = rmt.channel7.configure(io.pins.gpio0, rx_config).unwrap();
let mut channel = rmt.channel7.configure(io.pins.gpio4, rx_config).unwrap();
} else {
let mut channel = rmt.channel2.configure(io.pins.gpio0, rx_config).unwrap();
let mut channel = rmt.channel2.configure(io.pins.gpio4, rx_config).unwrap();
}
}

spawner
.spawn(signal_task(io.pins.gpio5.into_push_pull_output()))
.unwrap();

let mut data = [PulseCode {
level1: true,
length1: 1,
Expand All @@ -66,7 +90,6 @@ async fn main(_spawner: Spawner) {
loop {
println!("receive");
channel.receive(&mut data).await.unwrap();
println!("received");
let mut total = 0usize;
for entry in &data[..data.len()] {
if entry.length1 == 0 {
Expand Down
14 changes: 11 additions & 3 deletions examples/src/bin/embassy_rmt_tx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Demonstrates generating pulse sequences with RMT
//!
//! Connect a logic analyzer to GPIO1 to see the generated pulses.
//! Connect a logic analyzer to GPIO4 to see the generated pulses.

//% CHIPS: esp32 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
//% FEATURES: async embassy embassy-executor-thread embassy-time-timg0
Expand Down Expand Up @@ -36,12 +36,20 @@ async fn main(_spawner: Spawner) {

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

let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "esp32h2")] {
let freq = 32u32.MHz();
} else {
let freq = 80u32.MHz();
}
};

let rmt = Rmt::new(peripherals.RMT, freq, &clocks).unwrap();

let mut channel = rmt
.channel0
.configure(
io.pins.gpio1.into_push_pull_output(),
io.pins.gpio4.into_push_pull_output(),
TxChannelConfig {
clk_divider: 255,
..TxChannelConfig::default()
Expand Down
114 changes: 70 additions & 44 deletions examples/src/bin/rmt_rx.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Demonstrates decoding pulse sequences with RMT
//!
//! This uses the boot button as input - press the button a couple of
//! times to generate a pulse sequence and then wait for the idle timeout to see
//! the recorded pulse sequence
//! Connect GPIO5 to GPIO4

//% CHIPS: esp32 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3

Expand Down Expand Up @@ -30,21 +27,31 @@ fn main() -> ! {
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let mut out = io.pins.gpio5.into_push_pull_output();

cfg_if::cfg_if! {
if #[cfg(feature = "esp32h2")] {
let freq = 32u32.MHz();
} else {
let freq = 80u32.MHz();
}
};

let rmt = Rmt::new(peripherals.RMT, freq, &clocks).unwrap();

let rmt = Rmt::new(peripherals.RMT, 1u32.MHz(), &clocks).unwrap();
let rx_config = RxChannelConfig {
clk_divider: 255,
clk_divider: 1,
idle_threshold: 10000,
..RxChannelConfig::default()
};

cfg_if::cfg_if! {
if #[cfg(any(feature = "esp32", feature = "esp32s2"))] {
let mut channel = rmt.channel0.configure(io.pins.gpio0, rx_config).unwrap();
let mut channel = rmt.channel0.configure(io.pins.gpio4, rx_config).unwrap();
} else if #[cfg(feature = "esp32s3")] {
let mut channel = rmt.channel7.configure(io.pins.gpio0, rx_config).unwrap();
let mut channel = rmt.channel7.configure(io.pins.gpio4, rx_config).unwrap();
} else {
let mut channel = rmt.channel2.configure(io.pins.gpio0, rx_config).unwrap();
let mut channel = rmt.channel2.configure(io.pins.gpio4, rx_config).unwrap();
}
}

Expand All @@ -58,48 +65,67 @@ fn main() -> ! {
}; 48];

loop {
let transaction = channel.receive(&mut data).unwrap();
channel = transaction.wait().unwrap();

let mut total = 0usize;
for entry in &data[..data.len()] {
if entry.length1 == 0 {
break;
}
total += entry.length1 as usize;

if entry.length2 == 0 {
break;
}
total += entry.length2 as usize;
for x in data.iter_mut() {
x.length1 = 0;
x.length2 = 0;
}

for entry in &data[..data.len()] {
if entry.length1 == 0 {
break;
}
let transaction = channel.receive(&mut data).unwrap();

let count = WIDTH / (total / entry.length1 as usize);
let c = if entry.level1 { '-' } else { '_' };
for _ in 0..count + 1 {
print!("{}", c);
}
// simulate input
for i in 0u32..5u32 {
out.set_high().unwrap();
delay.delay_us(i * 10 + 20);
out.set_low().unwrap();
delay.delay_us(i * 20 + 20);
}

if entry.length2 == 0 {
break;
match transaction.wait() {
Ok(channel_res) => {
channel = channel_res;
let mut total = 0usize;
for entry in &data[..data.len()] {
if entry.length1 == 0 {
break;
}
total += entry.length1 as usize;

if entry.length2 == 0 {
break;
}
total += entry.length2 as usize;
}

for entry in &data[..data.len()] {
if entry.length1 == 0 {
break;
}

let count = WIDTH / (total / entry.length1 as usize);
let c = if entry.level1 { '-' } else { '_' };
for _ in 0..count + 1 {
print!("{}", c);
}

if entry.length2 == 0 {
break;
}

let count = WIDTH / (total / entry.length2 as usize);
let c = if entry.level2 { '-' } else { '_' };
for _ in 0..count + 1 {
print!("{}", c);
}
}

println!();
}

let count = WIDTH / (total / entry.length2 as usize);
let c = if entry.level2 { '-' } else { '_' };
for _ in 0..count + 1 {
print!("{}", c);
Err((_err, channel_res)) => {
channel = channel_res;
println!("Error");
}
}

println!();
println!();
println!();

delay.delay_ms(500u32);
delay.delay_ms(1500u32);
}
}
15 changes: 12 additions & 3 deletions examples/src/bin/rmt_tx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Demonstrates generating pulse sequences with RMT
//!
//! Connect a logic analyzer to GPIO0 to see the generated pulses.
//! Connect a logic analyzer to GPIO4 to see the generated pulses.

//% CHIPS: esp32 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3

Expand All @@ -26,13 +26,22 @@ fn main() -> ! {

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

let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap();
cfg_if::cfg_if! {
if #[cfg(feature = "esp32h2")] {
let freq = 32u32.MHz();
} else {
let freq = 80u32.MHz();
}
};

let rmt = Rmt::new(peripherals.RMT, freq, &clocks).unwrap();

let tx_config = TxChannelConfig {
clk_divider: 255,
..TxChannelConfig::default()
};

let mut channel = rmt.channel0.configure(io.pins.gpio0, tx_config).unwrap();
let mut channel = rmt.channel0.configure(io.pins.gpio4, tx_config).unwrap();

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

Expand Down
Loading