Skip to content

Commit

Permalink
Mark Async drivers !Send
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Jan 17, 2025
1 parent 1a1e1f0 commit 3189e14
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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)

- `Async` drivers are no longer `Send` (#2980)

### Fixed

- `DmaDescriptor` is now `#[repr(C)]` (#2988)
Expand Down
23 changes: 22 additions & 1 deletion esp-hal/MIGRATING-0.23.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Migration Guide from v0.23.x to v?.??.?
# Migration Guide from v0.23.x to v1.0.0-beta.0

## RMT changes

Expand Down Expand Up @@ -70,3 +70,24 @@ The more descriptive `gpio::Level` enum is now used to specify output levels of
- let code = PulseCode::new(true, 200, false, 50);
+ let code = PulseCode::new(Level::High, 200, Level::Low, 50);
```

## `Async` drivers can no longer be sent between cores and executors

To work around this limitation, send the blocking driver, and configure it into `Async` mode
in the target context.

```diff
#[embassy_executor::task]
-async fn interrupt_driven_task(mut spi: Spi<'static, Async>) {
+async fn interrupt_driven_task(spi: Spi<'static, Blocking>) {
+ let mut spi = spi.into_async();
...
}

let spi = Spi::new(...)
.unwrap()
// ...
- .into_async();

send_spawner.spawn(interrupt_driven_task(spi)).unwrap();
```
6 changes: 5 additions & 1 deletion esp-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
// MUST be the first module
mod fmt;

use core::marker::PhantomData;

#[cfg(riscv)]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
#[cfg_attr(not(feature = "unstable"), doc(hidden))]
Expand Down Expand Up @@ -320,7 +322,9 @@ pub struct Blocking;

/// Driver initialized in async mode.
#[derive(Debug)]
pub struct Async;
pub struct Async(PhantomData<*const ()>);

unsafe impl Sync for Async {}

impl crate::DriverMode for Blocking {}
impl crate::DriverMode for Async {}
Expand Down
20 changes: 8 additions & 12 deletions hil-test/tests/embassy_interrupt_spi_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use esp_hal::{
},
time::RateExtU32,
timer::AnyTimer,
Async,
Blocking,
};
use esp_hal_embassy::InterruptExecutor;
use hil_test as _;
Expand All @@ -37,14 +37,14 @@ static INTERRUPT_TASK_WORKING: AtomicBool = AtomicBool::new(false);

#[cfg(any(esp32, esp32s2, esp32s3))]
#[embassy_executor::task]
async fn interrupt_driven_task(spi: esp_hal::spi::master::SpiDma<'static, Async>) {
async fn interrupt_driven_task(spi: esp_hal::spi::master::SpiDma<'static, Blocking>) {
let mut ticker = Ticker::every(Duration::from_millis(1));

let (rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) = dma_buffers!(128);
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();

let mut spi = spi.with_buffers(dma_rx_buf, dma_tx_buf);
let mut spi = spi.with_buffers(dma_rx_buf, dma_tx_buf).into_async();

loop {
let mut buffer: [u8; 8] = [0; 8];
Expand All @@ -59,9 +59,11 @@ async fn interrupt_driven_task(spi: esp_hal::spi::master::SpiDma<'static, Async>

#[cfg(not(any(esp32, esp32s2, esp32s3)))]
#[embassy_executor::task]
async fn interrupt_driven_task(mut i2s_tx: esp_hal::i2s::master::I2sTx<'static, Async>) {
async fn interrupt_driven_task(i2s_tx: esp_hal::i2s::master::I2s<'static, Blocking>) {
let mut ticker = Ticker::every(Duration::from_millis(1));

let mut i2s_tx = i2s_tx.into_async().i2s_tx.build();

loop {
let mut buffer: [u8; 8] = [0; 8];

Expand Down Expand Up @@ -137,14 +139,13 @@ mod test {
.with_mode(Mode::_0),
)
.unwrap()
.with_dma(dma_channel2)
.into_async();
.with_dma(dma_channel2);

#[cfg(not(any(esp32, esp32s2, esp32s3)))]
let other_peripheral = {
let (_, rx_descriptors, _, tx_descriptors) = dma_buffers!(128);

let other_peripheral = esp_hal::i2s::master::I2s::new(
esp_hal::i2s::master::I2s::new(
peripherals.I2S0,
esp_hal::i2s::master::Standard::Philips,
esp_hal::i2s::master::DataFormat::Data8Channel8,
Expand All @@ -153,11 +154,6 @@ mod test {
rx_descriptors,
tx_descriptors,
)
.into_async()
.i2s_tx
.build();

other_peripheral
};

let sw_ints = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
Expand Down

0 comments on commit 3189e14

Please sign in to comment.