aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf/src
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 /examples/nrf/src
parentdce3f8c47df611b51c47559ba8f4c301eb86af95 (diff)
nrf-usbd
Diffstat (limited to 'examples/nrf/src')
-rw-r--r--examples/nrf/src/bin/usb_uart.rs94
1 files changed, 94 insertions, 0 deletions
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}