aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2022-12-01 22:09:45 +0100
committerUlf Lilleengen <[email protected]>2022-12-01 22:09:45 +0100
commitf4c9014fe4d5bb96f583d4b96122bcc536631d18 (patch)
treef2ce07525811ee7a202ee4c06aeabda0ee0a26ea /examples
parent8a81114baf4ffe12ec54e80e342f098c596177d1 (diff)
feat: use async fn in trait
Diffstat (limited to 'examples')
-rw-r--r--examples/rpi-pico-w/Cargo.toml16
-rw-r--r--examples/rpi-pico-w/src/main.rs97
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"
22futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] } 22futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] }
23 23
24embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" } 24embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.9" }
25embedded-hal-async = { version = "0.1.0-alpha.3" } 25embedded-hal-async = { version = "0.2.0-alpha.0" }
26embedded-io = { version = "0.3.0", features = ["async", "defmt"] } 26embedded-io = { version = "0.4.0", features = ["async", "defmt"] }
27heapless = "0.7.15" 27heapless = "0.7.15"
28 28
29 29
30[patch.crates-io] 30[patch.crates-io]
31embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } 31embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
32embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } 32embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
33embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } 33embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
34embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } 34embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
35embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } 35embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
36embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "c53614f057cd7d9ac6e86aebd1fb6c1a1055d8b6" } 36embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "645fb66a5122bdc8180e0e65d076ca103431a426" }
37 37
38[profile.dev] 38[profile.dev]
39debug = 2 39debug = 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
5use core::convert::Infallible; 7use core::convert::Infallible;
6use core::future::Future;
7 8
8use defmt::*; 9use defmt::*;
9use embassy_executor::Spawner; 10use embassy_executor::Spawner;
@@ -155,74 +156,58 @@ impl ErrorType for MySpi {
155} 156}
156 157
157impl SpiBusFlush for MySpi { 158impl 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
167impl SpiBusRead<u32> for MySpi { 164impl 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
197impl SpiBusWrite<u32> for MySpi { 188impl 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}