diff options
| author | alexmoon <[email protected]> | 2023-02-02 16:13:16 -0500 |
|---|---|---|
| committer | alexmoon <[email protected]> | 2023-02-07 14:24:35 -0500 |
| commit | 9f9230ae7abb545822e59c6f06cabb721b63e0a1 (patch) | |
| tree | 0505e1793cef698a671a5b12f7567066a901eb7b /examples | |
| parent | b9ecdb72bb55792a8fa5a0bace8cdad498fee9b0 (diff) | |
Convert MS OS descriptor builder to a writer API
This brings it inline with the other embassy-usb descriptor APIs and allows it to integrate well with the Builder to allow class constructors to add MS OS descriptors.
Also adds a `usb_serial_winusb` example to demonstrate how to use the API.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf52840/Cargo.toml | 7 | ||||
| -rw-r--r-- | examples/nrf52840/src/bin/usb_serial_winusb.rs | 139 |
2 files changed, 145 insertions, 1 deletions
diff --git a/examples/nrf52840/Cargo.toml b/examples/nrf52840/Cargo.toml index 95d939873..cfdda076e 100644 --- a/examples/nrf52840/Cargo.toml +++ b/examples/nrf52840/Cargo.toml | |||
| @@ -6,6 +6,7 @@ license = "MIT OR Apache-2.0" | |||
| 6 | 6 | ||
| 7 | [features] | 7 | [features] |
| 8 | default = ["nightly"] | 8 | default = ["nightly"] |
| 9 | msos-descriptor = ["embassy-usb/msos-descriptor"] | ||
| 9 | nightly = ["embassy-executor/nightly", "embassy-nrf/nightly", "embassy-net/nightly", "embassy-nrf/unstable-traits", "embassy-usb", "embedded-io/async", "embassy-net", | 10 | nightly = ["embassy-executor/nightly", "embassy-nrf/nightly", "embassy-net/nightly", "embassy-nrf/unstable-traits", "embassy-usb", "embedded-io/async", "embassy-net", |
| 10 | "embassy-lora", "lorawan-device", "lorawan"] | 11 | "embassy-lora", "lorawan-device", "lorawan"] |
| 11 | 12 | ||
| @@ -34,4 +35,8 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 34 | rand = { version = "0.8.4", default-features = false } | 35 | rand = { version = "0.8.4", default-features = false } |
| 35 | embedded-storage = "0.3.0" | 36 | embedded-storage = "0.3.0" |
| 36 | usbd-hid = "0.6.0" | 37 | usbd-hid = "0.6.0" |
| 37 | serde = { version = "1.0.136", default-features = false } \ No newline at end of file | 38 | serde = { version = "1.0.136", default-features = false } |
| 39 | |||
| 40 | [[bin]] | ||
| 41 | name = "usb_serial_winusb" | ||
| 42 | required-features = ["msos-descriptor"] | ||
diff --git a/examples/nrf52840/src/bin/usb_serial_winusb.rs b/examples/nrf52840/src/bin/usb_serial_winusb.rs new file mode 100644 index 000000000..443379a07 --- /dev/null +++ b/examples/nrf52840/src/bin/usb_serial_winusb.rs | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use core::mem; | ||
| 6 | |||
| 7 | use defmt::{info, panic}; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_futures::join::join; | ||
| 10 | use embassy_nrf::usb::{Driver, Instance, PowerUsb, UsbSupply}; | ||
| 11 | use embassy_nrf::{interrupt, pac}; | ||
| 12 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 13 | use embassy_usb::driver::EndpointError; | ||
| 14 | use embassy_usb::msos::{self, windows_version}; | ||
| 15 | use embassy_usb::{Builder, Config}; | ||
| 16 | use {defmt_rtt as _, panic_probe as _}; | ||
| 17 | |||
| 18 | const DEVICE_INTERFACE_GUIDS: &[u16] = { | ||
| 19 | // Can't use defmt::panic in constant expressions (inside u16str!) | ||
| 20 | macro_rules! panic { | ||
| 21 | ($($x:tt)*) => { | ||
| 22 | { | ||
| 23 | ::core::panic!($($x)*); | ||
| 24 | } | ||
| 25 | }; | ||
| 26 | } | ||
| 27 | msos::u16str!("{EAA9A5DC-30BA-44BC-9232-606CDC875321}\0\0").as_slice() | ||
| 28 | }; | ||
| 29 | |||
| 30 | #[embassy_executor::main] | ||
| 31 | async fn main(_spawner: Spawner) { | ||
| 32 | let p = embassy_nrf::init(Default::default()); | ||
| 33 | let clock: pac::CLOCK = unsafe { mem::transmute(()) }; | ||
| 34 | |||
| 35 | info!("Enabling ext hfosc..."); | ||
| 36 | clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) }); | ||
| 37 | while clock.events_hfclkstarted.read().bits() != 1 {} | ||
| 38 | |||
| 39 | // Create the driver, from the HAL. | ||
| 40 | let irq = interrupt::take!(USBD); | ||
| 41 | let power_irq = interrupt::take!(POWER_CLOCK); | ||
| 42 | let driver = Driver::new(p.USBD, irq, PowerUsb::new(power_irq)); | ||
| 43 | |||
| 44 | // Create embassy-usb Config | ||
| 45 | let mut config = Config::new(0xc0de, 0xcafe); | ||
| 46 | config.manufacturer = Some("Embassy"); | ||
| 47 | config.product = Some("USB-serial example"); | ||
| 48 | config.serial_number = Some("12345678"); | ||
| 49 | config.max_power = 100; | ||
| 50 | config.max_packet_size_0 = 64; | ||
| 51 | |||
| 52 | // Required for windows compatiblity. | ||
| 53 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 54 | config.device_class = 0xEF; | ||
| 55 | config.device_sub_class = 0x02; | ||
| 56 | config.device_protocol = 0x01; | ||
| 57 | config.composite_with_iads = true; | ||
| 58 | |||
| 59 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 60 | // It needs some buffers for building the descriptors. | ||
| 61 | let mut device_descriptor = [0; 256]; | ||
| 62 | let mut config_descriptor = [0; 256]; | ||
| 63 | let mut bos_descriptor = [0; 256]; | ||
| 64 | let mut msos_descriptor = [0; 256]; | ||
| 65 | let mut control_buf = [0; 64]; | ||
| 66 | |||
| 67 | let mut state = State::new(); | ||
| 68 | |||
| 69 | let mut builder = Builder::new( | ||
| 70 | driver, | ||
| 71 | config, | ||
| 72 | &mut device_descriptor, | ||
| 73 | &mut config_descriptor, | ||
| 74 | &mut bos_descriptor, | ||
| 75 | &mut msos_descriptor, | ||
| 76 | &mut control_buf, | ||
| 77 | None, | ||
| 78 | ); | ||
| 79 | |||
| 80 | builder.msos_descriptor(windows_version::WIN8_1, 2); | ||
| 81 | |||
| 82 | // Create classes on the builder. | ||
| 83 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 84 | |||
| 85 | // Since we want to create MS OS feature descriptors that apply to a function that has already been added to the | ||
| 86 | // builder, need to get the MsOsDescriptorWriter from the builder and manually add those descriptors. | ||
| 87 | // Inside a class constructor, you would just need to call `FunctionBuilder::msos_feature` instead. | ||
| 88 | let msos_writer = builder.msos_writer(); | ||
| 89 | msos_writer.configuration(0); | ||
| 90 | msos_writer.function(0); | ||
| 91 | msos_writer.function_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", "")); | ||
| 92 | msos_writer.function_feature(msos::RegistryPropertyFeatureDescriptor::new_multi_string( | ||
| 93 | msos::RegistryPropertyFeatureDescriptor::DEVICE_INTERFACE_GUIDS_NAME, | ||
| 94 | DEVICE_INTERFACE_GUIDS, | ||
| 95 | )); | ||
| 96 | |||
| 97 | // Build the builder. | ||
| 98 | let mut usb = builder.build(); | ||
| 99 | |||
| 100 | // Run the USB device. | ||
| 101 | let usb_fut = usb.run(); | ||
| 102 | |||
| 103 | // Do stuff with the class! | ||
| 104 | let echo_fut = async { | ||
| 105 | loop { | ||
| 106 | class.wait_connection().await; | ||
| 107 | info!("Connected"); | ||
| 108 | let _ = echo(&mut class).await; | ||
| 109 | info!("Disconnected"); | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | // Run everything concurrently. | ||
| 114 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 115 | join(usb_fut, echo_fut).await; | ||
| 116 | } | ||
| 117 | |||
| 118 | struct Disconnected {} | ||
| 119 | |||
| 120 | impl From<EndpointError> for Disconnected { | ||
| 121 | fn from(val: EndpointError) -> Self { | ||
| 122 | match val { | ||
| 123 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 124 | EndpointError::Disabled => Disconnected {}, | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | async fn echo<'d, T: Instance + 'd, P: UsbSupply + 'd>( | ||
| 130 | class: &mut CdcAcmClass<'d, Driver<'d, T, P>>, | ||
| 131 | ) -> Result<(), Disconnected> { | ||
| 132 | let mut buf = [0; 64]; | ||
| 133 | loop { | ||
| 134 | let n = class.read_packet(&mut buf).await?; | ||
| 135 | let data = &buf[..n]; | ||
| 136 | info!("data: {:x}", data); | ||
| 137 | class.write_packet(data).await?; | ||
| 138 | } | ||
| 139 | } | ||
