Skip to content

Commit

Permalink
Async SPI (#385)
Browse files Browse the repository at this point in the history
* ground work for async dma (gdma only atm)

* Add async DMA (GDMA) - esp32c3/esp32c2

* Add Async SPI impl for esp32c3/c2

* Remove private modules from DMA

* add async spi example for esp32c3

* Switch to assoc wakers instead of a static array

* add support for esp32/esp32s2

* add support for esp32s3

* run fmt

* add c2 example, fix CI

* Remove redundant comments
  • Loading branch information
MabezDev authored Feb 8, 2023
1 parent e65cace commit 3f7181f
Show file tree
Hide file tree
Showing 17 changed files with 1,816 additions and 502 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jobs:
run: cd esp32-hal/ && cargo check --example=embassy_hello_world --features=embassy,embassy-time-timg0
- name: check esp32-hal (async, gpio)
run: cd esp32-hal/ && cargo check --example=embassy_wait --features=embassy,embassy-time-timg0,async
- name: check esp32-hal (async, spi)
run: cd esp32-hal/ && cargo check --example=embassy_spi --features=embassy,embassy-time-timg0,async

esp32c2-hal:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -77,6 +79,8 @@ jobs:
run: cd esp32c2-hal/ && cargo check --example=embassy_hello_world --features=embassy,embassy-time-timg0
- name: check esp32c2-hal (async, gpio)
run: cd esp32c2-hal/ && cargo check --example=embassy_wait --features=embassy,embassy-time-systick,async
- name: check esp32c2-hal (async, spi)
run: cd esp32c2-hal/ && cargo check --example=embassy_spi --features=embassy,embassy-time-systick,async

esp32c3-hal:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -111,6 +115,8 @@ jobs:
run: cargo check --manifest-path=esp32c3-hal/Cargo.toml --target=riscv32imc-unknown-none-elf --example=embassy_hello_world --features=embassy,embassy-time-timg0
- name: check esp32c3-hal (async, gpio)
run: cd esp32c3-hal/ && cargo check --example=embassy_wait --features=embassy,embassy-time-systick,async
- name: check esp32c3-hal (async, spi)
run: cd esp32c3-hal/ && cargo check --example=embassy_spi --features=embassy,embassy-time-systick,async

esp32s2-hal:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -139,6 +145,8 @@ jobs:
run: cd esp32s2-hal/ && cargo check --example=embassy_hello_world --features=embassy,embassy-time-timg0
- name: check esp32s2-hal (async, gpio)
run: cd esp32s2-hal/ && cargo check --example=embassy_wait --features=embassy,embassy-time-timg0,async
- name: check esp32s2-hal (async, spi)
run: cd esp32s2-hal/ && cargo check --example=embassy_spi --features=embassy,embassy-time-timg0,async

esp32s3-hal:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -170,6 +178,8 @@ jobs:
run: cd esp32s3-hal/ && cargo check --example=embassy_hello_world --features=embassy,embassy-time-timg0
- name: check esp32s3-hal (async, gpio)
run: cd esp32s3-hal/ && cargo check --example=embassy_wait --features=embassy,embassy-time-timg0,async
- name: check esp32s3-hal (async, spi)
run: cd esp32s3-hal/ && cargo check --example=embassy_spi --features=embassy,embassy-time-timg0,async
# --------------------------------------------------------------------------
# MSRV

Expand Down
3 changes: 2 additions & 1 deletion esp-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ usb-device = { version = "0.2.9", optional = true }
embedded-hal-async = { version = "0.2.0-alpha.0", optional = true }
embassy-sync = { version = "0.1.0", optional = true }
embassy-time = { version = "0.1.0", features = ["nightly"], optional = true }
embassy-futures = { version = "0.1.0", optional = true }

# RISC-V
esp-riscv-rt = { version = "0.1.0", optional = true }
Expand Down Expand Up @@ -81,7 +82,7 @@ ufmt = ["ufmt-write"]
vectored = ["procmacros/interrupt"]

# Implement the `embedded-hal-async==1.0.0-alpha.x` traits
async = ["embedded-hal-async", "eh1", "embassy-sync"]
async = ["embedded-hal-async", "eh1", "embassy-sync", "embassy-futures"]
embassy = ["embassy-time"]

embassy-time-systick = []
Expand Down
103 changes: 86 additions & 17 deletions esp-hal-common/src/dma/gdma.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Direct Memory Access
use crate::{
dma::gdma::private::*,
dma::*,
peripheral::PeripheralRef,
system::{Peripheral, PeripheralClockControl},
};
Expand Down Expand Up @@ -263,15 +263,89 @@ macro_rules! impl_channel {
let dma = unsafe { &*crate::peripherals::DMA::PTR };
dma.[<in_dscr_bf0_ch $num>].read().inlink_dscr_bf0().bits() as usize
}

fn is_listening_in_eof() -> bool {
let dma = unsafe { &*crate::peripherals::DMA::PTR };
cfg_if::cfg_if! {
if #[cfg(esp32s3)] {
dma.[<in_int_ena_ch $num>].read().in_suc_eof().bit_is_set()
} else {
dma.[<int_ena_ch $num>].read().in_suc_eof().bit_is_set()
}
}
}
fn is_listening_out_eof() -> bool {
let dma = unsafe { &*crate::peripherals::DMA::PTR };
cfg_if::cfg_if! {
if #[cfg(esp32s3)] {
dma.[<out_int_ena_ch $num>].read().out_total_eof().bit_is_set()
} else {
dma.[<int_ena_ch $num>].read().out_total_eof().bit_is_set()
}
}
}

