diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f1/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f1/src/bin/usb_serial.rs | 117 | ||||
| -rw-r--r-- | examples/stm32f3/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f3/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/stm32f3/src/bin/usb_serial.rs | 116 | ||||
| -rw-r--r-- | examples/stm32l5/.cargo/config.toml | 6 | ||||
| -rw-r--r-- | examples/stm32l5/Cargo.toml | 30 | ||||
| -rw-r--r-- | examples/stm32l5/build.rs | 5 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/button_exti.rs | 28 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/rng.rs | 34 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/usb_ethernet.rs | 290 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/usb_hid_mouse.rs | 136 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/usb_serial.rs | 112 |
13 files changed, 881 insertions, 2 deletions
diff --git a/examples/stm32f1/Cargo.toml b/examples/stm32f1/Cargo.toml index e09e17fcd..8de736d6b 100644 --- a/examples/stm32f1/Cargo.toml +++ b/examples/stm32f1/Cargo.toml | |||
| @@ -8,6 +8,8 @@ resolver = "2" | |||
| 8 | [dependencies] | 8 | [dependencies] |
| 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "time-tick-32768hz"] } | 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "time-tick-32768hz"] } |
| 10 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f103c8", "unstable-pac", "memory-x", "time-driver-any"] } | 10 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f103c8", "unstable-pac", "memory-x", "time-driver-any"] } |
| 11 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 12 | embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"] } | ||
| 11 | 13 | ||
| 12 | defmt = "0.3" | 14 | defmt = "0.3" |
| 13 | defmt-rtt = "0.3" | 15 | defmt-rtt = "0.3" |
diff --git a/examples/stm32f1/src/bin/usb_serial.rs b/examples/stm32f1/src/bin/usb_serial.rs new file mode 100644 index 000000000..fe4aa4cc9 --- /dev/null +++ b/examples/stm32f1/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,117 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::panic; | ||
| 6 | use defmt::*; | ||
| 7 | use defmt_rtt as _; // global logger | ||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy::time::Duration; | ||
| 10 | use embassy::time::Timer; | ||
| 11 | use embassy_stm32::gpio::Level; | ||
| 12 | use embassy_stm32::gpio::Output; | ||
| 13 | use embassy_stm32::gpio::Speed; | ||
| 14 | use embassy_stm32::interrupt; | ||
| 15 | use embassy_stm32::time::Hertz; | ||
| 16 | use embassy_stm32::usb::{Driver, Instance}; | ||
| 17 | use embassy_stm32::{Config, Peripherals}; | ||
| 18 | use embassy_usb::driver::EndpointError; | ||
| 19 | use embassy_usb::Builder; | ||
| 20 | use embassy_usb_serial::{CdcAcmClass, State}; | ||
| 21 | use futures::future::join; | ||
| 22 | use panic_probe as _; | ||
| 23 | |||
| 24 | fn config() -> Config { | ||
| 25 | let mut config = Config::default(); | ||
| 26 | config.rcc.hse = Some(Hertz(8_000_000)); | ||
| 27 | config.rcc.sys_ck = Some(Hertz(48_000_000)); | ||
| 28 | config.rcc.pclk1 = Some(Hertz(24_000_000)); | ||
| 29 | config | ||
| 30 | } | ||
| 31 | |||
| 32 | #[embassy::main(config = "config()")] | ||
| 33 | async fn main(_spawner: Spawner, mut p: Peripherals) { | ||
| 34 | info!("Hello World!"); | ||
| 35 | |||
| 36 | { | ||
| 37 | // BluePill board has a pull-up resistor on the D+ line. | ||
| 38 | // Pull the D+ pin down to send a RESET condition to the USB bus. | ||
| 39 | // This forced reset is needed only for development, without it host | ||
| 40 | // will not reset your device when you upload new firmware. | ||
| 41 | let _dp = Output::new(&mut p.PA12, Level::Low, Speed::Low); | ||
| 42 | Timer::after(Duration::from_millis(10)).await; | ||
| 43 | } | ||
| 44 | |||
| 45 | // Create the driver, from the HAL. | ||
| 46 | let irq = interrupt::take!(USB_LP_CAN1_RX0); | ||
| 47 | let driver = Driver::new(p.USB, irq, p.PA12, p.PA11); | ||
| 48 | |||
| 49 | // Create embassy-usb Config | ||
| 50 | let config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 51 | //config.max_packet_size_0 = 64; | ||
| 52 | |||
| 53 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 54 | // It needs some buffers for building the descriptors. | ||
| 55 | let mut device_descriptor = [0; 256]; | ||
| 56 | let mut config_descriptor = [0; 256]; | ||
| 57 | let mut bos_descriptor = [0; 256]; | ||
| 58 | let mut control_buf = [0; 7]; | ||
| 59 | |||
| 60 | let mut state = State::new(); | ||
| 61 | |||
| 62 | let mut builder = Builder::new( | ||
| 63 | driver, | ||
| 64 | config, | ||
| 65 | &mut device_descriptor, | ||
| 66 | &mut config_descriptor, | ||
| 67 | &mut bos_descriptor, | ||
| 68 | &mut control_buf, | ||
| 69 | None, | ||
| 70 | ); | ||
| 71 | |||
| 72 | // Create classes on the builder. | ||
| 73 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 74 | |||
| 75 | // Build the builder. | ||
| 76 | let mut usb = builder.build(); | ||
| 77 | |||
| 78 | // Run the USB device. | ||
| 79 | let usb_fut = usb.run(); | ||
| 80 | |||
| 81 | // Do stuff with the class! | ||
| 82 | let echo_fut = async { | ||
| 83 | loop { | ||
| 84 | class.wait_connection().await; | ||
| 85 | info!("Connected"); | ||
| 86 | let _ = echo(&mut class).await; | ||
| 87 | info!("Disconnected"); | ||
| 88 | } | ||
| 89 | }; | ||
| 90 | |||
| 91 | // Run everything concurrently. | ||
| 92 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 93 | join(usb_fut, echo_fut).await; | ||
| 94 | } | ||
| 95 | |||
| 96 | struct Disconnected {} | ||
| 97 | |||
| 98 | impl From<EndpointError> for Disconnected { | ||
| 99 | fn from(val: EndpointError) -> Self { | ||
| 100 | match val { | ||
| 101 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 102 | EndpointError::Disabled => Disconnected {}, | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | async fn echo<'d, T: Instance + 'd>( | ||
| 108 | class: &mut CdcAcmClass<'d, Driver<'d, T>>, | ||
| 109 | ) -> Result<(), Disconnected> { | ||
| 110 | let mut buf = [0; 64]; | ||
| 111 | loop { | ||
| 112 | let n = class.read_packet(&mut buf).await?; | ||
| 113 | let data = &buf[..n]; | ||
| 114 | info!("data: {:x}", data); | ||
| 115 | class.write_packet(data).await?; | ||
| 116 | } | ||
| 117 | } | ||
diff --git a/examples/stm32f3/.cargo/config.toml b/examples/stm32f3/.cargo/config.toml index eb8a8b335..84b4b2f19 100644 --- a/examples/stm32f3/.cargo/config.toml +++ b/examples/stm32f3/.cargo/config.toml | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] |
| 2 | # replace STM32F429ZITx with your chip as listed in `probe-run --list-chips` | 2 | # replace STM32F429ZITx with your chip as listed in `probe-run --list-chips` |
| 3 | runner = "probe-run --chip STM32F303VCTx" | 3 | runner = "probe-run --chip STM32F303ZETx" |
| 4 | 4 | ||
| 5 | [build] | 5 | [build] |
| 6 | target = "thumbv7em-none-eabihf" | 6 | target = "thumbv7em-none-eabihf" |
diff --git a/examples/stm32f3/Cargo.toml b/examples/stm32f3/Cargo.toml index de81f1002..15128ecc9 100644 --- a/examples/stm32f3/Cargo.toml +++ b/examples/stm32f3/Cargo.toml | |||
| @@ -7,7 +7,10 @@ resolver = "2" | |||
| 7 | 7 | ||
| 8 | [dependencies] | 8 | [dependencies] |
| 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "time-tick-32768hz"] } | 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "time-tick-32768hz"] } |
| 10 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f303vc", "unstable-pac", "memory-x", "time-driver-any", "exti"] } | 10 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f303ze", "unstable-pac", "memory-x", "time-driver-any", "exti"] } |
| 11 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 12 | embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"] } | ||
| 13 | embassy-usb-hid = { version = "0.1.0", path = "../../embassy-usb-hid", features = ["defmt"] } | ||
| 11 | 14 | ||
| 12 | defmt = "0.3" | 15 | defmt = "0.3" |
| 13 | defmt-rtt = "0.3" | 16 | defmt-rtt = "0.3" |
diff --git a/examples/stm32f3/src/bin/usb_serial.rs b/examples/stm32f3/src/bin/usb_serial.rs new file mode 100644 index 000000000..fc33d0bc7 --- /dev/null +++ b/examples/stm32f3/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::panic; | ||
| 6 | use defmt::*; | ||
| 7 | use defmt_rtt as _; // global logger | ||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy::time::Duration; | ||
| 10 | use embassy::time::Timer; | ||
| 11 | use embassy_stm32::gpio::Level; | ||
| 12 | use embassy_stm32::gpio::Output; | ||
| 13 | use embassy_stm32::gpio::Speed; | ||
| 14 | use embassy_stm32::interrupt; | ||
| 15 | use embassy_stm32::time::U32Ext; | ||
| 16 | use embassy_stm32::usb::{Driver, Instance}; | ||
| 17 | use embassy_stm32::{Config, Peripherals}; | ||
| 18 | use embassy_usb::driver::EndpointError; | ||
| 19 | use embassy_usb::Builder; | ||
| 20 | use embassy_usb_serial::{CdcAcmClass, State}; | ||
| 21 | use futures::future::join; | ||
| 22 | use panic_probe as _; | ||
| 23 | |||
| 24 | fn config() -> Config { | ||
| 25 | let mut config = Config::default(); | ||
| 26 | |||
| 27 | config.rcc.hse = Some(8.mhz().into()); | ||
| 28 | config.rcc.sysclk = Some(48.mhz().into()); | ||
| 29 | config.rcc.pclk1 = Some(24.mhz().into()); | ||
| 30 | config.rcc.pclk2 = Some(24.mhz().into()); | ||
| 31 | config.rcc.pll48 = true; | ||
| 32 | |||
| 33 | config | ||
| 34 | } | ||
| 35 | |||
| 36 | #[embassy::main(config = "config()")] | ||
| 37 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 38 | info!("Hello World!"); | ||
| 39 | |||
| 40 | // Needed for nucleo-stm32f303ze | ||
| 41 | let mut dp_pullup = Output::new(p.PG6, Level::Low, Speed::Medium); | ||
| 42 | Timer::after(Duration::from_millis(10)).await; | ||
| 43 | dp_pullup.set_high(); | ||
| 44 | |||
| 45 | // Create the driver, from the HAL. | ||
| 46 | let irq = interrupt::take!(USB_LP_CAN_RX0); | ||
| 47 | let driver = Driver::new(p.USB, irq, p.PA12, p.PA11); | ||
| 48 | |||
| 49 | // Create embassy-usb Config | ||
| 50 | let config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 51 | |||
| 52 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 53 | // It needs some buffers for building the descriptors. | ||
| 54 | let mut device_descriptor = [0; 256]; | ||
| 55 | let mut config_descriptor = [0; 256]; | ||
| 56 | let mut bos_descriptor = [0; 256]; | ||
| 57 | let mut control_buf = [0; 7]; | ||
| 58 | |||
| 59 | let mut state = State::new(); | ||
| 60 | |||
| 61 | let mut builder = Builder::new( | ||
| 62 | driver, | ||
| 63 | config, | ||
| 64 | &mut device_descriptor, | ||
| 65 | &mut config_descriptor, | ||
| 66 | &mut bos_descriptor, | ||
| 67 | &mut control_buf, | ||
| 68 | None, | ||
| 69 | ); | ||
| 70 | |||
| 71 | // Create classes on the builder. | ||
| 72 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 73 | |||
| 74 | // Build the builder. | ||
| 75 | let mut usb = builder.build(); | ||
| 76 | |||
| 77 | // Run the USB device. | ||
| 78 | let usb_fut = usb.run(); | ||
| 79 | |||
| 80 | // Do stuff with the class! | ||
| 81 | let echo_fut = async { | ||
| 82 | loop { | ||
| 83 | class.wait_connection().await; | ||
| 84 | info!("Connected"); | ||
| 85 | let _ = echo(&mut class).await; | ||
| 86 | info!("Disconnected"); | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | // Run everything concurrently. | ||
| 91 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 92 | join(usb_fut, echo_fut).await; | ||
| 93 | } | ||
| 94 | |||
| 95 | struct Disconnected {} | ||
| 96 | |||
| 97 | impl From<EndpointError> for Disconnected { | ||
| 98 | fn from(val: EndpointError) -> Self { | ||
| 99 | match val { | ||
| 100 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 101 | EndpointError::Disabled => Disconnected {}, | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | async fn echo<'d, T: Instance + 'd>( | ||
| 107 | class: &mut CdcAcmClass<'d, Driver<'d, T>>, | ||
| 108 | ) -> Result<(), Disconnected> { | ||
| 109 | let mut buf = [0; 64]; | ||
| 110 | loop { | ||
| 111 | let n = class.read_packet(&mut buf).await?; | ||
| 112 | let data = &buf[..n]; | ||
| 113 | info!("data: {:x}", data); | ||
| 114 | class.write_packet(data).await?; | ||
| 115 | } | ||
| 116 | } | ||
diff --git a/examples/stm32l5/.cargo/config.toml b/examples/stm32l5/.cargo/config.toml new file mode 100644 index 000000000..e63fe37e2 --- /dev/null +++ b/examples/stm32l5/.cargo/config.toml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace STM32F429ZITx with your chip as listed in `probe-run --list-chips` | ||
| 3 | runner = "probe-run --chip STM32L552ZETxQ" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv8m.main-none-eabihf" | ||
diff --git a/examples/stm32l5/Cargo.toml b/examples/stm32l5/Cargo.toml new file mode 100644 index 000000000..7f60e26da --- /dev/null +++ b/examples/stm32l5/Cargo.toml | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Dario Nieuwenhuis <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-stm32l5-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | resolver = "2" | ||
| 7 | |||
| 8 | [features] | ||
| 9 | |||
| 10 | [dependencies] | ||
| 11 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "time-tick-32768hz"] } | ||
| 12 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "unstable-traits", "memory-x"] } | ||
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 14 | embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"] } | ||
| 15 | embassy-usb-hid = { version = "0.1.0", path = "../../embassy-usb-hid", features = ["defmt"] } | ||
| 16 | embassy-usb-ncm = { version = "0.1.0", path = "../../embassy-usb-ncm", features = ["defmt"] } | ||
| 17 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } | ||
| 18 | usbd-hid = "0.5.2" | ||
| 19 | |||
| 20 | defmt = "0.3" | ||
| 21 | defmt-rtt = "0.3" | ||
| 22 | panic-probe = { version = "0.3", features = ["print-defmt"] } | ||
| 23 | |||
| 24 | cortex-m = "0.7.3" | ||
| 25 | cortex-m-rt = "0.7.0" | ||
| 26 | embedded-hal = "0.2.6" | ||
| 27 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | ||
| 28 | heapless = { version = "0.7.5", default-features = false } | ||
| 29 | rand_core = { version = "0.6.3", default-features = false } | ||
| 30 | embedded-io = { version = "0.3.0", features = ["async"] } | ||
diff --git a/examples/stm32l5/build.rs b/examples/stm32l5/build.rs new file mode 100644 index 000000000..8cd32d7ed --- /dev/null +++ b/examples/stm32l5/build.rs | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | fn main() { | ||
| 2 | println!("cargo:rustc-link-arg-bins=--nmagic"); | ||
| 3 | println!("cargo:rustc-link-arg-bins=-Tlink.x"); | ||
| 4 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); | ||
| 5 | } | ||
diff --git a/examples/stm32l5/src/bin/button_exti.rs b/examples/stm32l5/src/bin/button_exti.rs new file mode 100644 index 000000000..304ce0a8a --- /dev/null +++ b/examples/stm32l5/src/bin/button_exti.rs | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use defmt_rtt as _; // global logger | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy_stm32::exti::ExtiInput; | ||
| 9 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 10 | use embassy_stm32::Peripherals; | ||
| 11 | use panic_probe as _; | ||
| 12 | |||
| 13 | #[embassy::main] | ||
| 14 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 15 | info!("Hello World!"); | ||
| 16 | |||
| 17 | let button = Input::new(p.PC13, Pull::Down); | ||
| 18 | let mut button = ExtiInput::new(button, p.EXTI13); | ||
| 19 | |||
| 20 | info!("Press the USER button..."); | ||
| 21 | |||
| 22 | loop { | ||
| 23 | button.wait_for_falling_edge().await; | ||
| 24 | info!("Pressed!"); | ||
| 25 | button.wait_for_rising_edge().await; | ||
| 26 | info!("Released!"); | ||
| 27 | } | ||
| 28 | } | ||
diff --git a/examples/stm32l5/src/bin/rng.rs b/examples/stm32l5/src/bin/rng.rs new file mode 100644 index 000000000..5f75c1ff1 --- /dev/null +++ b/examples/stm32l5/src/bin/rng.rs | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use defmt_rtt as _; // global logger | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy_stm32::rcc::{ClockSrc, PLLClkDiv, PLLMul, PLLSource, PLLSrcDiv}; | ||
| 9 | use embassy_stm32::rng::Rng; | ||
| 10 | use embassy_stm32::{Config, Peripherals}; | ||
| 11 | use panic_probe as _; | ||
| 12 | |||
| 13 | fn config() -> Config { | ||
| 14 | let mut config = Config::default(); | ||
| 15 | config.rcc.mux = ClockSrc::PLL( | ||
| 16 | PLLSource::HSI16, | ||
| 17 | PLLClkDiv::Div2, | ||
| 18 | PLLSrcDiv::Div1, | ||
| 19 | PLLMul::Mul8, | ||
| 20 | Some(PLLClkDiv::Div2), | ||
| 21 | ); | ||
| 22 | config | ||
| 23 | } | ||
| 24 | |||
| 25 | #[embassy::main(config = "config()")] | ||
| 26 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 27 | info!("Hello World!"); | ||
| 28 | |||
| 29 | let mut rng = Rng::new(p.RNG); | ||
| 30 | |||
| 31 | let mut buf = [0u8; 16]; | ||
| 32 | unwrap!(rng.async_fill_bytes(&mut buf).await); | ||
| 33 | info!("random bytes: {:02x}", buf); | ||
| 34 | } | ||
diff --git a/examples/stm32l5/src/bin/usb_ethernet.rs b/examples/stm32l5/src/bin/usb_ethernet.rs new file mode 100644 index 000000000..fa445eece --- /dev/null +++ b/examples/stm32l5/src/bin/usb_ethernet.rs | |||
| @@ -0,0 +1,290 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(generic_associated_types)] | ||
| 4 | #![feature(type_alias_impl_trait)] | ||
| 5 | |||
| 6 | use core::sync::atomic::{AtomicBool, Ordering}; | ||
| 7 | use core::task::Waker; | ||
| 8 | use defmt::*; | ||
| 9 | use defmt_rtt as _; // global logger | ||
| 10 | use embassy::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 11 | use embassy::channel::Channel; | ||
| 12 | use embassy::executor::Spawner; | ||
| 13 | use embassy::util::Forever; | ||
| 14 | use embassy_net::tcp::TcpSocket; | ||
| 15 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; | ||
| 16 | use embassy_stm32::interrupt; | ||
| 17 | use embassy_stm32::rcc::*; | ||
| 18 | use embassy_stm32::rng::Rng; | ||
| 19 | use embassy_stm32::time::Hertz; | ||
| 20 | use embassy_stm32::usb::Driver; | ||
| 21 | use embassy_stm32::{Config, Peripherals}; | ||
| 22 | use embassy_usb::{Builder, UsbDevice}; | ||
| 23 | use embassy_usb_ncm::{CdcNcmClass, Receiver, Sender, State}; | ||
| 24 | use panic_probe as _; | ||
| 25 | |||
| 26 | use defmt_rtt as _; | ||
| 27 | use embedded_io::asynch::{Read, Write}; | ||
| 28 | // global logger | ||
| 29 | use panic_probe as _; | ||
| 30 | use rand_core::RngCore; | ||
| 31 | |||
| 32 | type MyDriver = Driver<'static, embassy_stm32::peripherals::USB>; | ||
| 33 | |||
| 34 | macro_rules! forever { | ||
| 35 | ($val:expr) => {{ | ||
| 36 | type T = impl Sized; | ||
| 37 | static FOREVER: Forever<T> = Forever::new(); | ||
| 38 | FOREVER.put_with(move || $val) | ||
| 39 | }}; | ||
| 40 | } | ||
| 41 | |||
| 42 | #[embassy::task] | ||
| 43 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { | ||
| 44 | device.run().await | ||
| 45 | } | ||
| 46 | |||
| 47 | #[embassy::task] | ||
| 48 | async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { | ||
| 49 | loop { | ||
| 50 | warn!("WAITING for connection"); | ||
| 51 | LINK_UP.store(false, Ordering::Relaxed); | ||
| 52 | |||
| 53 | class.wait_connection().await.unwrap(); | ||
| 54 | |||
| 55 | warn!("Connected"); | ||
| 56 | LINK_UP.store(true, Ordering::Relaxed); | ||
| 57 | |||
| 58 | loop { | ||
| 59 | let mut p = unwrap!(PacketBox::new(embassy_net::Packet::new())); | ||
| 60 | let n = match class.read_packet(&mut p[..]).await { | ||
| 61 | Ok(n) => n, | ||
| 62 | Err(e) => { | ||
| 63 | warn!("error reading packet: {:?}", e); | ||
| 64 | break; | ||
| 65 | } | ||
| 66 | }; | ||
| 67 | |||
| 68 | let buf = p.slice(0..n); | ||
| 69 | if RX_CHANNEL.try_send(buf).is_err() { | ||
| 70 | warn!("Failed pushing rx'd packet to channel."); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | #[embassy::task] | ||
| 77 | async fn usb_ncm_tx_task(mut class: Sender<'static, MyDriver>) { | ||
| 78 | loop { | ||
| 79 | let pkt = TX_CHANNEL.recv().await; | ||
| 80 | if let Err(e) = class.write_packet(&pkt[..]).await { | ||
| 81 | warn!("Failed to TX packet: {:?}", e); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | #[embassy::task] | ||
| 87 | async fn net_task(stack: &'static Stack<Device>) -> ! { | ||
| 88 | stack.run().await | ||
| 89 | } | ||
| 90 | |||
| 91 | fn config() -> Config { | ||
| 92 | let mut config = Config::default(); | ||
| 93 | config.rcc.mux = ClockSrc::HSE(Hertz(16_000_000)); | ||
| 94 | |||
| 95 | config.rcc.mux = ClockSrc::PLL( | ||
| 96 | PLLSource::HSI16, | ||
| 97 | PLLClkDiv::Div2, | ||
| 98 | PLLSrcDiv::Div1, | ||
| 99 | PLLMul::Mul10, | ||
| 100 | None, | ||
| 101 | ); | ||
| 102 | config.rcc.hsi48 = true; | ||
| 103 | |||
| 104 | config | ||
| 105 | } | ||
| 106 | |||
| 107 | #[embassy::main(config = "config()")] | ||
| 108 | async fn main(spawner: Spawner, p: Peripherals) { | ||
| 109 | // Create the driver, from the HAL. | ||
| 110 | let irq = interrupt::take!(USB_FS); | ||
| 111 | let driver = Driver::new(p.USB, irq, p.PA12, p.PA11); | ||
| 112 | |||
| 113 | // Create embassy-usb Config | ||
| 114 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 115 | config.manufacturer = Some("Embassy"); | ||
| 116 | config.product = Some("USB-Ethernet example"); | ||
| 117 | config.serial_number = Some("12345678"); | ||
| 118 | config.max_power = 100; | ||
| 119 | config.max_packet_size_0 = 64; | ||
| 120 | |||
| 121 | // Required for Windows support. | ||
| 122 | config.composite_with_iads = true; | ||
| 123 | config.device_class = 0xEF; | ||
| 124 | config.device_sub_class = 0x02; | ||
| 125 | config.device_protocol = 0x01; | ||
| 126 | |||
| 127 | struct Resources { | ||
| 128 | device_descriptor: [u8; 256], | ||
| 129 | config_descriptor: [u8; 256], | ||
| 130 | bos_descriptor: [u8; 256], | ||
| 131 | control_buf: [u8; 128], | ||
| 132 | serial_state: State<'static>, | ||
| 133 | } | ||
| 134 | let res: &mut Resources = forever!(Resources { | ||
| 135 | device_descriptor: [0; 256], | ||
| 136 | config_descriptor: [0; 256], | ||
| 137 | bos_descriptor: [0; 256], | ||
| 138 | control_buf: [0; 128], | ||
| 139 | serial_state: State::new(), | ||
| 140 | }); | ||
| 141 | |||
| 142 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 143 | let mut builder = Builder::new( | ||
| 144 | driver, | ||
| 145 | config, | ||
| 146 | &mut res.device_descriptor, | ||
| 147 | &mut res.config_descriptor, | ||
| 148 | &mut res.bos_descriptor, | ||
| 149 | &mut res.control_buf, | ||
| 150 | None, | ||
| 151 | ); | ||
| 152 | |||
| 153 | // WARNINGS for Android ethernet tethering: | ||
| 154 | // - On Pixel 4a, it refused to work on Android 11, worked on Android 12. | ||
| 155 | // - if the host's MAC address has the "locally-administered" bit set (bit 1 of first byte), | ||
| 156 | // it doesn't work! The "Ethernet tethering" option in settings doesn't get enabled. | ||
| 157 | // This is due to regex spaghetti: https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-mainline-12.0.0_r84/core/res/res/values/config.xml#417 | ||
| 158 | // and this nonsense in the linux kernel: https://github.com/torvalds/linux/blob/c00c5e1d157bec0ef0b0b59aa5482eb8dc7e8e49/drivers/net/usb/usbnet.c#L1751-L1757 | ||
| 159 | |||
| 160 | // Our MAC addr. | ||
| 161 | let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; | ||
| 162 | // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. | ||
| 163 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; | ||
| 164 | |||
| 165 | // Create classes on the builder. | ||
| 166 | let class = CdcNcmClass::new(&mut builder, &mut res.serial_state, host_mac_addr, 64); | ||
| 167 | |||
| 168 | // Build the builder. | ||
| 169 | let usb = builder.build(); | ||
| 170 | |||
| 171 | unwrap!(spawner.spawn(usb_task(usb))); | ||
| 172 | |||
| 173 | let (tx, rx) = class.split(); | ||
| 174 | unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); | ||
| 175 | unwrap!(spawner.spawn(usb_ncm_tx_task(tx))); | ||
| 176 | |||
| 177 | let config = embassy_net::ConfigStrategy::Dhcp; | ||
| 178 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { | ||
| 179 | // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24), | ||
| 180 | // dns_servers: Vec::new(), | ||
| 181 | // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), | ||
| 182 | //}); | ||
| 183 | |||
| 184 | // Generate random seed | ||
| 185 | let mut rng = Rng::new(p.RNG); | ||
| 186 | let seed = rng.next_u64(); | ||
| 187 | |||
| 188 | // Init network stack | ||
| 189 | let device = Device { | ||
| 190 | mac_addr: our_mac_addr, | ||
| 191 | }; | ||
| 192 | let stack = &*forever!(Stack::new( | ||
| 193 | device, | ||
| 194 | config, | ||
| 195 | forever!(StackResources::<1, 2, 8>::new()), | ||
| 196 | seed | ||
| 197 | )); | ||
| 198 | |||
| 199 | unwrap!(spawner.spawn(net_task(stack))); | ||
| 200 | |||
| 201 | // And now we can use it! | ||
| 202 | |||
| 203 | let mut rx_buffer = [0; 4096]; | ||
| 204 | let mut tx_buffer = [0; 4096]; | ||
| 205 | let mut buf = [0; 4096]; | ||
| 206 | |||
| 207 | loop { | ||
| 208 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | ||
| 209 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | ||
| 210 | |||
| 211 | info!("Listening on TCP:1234..."); | ||
| 212 | if let Err(e) = socket.accept(1234).await { | ||
| 213 | warn!("accept error: {:?}", e); | ||
| 214 | continue; | ||
| 215 | } | ||
| 216 | |||
| 217 | info!("Received connection from {:?}", socket.remote_endpoint()); | ||
| 218 | |||
| 219 | loop { | ||
| 220 | let n = match socket.read(&mut buf).await { | ||
| 221 | Ok(0) => { | ||
| 222 | warn!("read EOF"); | ||
| 223 | break; | ||
| 224 | } | ||
| 225 | Ok(n) => n, | ||
| 226 | Err(e) => { | ||
| 227 | warn!("read error: {:?}", e); | ||
| 228 | break; | ||
| 229 | } | ||
| 230 | }; | ||
| 231 | |||
| 232 | info!("rxd {:02x}", &buf[..n]); | ||
| 233 | |||
| 234 | match socket.write_all(&buf[..n]).await { | ||
| 235 | Ok(()) => {} | ||
| 236 | Err(e) => { | ||
| 237 | warn!("write error: {:?}", e); | ||
| 238 | break; | ||
| 239 | } | ||
| 240 | }; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 246 | static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 247 | static LINK_UP: AtomicBool = AtomicBool::new(false); | ||
| 248 | |||
| 249 | struct Device { | ||
| 250 | mac_addr: [u8; 6], | ||
| 251 | } | ||
| 252 | |||
| 253 | impl embassy_net::Device for Device { | ||
| 254 | fn register_waker(&mut self, waker: &Waker) { | ||
| 255 | // loopy loopy wakey wakey | ||
| 256 | waker.wake_by_ref() | ||
| 257 | } | ||
| 258 | |||
| 259 | fn link_state(&mut self) -> embassy_net::LinkState { | ||
| 260 | match LINK_UP.load(Ordering::Relaxed) { | ||
| 261 | true => embassy_net::LinkState::Up, | ||
| 262 | false => embassy_net::LinkState::Down, | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | fn capabilities(&self) -> embassy_net::DeviceCapabilities { | ||
| 267 | let mut caps = embassy_net::DeviceCapabilities::default(); | ||
| 268 | caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header | ||
| 269 | caps.medium = embassy_net::Medium::Ethernet; | ||
| 270 | caps | ||
| 271 | } | ||
| 272 | |||
| 273 | fn is_transmit_ready(&mut self) -> bool { | ||
| 274 | true | ||
| 275 | } | ||
| 276 | |||
| 277 | fn transmit(&mut self, pkt: PacketBuf) { | ||
| 278 | if TX_CHANNEL.try_send(pkt).is_err() { | ||
| 279 | warn!("TX failed") | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | fn receive<'a>(&mut self) -> Option<PacketBuf> { | ||
| 284 | RX_CHANNEL.try_recv().ok() | ||
| 285 | } | ||
| 286 | |||
| 287 | fn ethernet_address(&self) -> [u8; 6] { | ||
| 288 | self.mac_addr | ||
| 289 | } | ||
| 290 | } | ||
diff --git a/examples/stm32l5/src/bin/usb_hid_mouse.rs b/examples/stm32l5/src/bin/usb_hid_mouse.rs new file mode 100644 index 000000000..d275aba36 --- /dev/null +++ b/examples/stm32l5/src/bin/usb_hid_mouse.rs | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(generic_associated_types)] | ||
| 4 | #![feature(type_alias_impl_trait)] | ||
| 5 | |||
| 6 | use defmt::*; | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy::time::{Duration, Timer}; | ||
| 9 | use embassy_stm32::interrupt; | ||
| 10 | use embassy_stm32::rcc::*; | ||
| 11 | use embassy_stm32::time::Hertz; | ||
| 12 | use embassy_stm32::usb::Driver; | ||
| 13 | use embassy_stm32::{Config, Peripherals}; | ||
| 14 | use embassy_usb::control::OutResponse; | ||
| 15 | use embassy_usb::Builder; | ||
| 16 | use embassy_usb_hid::{HidWriter, ReportId, RequestHandler, State}; | ||
| 17 | use futures::future::join; | ||
| 18 | use usbd_hid::descriptor::{MouseReport, SerializedDescriptor}; | ||
| 19 | |||
| 20 | use defmt_rtt as _; // global logger | ||
| 21 | use panic_probe as _; | ||
| 22 | |||
| 23 | fn config() -> Config { | ||
| 24 | let mut config = Config::default(); | ||
| 25 | config.rcc.mux = ClockSrc::HSE(Hertz(16_000_000)); | ||
| 26 | |||
| 27 | config.rcc.mux = ClockSrc::PLL( | ||
| 28 | PLLSource::HSI16, | ||
| 29 | PLLClkDiv::Div2, | ||
| 30 | PLLSrcDiv::Div1, | ||
| 31 | PLLMul::Mul10, | ||
| 32 | None, | ||
| 33 | ); | ||
| 34 | config.rcc.hsi48 = true; | ||
| 35 | |||
| 36 | config | ||
| 37 | } | ||
| 38 | |||
| 39 | #[embassy::main(config = "config()")] | ||
| 40 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 41 | // Create the driver, from the HAL. | ||
| 42 | let irq = interrupt::take!(USB_FS); | ||
| 43 | let driver = Driver::new(p.USB, irq, p.PA12, p.PA11); | ||
| 44 | |||
| 45 | // Create embassy-usb Config | ||
| 46 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 47 | config.manufacturer = Some("Embassy"); | ||
| 48 | config.product = Some("HID mouse example"); | ||
| 49 | config.serial_number = Some("12345678"); | ||
| 50 | config.max_power = 100; | ||
| 51 | config.max_packet_size_0 = 64; | ||
| 52 | |||
| 53 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 54 | // It needs some buffers for building the descriptors. | ||
| 55 | let mut device_descriptor = [0; 256]; | ||
| 56 | let mut config_descriptor = [0; 256]; | ||
| 57 | let mut bos_descriptor = [0; 256]; | ||
| 58 | let mut control_buf = [0; 64]; | ||
| 59 | let request_handler = MyRequestHandler {}; | ||
| 60 | |||
| 61 | let mut state = State::new(); | ||
| 62 | |||
| 63 | let mut builder = Builder::new( | ||
| 64 | driver, | ||
| 65 | config, | ||
| 66 | &mut device_descriptor, | ||
| 67 | &mut config_descriptor, | ||
| 68 | &mut bos_descriptor, | ||
| 69 | &mut control_buf, | ||
| 70 | None, | ||
| 71 | ); | ||
| 72 | |||
| 73 | // Create classes on the builder. | ||
| 74 | let config = embassy_usb_hid::Config { | ||
| 75 | report_descriptor: MouseReport::desc(), | ||
| 76 | request_handler: Some(&request_handler), | ||
| 77 | poll_ms: 60, | ||
| 78 | max_packet_size: 8, | ||
| 79 | }; | ||
| 80 | |||
| 81 | let mut writer = HidWriter::<_, 5>::new(&mut builder, &mut state, config); | ||
| 82 | |||
| 83 | // Build the builder. | ||
| 84 | let mut usb = builder.build(); | ||
| 85 | |||
| 86 | // Run the USB device. | ||
| 87 | let usb_fut = usb.run(); | ||
| 88 | |||
| 89 | // Do stuff with the class! | ||
| 90 | let hid_fut = async { | ||
| 91 | let mut y: i8 = 5; | ||
| 92 | loop { | ||
| 93 | Timer::after(Duration::from_millis(500)).await; | ||
| 94 | |||
| 95 | y = -y; | ||
| 96 | let report = MouseReport { | ||
| 97 | buttons: 0, | ||
| 98 | x: 0, | ||
| 99 | y, | ||
| 100 | wheel: 0, | ||
| 101 | pan: 0, | ||
| 102 | }; | ||
| 103 | match writer.write_serialize(&report).await { | ||
| 104 | Ok(()) => {} | ||
| 105 | Err(e) => warn!("Failed to send report: {:?}", e), | ||
| 106 | } | ||
| 107 | } | ||
| 108 | }; | ||
| 109 | |||
| 110 | // Run everything concurrently. | ||
| 111 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 112 | join(usb_fut, hid_fut).await; | ||
| 113 | } | ||
| 114 | |||
| 115 | struct MyRequestHandler {} | ||
| 116 | |||
| 117 | impl RequestHandler for MyRequestHandler { | ||
| 118 | fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> { | ||
| 119 | info!("Get report for {:?}", id); | ||
| 120 | None | ||
| 121 | } | ||
| 122 | |||
| 123 | fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse { | ||
| 124 | info!("Set report for {:?}: {=[u8]}", id, data); | ||
| 125 | OutResponse::Accepted | ||
| 126 | } | ||
| 127 | |||
| 128 | fn set_idle(&self, id: Option<ReportId>, dur: Duration) { | ||
| 129 | info!("Set idle rate for {:?} to {:?}", id, dur); | ||
| 130 | } | ||
| 131 | |||
| 132 | fn get_idle(&self, id: Option<ReportId>) -> Option<Duration> { | ||
| 133 | info!("Get idle rate for {:?}", id); | ||
| 134 | None | ||
| 135 | } | ||
| 136 | } | ||
diff --git a/examples/stm32l5/src/bin/usb_serial.rs b/examples/stm32l5/src/bin/usb_serial.rs new file mode 100644 index 000000000..987f1b692 --- /dev/null +++ b/examples/stm32l5/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::panic; | ||
| 6 | use defmt::*; | ||
| 7 | use defmt_rtt as _; // global logger | ||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy_stm32::interrupt; | ||
| 10 | use embassy_stm32::rcc::*; | ||
| 11 | use embassy_stm32::time::Hertz; | ||
| 12 | use embassy_stm32::usb::{Driver, Instance}; | ||
| 13 | use embassy_stm32::{Config, Peripherals}; | ||
| 14 | use embassy_usb::driver::EndpointError; | ||
| 15 | use embassy_usb::Builder; | ||
| 16 | use embassy_usb_serial::{CdcAcmClass, State}; | ||
| 17 | use futures::future::join; | ||
| 18 | use panic_probe as _; | ||
| 19 | |||
| 20 | fn config() -> Config { | ||
| 21 | let mut config = Config::default(); | ||
| 22 | config.rcc.mux = ClockSrc::HSE(Hertz(16_000_000)); | ||
| 23 | |||
| 24 | config.rcc.mux = ClockSrc::PLL( | ||
| 25 | PLLSource::HSI16, | ||
| 26 | PLLClkDiv::Div2, | ||
| 27 | PLLSrcDiv::Div1, | ||
| 28 | PLLMul::Mul10, | ||
| 29 | None, | ||
| 30 | ); | ||
| 31 | config.rcc.hsi48 = true; | ||
| 32 | |||
| 33 | config | ||
| 34 | } | ||
| 35 | |||
| 36 | #[embassy::main(config = "config()")] | ||
| 37 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 38 | info!("Hello World!"); | ||
| 39 | |||
| 40 | // Create the driver, from the HAL. | ||
| 41 | let irq = interrupt::take!(USB_FS); | ||
| 42 | let driver = Driver::new(p.USB, irq, p.PA12, p.PA11); | ||
| 43 | |||
| 44 | // Create embassy-usb Config | ||
| 45 | let config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 46 | //config.max_packet_size_0 = 64; | ||
| 47 | |||
| 48 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 49 | // It needs some buffers for building the descriptors. | ||
| 50 | let mut device_descriptor = [0; 256]; | ||
| 51 | let mut config_descriptor = [0; 256]; | ||
| 52 | let mut bos_descriptor = [0; 256]; | ||
| 53 | let mut control_buf = [0; 7]; | ||
| 54 | |||
| 55 | let mut state = State::new(); | ||
| 56 | |||
| 57 | let mut builder = Builder::new( | ||
| 58 | driver, | ||
| 59 | config, | ||
| 60 | &mut device_descriptor, | ||
| 61 | &mut config_descriptor, | ||
| 62 | &mut bos_descriptor, | ||
| 63 | &mut control_buf, | ||
| 64 | None, | ||
| 65 | ); | ||
| 66 | |||
| 67 | // Create classes on the builder. | ||
| 68 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 69 | |||
| 70 | // Build the builder. | ||
| 71 | let mut usb = builder.build(); | ||
| 72 | |||
| 73 | // Run the USB device. | ||
| 74 | let usb_fut = usb.run(); | ||
| 75 | |||
| 76 | // Do stuff with the class! | ||
| 77 | let echo_fut = async { | ||
| 78 | loop { | ||
| 79 | class.wait_connection().await; | ||
| 80 | info!("Connected"); | ||
| 81 | let _ = echo(&mut class).await; | ||
| 82 | info!("Disconnected"); | ||
| 83 | } | ||
| 84 | }; | ||
| 85 | |||
| 86 | // Run everything concurrently. | ||
| 87 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 88 | join(usb_fut, echo_fut).await; | ||
| 89 | } | ||
| 90 | |||
| 91 | struct Disconnected {} | ||
| 92 | |||
| 93 | impl From<EndpointError> for Disconnected { | ||
| 94 | fn from(val: EndpointError) -> Self { | ||
| 95 | match val { | ||
| 96 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 97 | EndpointError::Disabled => Disconnected {}, | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | async fn echo<'d, T: Instance + 'd>( | ||
| 103 | class: &mut CdcAcmClass<'d, Driver<'d, T>>, | ||
| 104 | ) -> Result<(), Disconnected> { | ||
| 105 | let mut buf = [0; 64]; | ||
| 106 | loop { | ||
| 107 | let n = class.read_packet(&mut buf).await?; | ||
| 108 | let data = &buf[..n]; | ||
| 109 | info!("data: {:x}", data); | ||
| 110 | class.write_packet(data).await?; | ||
| 111 | } | ||
| 112 | } | ||
