Skip to content

Commit

Permalink
ll: add async traits
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Aug 14, 2022
1 parent 074ea96 commit d80fff4
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[alias]
test-all = "test --features std,log,embedded-hal,time,chrono,num-rational,w5500-tls"
test-dhcp = "test -p w5500-dhcp --features log,std"
test-ll = "test -p w5500-ll --features embedded-hal,std"
test-ll = "test -p w5500-ll --features embedded-hal,std,embedded-hal-async"
test-hl = "test -p w5500-hl --features embedded-hal,std"
test-mqtt = "test -p w5500-mqtt --features log,std,w5500-tls"
test-regsim = "test -p w5500-regsim"
Expand Down
26 changes: 14 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
env: {"RUSTFLAGS": "-D warnings"}
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- run: cargo build
- run: cargo build --all-features
- run: cargo build -p testsuite
Expand All @@ -33,7 +33,7 @@ jobs:
- "thumbv7em-none-eabi"
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
target: ${{ matrix.target }}
- run: cargo build --target ${{ matrix.target }} -p w5500-ll
Expand Down Expand Up @@ -65,6 +65,8 @@ jobs:
- run: cargo build --target ${{ matrix.target }} -p w5500-sntp --features chrono
- run: cargo build --target ${{ matrix.target }} -p w5500-sntp --features num-rational
- run: cargo build --target ${{ matrix.target }} -p w5500-mqtt --features w5500-tls
- run: cargo build --target ${{ matrix.target }} -p w5500-ll --features async
- run: cargo build --target ${{ matrix.target }} -p w5500-ll --features embedded-hal-async
- run: cargo build --target ${{ matrix.target }} -p w5500-tls --features p256-cm4
if: ${{ matrix.target == 'thumbv7em-none-eabi' }}
- run: cargo build --target ${{ matrix.target }} -p w5500-mqtt --features w5500-tls,p256-cm4
Expand All @@ -76,15 +78,15 @@ jobs:
env: {"RUSTFLAGS": "-D warnings"}
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- run: cargo test-all

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
components: clippy
- run: cargo clippy --all-features -- --deny warnings
Expand Down Expand Up @@ -136,7 +138,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- name: Publish
run: |
cd ll
Expand All @@ -157,7 +159,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- name: Publish
run: |
cd hl
Expand All @@ -178,7 +180,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- name: Publish
run: |
cd dhcp
Expand All @@ -199,7 +201,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- name: Publish
run: |
cd regsim
Expand All @@ -220,7 +222,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- name: Publish
run: |
cd dns
Expand All @@ -241,7 +243,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- name: Publish
run: |
cd mqtt
Expand All @@ -262,7 +264,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- name: Publish
run: |
cd sntp
Expand All @@ -283,7 +285,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
- name: Publish
run: |
cd tls
Expand Down
4 changes: 4 additions & 0 deletions ll/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added an `aio` module with async traits.
- Added an optional dependency on `embedded-hal-async` for async trait implementations.

### Changed
- Updated `embedded-hal` from `~0.2.4` to `^1`.
- Renamed the `blocking` module to `eh`.
Expand Down
3 changes: 3 additions & 0 deletions ll/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ homepage = "https://github.com/newAM/w5500-rs"

[features]
std = []
async = []
embedded-hal-async = ["async", "dep:embedded-hal-async", "embedded-hal"]

[dependencies]
embedded-hal-async = { version = "0.1.0-alpha.1", optional = true }
embedded-hal = { version = "1.0.0-alpha.8", optional = true }
defmt = { version = "0.3", optional = true }

Expand Down
84 changes: 84 additions & 0 deletions ll/src/aio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//! Asynchronous W5500 traits.
use super::Sn;

/// Asynchronous buffer IO extension.
///
/// This extends [`crate::Registers`].
///
/// This only implements async buffer IO functions because async functions often
/// do not make sense for short SPI transactions where the cycles to perform a
/// context switch is greater than the number of cycles to wait for a short SPI
/// transaction to complete.
/// That being said, other asynchronous methods will be added if there is demand.
pub trait Registers: crate::Registers {
/// Future for asynchronous buffer read operations.
type ReadFuture<'a>: core::future::Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a,
Self::Error: 'a;

/// Future for asynchronous buffer write operations.
type WriteFuture<'a>: core::future::Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a,
Self::Error: 'a;

/// Read from the W5500 asynchronously.
///
/// See [`crate::Registers::read`] for details.
fn aio_read<'a>(&'a mut self, addr: u16, block: u8, data: &'a mut [u8])
-> Self::ReadFuture<'a>;

/// Write to the W5500 asynchronously.
///
/// See [`crate::Registers::write`] for details.
fn aio_write<'a>(&'a mut self, addr: u16, block: u8, data: &'a [u8]) -> Self::WriteFuture<'a>;

/// Write to the socket TX buffer asynchronously.
///
/// See [`crate::Registers::set_sn_tx_buf`] for details.
fn aio_set_sn_tx_buf<'a>(
&'a mut self,
sn: Sn,
ptr: u16,
buf: &'a [u8],
) -> Self::WriteFuture<'a> {
self.aio_write(ptr, sn.tx_block(), buf)
}