fn listen_in_eof() {
let dma = unsafe { &*crate::peripherals::DMA::PTR };
cfg_if::cfg_if! {
if #[cfg(esp32s3)] {
dma.[<in_int_ena_ch $num>].modify(|_, w| w.in_suc_eof().set_bit())
} else {
dma.[<int_ena_ch $num>].modify(|_, w| w.in_suc_eof().set_bit())
}
}
}
fn listen_out_eof() {
let dma = unsafe { &*crate::peripherals::DMA::PTR };
cfg_if::cfg_if! {
if #[cfg(esp32s3)] {
dma.[<out_int_ena_ch $num>].modify(|_, w| w.out_total_eof().set_bit())
} else {
dma.[<int_ena_ch $num>].modify(|_, w| w.out_total_eof().set_bit())
}
}
}
fn unlisten_in_eof() {
let dma = unsafe { &*crate::peripherals::DMA::PTR };
cfg_if::cfg_if! {
if #[cfg(esp32s3)] {
dma.[<in_int_ena_ch $num>].modify(|_, w| w.in_suc_eof().clear_bit())
} else {
dma.[<int_ena_ch $num>].modify(|_, w| w.in_suc_eof().clear_bit())
}
}
}
fn unlisten_out_eof() {
let dma = unsafe { &*crate::peripherals::DMA::PTR };
cfg_if::cfg_if! {
if #[cfg(esp32s3)] {
dma.[<out_int_ena_ch $num>].modify(|_, w| w.out_total_eof().clear_bit())
} else {
dma.[<int_ena_ch $num>].modify(|_, w| w.out_total_eof().clear_bit())
}
}
}
}

pub struct [<Channel $num TxImpl>] {}

impl<'a> TxChannel<[<Channel $num>]> for [<Channel $num TxImpl>] {}
impl<'a> TxChannel<[<Channel $num>]> for [<Channel $num TxImpl>] {
#[cfg(feature = "async")]
fn waker() -> &'static embassy_sync::waitqueue::AtomicWaker {
static WAKER: embassy_sync::waitqueue::AtomicWaker = embassy_sync::waitqueue::AtomicWaker::new();
&WAKER
}
}

pub struct [<Channel $num RxImpl>] {}

impl<'a> RxChannel<[<Channel $num>]> for [<Channel $num RxImpl>] {}
impl<'a> RxChannel<[<Channel $num>]> for [<Channel $num RxImpl>] {
#[cfg(feature = "async")]
fn waker() -> &'static embassy_sync::waitqueue::AtomicWaker {
static WAKER: embassy_sync::waitqueue::AtomicWaker = embassy_sync::waitqueue::AtomicWaker::new();
&WAKER
}
}

pub struct [<ChannelCreator $num>] {}

Expand Down Expand Up @@ -337,20 +411,15 @@ macro_rules! impl_channel {
};
}

/// Crate private implementatin details
pub(crate) mod private {
use crate::dma::{private::*, *};

impl_channel!(0);
#[cfg(not(esp32c2))]
impl_channel!(1);
#[cfg(not(esp32c2))]
impl_channel!(2);
#[cfg(esp32s3)]
impl_channel!(3);
#[cfg(esp32s3)]
impl_channel!(4);
}
impl_channel!(0);
#[cfg(not(esp32c2))]
impl_channel!(1);
#[cfg(not(esp32c2))]
impl_channel!(2);
#[cfg(esp32s3)]
impl_channel!(3);
#[cfg(esp32s3)]
impl_channel!(4);

/// GDMA Peripheral
///
Expand Down
Loading

0 comments on commit 3f7181f

Please sign in to comment.