From f4c9014fe4d5bb96f583d4b96122bcc536631d18 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Thu, 1 Dec 2022 22:09:45 +0100 Subject: [PATCH] feat: use async fn in trait --- Cargo.toml | 2 +- examples/rpi-pico-w/Cargo.toml | 16 +++--- examples/rpi-pico-w/src/main.rs | 95 ++++++++++++++------------------- rust-toolchain.toml | 2 +- 4 files changed, 50 insertions(+), 65 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8e1eddc1..cc19c938 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } 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 } diff --git a/examples/rpi-pico-w/Cargo.toml b/examples/rpi-pico-w/Cargo.toml index 7ba22a69..bb44667d 100644 --- a/examples/rpi-pico-w/Cargo.toml +++ b/examples/rpi-pico-w/Cargo.toml @@ -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"] } embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" } -embedded-hal-async = { version = "0.1.0-alpha.3" } -embedded-io = { version = "0.3.0", features = ["async", "defmt"] } +embedded-hal-async = { version = "0.2.0-alpha.0" } +embedded-io = { version = "0.4.0", features = ["async", "defmt"] } heapless = "0.7.15" [patch.crates-io] -embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } -embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } -embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } -embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } -embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } -embassy-net = { 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 = "645fb66a5122bdc8180e0e65d076ca103431a426" } +embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } +embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } +embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } +embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } [profile.dev] debug = 2 diff --git a/examples/rpi-pico-w/src/main.rs b/examples/rpi-pico-w/src/main.rs index 705c7acc..a19f3859 100644 --- a/examples/rpi-pico-w/src/main.rs +++ b/examples/rpi-pico-w/src/main.rs @@ -1,9 +1,10 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] use core::convert::Infallible; -use core::future::Future; use defmt::*; use embassy_executor::Spawner; @@ -155,74 +156,58 @@ impl ErrorType for MySpi { } impl SpiBusFlush for MySpi { - type FlushFuture<'a> = impl Future> - where - Self: 'a; - - fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { - async move { Ok(()) } + async fn flush(&mut self) -> Result<(), Self::Error> { + Ok(()) } } impl SpiBusRead for MySpi { - type ReadFuture<'a> = impl Future> + 'a - where - Self: 'a; + async fn read(&mut self, words: &mut [u32]) -> Result<(), Self::Error> { + self.dio.set_as_input(); + 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> { - async move { - self.dio.set_as_input(); - 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(); + // rising edge, sample data + if self.dio.is_high() { + w |= 0x01; } - *word = w - } + self.clk.set_high(); - Ok(()) + // falling edge + self.clk.set_low(); + } + *word = w } + + Ok(()) } } impl SpiBusWrite for MySpi { - type WriteFuture<'a> = impl Future> + 'a - where - Self: 'a; - - fn write<'a>(&'a mut self, words: &'a [u32]) -> Self::WriteFuture<'a> { - async move { - self.dio.set_as_output(); - for word in words { - let mut word = *word; - for _ in 0..32 { - // falling edge, setup data - 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; + async fn write(&mut self, words: &[u32]) -> Result<(), Self::Error> { + self.dio.set_as_output(); + for word in words { + let mut word = *word; + for _ in 0..32 { + // falling edge, setup data + self.clk.set_low(); + if word & 0x8000_0000 == 0 { + self.dio.set_low(); + } else { + self.dio.set_high(); } - } - self.clk.set_low(); - self.dio.set_as_input(); - Ok(()) + // rising edge + self.clk.set_high(); + + word = word << 1; + } } + self.clk.set_low(); + + self.dio.set_as_input(); + Ok(()) } } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 3e219b0c..ffbcbd6f 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,7 +1,7 @@ # Before upgrading check that everything is available on all tier1 targets here: # https://rust-lang.github.io/rustup-components-history [toolchain] -channel = "nightly-2022-10-25" +channel = "nightly-2022-11-22" components = [ "rust-src", "rustfmt" ] targets = [ "thumbv6m-none-eabi",