diff options
| author | Ulf Lilleengen <[email protected]> | 2022-12-01 22:09:45 +0100 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2022-12-01 22:09:45 +0100 |
| commit | f4c9014fe4d5bb96f583d4b96122bcc536631d18 (patch) | |
| tree | f2ce07525811ee7a202ee4c06aeabda0ee0a26ea /examples | |
| parent | 8a81114baf4ffe12ec54e80e342f098c596177d1 (diff) | |
feat: use async fn in trait
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rpi-pico-w/Cargo.toml | 16 | ||||
| -rw-r--r-- | examples/rpi-pico-w/src/main.rs | 97 |
2 files changed, 49 insertions, 64 deletions
diff --git a/examples/rpi-pico-w/Cargo.toml b/examples/rpi-pico-w/Cargo.toml index 7ba22a69e..bb44667de 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" | |||
| 22 | futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] } | 22 | futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] } |
| 23 | 23 | ||
| 24 | embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" } | 24 | embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" } |
| 25 | embedded-hal-async = { version = "0.1.0-alpha.3" } | 25 | embedded-hal-async = { version = "0.2.0-alpha.0" } |
| 26 | embedded-io = { version = "0.3.0", features = ["async", "defmt"] } | 26 | embedded-io = { version = "0.4.0", features = ["async", "defmt"] } |
| 27 | heapless = "0.7.15" | 27 | heapless = "0.7.15" |
| 28 | 28 | ||
| 29 | 29 | ||
| 30 | [patch.crates-io] | 30 | [patch.crates-io] |
| 31 | embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } | 31 | embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } |
| 32 | embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } | 32 | embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } |
| 33 | embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } | 33 | embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } |
| 34 | embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } | 34 | embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } |
| 35 | embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } | 35 | embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } |
| 36 | embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } | 36 | embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" } |
| 37 | 37 | ||
| 38 | [profile.dev] | 38 | [profile.dev] |
| 39 | debug = 2 | 39 | debug = 2 |
diff --git a/examples/rpi-pico-w/src/main.rs b/examples/rpi-pico-w/src/main.rs index 705c7accb..a19f38591 100644 --- a/examples/rpi-pico-w/src/main.rs +++ b/examples/rpi-pico-w/src/main.rs | |||
| @@ -1,9 +1,10 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | #![feature(async_fn_in_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 4 | 6 | ||
| 5 | use core::convert::Infallible; | 7 | use core::convert::Infallible; |
| 6 | use core::future::Future; | ||
| 7 | 8 | ||
| 8 | use defmt::*; | 9 | use defmt::*; |
| 9 | use embassy_executor::Spawner; | 10 | use embassy_executor::Spawner; |
| @@ -155,74 +156,58 @@ impl ErrorType for MySpi { | |||
| 155 | } | 156 | } |
| 156 | 157 | ||
| 157 | impl SpiBusFlush for MySpi { | 158 | impl SpiBusFlush for MySpi { |
| 158 | type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> | 159 | async fn flush(&mut self) -> Result<(), Self::Error> { |
| 159 | where | 160 | Ok(()) |
| 160 | Self: 'a; | ||
| 161 | |||
| 162 | fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { | ||
| 163 | async move { Ok(()) } | ||
| 164 | } | 161 | } |
| 165 | } | 162 | } |
| 166 | 163 | ||
| 167 | impl SpiBusRead<u32> for MySpi { | 164 | impl SpiBusRead<u32> for MySpi { |
| 168 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a | 165 | async fn read(&mut self, words: &mut [u32]) -> Result<(), Self::Error> { |
| 169 | where | 166 | self.dio.set_as_input(); |
| 170 | Self: 'a; | 167 | for word in words { |
| 171 | 168 | let mut w = 0; | |
| 172 | fn read<'a>(&'a mut self, words: &'a mut [u32]) -> Self::ReadFuture<'a> { | 169 | for _ in 0..32 { |
| 173 | async move { | 170 | w = w << 1; |
| 174 | self.dio.set_as_input(); | 171 | |
| 175 | for word in words { | 172 | // rising edge, sample data |
| 176 | let mut w = 0; | 173 | if self.dio.is_high() { |
| 177 | for _ in 0..32 { | 174 | w |= 0x01; |
| 178 | w = w << 1; | ||
| 179 | |||
| 180 | // rising edge, sample data | ||
| 181 | if self.dio.is_high() { | ||
| 182 | w |= 0x01; | ||
| 183 | } | ||
| 184 | self.clk.set_high(); | ||
| 185 | |||
| 186 | // falling edge | ||
| 187 | self.clk.set_low(); | ||
| 188 | } | 175 | } |
| 189 | *word = w | 176 | self.clk.set_high(); |
| 190 | } | ||
| 191 | 177 | ||
| 192 | Ok(()) | 178 | // falling edge |
| 179 | self.clk.set_low(); | ||
| 180 | } | ||
| 181 | *word = w | ||
| 193 | } | 182 | } |
| 183 | |||
| 184 | Ok(()) | ||
| 194 | } | 185 | } |
| 195 | } | 186 | } |
| 196 | 187 | ||
| 197 | impl SpiBusWrite<u32> for MySpi { | 188 | impl SpiBusWrite<u32> for MySpi { |
| 198 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a | 189 | async fn write(&mut self, words: &[u32]) -> Result<(), Self::Error> { |
| 199 | where | 190 | self.dio.set_as_output(); |
| 200 | Self: 'a; | 191 | for word in words { |
| 201 | 192 | let mut word = *word; | |
| 202 | fn write<'a>(&'a mut self, words: &'a [u32]) -> Self::WriteFuture<'a> { | 193 | for _ in 0..32 { |
| 203 | async move { | 194 | // falling edge, setup data |
| 204 | self.dio.set_as_output(); | 195 | self.clk.set_low(); |
| 205 | for word in words { | 196 | if word & 0x8000_0000 == 0 { |
| 206 | let mut word = *word; | 197 | self.dio.set_low(); |
| 207 | for _ in 0..32 { | 198 | } else { |
| 208 | // falling edge, setup data | 199 | self.dio.set_high(); |
| 209 | self.clk.set_low(); | ||
| 210 | if word & 0x8000_0000 == 0 { | ||
| 211 | self.dio.set_low(); | ||
| 212 | } else { | ||
| 213 | self.dio.set_high(); | ||
| 214 | } | ||
| 215 | |||
| 216 | // rising edge | ||
| 217 | self.clk.set_high(); | ||
| 218 | |||
| 219 | word = word << 1; | ||
| 220 | } | 200 | } |
| 221 | } | ||
| 222 | self.clk.set_low(); | ||
| 223 | 201 | ||
| 224 | self.dio.set_as_input(); | 202 | // rising edge |
| 225 | Ok(()) | 203 | self.clk.set_high(); |
| 204 | |||
| 205 | word = word << 1; | ||
| 206 | } | ||
| 226 | } | 207 | } |
| 208 | self.clk.set_low(); | ||
| 209 | |||
| 210 | self.dio.set_as_input(); | ||
| 211 | Ok(()) | ||
| 227 | } | 212 | } |
| 228 | } | 213 | } |
