diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-01-04 07:41:54 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-04 07:41:54 +0000 |
| commit | f744b74e90d3bb1a571c10f0749f22132b51d303 (patch) | |
| tree | 9fd7024e03a167b039a1a27696acf265e1323d77 | |
| parent | cdfd128185897b67bc1e478af6b637c863b66565 (diff) | |
| parent | 7be34aa22fe735850c61a5a1c3b7304cce6ef102 (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.rs | 2 | ||||
| -rw-r--r-- | embassy-nrf/Cargo.toml | 2 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf52820.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf52833.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf52840.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf5340_app.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/lib.rs | 7 | ||||
| -rw-r--r-- | embassy-nrf/src/usb.rs | 65 | ||||
| -rw-r--r-- | examples/nrf/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb_uart.rs | 92 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb_uart_io.rs | 69 |
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 | ||
| 12 | use crate::peripheral::{PeripheralMutex, PeripheralState, StateStorage}; | 12 | use crate::peripheral::{PeripheralMutex, PeripheralState, StateStorage}; |
| 13 | use embassy::interrupt::Interrupt; | 13 | use embassy::interrupt::Interrupt; |
| 14 | use usb_serial::{ReadInterface, UsbSerial, WriteInterface}; | 14 | pub 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. |
| 17 | pub unsafe trait USBInterrupt: Interrupt + Send {} | 17 | pub 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" | |||
| 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 = {version = "0.1.1"} | ||
| 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/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; | |||
| 7 | pub const FLASH_SIZE: usize = 256 * 1024; | 7 | pub const FLASH_SIZE: usize = 256 * 1024; |
| 8 | 8 | ||
| 9 | embassy_hal_common::peripherals! { | 9 | embassy_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 | ||
| 128 | impl_usb!(USBD, USBD, USBD); | ||
| 129 | |||
| 125 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); | 130 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); |
| 126 | 131 | ||
| 127 | impl_spim!(TWISPI0, SPIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | 132 | impl_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; | |||
| 7 | pub const FLASH_SIZE: usize = 512 * 1024; | 7 | pub const FLASH_SIZE: usize = 512 * 1024; |
| 8 | 8 | ||
| 9 | embassy_hal_common::peripherals! { | 9 | embassy_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 | ||
| 160 | impl_usb!(USBD, USBD, USBD); | ||
| 161 | |||
| 157 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); | 162 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); |
| 158 | impl_uarte!(UARTE1, UARTE1, UARTE1); | 163 | impl_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; | |||
| 7 | pub const FLASH_SIZE: usize = 1024 * 1024; | 7 | pub const FLASH_SIZE: usize = 1024 * 1024; |
| 8 | 8 | ||
| 9 | embassy_hal_common::peripherals! { | 9 | embassy_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 | ||
| 163 | impl_usb!(USBD, USBD, USBD); | ||
| 164 | |||
| 160 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); | 165 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); |
| 161 | impl_uarte!(UARTE1, UARTE1, UARTE1); | 166 | impl_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; | |||
| 211 | pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; | 211 | pub const FORCE_COPY_BUFFER_SIZE: usize = 1024; |
| 212 | 212 | ||
| 213 | embassy_hal_common::peripherals! { | 213 | embassy_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 | ||
| 348 | impl_usb!(USBD, USBD, USBD); | ||
| 349 | |||
| 345 | impl_uarte!(UARTETWISPI0, UARTE0, SERIAL0); | 350 | impl_uarte!(UARTETWISPI0, UARTE0, SERIAL0); |
| 346 | impl_uarte!(UARTETWISPI1, UARTE1, SERIAL1); | 351 | impl_uarte!(UARTETWISPI1, UARTE1, SERIAL1); |
| 347 | impl_uarte!(UARTETWISPI2, UARTE2, SERIAL2); | 352 | impl_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; | |||
| 47 | pub mod timer; | 47 | pub mod timer; |
| 48 | pub mod twim; | 48 | pub mod twim; |
| 49 | pub mod uarte; | 49 | pub mod uarte; |
| 50 | #[cfg(any( | ||
| 51 | feature = "_nrf5340-app", | ||
| 52 | feature = "nrf52820", | ||
| 53 | feature = "nrf52833", | ||
| 54 | feature = "nrf52840" | ||
| 55 | ))] | ||
| 56 | pub mod usb; | ||
| 50 | #[cfg(not(feature = "_nrf5340"))] | 57 | #[cfg(not(feature = "_nrf5340"))] |
| 51 | pub mod wdt; | 58 | pub 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 | |||
| 3 | use crate::interrupt::Interrupt; | ||
| 4 | use crate::pac; | ||
| 5 | |||
| 6 | use core::marker::PhantomData; | ||
| 7 | use embassy::util::Unborrow; | ||
| 8 | use nrf_usbd::{UsbPeripheral, Usbd}; | ||
| 9 | use usb_device::bus::UsbBusAllocator; | ||
| 10 | |||
| 11 | pub use embassy_hal_common::usb::*; | ||
| 12 | |||
| 13 | pub struct UsbBus<'d, T: Instance> { | ||
| 14 | phantom: PhantomData<&'d mut T>, | ||
| 15 | } | ||
| 16 | |||
| 17 | unsafe 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 | |||
| 22 | impl<'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 | |||
| 40 | unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} | ||
| 41 | |||
| 42 | pub(crate) mod sealed { | ||
| 43 | use super::*; | ||
| 44 | |||
| 45 | pub trait Instance { | ||
| 46 | fn regs() -> &'static pac::usbd::RegisterBlock; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static + Send { | ||
| 51 | type Interrupt: Interrupt; | ||
| 52 | } | ||
| 53 | |||
| 54 | macro_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"] } | |||
| 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 21 | rand = { version = "0.8.4", default-features = false } | 21 | rand = { version = "0.8.4", default-features = false } |
| 22 | embedded-storage = "0.2.0" | 22 | embedded-storage = "0.2.0" |
| 23 | |||
| 24 | usb-device = "0.2" | ||
| 25 | usbd-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"] | ||
| 6 | mod example_common; | ||
| 7 | |||
| 8 | use defmt::{info, unwrap}; | ||
| 9 | use defmt_rtt as _; // global logger | ||
| 10 | use embassy::interrupt::InterruptExt; | ||
| 11 | use futures::pin_mut; | ||
| 12 | use panic_probe as _; // print out panic messages | ||
| 13 | |||
| 14 | use embassy::executor::Spawner; | ||
| 15 | use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; | ||
| 16 | use embassy_nrf::usb::{State, Usb, UsbBus, UsbSerial}; | ||
| 17 | use embassy_nrf::{interrupt, Peripherals}; | ||
| 18 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; | ||
| 19 | |||
| 20 | #[embassy::main] | ||
| 21 | async 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"] | ||
| 6 | mod example_common; | ||
| 7 | |||
| 8 | use defmt::{info, unwrap}; | ||
| 9 | use defmt_rtt as _; // global logger | ||
| 10 | use panic_probe as _; // print out panic messages | ||
| 11 | |||
| 12 | use embassy::executor::Spawner; | ||
| 13 | use embassy::interrupt::InterruptExt; | ||
| 14 | use embassy::io::{read_line, AsyncWriteExt}; | ||
| 15 | use embassy_nrf::usb::{State, Usb, UsbBus, UsbSerial}; | ||
| 16 | use embassy_nrf::{interrupt, Peripherals}; | ||
| 17 | use futures::pin_mut; | ||
| 18 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; | ||
| 19 | |||
| 20 | #[embassy::main] | ||
| 21 | async 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 | } | ||
