Skip to content

Commit

Permalink
Erase TWAI instance
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 21, 2024
1 parent 370e8a3 commit 8a1d02b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 23 deletions.
4 changes: 4 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add burst transfer support to DMA buffers (#2336)
- `AnyPin` now implements `From<GpioPin<N>>`. (#2326)
- Added `AnySpi` and `AnySpiDmaChannel`. (#2334)
- Added `degrade` function for SPI instances (SPI2, SPI3) to obtain `AnySpi`. (#2334)
- Added `degrade` function for TWAI instances (TWAI0, TWAI1) to obtain `AnyTwai`. (#?)

- `Pins::steal()` to unsafely obtain GPIO. (#2335)
- `TwaiConfiguration::into_async` (#?)

### Changed

- Peripheral type erasure for SPI (#2334)
- Peripheral type erasure for TWAI (#?)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions esp-hal/MIGRATING-0.21.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You no longer have to specify the peripheral instance in the driver's type for t
peripherals:

- SPI (both master and slave)
- TWAI

```diff
-Spi<'static, SPI2, FullDuplexMode>
Expand Down
71 changes: 65 additions & 6 deletions esp-hal/src/twai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ impl BaudRate {
}

/// An inactive TWAI peripheral in the "Reset"/configuration state.
pub struct TwaiConfiguration<'d, DM: crate::Mode, T> {
pub struct TwaiConfiguration<'d, DM: crate::Mode, T = AnyTwai> {
twai: PeripheralRef<'d, T>,
phantom: PhantomData<DM>,
mode: TwaiMode,
Expand Down Expand Up @@ -1021,14 +1021,44 @@ where
}
}

impl<'d> TwaiConfiguration<'d, crate::Blocking> {
/// Create a new instance of [TwaiConfiguration]
///
/// You will need to use a transceiver to connect to the TWAI bus
pub fn new<RX: PeripheralInput, TX: PeripheralOutput>(
peripheral: impl Peripheral<P = impl Into<AnyTwai>> + 'd,
rx_pin: impl Peripheral<P = RX> + 'd,
tx_pin: impl Peripheral<P = TX> + 'd,
baud_rate: BaudRate,
mode: TwaiMode,
) -> Self {
Self::new_typed(peripheral.map_into(), rx_pin, tx_pin, baud_rate, mode)
}

/// Create a new instance of [TwaiConfiguration] meant to connect two ESP32s
/// directly
///
/// You don't need a transceiver by following the description in the
/// `twai.rs` example
pub fn new_no_transceiver<RX: PeripheralInput, TX: PeripheralOutput>(
peripheral: impl Peripheral<P = impl Into<AnyTwai>> + 'd,
rx_pin: impl Peripheral<P = RX> + 'd,
tx_pin: impl Peripheral<P = TX> + 'd,
baud_rate: BaudRate,
mode: TwaiMode,
) -> Self {
Self::new_no_transceiver_typed(peripheral.map_into(), rx_pin, tx_pin, baud_rate, mode)
}
}

impl<'d, T> TwaiConfiguration<'d, crate::Blocking, T>
where
T: Instance,
{
/// Create a new instance of [TwaiConfiguration]
///
/// You will need to use a transceiver to connect to the TWAI bus
pub fn new<RX: PeripheralInput, TX: PeripheralOutput>(
pub fn new_typed<RX: PeripheralInput, TX: PeripheralOutput>(
peripheral: impl Peripheral<P = T> + 'd,
rx_pin: impl Peripheral<P = RX> + 'd,
tx_pin: impl Peripheral<P = TX> + 'd,
Expand All @@ -1043,7 +1073,7 @@ where
///
/// You don't need a transceiver by following the description in the
/// `twai.rs` example
pub fn new_no_transceiver<RX: PeripheralInput, TX: PeripheralOutput>(
pub fn new_no_transceiver_typed<RX: PeripheralInput, TX: PeripheralOutput>(
peripheral: impl Peripheral<P = T> + 'd,
rx_pin: impl Peripheral<P = RX> + 'd,
tx_pin: impl Peripheral<P = TX> + 'd,
Expand Down Expand Up @@ -1080,7 +1110,7 @@ where
///
/// In this mode, the TWAI controller can transmit and receive messages
/// including error signals (such as error and overload frames).
pub struct Twai<'d, DM: crate::Mode, T> {
pub struct Twai<'d, DM: crate::Mode, T = AnyTwai> {
twai: PeripheralRef<'d, T>,
tx: TwaiTx<'d, DM, T>,
rx: TwaiRx<'d, DM, T>,
Expand Down Expand Up @@ -1198,7 +1228,7 @@ where
}

/// Interface to the TWAI transmitter part.
pub struct TwaiTx<'d, DM: crate::Mode, T> {
pub struct TwaiTx<'d, DM: crate::Mode, T = AnyTwai> {
twai: PeripheralRef<'d, T>,
phantom: PhantomData<DM>,
}
Expand Down Expand Up @@ -1240,7 +1270,7 @@ where
}

/// Interface to the TWAI receiver part.
pub struct TwaiRx<'d, DM: crate::Mode, T> {
pub struct TwaiRx<'d, DM: crate::Mode, T = AnyTwai> {
twai: PeripheralRef<'d, T>,
phantom: PhantomData<DM>,
}
Expand Down Expand Up @@ -1659,6 +1689,35 @@ impl Instance for crate::peripherals::TWAI1 {
}
}

crate::any_peripheral! {
/// Any TWAI peripheral.
pub peripheral AnyTwai {
#[cfg(twai0)]
Twai0(crate::peripherals::TWAI0),
#[cfg(twai1)]
Twai1(crate::peripherals::TWAI1),
}
}

impl Instance for AnyTwai {
delegate::delegate! {
to match &self.0 {
#[cfg(twai0)]
AnyTwaiInner::Twai0(twai) => twai,
#[cfg(twai1)]
AnyTwaiInner::Twai1(twai) => twai,
} {
fn number(&self) -> usize;
fn input_signal(&self) -> InputSignal;
fn output_signal(&self) -> OutputSignal;
fn interrupt(&self) -> crate::peripherals::Interrupt;
fn async_handler(&self) -> InterruptHandler;
fn register_block(&self) -> &RegisterBlock;
fn async_state(&self) -> &asynch::TwaiAsyncState;
}
}
}

mod asynch {
use core::{future::poll_fn, task::Poll};

Expand Down
17 changes: 2 additions & 15 deletions examples/src/bin/embassy_twai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use esp_backtrace as _;
use esp_hal::{
gpio::Io,
interrupt,
peripherals::{self, TWAI0},
timer::timg::TimerGroup,
twai::{self, EspTwaiFrame, StandardId, TwaiMode, TwaiRx, TwaiTx},
};
Expand All @@ -47,10 +46,7 @@ use static_cell::StaticCell;
type TwaiOutbox = Channel<NoopRawMutex, EspTwaiFrame, 16>;

#[embassy_executor::task]
async fn receiver(
mut rx: TwaiRx<'static, esp_hal::Async, TWAI0>,
channel: &'static TwaiOutbox,
) -> ! {
async fn receiver(mut rx: TwaiRx<'static, esp_hal::Async>, channel: &'static TwaiOutbox) -> ! {
loop {
let frame = rx.receive_async().await;

Expand All @@ -72,10 +68,7 @@ async fn receiver(
}

#[embassy_executor::task]
async fn transmitter(
mut tx: TwaiTx<'static, TWAI0, esp_hal::Async>,
channel: &'static TwaiOutbox,
) -> ! {
async fn transmitter(mut tx: TwaiTx<'static, esp_hal::Async>, channel: &'static TwaiOutbox) -> ! {
loop {
let frame = channel.receive().await;
let result = tx.transmit_async(&frame).await;
Expand Down Expand Up @@ -142,12 +135,6 @@ async fn main(spawner: Spawner) {
// Get separate transmit and receive halves of the peripheral.
let (rx, tx) = twai.split();

interrupt::enable(
peripherals::Interrupt::TWAI0,
interrupt::Priority::Priority1,
)
.unwrap();

static CHANNEL: StaticCell<TwaiOutbox> = StaticCell::new();
let channel = &*CHANNEL.init(Channel::new());

Expand Down
3 changes: 1 addition & 2 deletions hil-test/tests/twai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use embedded_hal_02::can::Frame;
use esp_hal::{
gpio::Io,
peripherals::TWAI0,
prelude::*,
twai::{self, filter::SingleStandardFilter, EspTwaiFrame, StandardId, TwaiMode},
Blocking,
Expand All @@ -17,7 +16,7 @@ use hil_test as _;
use nb::block;

struct Context {
twai: twai::Twai<'static, Blocking, TWAI0>,
twai: twai::Twai<'static, Blocking>,
}

#[cfg(test)]
Expand Down

0 comments on commit 8a1d02b

Please sign in to comment.