aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Rosenthal <[email protected]>2021-12-12 17:13:37 -0700
committerJacob Rosenthal <[email protected]>2021-12-12 19:20:02 -0700
commitf430c0e8c2e9703220978d9976b058ecf0117c45 (patch)
tree03d86f07e877dbd5479680d1c53c747bb00a6e6c
parentdce3f8c47df611b51c47559ba8f4c301eb86af95 (diff)
nrf-usbd
-rw-r--r--embassy-nrf/Cargo.toml2
-rw-r--r--embassy-nrf/src/lib.rs16
-rw-r--r--examples/nrf/Cargo.toml5
-rw-r--r--examples/nrf/src/bin/usb_uart.rs94
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"
59fixed = "1.10.0" 59fixed = "1.10.0"
60embedded-storage = "0.2.0" 60embedded-storage = "0.2.0"
61cfg-if = "1.0.0" 61cfg-if = "1.0.0"
62nrf-usbd = "0.1.0"
63usb-device = "0.2.8"
62 64
63nrf52805-pac = { version = "0.10.1", optional = true, features = [ "rt" ] } 65nrf52805-pac = { version = "0.10.1", optional = true, features = [ "rt" ] }
64nrf52810-pac = { version = "0.10.1", optional = true, features = [ "rt" ] } 66nrf52810-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
74pub use chip::{peripherals, Peripherals}; 74pub use chip::{peripherals, Peripherals};
75 75
76use nrf_usbd::{UsbPeripheral, Usbd};
77use usb_device::bus::UsbBusAllocator;
78
79pub struct UsbBus;
80unsafe impl UsbPeripheral for UsbBus {
81 const REGISTERS: *const () = pac::USBD::ptr() as *const ();
82}
83
84impl UsbBus {
85 pub fn new() -> UsbBusAllocator<Usbd<UsbBus>> {
86 Usbd::new(UsbBus)
87 }
88}
89
90unsafe impl embassy_hal_common::usb::USBInterrupt for interrupt::USBD {}
91
76pub mod interrupt { 92pub 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"
9embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] } 9embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt"] }
10embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } 10embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
11embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote"] } 11embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote"] }
12embassy-hal-common = { version = "0.1.0", path = "../../embassy-hal-common" }
12 13
13defmt = "0.3" 14defmt = "0.3"
14defmt-rtt = "0.3" 15defmt-rtt = "0.3"
@@ -20,3 +21,7 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
20futures = { version = "0.3.17", default-features = false, features = ["async-await"] } 21futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
21rand = { version = "0.8.4", default-features = false } 22rand = { version = "0.8.4", default-features = false }
22embedded-storage = "0.2.0" 23embedded-storage = "0.2.0"
24
25usb-device = "0.2"
26usbd-serial = "0.1.1"
27nrf-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"]
6mod example_common;
7
8use defmt::{info, unwrap};
9use defmt_rtt as _;
10use embassy::interrupt::InterruptExt;
11use futures::future::{select, Either};
12use futures::pin_mut;
13// global logger
14use panic_probe as _; // print out panic messages
15
16use embassy::executor::Spawner;
17use embassy::io::{AsyncBufReadExt, AsyncWriteExt};
18use embassy::time::{Duration, Timer};
19use embassy_hal_common::usb::{State, Usb, UsbSerial};
20use embassy_nrf::UsbBus;
21use embassy_nrf::{interrupt, Peripherals};
22use usb_device::device::{UsbDeviceBuilder, UsbVidPid};
23
24#[embassy::main]
25async 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}