aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-11-21 23:31:31 +0100
committerDario Nieuwenhuis <[email protected]>2022-11-25 21:02:06 +0100
commit1e2fb0459d8546ba658bb9fe150be5f1f537b48e (patch)
treeeb40a5027581896c7b78db58f509431ed6b11892 /embassy-nrf/src
parent758f5d7ea29f1df14d5ef59c82e4b7f22545d775 (diff)
Switch to async-fn-in-trait
Diffstat (limited to 'embassy-nrf/src')
-rw-r--r--embassy-nrf/src/buffered_uarte.rs66
-rw-r--r--embassy-nrf/src/gpiote.rs62
-rw-r--r--embassy-nrf/src/lib.rs6
-rw-r--r--embassy-nrf/src/spim.rs31
-rw-r--r--embassy-nrf/src/twim.rs28
-rw-r--r--embassy-nrf/src/uarte.rs26
-rw-r--r--embassy-nrf/src/usb.rs381
7 files changed, 257 insertions, 343 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs
index 9c8fe65f4..ea25236f0 100644
--- a/embassy-nrf/src/buffered_uarte.rs
+++ b/embassy-nrf/src/buffered_uarte.rs
@@ -15,7 +15,7 @@
15 15
16use core::cell::RefCell; 16use core::cell::RefCell;
17use core::cmp::min; 17use core::cmp::min;
18use core::future::{poll_fn, Future}; 18use core::future::poll_fn;
19use core::sync::atomic::{compiler_fence, Ordering}; 19use core::sync::atomic::{compiler_fence, Ordering};
20use core::task::Poll; 20use core::task::Poll;
21 21
@@ -341,32 +341,20 @@ impl<'u, 'd, U: UarteInstance, T: TimerInstance> embedded_io::Io for BufferedUar
341} 341}
342 342
343impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for BufferedUarte<'d, U, T> { 343impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for BufferedUarte<'d, U, T> {
344 type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 344 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
345 where 345 self.inner_read(buf).await
346 Self: 'a;
347
348 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
349 self.inner_read(buf)
350 } 346 }
351} 347}
352 348
353impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for BufferedUarteRx<'u, 'd, U, T> { 349impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for BufferedUarteRx<'u, 'd, U, T> {
354 type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 350 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
355 where 351 self.inner.inner_read(buf).await
356 Self: 'a;
357
358 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
359 self.inner.inner_read(buf)
360 } 352 }
361} 353}
362 354
363impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead for BufferedUarte<'d, U, T> { 355impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead for BufferedUarte<'d, U, T> {
364 type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>> + 'a 356 async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
365 where 357 self.inner_fill_buf().await
366 Self: 'a;
367
368 fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> {
369 self.inner_fill_buf()
370 } 358 }
371 359
372 fn consume(&mut self, amt: usize) { 360 fn consume(&mut self, amt: usize) {
@@ -375,12 +363,8 @@ impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead for Bu
375} 363}
376 364
377impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead for BufferedUarteRx<'u, 'd, U, T> { 365impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead for BufferedUarteRx<'u, 'd, U, T> {
378 type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>> + 'a 366 async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
379 where 367 self.inner.inner_fill_buf().await
380 Self: 'a;
381
382 fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> {
383 self.inner.inner_fill_buf()
384 } 368 }
385 369
386 fn consume(&mut self, amt: usize) { 370 fn consume(&mut self, amt: usize) {
@@ -389,38 +373,22 @@ impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRea
389} 373}
390 374
391impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for BufferedUarte<'d, U, T> { 375impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for BufferedUarte<'d, U, T> {
392 type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 376 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
393 where 377 self.inner_write(buf).await
394 Self: 'a;
395
396 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
397 self.inner_write(buf)
398 } 378 }
399 379
400 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a 380 async fn flush(&mut self) -> Result<(), Self::Error> {
401 where 381 self.inner_flush().await
402 Self: 'a;
403
404 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
405 self.inner_flush()
406 } 382 }
407} 383}
408 384
409impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for BufferedUarteTx<'u, 'd, U, T> { 385impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for BufferedUarteTx<'u, 'd, U, T> {
410 type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a 386 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
411 where 387 self.inner.inner_write(buf).await
412 Self: 'a;
413
414 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
415 self.inner.inner_write(buf)
416 } 388 }
417 389
418 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a 390 async fn flush(&mut self) -> Result<(), Self::Error> {
419 where 391 self.inner.inner_flush().await
420 Self: 'a;
421
422 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
423 self.inner.inner_flush()
424 } 392 }
425} 393}
426 394
diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs
index 7f7468a20..7467dbc60 100644
--- a/embassy-nrf/src/gpiote.rs
+++ b/embassy-nrf/src/gpiote.rs
@@ -473,71 +473,49 @@ mod eh1 {
473 473
474#[cfg(all(feature = "unstable-traits", feature = "nightly"))] 474#[cfg(all(feature = "unstable-traits", feature = "nightly"))]
475mod eha { 475mod eha {
476 use futures::FutureExt;
477
478 use super::*; 476 use super::*;
479 477
480 impl<'d, T: GpioPin> embedded_hal_async::digital::Wait for Input<'d, T> { 478 impl<'d, T: GpioPin> embedded_hal_async::digital::Wait for Input<'d, T> {
481 type WaitForHighFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 479 async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
482 480 Ok(self.wait_for_high().await)
483 fn wait_for_high<'a>(&'a mut self) -> Self::WaitForHighFuture<'a> {
484 self.wait_for_high().map(Ok)
485 } 481 }
486 482
487 type WaitForLowFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 483 async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
488 484 Ok(self.wait_for_low().await)
489 fn wait_for_low<'a>(&'a mut self) -> Self::WaitForLowFuture<'a> {
490 self.wait_for_low().map(Ok)
491 } 485 }
492 486
493 type WaitForRisingEdgeFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 487 async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
494 488 Ok(self.wait_for_rising_edge().await)
495 fn wait_for_rising_edge<'a>(&'a mut self) -> Self::WaitForRisingEdgeFuture<'a> {
496 self.wait_for_rising_edge().map(Ok)
497 } 489 }
498 490
499 type WaitForFallingEdgeFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 491 async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
500 492 Ok(self.wait_for_falling_edge().await)
501 fn wait_for_falling_edge<'a>(&'a mut self) -> Self::WaitForFallingEdgeFuture<'a> {
502 self.wait_for_falling_edge().map(Ok)
503 } 493 }
504 494
505 type WaitForAnyEdgeFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 495 async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
506 496 Ok(self.wait_for_any_edge().await)
507 fn wait_for_any_edge<'a>(&'a mut self) -> Self::WaitForAnyEdgeFuture<'a> {
508 self.wait_for_any_edge().map(Ok)
509 } 497 }
510 } 498 }
511 499
512 impl<'d, T: GpioPin> embedded_hal_async::digital::Wait for Flex<'d, T> { 500 impl<'d, T: GpioPin> embedded_hal_async::digital::Wait for Flex<'d, T> {
513 type WaitForHighFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 501 async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
514 502 Ok(self.wait_for_high().await)
515 fn wait_for_high<'a>(&'a mut self) -> Self::WaitForHighFuture<'a> {
516 self.wait_for_high().map(Ok)
517 } 503 }
518 504
519 type WaitForLowFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 505 async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
520 506 Ok(self.wait_for_low().await)
521 fn wait_for_low<'a>(&'a mut self) -> Self::WaitForLowFuture<'a> {
522 self.wait_for_low().map(Ok)
523 } 507 }
524 508
525 type WaitForRisingEdgeFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 509 async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
526 510 Ok(self.wait_for_rising_edge().await)
527 fn wait_for_rising_edge<'a>(&'a mut self) -> Self::WaitForRisingEdgeFuture<'a> {
528 self.wait_for_rising_edge().map(Ok)
529 } 511 }
530 512
531 type WaitForFallingEdgeFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 513 async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
532 514 Ok(self.wait_for_falling_edge().await)
533 fn wait_for_falling_edge<'a>(&'a mut self) -> Self::WaitForFallingEdgeFuture<'a> {
534 self.wait_for_falling_edge().map(Ok)
535 } 515 }
536 516
537 type WaitForAnyEdgeFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 517 async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
538 518 Ok(self.wait_for_any_edge().await)
539 fn wait_for_any_edge<'a>(&'a mut self) -> Self::WaitForAnyEdgeFuture<'a> {
540 self.wait_for_any_edge().map(Ok)
541 } 519 }
542 } 520 }
543} 521}
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs
index dc6f16867..9054bc300 100644
--- a/embassy-nrf/src/lib.rs
+++ b/embassy-nrf/src/lib.rs
@@ -43,7 +43,11 @@
43//! mutable slices always reside in RAM. 43//! mutable slices always reside in RAM.
44 44
45#![no_std] 45#![no_std]
46#![cfg_attr(feature = "nightly", feature(type_alias_impl_trait))] 46#![cfg_attr(
47 feature = "nightly",
48 feature(type_alias_impl_trait, async_fn_in_trait, impl_trait_projections)
49)]
50#![cfg_attr(feature = "nightly", allow(incomplete_features))]
47 51
48#[cfg(not(any( 52#[cfg(not(any(
49 feature = "nrf51", 53 feature = "nrf51",
diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs
index d821d2353..7bb4e39f7 100644
--- a/embassy-nrf/src/spim.rs
+++ b/embassy-nrf/src/spim.rs
@@ -477,45 +477,34 @@ mod eh1 {
477 477
478#[cfg(all(feature = "unstable-traits", feature = "nightly"))] 478#[cfg(all(feature = "unstable-traits", feature = "nightly"))]
479mod eha { 479mod eha {
480 use core::future::Future;
481 480
482 use super::*; 481 use super::*;
483 482
484 impl<'d, T: Instance> embedded_hal_async::spi::SpiBusFlush for Spim<'d, T> { 483 impl<'d, T: Instance> embedded_hal_async::spi::SpiBusFlush for Spim<'d, T> {
485 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 484 async fn flush(&mut self) -> Result<(), Error> {
486 485 Ok(())
487 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
488 async move { Ok(()) }
489 } 486 }
490 } 487 }
491 488
492 impl<'d, T: Instance> embedded_hal_async::spi::SpiBusRead<u8> for Spim<'d, T> { 489 impl<'d, T: Instance> embedded_hal_async::spi::SpiBusRead<u8> for Spim<'d, T> {
493 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 490 async fn read(&mut self, words: &mut [u8]) -> Result<(), Error> {
494 491 self.read(words).await
495 fn read<'a>(&'a mut self, words: &'a mut [u8]) -> Self::ReadFuture<'a> {
496 self.read(words)
497 } 492 }
498 } 493 }
499 494
500 impl<'d, T: Instance> embedded_hal_async::spi::SpiBusWrite<u8> for Spim<'d, T> { 495 impl<'d, T: Instance> embedded_hal_async::spi::SpiBusWrite<u8> for Spim<'d, T> {
501 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 496 async fn write(&mut self, data: &[u8]) -> Result<(), Error> {
502 497 self.write(data).await
503 fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
504 self.write(data)
505 } 498 }
506 } 499 }
507 500
508 impl<'d, T: Instance> embedded_hal_async::spi::SpiBus<u8> for Spim<'d, T> { 501 impl<'d, T: Instance> embedded_hal_async::spi::SpiBus<u8> for Spim<'d, T> {
509 type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 502 async fn transfer(&mut self, rx: &mut [u8], tx: &[u8]) -> Result<(), Error> {
510 503 self.transfer(rx, tx).await
511 fn transfer<'a>(&'a mut self, rx: &'a mut [u8], tx: &'a [u8]) -> Self::TransferFuture<'a> {
512 self.transfer(rx, tx)
513 } 504 }
514 505
515 type TransferInPlaceFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 506 async fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<(), Error> {
516 507 self.transfer_in_place(words).await
517 fn transfer_in_place<'a>(&'a mut self, words: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> {
518 self.transfer_in_place(words)
519 } 508 }
520 } 509 }
521} 510}
diff --git a/embassy-nrf/src/twim.rs b/embassy-nrf/src/twim.rs
index 8d6171fac..4eafd18c2 100644
--- a/embassy-nrf/src/twim.rs
+++ b/embassy-nrf/src/twim.rs
@@ -841,39 +841,31 @@ mod eh1 {
841mod eha { 841mod eha {
842 use super::*; 842 use super::*;
843 impl<'d, T: Instance> embedded_hal_async::i2c::I2c for Twim<'d, T> { 843 impl<'d, T: Instance> embedded_hal_async::i2c::I2c for Twim<'d, T> {
844 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 844 async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), Error> {
845 845 self.read(address, buffer).await
846 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
847 self.read(address, buffer)
848 } 846 }
849 847
850 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 848 async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), Error> {
851 849 self.write(address, bytes).await
852 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
853 self.write(address, bytes)
854 } 850 }
855 851
856 type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 852 async fn write_read<'a>(
857
858 fn write_read<'a>(
859 &'a mut self, 853 &'a mut self,
860 address: u8, 854 address: u8,
861 wr_buffer: &'a [u8], 855 wr_buffer: &'a [u8],
862 rd_buffer: &'a mut [u8], 856 rd_buffer: &'a mut [u8],
863 ) -> Self::WriteReadFuture<'a> { 857 ) -> Result<(), Error> {
864 self.write_read(address, wr_buffer, rd_buffer) 858 self.write_read(address, wr_buffer, rd_buffer).await
865 } 859 }
866 860
867 type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; 861 async fn transaction<'a, 'b>(
868
869 fn transaction<'a, 'b>(
870 &'a mut self, 862 &'a mut self,
871 address: u8, 863 address: u8,
872 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], 864 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
873 ) -> Self::TransactionFuture<'a, 'b> { 865 ) -> Result<(), Error> {
874 let _ = address; 866 let _ = address;
875 let _ = operations; 867 let _ = operations;
876 async move { todo!() } 868 todo!()
877 } 869 }
878 } 870 }
879} 871}
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs
index 636d6c7a3..63df1b682 100644
--- a/embassy-nrf/src/uarte.rs
+++ b/embassy-nrf/src/uarte.rs
@@ -986,7 +986,7 @@ mod eha {
986 986
987 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 987 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
988 988
989 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { 989 fn flush(&mut self) -> Result<(), Self::Error> {
990 async move { Ok(()) } 990 async move { Ok(()) }
991 } 991 }
992 } 992 }
@@ -1000,7 +1000,7 @@ mod eha {
1000 1000
1001 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 1001 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
1002 1002
1003 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { 1003 fn flush(&mut self) -> Result<(), Self::Error> {
1004 async move { Ok(()) } 1004 async move { Ok(()) }
1005 } 1005 }
1006 } 1006 }
@@ -1012,4 +1012,26 @@ mod eha {
1012 self.read(buffer) 1012 self.read(buffer)
1013 } 1013 }
1014 } 1014 }
1015
1016 impl<'d, U: Instance, T: TimerInstance> embedded_hal_async::serial::Read for UarteWithIdle<'d, U, T> {
1017 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
1018
1019 fn read<'a>(&'a mut self, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
1020 self.read(buffer)
1021 }
1022 }
1023
1024 impl<'d, U: Instance, T: TimerInstance> embedded_hal_async::serial::Write for UarteWithIdle<'d, U, T> {
1025 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
1026
1027 fn write<'a>(&'a mut self, buffer: &'a [u8]) -> Self::WriteFuture<'a> {
1028 self.write(buffer)
1029 }
1030
1031 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
1032
1033 fn flush(&mut self) -> Result<(), Self::Error> {
1034 async move { Ok(()) }
1035 }
1036 }
1015} 1037}
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs
index eb1472fa5..ed4d5cf35 100644
--- a/embassy-nrf/src/usb.rs
+++ b/embassy-nrf/src/usb.rs
@@ -1,6 +1,6 @@
1#![macro_use] 1#![macro_use]
2 2
3use core::future::{poll_fn, Future}; 3use core::future::poll_fn;
4use core::marker::PhantomData; 4use core::marker::PhantomData;
5use core::mem::MaybeUninit; 5use core::mem::MaybeUninit;
6use core::sync::atomic::{compiler_fence, AtomicBool, AtomicU32, Ordering}; 6use core::sync::atomic::{compiler_fence, AtomicBool, AtomicU32, Ordering};
@@ -28,11 +28,7 @@ static READY_ENDPOINTS: AtomicU32 = AtomicU32::new(0);
28/// here provides a hook into determining whether it is. 28/// here provides a hook into determining whether it is.
29pub trait UsbSupply { 29pub trait UsbSupply {
30 fn is_usb_detected(&self) -> bool; 30 fn is_usb_detected(&self) -> bool;
31 31 async fn wait_power_ready(&mut self) -> Result<(), ()>;
32 type UsbPowerReadyFuture<'a>: Future<Output = Result<(), ()>> + 'a
33 where
34 Self: 'a;
35 fn wait_power_ready(&mut self) -> Self::UsbPowerReadyFuture<'_>;
36} 32}
37 33
38pub struct Driver<'d, T: Instance, P: UsbSupply> { 34pub struct Driver<'d, T: Instance, P: UsbSupply> {
@@ -102,8 +98,7 @@ impl UsbSupply for PowerUsb {
102 regs.usbregstatus.read().vbusdetect().is_vbus_present() 98 regs.usbregstatus.read().vbusdetect().is_vbus_present()
103 } 99 }
104 100
105 type UsbPowerReadyFuture<'a> = impl Future<Output = Result<(), ()>> + 'a where Self: 'a; 101 async fn wait_power_ready(&mut self) -> Result<(), ()> {
106 fn wait_power_ready(&mut self) -> Self::UsbPowerReadyFuture<'_> {
107 poll_fn(move |cx| { 102 poll_fn(move |cx| {
108 POWER_WAKER.register(cx.waker()); 103 POWER_WAKER.register(cx.waker());
109 let regs = unsafe { &*pac::POWER::ptr() }; 104 let regs = unsafe { &*pac::POWER::ptr() };
@@ -116,6 +111,7 @@ impl UsbSupply for PowerUsb {
116 Poll::Pending 111 Poll::Pending
117 } 112 }
118 }) 113 })
114 .await
119 } 115 }
120} 116}
121 117
@@ -147,8 +143,7 @@ impl UsbSupply for &SignalledSupply {
147 self.usb_detected.load(Ordering::Relaxed) 143 self.usb_detected.load(Ordering::Relaxed)
148 } 144 }
149 145
150 type UsbPowerReadyFuture<'a> = impl Future<Output = Result<(), ()>> + 'a where Self: 'a; 146 async fn wait_power_ready(&mut self) -> Result<(), ()> {
151 fn wait_power_ready(&mut self) -> Self::UsbPowerReadyFuture<'_> {
152 poll_fn(move |cx| { 147 poll_fn(move |cx| {
153 POWER_WAKER.register(cx.waker()); 148 POWER_WAKER.register(cx.waker());
154 149
@@ -160,6 +155,7 @@ impl UsbSupply for &SignalledSupply {
160 Poll::Pending 155 Poll::Pending
161 } 156 }
162 }) 157 })
158 .await
163 } 159 }
164} 160}
165 161
@@ -289,61 +285,52 @@ pub struct Bus<'d, T: Instance, P: UsbSupply> {
289} 285}
290 286
291impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> { 287impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> {
292 type EnableFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a; 288 async fn enable(&mut self) {
293 type DisableFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a; 289 let regs = T::regs();
294 type PollFuture<'a> = impl Future<Output = Event> + 'a where Self: 'a;
295 type RemoteWakeupFuture<'a> = impl Future<Output = Result<(), Unsupported>> + 'a where Self: 'a;
296
297 fn enable(&mut self) -> Self::EnableFuture<'_> {
298 async move {
299 let regs = T::regs();
300
301 errata::pre_enable();
302
303 regs.enable.write(|w| w.enable().enabled());
304
305 // Wait until the peripheral is ready.
306 regs.intenset.write(|w| w.usbevent().set_bit());
307 poll_fn(|cx| {
308 BUS_WAKER.register(cx.waker());
309 if regs.eventcause.read().ready().is_ready() {
310 Poll::Ready(())
311 } else {
312 Poll::Pending
313 }
314 })
315 .await;
316 regs.eventcause.write(|w| w.ready().clear_bit_by_one());
317
318 errata::post_enable();
319 290
320 unsafe { NVIC::unmask(pac::Interrupt::USBD) }; 291 errata::pre_enable();
321 292
322 regs.intenset.write(|w| { 293 regs.enable.write(|w| w.enable().enabled());
323 w.usbreset().set_bit();
324 w.usbevent().set_bit();
325 w.epdata().set_bit();
326 w
327 });
328 294
329 if self.usb_supply.wait_power_ready().await.is_ok() { 295 // Wait until the peripheral is ready.
330 // Enable the USB pullup, allowing enumeration. 296 regs.intenset.write(|w| w.usbevent().set_bit());
331 regs.usbpullup.write(|w| w.connect().enabled()); 297 poll_fn(|cx| {
332 trace!("enabled"); 298 BUS_WAKER.register(cx.waker());
299 if regs.eventcause.read().ready().is_ready() {
300 Poll::Ready(())
333 } else { 301 } else {
334 trace!("usb power not ready due to usb removal"); 302 Poll::Pending
335 } 303 }
304 })
305 .await;
306 regs.eventcause.write(|w| w.ready().clear_bit_by_one());
307
308 errata::post_enable();
309
310 unsafe { NVIC::unmask(pac::Interrupt::USBD) };
311
312 regs.intenset.write(|w| {
313 w.usbreset().set_bit();
314 w.usbevent().set_bit();
315 w.epdata().set_bit();
316 w
317 });
318
319 if self.usb_supply.wait_power_ready().await.is_ok() {
320 // Enable the USB pullup, allowing enumeration.
321 regs.usbpullup.write(|w| w.connect().enabled());
322 trace!("enabled");
323 } else {
324 trace!("usb power not ready due to usb removal");
336 } 325 }
337 } 326 }
338 327
339 fn disable(&mut self) -> Self::DisableFuture<'_> { 328 async fn disable(&mut self) {
340 async move { 329 let regs = T::regs();
341 let regs = T::regs(); 330 regs.enable.write(|x| x.enable().disabled());
342 regs.enable.write(|x| x.enable().disabled());
343 }
344 } 331 }
345 332
346 fn poll<'a>(&'a mut self) -> Self::PollFuture<'a> { 333 async fn poll(&mut self) -> Event {
347 poll_fn(move |cx| { 334 poll_fn(move |cx| {
348 BUS_WAKER.register(cx.waker()); 335 BUS_WAKER.register(cx.waker());
349 let regs = T::regs(); 336 let regs = T::regs();
@@ -401,6 +388,7 @@ impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> {
401 388
402 Poll::Pending 389 Poll::Pending
403 }) 390 })
391 .await
404 } 392 }
405 393
406 #[inline] 394 #[inline]
@@ -493,42 +481,40 @@ impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> {
493 } 481 }
494 482
495 #[inline] 483 #[inline]
496 fn remote_wakeup(&mut self) -> Self::RemoteWakeupFuture<'_> { 484 async fn remote_wakeup(&mut self) -> Result<(), Unsupported> {
497 async move { 485 let regs = T::regs();
498 let regs = T::regs();
499
500 if regs.lowpower.read().lowpower().is_low_power() {
501 errata::pre_wakeup();
502 486
503 regs.lowpower.write(|w| w.lowpower().force_normal()); 487 if regs.lowpower.read().lowpower().is_low_power() {
488 errata::pre_wakeup();
504 489
505 poll_fn(|cx| { 490 regs.lowpower.write(|w| w.lowpower().force_normal());
506 BUS_WAKER.register(cx.waker());
507 let regs = T::regs();
508 let r = regs.eventcause.read();
509 491
510 if regs.events_usbreset.read().bits() != 0 { 492 poll_fn(|cx| {
511 Poll::Ready(()) 493 BUS_WAKER.register(cx.waker());
512 } else if r.resume().bit() { 494 let regs = T::regs();
513 Poll::Ready(()) 495 let r = regs.eventcause.read();
514 } else if r.usbwuallowed().bit() {
515 regs.eventcause.write(|w| w.usbwuallowed().allowed());
516 496
517 regs.dpdmvalue.write(|w| w.state().resume()); 497 if regs.events_usbreset.read().bits() != 0 {
518 regs.tasks_dpdmdrive.write(|w| w.tasks_dpdmdrive().set_bit()); 498 Poll::Ready(())
499 } else if r.resume().bit() {
500 Poll::Ready(())
501 } else if r.usbwuallowed().bit() {
502 regs.eventcause.write(|w| w.usbwuallowed().allowed());
519 503
520 Poll::Ready(()) 504 regs.dpdmvalue.write(|w| w.state().resume());
521 } else { 505 regs.tasks_dpdmdrive.write(|w| w.tasks_dpdmdrive().set_bit());
522 Poll::Pending
523 }
524 })
525 .await;
526 506
527 errata::post_wakeup(); 507 Poll::Ready(())
528 } 508 } else {
509 Poll::Pending
510 }
511 })
512 .await;
529 513
530 Ok(()) 514 errata::post_wakeup();
531 } 515 }
516
517 Ok(())
532 } 518 }
533} 519}
534 520
@@ -594,9 +580,7 @@ impl<'d, T: Instance, Dir: EndpointDir> driver::Endpoint for Endpoint<'d, T, Dir
594 &self.info 580 &self.info
595 } 581 }
596 582
597 type WaitEnabledFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a; 583 async fn wait_enabled(&mut self) {
598
599 fn wait_enabled(&mut self) -> Self::WaitEnabledFuture<'_> {
600 let i = self.info.addr.index(); 584 let i = self.info.addr.index();
601 assert!(i != 0); 585 assert!(i != 0);
602 586
@@ -608,6 +592,7 @@ impl<'d, T: Instance, Dir: EndpointDir> driver::Endpoint for Endpoint<'d, T, Dir
608 Poll::Pending 592 Poll::Pending
609 } 593 }
610 }) 594 })
595 .await
611 } 596 }
612} 597}
613 598
@@ -712,34 +697,26 @@ unsafe fn write_dma<T: Instance>(i: usize, buf: &[u8]) {
712} 697}
713 698
714impl<'d, T: Instance> driver::EndpointOut for Endpoint<'d, T, Out> { 699impl<'d, T: Instance> driver::EndpointOut for Endpoint<'d, T, Out> {
715 type ReadFuture<'a> = impl Future<Output = Result<usize, EndpointError>> + 'a where Self: 'a; 700 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, EndpointError> {
716 701 let i = self.info.addr.index();
717 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { 702 assert!(i != 0);
718 async move {
719 let i = self.info.addr.index();
720 assert!(i != 0);
721 703
722 self.wait_data_ready().await.map_err(|_| EndpointError::Disabled)?; 704 self.wait_data_ready().await.map_err(|_| EndpointError::Disabled)?;
723 705
724 unsafe { read_dma::<T>(i, buf) } 706 unsafe { read_dma::<T>(i, buf) }
725 }
726 } 707 }
727} 708}
728 709
729impl<'d, T: Instance> driver::EndpointIn for Endpoint<'d, T, In> { 710impl<'d, T: Instance> driver::EndpointIn for Endpoint<'d, T, In> {
730 type WriteFuture<'a> = impl Future<Output = Result<(), EndpointError>> + 'a where Self: 'a; 711 async fn write(&mut self, buf: &[u8]) -> Result<(), EndpointError> {
731 712 let i = self.info.addr.index();
732 fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { 713 assert!(i != 0);
733 async move {
734 let i = self.info.addr.index();
735 assert!(i != 0);
736 714
737 self.wait_data_ready().await.map_err(|_| EndpointError::Disabled)?; 715 self.wait_data_ready().await.map_err(|_| EndpointError::Disabled)?;
738 716
739 unsafe { write_dma::<T>(i, buf) } 717 unsafe { write_dma::<T>(i, buf) }
740 718
741 Ok(()) 719 Ok(())
742 }
743 } 720 }
744} 721}
745 722
@@ -749,136 +726,120 @@ pub struct ControlPipe<'d, T: Instance> {
749} 726}
750 727
751impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> { 728impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
752 type SetupFuture<'a> = impl Future<Output = [u8;8]> + 'a where Self: 'a;
753 type DataOutFuture<'a> = impl Future<Output = Result<usize, EndpointError>> + 'a where Self: 'a;
754 type DataInFuture<'a> = impl Future<Output = Result<(), EndpointError>> + 'a where Self: 'a;
755 type AcceptFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a;
756 type RejectFuture<'a> = impl Future<Output = ()> + 'a where Self: 'a;
757
758 fn max_packet_size(&self) -> usize { 729 fn max_packet_size(&self) -> usize {
759 usize::from(self.max_packet_size) 730 usize::from(self.max_packet_size)
760 } 731 }
761 732
762 fn setup<'a>(&'a mut self) -> Self::SetupFuture<'a> { 733 async fn setup(&mut self) -> [u8; 8] {
763 async move { 734 let regs = T::regs();
764 let regs = T::regs();
765 735
766 // Reset shorts 736 // Reset shorts
767 regs.shorts.write(|w| w); 737 regs.shorts.write(|w| w);
768 738
769 // Wait for SETUP packet 739 // Wait for SETUP packet
770 regs.intenset.write(|w| w.ep0setup().set()); 740 regs.intenset.write(|w| w.ep0setup().set());
771 poll_fn(|cx| { 741 poll_fn(|cx| {
772 EP0_WAKER.register(cx.waker()); 742 EP0_WAKER.register(cx.waker());
773 let regs = T::regs(); 743 let regs = T::regs();
774 if regs.events_ep0setup.read().bits() != 0 { 744 if regs.events_ep0setup.read().bits() != 0 {
775 Poll::Ready(()) 745 Poll::Ready(())
776 } else { 746 } else {
777 Poll::Pending 747 Poll::Pending
778 } 748 }
779 }) 749 })
780 .await; 750 .await;
781 751
782 regs.events_ep0setup.reset(); 752 regs.events_ep0setup.reset();
783 753
784 let mut buf = [0; 8]; 754 let mut buf = [0; 8];
785 buf[0] = regs.bmrequesttype.read().bits() as u8; 755 buf[0] = regs.bmrequesttype.read().bits() as u8;
786 buf[1] = regs.brequest.read().brequest().bits(); 756 buf[1] = regs.brequest.read().brequest().bits();
787 buf[2] = regs.wvaluel.read().wvaluel().bits(); 757 buf[2] = regs.wvaluel.read().wvaluel().bits();
788 buf[3] = regs.wvalueh.read().wvalueh().bits(); 758 buf[3] = regs.wvalueh.read().wvalueh().bits();
789 buf[4] = regs.windexl.read().windexl().bits(); 759 buf[4] = regs.windexl.read().windexl().bits();
790 buf[5] = regs.windexh.read().windexh().bits(); 760 buf[5] = regs.windexh.read().windexh().bits();
791 buf[6] = regs.wlengthl.read().wlengthl().bits(); 761 buf[6] = regs.wlengthl.read().wlengthl().bits();
792 buf[7] = regs.wlengthh.read().wlengthh().bits(); 762 buf[7] = regs.wlengthh.read().wlengthh().bits();
793 763
794 buf 764 buf
795 }
796 } 765 }
797 766
798 fn data_out<'a>(&'a mut self, buf: &'a mut [u8], _first: bool, _last: bool) -> Self::DataOutFuture<'a> { 767 async fn data_out(&mut self, buf: &mut [u8], _first: bool, _last: bool) -> Result<usize, EndpointError> {
799 async move { 768 let regs = T::regs();
800 let regs = T::regs();
801 769
802 regs.events_ep0datadone.reset(); 770 regs.events_ep0datadone.reset();
803 771
804 // This starts a RX on EP0. events_ep0datadone notifies when done. 772 // This starts a RX on EP0. events_ep0datadone notifies when done.
805 regs.tasks_ep0rcvout.write(|w| w.tasks_ep0rcvout().set_bit()); 773 regs.tasks_ep0rcvout.write(|w| w.tasks_ep0rcvout().set_bit());
806 774
807 // Wait until ready 775 // Wait until ready
808 regs.intenset.write(|w| { 776 regs.intenset.write(|w| {
809 w.usbreset().set(); 777 w.usbreset().set();
810 w.ep0setup().set(); 778 w.ep0setup().set();
811 w.ep0datadone().set() 779 w.ep0datadone().set()
812 }); 780 });
813 poll_fn(|cx| { 781 poll_fn(|cx| {
814 EP0_WAKER.register(cx.waker()); 782 EP0_WAKER.register(cx.waker());
815 let regs = T::regs(); 783 let regs = T::regs();
816 if regs.events_ep0datadone.read().bits() != 0 { 784 if regs.events_ep0datadone.read().bits() != 0 {
817 Poll::Ready(Ok(())) 785 Poll::Ready(Ok(()))
818 } else if regs.events_usbreset.read().bits() != 0 { 786 } else if regs.events_usbreset.read().bits() != 0 {
819 trace!("aborted control data_out: usb reset"); 787 trace!("aborted control data_out: usb reset");
820 Poll::Ready(Err(EndpointError::Disabled)) 788 Poll::Ready(Err(EndpointError::Disabled))
821 } else if regs.events_ep0setup.read().bits() != 0 { 789 } else if regs.events_ep0setup.read().bits() != 0 {
822 trace!("aborted control data_out: received another SETUP"); 790 trace!("aborted control data_out: received another SETUP");
823 Poll::Ready(Err(EndpointError::Disabled)) 791 Poll::Ready(Err(EndpointError::Disabled))
824 } else { 792 } else {
825 Poll::Pending 793 Poll::Pending
826 } 794 }
827 }) 795 })
828 .await?; 796 .await?;
829 797
830 unsafe { read_dma::<T>(0, buf) } 798 unsafe { read_dma::<T>(0, buf) }
831 }
832 } 799 }
833 800
834 fn data_in<'a>(&'a mut self, buf: &'a [u8], _first: bool, last: bool) -> Self::DataInFuture<'a> { 801 async fn data_in(&mut self, buf: &[u8], _first: bool, last: bool) -> Result<(), EndpointError> {
835 async move { 802 let regs = T::regs();
836 let regs = T::regs(); 803 regs.events_ep0datadone.reset();
837 regs.events_ep0datadone.reset();
838 804
839 regs.shorts.write(|w| w.ep0datadone_ep0status().bit(last)); 805 regs.shorts.write(|w| w.ep0datadone_ep0status().bit(last));
840 806
841 // This starts a TX on EP0. events_ep0datadone notifies when done. 807 // This starts a TX on EP0. events_ep0datadone notifies when done.
842 unsafe { write_dma::<T>(0, buf) } 808 unsafe { write_dma::<T>(0, buf) }
843 809
844 regs.intenset.write(|w| { 810 regs.intenset.write(|w| {
845 w.usbreset().set(); 811 w.usbreset().set();
846 w.ep0setup().set(); 812 w.ep0setup().set();
847 w.ep0datadone().set() 813 w.ep0datadone().set()
848 }); 814 });
849 815
850 poll_fn(|cx| { 816 poll_fn(|cx| {
851 cx.waker().wake_by_ref(); 817 cx.waker().wake_by_ref();
852 EP0_WAKER.register(cx.waker()); 818 EP0_WAKER.register(cx.waker());
853 let regs = T::regs(); 819 let regs = T::regs();
854 if regs.events_ep0datadone.read().bits() != 0 { 820 if regs.events_ep0datadone.read().bits() != 0 {
855 Poll::Ready(Ok(())) 821 Poll::Ready(Ok(()))
856 } else if regs.events_usbreset.read().bits() != 0 { 822 } else if regs.events_usbreset.read().bits() != 0 {
857 trace!("aborted control data_in: usb reset"); 823 trace!("aborted control data_in: usb reset");
858 Poll::Ready(Err(EndpointError::Disabled)) 824 Poll::Ready(Err(EndpointError::Disabled))
859 } else if regs.events_ep0setup.read().bits() != 0 { 825 } else if regs.events_ep0setup.read().bits() != 0 {
860 trace!("aborted control data_in: received another SETUP"); 826 trace!("aborted control data_in: received another SETUP");
861 Poll::Ready(Err(EndpointError::Disabled)) 827 Poll::Ready(Err(EndpointError::Disabled))
862 } else { 828 } else {
863 Poll::Pending 829 Poll::Pending
864 } 830 }
865 }) 831 })
866 .await 832 .await
867 }
868 } 833 }
869 834
870 fn accept<'a>(&'a mut self) -> Self::AcceptFuture<'a> { 835 async fn accept(&mut self) {
871 async move { 836 let regs = T::regs();
872 let regs = T::regs(); 837 regs.tasks_ep0status.write(|w| w.tasks_ep0status().bit(true));
873 regs.tasks_ep0status.write(|w| w.tasks_ep0status().bit(true));
874 }
875 } 838 }
876 839
877 fn reject<'a>(&'a mut self) -> Self::RejectFuture<'a> { 840 async fn reject(&mut self) {
878 async move { 841 let regs = T::regs();
879 let regs = T::regs(); 842 regs.tasks_ep0stall.write(|w| w.tasks_ep0stall().bit(true));
880 regs.tasks_ep0stall.write(|w| w.tasks_ep0stall().bit(true));
881 }
882 } 843 }
883} 844}
884 845