diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2023-02-07 19:31:10 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-02-07 19:31:10 +0000 |
| commit | 366fab5b872e5d00fd84408f04cf00faf988d160 (patch) | |
| tree | 9b45fb26d9a32b806931fcc6d379af00f4ffec21 /examples | |
| parent | a7fa7d0de2fa7b8fab889879b6003df8427c6841 (diff) | |
| parent | aa21aebb0b321a2085571e5be5fffcea4703584d (diff) | |
Merge #1189
1189: USB: Add MS OS Descriptors (alternate implementation) r=Dirbaio a=alexmoon
This is an alternate API for #1152 based on the work of `@mattico.` By switching to a writer-style API instead of a builder API some compile-time guarantees are lost, but it integrates better into the usb `Builder` and makes an api that can be used by USB device class implementations.
It also adds a feature flag so there is zero cost to the MS OS descriptors for devices that don't need to use them.
I've added an example based on `usb_serial` which tells Windows to use the generic `WinUSB` driver instead of the serial port driver for the device.
Comments are welcome. It would be nice to see either this or #1152 merged as my project is going to require the MS OS Descriptors soon.
Co-authored-by: Matt Ickstadt <[email protected]>
Co-authored-by: alexmoon <[email protected]>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf52840/Cargo.toml | 7 | ||||
| -rw-r--r-- | examples/nrf52840/src/bin/usb_serial_winusb.rs | 130 |
2 files changed, 136 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..f4b828de6 --- /dev/null +++ b/examples/nrf52840/src/bin/usb_serial_winusb.rs | |||
| @@ -0,0 +1,130 @@ | |||
| 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, HardwareVbusDetect, Instance, VbusDetect}; | ||
| 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 | // This is a randomly generated GUID to allow clients on Windows to find our device | ||
| 19 | const DEVICE_INTERFACE_GUIDS: &[&str] = &["{EAA9A5DC-30BA-44BC-9232-606CDC875321}"]; | ||
| 20 | |||
| 21 | #[embassy_executor::main] | ||
| 22 | async fn main(_spawner: Spawner) { | ||
| 23 | let p = embassy_nrf::init(Default::default()); | ||
| 24 | let clock: pac::CLOCK = unsafe { mem::transmute(()) }; | ||
| 25 | |||
| 26 | info!("Enabling ext hfosc..."); | ||
| 27 | clock.tasks_hfclkstart.write(|w| unsafe { w.bits(1) }); | ||
| 28 | while clock.events_hfclkstarted.read().bits() != 1 {} | ||
| 29 | |||
| 30 | // Create the driver, from the HAL. | ||
| 31 | let irq = interrupt::take!(USBD); | ||
| 32 | let power_irq = interrupt::take!(POWER_CLOCK); | ||
| 33 | let driver = Driver::new(p.USBD, irq, HardwareVbusDetect::new(power_irq)); | ||
| 34 | |||
| 35 | // Create embassy-usb Config | ||
| 36 | let mut config = Config::new(0xc0de, 0xcafe); | ||
| 37 | config.manufacturer = Some("Embassy"); | ||
| 38 | config.product = Some("USB-serial example"); | ||
| 39 | config.serial_number = Some("12345678"); | ||
| 40 | config.max_power = 100; | ||
| 41 | config.max_packet_size_0 = 64; | ||
| 42 | |||
| 43 | // Required for windows compatiblity. | ||
| 44 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 45 | config.device_class = 0xEF; | ||
| 46 | config.device_sub_class = 0x02; | ||
| 47 | config.device_protocol = 0x01; | ||
| 48 | config.composite_with_iads = true; | ||
| 49 | |||
| 50 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 51 | // It needs some buffers for building the descriptors. | ||
| 52 | let mut device_descriptor = [0; 256]; | ||
| 53 | let mut config_descriptor = [0; 256]; | ||
| 54 | let mut bos_descriptor = [0; 256]; | ||
| 55 | let mut msos_descriptor = [0; 256]; | ||
| 56 | let mut control_buf = [0; 64]; | ||
| 57 | |||
| 58 | let mut state = State::new(); | ||
| 59 | |||
| 60 | let mut builder = Builder::new( | ||
| 61 | driver, | ||
| 62 | config, | ||
| 63 | &mut device_descriptor, | ||
| 64 | &mut config_descriptor, | ||
| 65 | &mut bos_descriptor, | ||
| 66 | &mut msos_descriptor, | ||
| 67 | &mut control_buf, | ||
| 68 | None, | ||
| 69 | ); | ||
| 70 | |||
| 71 | builder.msos_descriptor(windows_version::WIN8_1, 2); | ||
| 72 | |||
| 73 | // Create classes on the builder. | ||
| 74 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 75 | |||
| 76 | // Since we want to create MS OS feature descriptors that apply to a function that has already been added to the | ||
| 77 | // builder, need to get the MsOsDescriptorWriter from the builder and manually add those descriptors. | ||
| 78 | // Inside a class constructor, you would just need to call `FunctionBuilder::msos_feature` instead. | ||
| 79 | let msos_writer = builder.msos_writer(); | ||
| 80 | msos_writer.configuration(0); | ||
| 81 | msos_writer.function(0); | ||
| 82 | msos_writer.function_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", "")); | ||
| 83 | msos_writer.function_feature(msos::RegistryPropertyFeatureDescriptor::new( | ||
| 84 | "DeviceInterfaceGUIDs", | ||
| 85 | msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS), | ||
| 86 | )); | ||
| 87 | |||
| 88 | // Build the builder. | ||
| 89 | let mut usb = builder.build(); | ||
| 90 | |||
| 91 | // Run the USB device. | ||
| 92 | let usb_fut = usb.run(); | ||
| 93 | |||
| 94 | // Do stuff with the class! | ||
| 95 | let echo_fut = async { | ||
| 96 | loop { | ||
| 97 | class.wait_connection().await; | ||
| 98 | info!("Connected"); | ||
| 99 | let _ = echo(&mut class).await; | ||
| 100 | info!("Disconnected"); | ||
| 101 | } | ||
| 102 | }; | ||
| 103 | |||
| 104 | // Run everything concurrently. | ||
| 105 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 106 | join(usb_fut, echo_fut).await; | ||
| 107 | } | ||
| 108 | |||
| 109 | struct Disconnected {} | ||
| 110 | |||
| 111 | impl From<EndpointError> for Disconnected { | ||
| 112 | fn from(val: EndpointError) -> Self { | ||
| 113 | match val { | ||
| 114 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 115 | EndpointError::Disabled => Disconnected {}, | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | async fn echo<'d, T: Instance + 'd, P: VbusDetect + 'd>( | ||
| 121 | class: &mut CdcAcmClass<'d, Driver<'d, T, P>>, | ||
| 122 | ) -> Result<(), Disconnected> { | ||
| 123 | let mut buf = [0; 64]; | ||
| 124 | loop { | ||
| 125 | let n = class.read_packet(&mut buf).await?; | ||
| 126 | let data = &buf[..n]; | ||
| 127 | info!("data: {:x}", data); | ||
| 128 | class.write_packet(data).await?; | ||
| 129 | } | ||
| 130 | } | ||
