Skip to content

Commit

Permalink
rmt: Use gpio::Level for PulseCode::level{1|2}
Browse files Browse the repository at this point in the history
  • Loading branch information
wisp3rwind committed Jan 17, 2025
1 parent 5f680d9 commit 0e794af
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- RMT: `TxChannelConfig` and `RxChannelConfig` now support the builder-lite pattern (#2978)
- RMT: Some fields of `TxChannelConfig` and `RxChannelConfig` are now `gpio::Level`-valued instead of `bool` (#2989)
- RMT: The `PulseCode` trait now uses `gpio::Level` to specify output levels instead of `bool` (#2989)

### Fixed

Expand Down
10 changes: 10 additions & 0 deletions esp-hal/MIGRATING-0.23.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ take the more descriptive `gpio::Level` type instead of a plain `bool`.
.unwrap();
```

### `PulseCode` now uses `gpio::Level` instead of `bool` to specify output levels

The more descriptive `gpio::Level` enum is now used to specify output levels of `PulseCode`:

```diff
+ use esp_hal::gpio::Level;
+
- let code = PulseCode::new(true, 200, false, 50);
+ let code = PulseCode::new(Level::High, 200, Level::Low, 50);
```
42 changes: 25 additions & 17 deletions esp-hal/src/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@
//! ### TX operation
//! ```rust, no_run
#![doc = crate::before_snippet!()]
//! # use esp_hal::rmt::{PulseCode, Rmt, TxChannel, TxChannelConfig, TxChannelCreator};
//! # use esp_hal::delay::Delay;
//! # use esp_hal::gpio::Level;
//! # use esp_hal::rmt::{PulseCode, Rmt, TxChannel, TxChannelConfig, TxChannelCreator};
//!
//! // Configure frequency based on chip type
#![cfg_attr(esp32h2, doc = "let freq = 32.MHz();")]
Expand All @@ -96,8 +97,8 @@
//!
//! let delay = Delay::new();
//!
//! let mut data = [PulseCode::new(true, 200, false, 50); 20];
//! data[data.len() - 2] = PulseCode::new(true, 3000, false, 500);
//! let mut data = [PulseCode::new(Level::High, 200, Level::Low, 50); 20];
//! data[data.len() - 2] = PulseCode::new(Level::High, 3000, Level::Low, 500);
//! data[data.len() - 1] = PulseCode::empty();
//!
//! loop {
Expand Down Expand Up @@ -179,7 +180,10 @@
//! }
//!
//! let count = WIDTH / (total / entry.length1() as usize);
//! let c = if entry.level1() { '-' } else { '_' };
//! let c = match entry.level1() {
//! Level::High => '-',
//! Level::Low => '_',
//! };
//! for _ in 0..count + 1 {
//! print!("{}", c);
//! }
Expand All @@ -189,7 +193,10 @@
//! }
//!
//! let count = WIDTH / (total / entry.length2() as usize);
//! let c = if entry.level2() { '-' } else { '_' };
//! let c = match entry.level2() {
//! Level::High => '-',
//! Level::Low => '_',
//! };
//! for _ in 0..count + 1 {
//! print!("{}", c);
//! }
Expand All @@ -210,6 +217,7 @@
//! > Note: on ESP32 and ESP32-S2 you cannot specify a base frequency other than 80 MHz
use core::{
default::Default,
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
Expand Down Expand Up @@ -251,7 +259,7 @@ pub enum Error {
/// Convenience trait to work with pulse codes.
pub trait PulseCode: crate::private::Sealed {
/// Create a new instance
fn new(level1: bool, length1: u16, level2: bool, length2: u16) -> Self;
fn new(level1: Level, length1: u16, level2: Level, length2: u16) -> Self;

/// Create a new empty instance
fn empty() -> Self;
Expand All @@ -260,22 +268,22 @@ pub trait PulseCode: crate::private::Sealed {
fn reset(&mut self);

/// Logical output level in the first pulse code interval
fn level1(&self) -> bool;
fn level1(&self) -> Level;

/// Length of the first pulse code interval (in clock cycles)
fn length1(&self) -> u16;

/// Logical output level in the second pulse code interval
fn level2(&self) -> bool;
fn level2(&self) -> Level;

/// Length of the second pulse code interval (in clock cycles)
fn length2(&self) -> u16;
}

impl PulseCode for u32 {
fn new(level1: bool, length1: u16, level2: bool, length2: u16) -> Self {
let level1 = ((level1 as u32) << 15) | (length1 as u32 & 0b111_1111_1111_1111);
let level2 = ((level2 as u32) << 15) | (length2 as u32 & 0b111_1111_1111_1111);
fn new(level1: Level, length1: u16, level2: Level, length2: u16) -> Self {
let level1 = ((bool::from(level1) as u32) << 15) | (length1 as u32 & 0b111_1111_1111_1111);
let level2 = ((bool::from(level2) as u32) << 15) | (length2 as u32 & 0b111_1111_1111_1111);
level1 | (level2 << 16)
}

Expand All @@ -287,16 +295,16 @@ impl PulseCode for u32 {
*self = 0
}

fn level1(&self) -> bool {
self & (1 << 15) != 0
fn level1(&self) -> Level {
(self & (1 << 15) != 0).into()
}

fn length1(&self) -> u16 {
(self & 0b111_1111_1111_1111) as u16
}

fn level2(&self) -> bool {
self & (1 << 31) != 0
fn level2(&self) -> Level {
(self & (1 << 31) != 0).into()
}

fn length2(&self) -> u16 {
Expand Down Expand Up @@ -324,7 +332,7 @@ pub struct TxChannelConfig {
pub carrier_level: Level,
}

impl core::default::Default for TxChannelConfig {
impl Default for TxChannelConfig {
fn default() -> Self {
Self {
clk_divider: Default::default(),
Expand Down Expand Up @@ -358,7 +366,7 @@ pub struct RxChannelConfig {
pub idle_threshold: u16,
}

impl core::default::Default for RxChannelConfig {
impl Default for RxChannelConfig {
fn default() -> Self {
Self {
clk_divider: Default::default(),
Expand Down
10 changes: 8 additions & 2 deletions examples/src/bin/embassy_rmt_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ async fn main(spawner: Spawner) {
}

let count = WIDTH / (total / entry.length1() as usize);
let c = if entry.level1() { '-' } else { '_' };
let c = match entry.level1() {
Level::High => '-',
Level::Low => '_',
};
for _ in 0..count + 1 {
print!("{}", c);
}
Expand All @@ -105,7 +108,10 @@ async fn main(spawner: Spawner) {
}

let count = WIDTH / (total / entry.length2() as usize);
let c = if entry.level2() { '-' } else { '_' };
let c = match entry.level2() {
Level::High => '-',
Level::Low => '_',
};
for _ in 0..count + 1 {
print!("{}", c);
}
Expand Down
5 changes: 3 additions & 2 deletions examples/src/bin/embassy_rmt_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
gpio::Level,
rmt::{PulseCode, Rmt, TxChannelAsync, TxChannelConfig, TxChannelCreatorAsync},
time::RateExtU32,
timer::timg::TimerGroup,
Expand Down Expand Up @@ -47,9 +48,9 @@ async fn main(_spawner: Spawner) {
)
.unwrap();

let mut data = [PulseCode::new(true, 200, false, 50); 20];
let mut data = [PulseCode::new(Level::High, 200, Level::Low, 50); 20];

data[data.len() - 2] = PulseCode::new(true, 3000, false, 500);
data[data.len() - 2] = PulseCode::new(Level::High, 3000, Level::Low, 500);
data[data.len() - 1] = PulseCode::empty();

loop {
Expand Down
20 changes: 8 additions & 12 deletions hil-test/tests/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![no_main]

use esp_hal::{
gpio::Level,
rmt::{PulseCode, Rmt, RxChannel, RxChannelConfig, TxChannel, TxChannelConfig},
time::RateExtU32,
};
Expand Down Expand Up @@ -38,21 +39,16 @@ mod tests {

let (rx, tx) = hil_test::common_test_pins!(peripherals);

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

let tx_channel = {
use esp_hal::rmt::TxChannelCreator;
rmt.channel0.configure(tx, tx_config).unwrap()
};

let rx_config = RxChannelConfig {
clk_divider: 255,
idle_threshold: 1000,
..RxChannelConfig::default()
};
let rx_config = RxChannelConfig::default()
.with_clk_divider(255)
.with_idle_threshold(1000);

cfg_if::cfg_if! {
if #[cfg(feature = "esp32")] {
Expand All @@ -78,9 +74,9 @@ mod tests {
}
}

let mut tx_data = [PulseCode::new(true, 200, false, 50); 20];
let mut tx_data = [PulseCode::new(Level::High, 200, Level::Low, 50); 20];

tx_data[tx_data.len() - 2] = PulseCode::new(true, 3000, false, 500);
tx_data[tx_data.len() - 2] = PulseCode::new(Level::High, 3000, Level::Low, 500);
tx_data[tx_data.len() - 1] = PulseCode::empty();

let mut rcv_data: [u32; 20] = [PulseCode::empty(); 20];
Expand Down Expand Up @@ -122,7 +118,7 @@ mod tests {
rmt.channel0.configure(tx, tx_config).unwrap()
};

let tx_data = [PulseCode::new(true, 200, false, 50); 20];
let tx_data = [PulseCode::new(Level::High, 200, Level::Low, 50); 20];

let tx_transaction = tx_channel.transmit(&tx_data);

Expand Down

0 comments on commit 0e794af

Please sign in to comment.