esp-hal/esp32-hal/examples/embassy_rmt_rx.rs
Scott Mabin db409ffe7b
Unify the system peripheral (#832)
* Unify the system peripheral

Whilst the PCR, SYSTEM and DPORT peripherals are different, we currently
use them all in the same way. This PR unifies the peripheral name in the
hal to `SYSTEM`. The idea is that they all do the same sort of thing, so
we can collect them under the same name, and later down the line we can
being to expose differences under an extended API.

The benifits to this are imo quite big, the examples now are all identical,
which makes things easier for esp-wifi, and paves a path towards the
multichip hal.

Why not do this in the PAC? Imo the pac should be as close to the
hardware as possible, and the HAL is where we should abstractions such
as this.

* changelog
2023-09-29 08:14:50 -07:00

135 lines
3.5 KiB
Rust

//! Demonstrates decoding pulse sequences with RMT
//! Connect GPIO15 to GPIO4
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_time::{Duration, Timer};
use esp32_hal::{
clock::ClockControl,
embassy::{self, executor::Executor},
peripherals::Peripherals,
prelude::*,
rmt::{asynch::RxChannelAsync, Channel2, PulseCode, RxChannelConfig, RxChannelCreator},
Rmt,
IO,
};
use esp_backtrace as _;
use esp_hal_common::gpio::{Gpio15, Output, PushPull};
use esp_println::{print, println};
use static_cell::make_static;
const WIDTH: usize = 80;
#[cfg(debug_assertions)]
compile_error!("Run this example in release mode");
#[embassy_executor::task]
async fn rmt_task(mut channel: Channel2<2>) {
let mut data = [PulseCode {
level1: true,
length1: 1,
level2: false,
length2: 1,
}; 48];
loop {
println!("receive");
channel.receive(&mut data).await.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 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!();
}
}
#[embassy_executor::task]
async fn signal_task(mut pin: Gpio15<Output<PushPull>>) {
loop {
for _ in 0..10 {
pin.toggle().unwrap();
Timer::after(Duration::from_micros(10)).await;
}
Timer::after(Duration::from_millis(1000)).await;
}
}
#[entry]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
println!("Init!");
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
#[cfg(feature = "embassy-time-timg0")]
{
let timer_group0 = esp32_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timer_group0.timer0);
}
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).unwrap();
let channel = rmt
.channel2
.configure(
io.pins.gpio4,
RxChannelConfig {
clk_divider: 1,
idle_threshold: 0b111_1111_1111_1111,
..RxChannelConfig::default()
},
)
.unwrap();
// you have to enable the interrupt for async to work
esp32_hal::interrupt::enable(
esp32_hal::peripherals::Interrupt::RMT,
esp32_hal::interrupt::Priority::Priority1,
)
.unwrap();
let executor = make_static!(Executor::new());
executor.run(|spawner| {
spawner.spawn(rmt_task(channel)).ok();
spawner
.spawn(signal_task(io.pins.gpio15.into_push_pull_output()))
.ok();
});
}