/// Read from the socket TX buffer asynchronously.
///
/// See [`crate::Registers::sn_tx_buf`] for details.
fn aio_sn_tx_buf<'a>(
&'a mut self,
sn: Sn,
ptr: u16,
buf: &'a mut [u8],
) -> Self::ReadFuture<'a> {
self.aio_read(ptr, sn.tx_block(), buf)
}

/// Read from the socket RX buffer asynchronously.
///
/// See [`crate::Registers::sn_rx_buf`] for details.
fn aio_sn_rx_buf<'a>(
&'a mut self,
sn: Sn,
ptr: u16,
buf: &'a mut [u8],
) -> Self::ReadFuture<'a> {
self.aio_read(ptr, sn.rx_block(), buf)
}

/// Write the socket RX buffer asynchronously.
///
/// See [`crate::Registers::set_sn_rx_buf`] for details.
fn aio_set_sn_rx_buf<'a>(
&'a mut self,
sn: Sn,
ptr: u16,
buf: &'a [u8],
) -> Self::WriteFuture<'a> {
self.aio_write(ptr, sn.rx_block(), buf)
}
}
63 changes: 63 additions & 0 deletions ll/src/eh/vdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use crate::spi::{vdm_header, AccessMode};
use embedded_hal::spi::blocking::{SpiBusRead, SpiBusWrite};

#[cfg(feature = "embedded-hal-async")]
use embedded_hal_async::spi::{SpiBusRead as AioSpiBusRead, SpiBusWrite as AioSpiBusWrite};

/// W5500 blocking variable data length implementation.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
Expand All @@ -35,6 +38,7 @@ where
///
/// let mut w5500: W5500<_> = W5500::new(spi);
/// ```
#[inline]
pub fn new(spi: SPI) -> Self {
W5500 { spi }
}
Expand All @@ -51,6 +55,7 @@ where
/// let mut w5500 = W5500::new(spi);
/// let spi = w5500.free();
/// ```
#[inline]
pub fn free(self) -> SPI {
self.spi
}
Expand Down Expand Up @@ -85,3 +90,61 @@ where
})
}
}

#[cfg(feature = "embedded-hal-async")]
#[allow(unsafe_code)]
impl<SPI, E> crate::aio::Registers for W5500<SPI>
where
SPI: embedded_hal_async::spi::SpiDevice<Error = E>
+ embedded_hal::spi::blocking::SpiDevice<Error = E>,

<SPI as embedded_hal_async::spi::SpiDevice>::Bus: embedded_hal_async::spi::SpiBusRead<Error = E>
+ embedded_hal_async::spi::SpiBusWrite<Error = E>,
<SPI as embedded_hal::spi::blocking::SpiDevice>::Bus: embedded_hal::spi::blocking::SpiBusRead<Error = E>
+ embedded_hal::spi::blocking::SpiBusWrite<Error = E>,
{
type ReadFuture<'a> = impl core::future::Future<Output = Result<(), E>> + 'a
where Self: 'a, E: 'a;
type WriteFuture<'a> = impl core::future::Future<Output = Result<(), E>> + 'a
where Self: 'a, E: 'a;

/// Read from the W5500 asynchronously.
///
/// See [`crate::Registers::read`] for details.
fn aio_read<'a>(
&'a mut self,
address: u16,
block: u8,
data: &'a mut [u8],
) -> Self::ReadFuture<'a> {
async move {
let header = vdm_header(address, block, AccessMode::Read);
embedded_hal_async::spi::SpiDevice::transaction(&mut self.spi, move |bus| async move {
let bus = unsafe { &mut *bus };
bus.write(&header).await?;
bus.read(data).await
})
.await
}
}

/// Write to the W5500 asynchronously.
///
/// See [`crate::Registers::write`] for details.
fn aio_write<'a>(
&'a mut self,
address: u16,
block: u8,
data: &'a [u8],
) -> Self::WriteFuture<'a> {
async move {
let header = vdm_header(address, block, AccessMode::Write);
embedded_hal_async::spi::SpiDevice::transaction(&mut self.spi, move |bus| async move {
let bus = unsafe { &mut *bus };
bus.write(&header).await?;
bus.write(data).await
})
.await
}
}
}
6 changes: 5 additions & 1 deletion ll/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@
//! [`w5500_ll::net`]: https://docs.rs/w5500-ll/latest/w5500_ll/net/index.html
#![cfg_attr(docsrs, feature(doc_cfg), feature(doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![cfg_attr(feature = "async", feature(generic_associated_types))]
#![cfg_attr(feature = "embedded-hal-async", feature(type_alias_impl_trait))]
#![deny(unsafe_code)]
#![warn(missing_docs)]

#[cfg(feature = "async")]
pub mod aio;
#[cfg(feature = "embedded-hal")]
pub mod eh;
pub mod net;
Expand Down

0 comments on commit d80fff4

Please sign in to comment.