diff options
| author | Jacob Rosenthal <[email protected]> | 2021-12-12 17:13:37 -0700 |
|---|---|---|
| committer | Jacob Rosenthal <[email protected]> | 2021-12-12 19:20:02 -0700 |
| commit | f430c0e8c2e9703220978d9976b058ecf0117c45 (patch) | |
| tree | 03d86f07e877dbd5479680d1c53c747bb00a6e6c | |
| parent | dce3f8c47df611b51c47559ba8f4c301eb86af95 (diff) | |
nrf-usbd
| -rw-r--r-- | embassy-nrf/Cargo.toml | 2 | ||||
| -rw-r--r-- | embassy-nrf/src/lib.rs | 16 | ||||
| -rw-r--r-- | examples/nrf/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb_uart.rs | 94 |
4 files changed, 117 insertions, 0 deletions
diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index dae9f26ae..b04d44b0c 100644 --- a/embassy-nrf/Cargo.toml +++ b/embassy-nrf/Cargo.toml | |||
| @@ -59,6 +59,8 @@ rand_core = "0.6.3" | |||
| 59 | fixed = "1.10.0" | 59 | fixed = "1.10.0" |
| 60 | embedded-storage = "0.2.0" | 60 | embedded-storage = "0.2.0" |
| 61 | cfg-if = "1.0.0" | 61 | cfg-if = "1.0.0" |
| 62 | nrf-usbd = "0.1.0" | ||
| 63 | usb-device = "0.2.8" | ||
| 62 | 64 | ||
| 63 | nrf52805-pac = { version = "0.10.1", optional = true, features = [ "rt" ] } | 65 | nrf52805-pac = { version = "0.10.1", optional = true, features = [ "rt" ] } |
| 64 | nrf52810-pac = { version = "0.10.1", optional = true, features = [ "rt" ] } | 66 | nrf52810-pac = { version = "0.10.1", optional = true, features = [ "rt" ] } |
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 5a73b87e0..95e3b3e7b 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -73,6 +73,22 @@ pub(crate) use chip::pac; | |||
| 73 | 73 | ||
| 74 | pub use chip::{peripherals, Peripherals}; | 74 | pub use chip::{peripherals, Peripherals}; |
| 75 | 75 | ||
| 76 | use nrf_usbd::{UsbPeripheral, Usbd}; | ||
| 77 | use usb_device::bus::UsbBusAllocator; | ||
| 78 | |||
| 79 | pub struct UsbBus; | ||
| 80 | unsafe impl UsbPeripheral for UsbBus { | ||
| 81 | const REGISTERS: *const () = pac::USBD::ptr() as *const (); | ||
| 82 | } | ||
| 83 | |||
| 84 | impl UsbBus { | ||
| 85 | pub fn new() -> UsbBusAllocator<Usbd<UsbBus>> { | ||
| 86 | Usbd::new(UsbBus) | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | unsafe impl embassy_hal_common::usb::USBInterrupt for interrupt::USBD {} | ||
| 91 | |||
| 76 | pub mod interrupt { | 92 | pub mod interrupt { |
| 77 | pub use crate::chip::irqs::*; | 93 | pub use crate::chip::irqs::*; |
| 78 | pub use cortex_m::interrupt::{CriticalSection, Mutex}; | 94 | pub use cortex_m::interrupt::{CriticalSection, Mutex}; |
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index 5c58541a6..646ba496f 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml | |||
| @@ -9,6 +9,7 @@ version = "0.1.0" | |||
| 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } | 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } |
| 10 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } | 10 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } |
| 11 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote"] } | 11 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote"] } |
| 12 | embassy-hal-common = { version = "0.1.0", path = "../../embassy-hal-common" } | ||
| 12 | 13 | ||
| 13 | defmt = "0.3" | 14 | defmt = "0.3" |
| 14 | defmt-rtt = "0.3" | 15 | defmt-rtt = "0.3" |
| @@ -20,3 +21,7 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } | |||
| 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 21 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 21 | rand = { version = "0.8.4", default-features = false } | 22 | rand = { version = "0.8.4", default-features = false } |
| 22 | embedded-storage = "0.2.0" | 23 | embedded-storage = "0.2.0" |
| 24 | |||
| 25 | usb-device = "0.2" | ||
| 26 | usbd-serial = "0.1.1" | ||
| 27 | nrf-usbd = "0.1.0" | ||
diff --git a/examples/nrf/src/bin/usb_uart.rs b/examples/nrf/src/bin/usb_uart.rs new file mode 100644 index 000000000..b81fd5ee8 --- /dev/null +++ b/examples/nrf/src/bin/usb_uart.rs | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | |||
| 8 | use defmt::{info, unwrap}; | ||
| 9 | use defmt_rtt as _; | ||
| 10 | use embassy::interrupt::InterruptExt; | ||
| 11 | use futures::future::{select, Either}; | ||
| 12 | use futures::pin_mut; | ||
| 13 | // global logger | ||
| 14 | use panic_probe as _; // print out panic messages | ||
| 15 | |||
| 16 | use embassy::executor::Spawner; | ||
| 17 | use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; | ||
| 18 | use embassy::time::{Duration, Timer}; | ||
| 19 | use embassy_hal_common::usb::{State, Usb, UsbSerial}; | ||
| 20 | use embassy_nrf::UsbBus; | ||
| 21 | use embassy_nrf::{interrupt, Peripherals}; | ||
| 22 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; | ||
| 23 | |||
| 24 | #[embassy::main] | ||
| 25 | async fn main(_spawner: Spawner, _p: Peripherals) { | ||
| 26 | let mut tx_buffer = [0u8; 1024]; | ||
| 27 | let mut rx_buffer = [0u8; 640]; | ||
| 28 | |||
| 29 | let usb_bus = UsbBus::new(); | ||
| 30 | |||
| 31 | let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); | ||
| 32 | |||
| 33 | let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) | ||
| 34 | .manufacturer("Fake company") | ||
| 35 | .product("Serial port") | ||
| 36 | .serial_number("TEST") | ||
| 37 | .device_class(0x02) | ||
| 38 | .build(); | ||
| 39 | |||
| 40 | let irq = interrupt::take!(USBD); | ||
| 41 | irq.set_priority(interrupt::Priority::P3); | ||
| 42 | |||
| 43 | let mut state = State::new(); | ||
| 44 | |||
| 45 | let usb = unsafe { Usb::new(&mut state, device, serial, irq) }; | ||
| 46 | pin_mut!(usb); | ||
| 47 | // usb.start(); | ||
| 48 | |||
| 49 | let (mut read_interface, mut write_interface) = usb.as_ref().take_serial_0(); | ||
| 50 | |||
| 51 | unwrap!(write_interface.write_all(b"\r\nSend something\r\n").await); | ||
| 52 | |||
| 53 | info!("usb initialized!"); | ||
| 54 | |||
| 55 | let mut buf = [0u8; 64]; | ||
| 56 | loop { | ||
| 57 | let mut n = 0; | ||
| 58 | let left = { | ||
| 59 | let recv_fut = async { | ||
| 60 | loop { | ||
| 61 | let byte = unwrap!(read_interface.read_byte().await); | ||
| 62 | unwrap!(write_interface.write_byte(byte).await); | ||
| 63 | buf[n] = byte; | ||
| 64 | |||
| 65 | n += 1; | ||
| 66 | if byte == b'\n' || byte == b'\r' || n == buf.len() { | ||
| 67 | break; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | }; | ||
| 71 | pin_mut!(recv_fut); | ||
| 72 | |||
| 73 | let timeout = Timer::after(Duration::from_ticks(32768 * 10)); | ||
| 74 | |||
| 75 | match select(recv_fut, timeout).await { | ||
| 76 | Either::Left(_) => true, | ||
| 77 | Either::Right(_) => false, | ||
| 78 | } | ||
| 79 | }; | ||
| 80 | |||
| 81 | if left { | ||
| 82 | for c in buf[..n].iter_mut() { | ||
| 83 | if 0x61 <= *c && *c <= 0x7a { | ||
| 84 | *c &= !0x20; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | unwrap!(write_interface.write_byte(b'\n').await); | ||
| 88 | unwrap!(write_interface.write_all(&buf[..n]).await); | ||
| 89 | unwrap!(write_interface.write_byte(b'\n').await); | ||
| 90 | } else { | ||
| 91 | unwrap!(write_interface.write_all(b"\r\nSend something\r\n").await); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
