Merge pull request #30 from embassy-rs/afit

feat: use async fn in trait
This commit is contained in:
Dario Nieuwenhuis 2022-12-01 22:58:41 +01:00 committed by GitHub
commit 432240162a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 65 deletions

View file

@ -22,5 +22,5 @@ cortex-m-rt = "0.7.0"
futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] } futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] }
embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" } embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" }
embedded-hal-async = { version = "0.1.0-alpha.3" } embedded-hal-async = { version = "0.2.0-alpha.0" }
num_enum = { version = "0.5.7", default-features = false } num_enum = { version = "0.5.7", default-features = false }

View file

@ -22,18 +22,18 @@ cortex-m-rt = "0.7.0"
futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] } futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] }
embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" } embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" }
embedded-hal-async = { version = "0.1.0-alpha.3" } embedded-hal-async = { version = "0.2.0-alpha.0" }
embedded-io = { version = "0.3.0", features = ["async", "defmt"] } embedded-io = { version = "0.4.0", features = ["async", "defmt"] }
heapless = "0.7.15" heapless = "0.7.15"
[patch.crates-io] [patch.crates-io]
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
[profile.dev] [profile.dev]
debug = 2 debug = 2

View file

@ -1,9 +1,10 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]
use core::convert::Infallible; use core::convert::Infallible;
use core::future::Future;
use defmt::*; use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
@ -155,74 +156,58 @@ impl ErrorType for MySpi {
} }
impl SpiBusFlush for MySpi { impl SpiBusFlush for MySpi {
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> async fn flush(&mut self) -> Result<(), Self::Error> {
where Ok(())
Self: 'a;
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
async move { Ok(()) }
} }
} }
impl SpiBusRead<u32> for MySpi { impl SpiBusRead<u32> for MySpi {
type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a async fn read(&mut self, words: &mut [u32]) -> Result<(), Self::Error> {
where self.dio.set_as_input();
Self: 'a; for word in words {
let mut w = 0;
for _ in 0..32 {
w = w << 1;
fn read<'a>(&'a mut self, words: &'a mut [u32]) -> Self::ReadFuture<'a> { // rising edge, sample data
async move { if self.dio.is_high() {
self.dio.set_as_input(); w |= 0x01;
for word in words {
let mut w = 0;
for _ in 0..32 {
w = w << 1;
// rising edge, sample data
if self.dio.is_high() {
w |= 0x01;
}
self.clk.set_high();
// falling edge
self.clk.set_low();
} }
*word = w self.clk.set_high();
}
Ok(()) // falling edge
self.clk.set_low();
}
*word = w
} }
Ok(())
} }
} }
impl SpiBusWrite<u32> for MySpi { impl SpiBusWrite<u32> for MySpi {
type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a async fn write(&mut self, words: &[u32]) -> Result<(), Self::Error> {
where self.dio.set_as_output();
Self: 'a; for word in words {
let mut word = *word;
fn write<'a>(&'a mut self, words: &'a [u32]) -> Self::WriteFuture<'a> { for _ in 0..32 {
async move { // falling edge, setup data
self.dio.set_as_output(); self.clk.set_low();
for word in words { if word & 0x8000_0000 == 0 {
let mut word = *word; self.dio.set_low();
for _ in 0..32 { } else {
// falling edge, setup data self.dio.set_high();
self.clk.set_low();
if word & 0x8000_0000 == 0 {
self.dio.set_low();
} else {
self.dio.set_high();
}
// rising edge
self.clk.set_high();
word = word << 1;
} }
}
self.clk.set_low();
self.dio.set_as_input(); // rising edge
Ok(()) self.clk.set_high();
word = word << 1;
}
} }
self.clk.set_low();
self.dio.set_as_input();
Ok(())
} }
} }

View file

@ -1,7 +1,7 @@
# Before upgrading check that everything is available on all tier1 targets here: # Before upgrading check that everything is available on all tier1 targets here:
# https://rust-lang.github.io/rustup-components-history # https://rust-lang.github.io/rustup-components-history
[toolchain] [toolchain]
channel = "nightly-2022-10-25" channel = "nightly-2022-11-22"
components = [ "rust-src", "rustfmt" ] components = [ "rust-src", "rustfmt" ]
targets = [ targets = [
"thumbv6m-none-eabi", "thumbv6m-none-eabi",