aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-01-04 07:41:54 +0000
committerGitHub <[email protected]>2022-01-04 07:41:54 +0000
commitf744b74e90d3bb1a571c10f0749f22132b51d303 (patch)
tree9fd7024e03a167b039a1a27696acf265e1323d77
parentcdfd128185897b67bc1e478af6b637c863b66565 (diff)
parent7be34aa22fe735850c61a5a1c3b7304cce6ef102 (diff)
Merge #539
539: nrf: async usb r=Dirbaio a=jacobrosenthal Frankensteined together from this old pr https://github.com/embassy-rs/embassy/pull/115 and nrf-usdb ~Doesnt currently work..~ Co-authored-by: Jacob Rosenthal <[email protected]>
-rw-r--r--embassy-hal-common/src/usb/mod.rs2
-rw-r--r--embassy-nrf/Cargo.toml2
-rw-r--r--embassy-nrf/src/chips/nrf52820.rs5
-rw-r--r--embassy-nrf/src/chips/nrf52833.rs5
-rw-r--r--embassy-nrf/src/chips/nrf52840.rs5
-rw-r--r--embassy-nrf/src/chips/nrf5340_app.rs5
-rw-r--r--embassy-nrf/src/lib.rs7
-rw-r--r--embassy-nrf/src/usb.rs65
-rw-r--r--examples/nrf/Cargo.toml3
-rw-r--r--examples/nrf/src/bin/usb_uart.rs92
-rw-r--r--examples/nrf/src/bin/usb_uart_io.rs69
11 files changed, 259 insertions, 1 deletions
diff --git a/embassy-hal-common/src/usb/mod.rs b/embassy-hal-common/src/usb/mod.rs
index 70a74bd52..bab72d8b6 100644
--- a/embassy-hal-common/src/usb/mod.rs
+++ b/embassy-hal-common/src/usb/mod.rs
@@ -11,7 +11,7 @@ pub mod usb_serial;
11 11
12use crate::peripheral::{PeripheralMutex, PeripheralState, StateStorage}; 12use crate::peripheral::{PeripheralMutex, PeripheralState, StateStorage};
13use embassy::interrupt::Interrupt; 13use embassy::interrupt::Interrupt;
14use usb_serial::{ReadInterface, UsbSerial, WriteInterface}; 14pub use usb_serial::{ReadInterface, UsbSerial, WriteInterface};
15 15
16/// Marker trait to mark an interrupt to be used with the [`Usb`] abstraction. 16/// Marker trait to mark an interrupt to be used with the [`Usb`] abstraction.
17pub unsafe trait USBInterrupt: Interrupt + Send {} 17pub unsafe trait USBInterrupt: Interrupt + Send {}
diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml
index dae9f26ae..3d0c171f3 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 = {version = "0.1.1"}
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/chips/nrf52820.rs b/embassy-nrf/src/chips/nrf52820.rs
index 128e1503f..aa2b2e61d 100644
--- a/embassy-nrf/src/chips/nrf52820.rs
+++ b/embassy-nrf/src/chips/nrf52820.rs
@@ -7,6 +7,9 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
7pub const FLASH_SIZE: usize = 256 * 1024; 7pub const FLASH_SIZE: usize = 256 * 1024;
8 8
9embassy_hal_common::peripherals! { 9embassy_hal_common::peripherals! {
10 // USB
11 USBD,
12
10 // RTC 13 // RTC
11 RTC0, 14 RTC0,
12 RTC1, 15 RTC1,
@@ -122,6 +125,8 @@ embassy_hal_common::peripherals! {
122 TEMP, 125 TEMP,
123} 126}
124 127
128impl_usb!(USBD, USBD, USBD);
129
125impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); 130impl_uarte!(UARTE0, UARTE0, UARTE0_UART0);
126 131
127impl_spim!(TWISPI0, SPIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); 132impl_spim!(TWISPI0, SPIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0);
diff --git a/embassy-nrf/src/chips/nrf52833.rs b/embassy-nrf/src/chips/nrf52833.rs
index 7c7198dfd..498a3c307 100644
--- a/embassy-nrf/src/chips/nrf52833.rs
+++ b/embassy-nrf/src/chips/nrf52833.rs
@@ -7,6 +7,9 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
7pub const FLASH_SIZE: usize = 512 * 1024; 7pub const FLASH_SIZE: usize = 512 * 1024;
8 8
9embassy_hal_common::peripherals! { 9embassy_hal_common::peripherals! {
10 // USB
11 USBD,
12
10 // RTC 13 // RTC
11 RTC0, 14 RTC0,
12 RTC1, 15 RTC1,
@@ -154,6 +157,8 @@ embassy_hal_common::peripherals! {
154 TEMP, 157 TEMP,
155} 158}
156 159
160impl_usb!(USBD, USBD, USBD);
161
157impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); 162impl_uarte!(UARTE0, UARTE0, UARTE0_UART0);
158impl_uarte!(UARTE1, UARTE1, UARTE1); 163impl_uarte!(UARTE1, UARTE1, UARTE1);
159 164
diff --git a/embassy-nrf/src/chips/nrf52840.rs b/embassy-nrf/src/chips/nrf52840.rs
index f5b90cd5a..411768146 100644
--- a/embassy-nrf/src/chips/nrf52840.rs
+++ b/embassy-nrf/src/chips/nrf52840.rs
@@ -7,6 +7,9 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
7pub const FLASH_SIZE: usize = 1024 * 1024; 7pub const FLASH_SIZE: usize = 1024 * 1024;
8 8
9embassy_hal_common::peripherals! { 9embassy_hal_common::peripherals! {
10 // USB
11 USBD,
12
10 // RTC 13 // RTC
11 RTC0, 14 RTC0,
12 RTC1, 15 RTC1,
@@ -157,6 +160,8 @@ embassy_hal_common::peripherals! {
157 TEMP, 160 TEMP,
158} 161}
159 162
163impl_usb!(USBD, USBD, USBD);
164
160impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); 165impl_uarte!(UARTE0, UARTE0, UARTE0_UART0);
161impl_uarte!(UARTE1, UARTE1, UARTE1); 166impl_uarte!(UARTE1, UARTE1, UARTE1);
162 167
diff --git a/embassy-nrf/src/chips/nrf5340_app.rs b/embassy-nrf/src/chips/nrf5340_app.rs
index ca761893f..4fcb742e8 100644
--- a/embassy-nrf/src/chips/nrf5340_app.rs
+++ b/embassy-nrf/src/chips/nrf5340_app.rs
@@ -211,6 +211,9 @@ pub const EASY_DMA_SIZE: usize = (1 << 16) - 1;
211pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; 211pub const FORCE_COPY_BUFFER_SIZE: usize = 1024;
212 212
213embassy_hal_common::peripherals! { 213embassy_hal_common::peripherals! {
214 // USB
215 USBD,
216
214 // RTC 217 // RTC
215 RTC0, 218 RTC0,
216 RTC1, 219 RTC1,
@@ -342,6 +345,8 @@ embassy_hal_common::peripherals! {
342 P1_15, 345 P1_15,
343} 346}
344 347
348impl_usb!(USBD, USBD, USBD);
349
345impl_uarte!(UARTETWISPI0, UARTE0, SERIAL0); 350impl_uarte!(UARTETWISPI0, UARTE0, SERIAL0);
346impl_uarte!(UARTETWISPI1, UARTE1, SERIAL1); 351impl_uarte!(UARTETWISPI1, UARTE1, SERIAL1);
347impl_uarte!(UARTETWISPI2, UARTE2, SERIAL2); 352impl_uarte!(UARTETWISPI2, UARTE2, SERIAL2);
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs
index b8adab473..8e05d9b6a 100644
--- a/embassy-nrf/src/lib.rs
+++ b/embassy-nrf/src/lib.rs
@@ -47,6 +47,13 @@ pub mod temp;
47pub mod timer; 47pub mod timer;
48pub mod twim; 48pub mod twim;
49pub mod uarte; 49pub mod uarte;
50#[cfg(any(
51 feature = "_nrf5340-app",
52 feature = "nrf52820",
53 feature = "nrf52833",
54 feature = "nrf52840"
55))]
56pub mod usb;
50#[cfg(not(feature = "_nrf5340"))] 57#[cfg(not(feature = "_nrf5340"))]
51pub mod wdt; 58pub mod wdt;
52 59
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs
new file mode 100644
index 000000000..deab94544
--- /dev/null
+++ b/embassy-nrf/src/usb.rs
@@ -0,0 +1,65 @@
1#![macro_use]
2
3use crate::interrupt::Interrupt;
4use crate::pac;
5
6use core::marker::PhantomData;
7use embassy::util::Unborrow;
8use nrf_usbd::{UsbPeripheral, Usbd};
9use usb_device::bus::UsbBusAllocator;
10
11pub use embassy_hal_common::usb::*;
12
13pub struct UsbBus<'d, T: Instance> {
14 phantom: PhantomData<&'d mut T>,
15}
16
17unsafe impl<'d, T: Instance> UsbPeripheral for UsbBus<'d, T> {
18 // todo how to use T::regs
19 const REGISTERS: *const () = pac::USBD::ptr() as *const ();
20}
21
22impl<'d, T: Instance> UsbBus<'d, T> {
23 pub fn new(_usb: impl Unborrow<Target = T> + 'd) -> UsbBusAllocator<Usbd<UsbBus<'d, T>>> {
24 let r = T::regs();
25
26 r.intenset.write(|w| {
27 w.sof().set_bit();
28 w.usbevent().set_bit();
29 w.ep0datadone().set_bit();
30 w.ep0setup().set_bit();
31 w.usbreset().set_bit()
32 });
33
34 Usbd::new(UsbBus {
35 phantom: PhantomData,
36 })
37 }
38}
39
40unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {}
41
42pub(crate) mod sealed {
43 use super::*;
44
45 pub trait Instance {
46 fn regs() -> &'static pac::usbd::RegisterBlock;
47 }
48}
49
50pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static + Send {
51 type Interrupt: Interrupt;
52}
53
54macro_rules! impl_usb {
55 ($type:ident, $pac_type:ident, $irq:ident) => {
56 impl crate::usb::sealed::Instance for peripherals::$type {
57 fn regs() -> &'static pac::usbd::RegisterBlock {
58 unsafe { &*pac::$pac_type::ptr() }
59 }
60 }
61 impl crate::usb::Instance for peripherals::$type {
62 type Interrupt = crate::interrupt::$irq;
63 }
64 };
65}
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml
index 5c58541a6..ca013f8b7 100644
--- a/examples/nrf/Cargo.toml
+++ b/examples/nrf/Cargo.toml
@@ -20,3 +20,6 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
20futures = { version = "0.3.17", default-features = false, features = ["async-await"] } 20futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
21rand = { version = "0.8.4", default-features = false } 21rand = { version = "0.8.4", default-features = false }
22embedded-storage = "0.2.0" 22embedded-storage = "0.2.0"
23
24usb-device = "0.2"
25usbd-serial = "0.1.1"
diff --git a/examples/nrf/src/bin/usb_uart.rs b/examples/nrf/src/bin/usb_uart.rs
new file mode 100644
index 000000000..c345df9e3
--- /dev/null
+++ b/examples/nrf/src/bin/usb_uart.rs
@@ -0,0 +1,92 @@
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 _; // global logger
10use embassy::interrupt::InterruptExt;
11use futures::pin_mut;
12use panic_probe as _; // print out panic messages
13
14use embassy::executor::Spawner;
15use embassy::io::{AsyncBufReadExt, AsyncWriteExt};
16use embassy_nrf::usb::{State, Usb, UsbBus, UsbSerial};
17use embassy_nrf::{interrupt, Peripherals};
18use usb_device::device::{UsbDeviceBuilder, UsbVidPid};
19
20#[embassy::main]
21async fn main(_spawner: Spawner, p: Peripherals) {
22 let mut rx_buffer = [0u8; 64];
23 // we send back input + cr + lf
24 let mut tx_buffer = [0u8; 66];
25
26 let usb_bus = UsbBus::new(p.USBD);
27
28 let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer);
29
30 let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
31 .manufacturer("Fake company")
32 .product("Serial port")
33 .serial_number("TEST")
34 .device_class(0x02)
35 .build();
36
37 let irq = interrupt::take!(USBD);
38 irq.set_priority(interrupt::Priority::P3);
39
40 let mut state = State::new();
41 let usb = unsafe { Usb::new(&mut state, device, serial, irq) };
42 pin_mut!(usb);
43
44 let (mut reader, mut writer) = usb.as_ref().take_serial_0();
45
46 info!("usb initialized!");
47
48 unwrap!(
49 writer
50 .write_all(b"\r\nInput returned upper cased on CR+LF\r\n")
51 .await
52 );
53
54 let mut buf = [0u8; 64];
55 loop {
56 let mut n = 0;
57
58 async {
59 loop {
60 let char = unwrap!(reader.read_byte().await);
61
62 // throw away, read more on cr, exit on lf
63 if char == b'\r' {
64 continue;
65 } else if char == b'\n' {
66 break;
67 }
68
69 buf[n] = char;
70 n += 1;
71
72 // stop if we're out of room
73 if n == buf.len() {
74 break;
75 }
76 }
77 }
78 .await;
79
80 if n > 0 {
81 for char in buf[..n].iter_mut() {
82 // upper case
83 if 0x61 <= *char && *char <= 0x7a {
84 *char &= !0x20;
85 }
86 }
87 unwrap!(writer.write_all(&buf[..n]).await);
88 unwrap!(writer.write_all(b"\r\n").await);
89 unwrap!(writer.flush().await);
90 }
91 }
92}
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 000000000..8fc615262
--- /dev/null
+++ b/examples/nrf/src/bin/usb_uart_io.rs
@@ -0,0 +1,69 @@
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 _; // global logger
10use panic_probe as _; // print out panic messages
11
12use embassy::executor::Spawner;
13use embassy::interrupt::InterruptExt;
14use embassy::io::{read_line, AsyncWriteExt};
15use embassy_nrf::usb::{State, Usb, UsbBus, UsbSerial};
16use embassy_nrf::{interrupt, Peripherals};
17use futures::pin_mut;
18use usb_device::device::{UsbDeviceBuilder, UsbVidPid};
19
20#[embassy::main]
21async fn main(_spawner: Spawner, p: Peripherals) {
22 let mut rx_buffer = [0u8; 64];
23 // we send back input + cr + lf
24 let mut tx_buffer = [0u8; 66];
25
26 let usb_bus = UsbBus::new(p.USBD);
27
28 let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer);
29
30 let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
31 .manufacturer("Fake company")
32 .product("Serial port")
33 .serial_number("TEST")
34 .device_class(0x02)
35 .build();
36
37 let irq = interrupt::take!(USBD);
38 irq.set_priority(interrupt::Priority::P3);
39
40 let mut state = State::new();
41 let usb = unsafe { Usb::new(&mut state, device, serial, irq) };
42 pin_mut!(usb);
43
44 let (mut reader, mut writer) = usb.as_ref().take_serial_0();
45
46 info!("usb initialized!");
47
48 unwrap!(
49 writer
50 .write_all(b"\r\nInput returned upper cased on CR+LF\r\n")
51 .await
52 );
53
54 let mut buf = [0u8; 64];
55 loop {
56 let n = unwrap!(read_line(&mut reader, &mut buf).await);
57
58 for char in buf[..n].iter_mut() {
59 // upper case
60 if 0x61 <= *char && *char <= 0x7a {
61 *char &= !0x20;
62 }
63 }
64
65 unwrap!(writer.write_all(&buf[..n]).await);
66 unwrap!(writer.write_all(b"\r\n").await);
67 unwrap!(writer.flush().await);
68 }
69}