diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/src/bin/usb_midi.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/examples/rp/src/bin/usb_midi.rs b/examples/rp/src/bin/usb_midi.rs new file mode 100644 index 000000000..f0b03c81b --- /dev/null +++ b/examples/rp/src/bin/usb_midi.rs | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | //! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! This creates a USB MIDI device that echoes MIDI messages back to the host. | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | #![feature(type_alias_impl_trait)] | ||
| 8 | |||
| 9 | use defmt::{info, panic}; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_futures::join::join; | ||
| 12 | use embassy_rp::bind_interrupts; | ||
| 13 | use embassy_rp::peripherals::USB; | ||
| 14 | use embassy_rp::usb::{Driver, Instance, InterruptHandler}; | ||
| 15 | use embassy_usb::class::midi::MidiClass; | ||
| 16 | use embassy_usb::driver::EndpointError; | ||
| 17 | use embassy_usb::{Builder, Config}; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | bind_interrupts!(struct Irqs { | ||
| 21 | USBCTRL_IRQ => InterruptHandler<USB>; | ||
| 22 | }); | ||
| 23 | |||
| 24 | #[embassy_executor::main] | ||
| 25 | async fn main(_spawner: Spawner) { | ||
| 26 | info!("Hello world!"); | ||
| 27 | |||
| 28 | let p = embassy_rp::init(Default::default()); | ||
| 29 | |||
| 30 | // Create the driver, from the HAL. | ||
| 31 | let driver = Driver::new(p.USB, Irqs); | ||
| 32 | |||
| 33 | // Create embassy-usb Config | ||
| 34 | let mut config = Config::new(0xc0de, 0xcafe); | ||
| 35 | config.manufacturer = Some("Embassy"); | ||
| 36 | config.product = Some("USB-MIDI example"); | ||
| 37 | config.serial_number = Some("12345678"); | ||
| 38 | config.max_power = 100; | ||
| 39 | config.max_packet_size_0 = 64; | ||
| 40 | |||
| 41 | // Required for windows compatibility. | ||
| 42 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 43 | config.device_class = 0xEF; | ||
| 44 | config.device_sub_class = 0x02; | ||
| 45 | config.device_protocol = 0x01; | ||
| 46 | config.composite_with_iads = true; | ||
| 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; 64]; | ||
| 54 | |||
| 55 | let mut builder = Builder::new( | ||
| 56 | driver, | ||
| 57 | config, | ||
| 58 | &mut device_descriptor, | ||
| 59 | &mut config_descriptor, | ||
| 60 | &mut bos_descriptor, | ||
| 61 | &mut control_buf, | ||
| 62 | ); | ||
| 63 | |||
| 64 | // Create classes on the builder. | ||
| 65 | let mut class = MidiClass::new(&mut builder, 1, 1, 64); | ||
| 66 | |||
| 67 | // The `MidiClass` can be split into `Sender` and `Receiver`, to be used in separate tasks. | ||
| 68 | // let (sender, receiver) = class.split(); | ||
| 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 | // Use the Midi class! | ||
| 77 | let midi_fut = async { | ||
| 78 | loop { | ||
| 79 | class.wait_connection().await; | ||
| 80 | info!("Connected"); | ||
| 81 | let _ = midi_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, midi_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 midi_echo<'d, T: Instance + 'd>(class: &mut MidiClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 103 | let mut buf = [0; 64]; | ||
| 104 | loop { | ||
| 105 | let n = class.read_packet(&mut buf).await?; | ||
| 106 | let data = &buf[..n]; | ||
| 107 | info!("data: {:x}", data); | ||
| 108 | class.write_packet(data).await?; | ||
| 109 | } | ||
| 110 | } | ||
