From 1d51f91368c4ba6b497cdfb5bca5b764d13074f9 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Thu, 16 Dec 2021 14:59:35 -0700 Subject: [PATCH] usb_uart_io example equivilent to usb_uart --- examples/nrf/src/bin/usb_uart_io.rs | 70 +++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 examples/nrf/src/bin/usb_uart_io.rs diff --git a/examples/nrf/src/bin/usb_uart_io.rs b/examples/nrf/src/bin/usb_uart_io.rs new file mode 100644 index 00000000..f07ed891 --- /dev/null +++ b/examples/nrf/src/bin/usb_uart_io.rs @@ -0,0 +1,70 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; + +use defmt::{info, unwrap}; +use defmt_rtt as _; +// global logger +use panic_probe as _; // print out panic messages + +use embassy::executor::Spawner; +use embassy::interrupt::InterruptExt; +use embassy::io::{read_line, AsyncWriteExt}; +use embassy_nrf::usb::{State, Usb, UsbBus, UsbSerial}; +use embassy_nrf::{interrupt, Peripherals}; +use futures::pin_mut; +use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let mut rx_buffer = [0u8; 64]; + // we send back input + cr + lf + let mut tx_buffer = [0u8; 66]; + + let usb_bus = UsbBus::new(p.USBD); + + let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); + + let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) + .manufacturer("Fake company") + .product("Serial port") + .serial_number("TEST") + .device_class(0x02) + .build(); + + let irq = interrupt::take!(USBD); + irq.set_priority(interrupt::Priority::P3); + + let mut state = State::new(); + let usb = unsafe { Usb::new(&mut state, device, serial, irq) }; + pin_mut!(usb); + + let (mut reader, mut writer) = usb.as_ref().take_serial_0(); + + info!("usb initialized!"); + + unwrap!( + writer + .write_all(b"\r\nInput returned upper cased on CR+LF\r\n") + .await + ); + + let mut buf = [0u8; 64]; + loop { + let n = unwrap!(read_line(&mut reader, &mut buf).await); + + for char in buf[..n].iter_mut() { + // upper case + if 0x61 <= *char && *char <= 0x7a { + *char &= !0x20; + } + } + + unwrap!(writer.write_all(&buf[..n]).await); + unwrap!(writer.write_all(b"\r\n").await); + unwrap!(writer.flush().await); + } +}