diff options
| author | alexmoon <[email protected]> | 2022-04-02 16:35:03 -0400 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-04-06 05:38:11 +0200 |
| commit | 2ce435dc341c0238392df5dab5db9b80db167117 (patch) | |
| tree | 19e64218922a67601540261749af81d2d1d0aac4 /examples/nrf/src | |
| parent | 99f95a33c30b08359fcd72123fea01f4de0903ec (diff) | |
Add basic device state handling for endpoints.
Diffstat (limited to 'examples/nrf/src')
| -rw-r--r-- | examples/nrf/src/bin/usb_serial.rs | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/examples/nrf/src/bin/usb_serial.rs b/examples/nrf/src/bin/usb_serial.rs index cd681c5ce..9437e835f 100644 --- a/examples/nrf/src/bin/usb_serial.rs +++ b/examples/nrf/src/bin/usb_serial.rs | |||
| @@ -4,12 +4,13 @@ | |||
| 4 | #![feature(type_alias_impl_trait)] | 4 | #![feature(type_alias_impl_trait)] |
| 5 | 5 | ||
| 6 | use core::mem; | 6 | use core::mem; |
| 7 | use defmt::*; | 7 | use defmt::{info, panic}; |
| 8 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 9 | use embassy_nrf::interrupt; | 9 | use embassy_nrf::interrupt; |
| 10 | use embassy_nrf::pac; | 10 | use embassy_nrf::pac; |
| 11 | use embassy_nrf::usb::Driver; | 11 | use embassy_nrf::usb::{Driver, Instance}; |
| 12 | use embassy_nrf::Peripherals; | 12 | use embassy_nrf::Peripherals; |
| 13 | use embassy_usb::driver::{ReadError, WriteError}; | ||
| 13 | use embassy_usb::{Config, UsbDeviceBuilder}; | 14 | use embassy_usb::{Config, UsbDeviceBuilder}; |
| 14 | use embassy_usb_serial::{CdcAcmClass, State}; | 15 | use embassy_usb_serial::{CdcAcmClass, State}; |
| 15 | use futures::future::join; | 16 | use futures::future::join; |
| @@ -66,12 +67,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 66 | 67 | ||
| 67 | // Do stuff with the class! | 68 | // Do stuff with the class! |
| 68 | let echo_fut = async { | 69 | let echo_fut = async { |
| 69 | let mut buf = [0; 64]; | ||
| 70 | loop { | 70 | loop { |
| 71 | let n = class.read_packet(&mut buf).await.unwrap(); | 71 | class.wait_connection().await; |
| 72 | let data = &buf[..n]; | 72 | info!("Connected"); |
| 73 | info!("data: {:x}", data); | 73 | let _ = echo(&mut class).await; |
| 74 | class.write_packet(data).await.unwrap(); | 74 | info!("Disconnected"); |
| 75 | } | 75 | } |
| 76 | }; | 76 | }; |
| 77 | 77 | ||
| @@ -79,3 +79,35 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 79 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | 79 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. |
| 80 | join(usb_fut, echo_fut).await; | 80 | join(usb_fut, echo_fut).await; |
| 81 | } | 81 | } |
| 82 | |||
| 83 | struct Disconnected {} | ||
| 84 | |||
| 85 | impl From<ReadError> for Disconnected { | ||
| 86 | fn from(val: ReadError) -> Self { | ||
| 87 | match val { | ||
| 88 | ReadError::BufferOverflow => panic!("Buffer overflow"), | ||
| 89 | ReadError::Disabled => Disconnected {}, | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | impl From<WriteError> for Disconnected { | ||
| 95 | fn from(val: WriteError) -> Self { | ||
| 96 | match val { | ||
| 97 | WriteError::BufferOverflow => panic!("Buffer overflow"), | ||
| 98 | WriteError::Disabled => Disconnected {}, | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | async fn echo<'d, T: Instance + 'd>( | ||
| 104 | class: &mut CdcAcmClass<'d, Driver<'d, T>>, | ||
| 105 | ) -> Result<(), Disconnected> { | ||
| 106 | let mut buf = [0; 64]; | ||
| 107 | loop { | ||
| 108 | let n = class.read_packet(&mut buf).await?; | ||
| 109 | let data = &buf[..n]; | ||
| 110 | info!("data: {:x}", data); | ||
| 111 | class.write_packet(data).await?; | ||
| 112 | } | ||
| 113 | } | ||
