diff options
| author | Jacob Rosenthal <[email protected]> | 2021-12-15 10:11:00 -0700 |
|---|---|---|
| committer | Jacob Rosenthal <[email protected]> | 2021-12-15 10:23:19 -0700 |
| commit | 61f12324ff987cc1b8bbd8fe3e7096bd7285485d (patch) | |
| tree | f8a933b292e0f2c8dfb4a87b2bb64aa35aa715d1 | |
| parent | 1c0a3688a478cd05067c879751180097e3abf491 (diff) | |
enable USB peripheral for relevant chips
| -rw-r--r-- | embassy-nrf/src/chips/nrf52820.rs | 3 | ||||
| -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 | 8 | ||||
| -rw-r--r-- | embassy-nrf/src/usb.rs | 54 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb_uart.rs | 4 |
7 files changed, 70 insertions, 14 deletions
diff --git a/embassy-nrf/src/chips/nrf52820.rs b/embassy-nrf/src/chips/nrf52820.rs index 128e1503f..8aa07bb73 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, |
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 7434905e6..3cd1768c1 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -48,8 +48,12 @@ pub mod temp; | |||
| 48 | pub mod timer; | 48 | pub mod timer; |
| 49 | pub mod twim; | 49 | pub mod twim; |
| 50 | pub mod uarte; | 50 | pub mod uarte; |
| 51 | //todo add nrf52833 nrf52840 | 51 | #[cfg(any( |
| 52 | #[cfg(feature = "nrf52840")] | 52 | feature = "_nrf5340-app", |
| 53 | feature = "nrf52820", | ||
| 54 | feature = "nrf52833", | ||
| 55 | feature = "nrf52840" | ||
| 56 | ))] | ||
| 53 | pub mod usb; | 57 | pub mod usb; |
| 54 | #[cfg(not(feature = "_nrf5340"))] | 58 | #[cfg(not(feature = "_nrf5340"))] |
| 55 | pub mod wdt; | 59 | pub mod wdt; |
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index 39e53429a..d0944b7f0 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs | |||
| @@ -1,20 +1,27 @@ | |||
| 1 | #![macro_use] | 1 | #![macro_use] |
| 2 | 2 | ||
| 3 | pub use embassy_hal_common::usb::*; | 3 | use crate::interrupt::Interrupt; |
| 4 | use crate::pac; | ||
| 5 | |||
| 6 | use core::marker::PhantomData; | ||
| 7 | use embassy::util::Unborrow; | ||
| 4 | use nrf_usbd::{UsbPeripheral, Usbd}; | 8 | use nrf_usbd::{UsbPeripheral, Usbd}; |
| 5 | use usb_device::bus::UsbBusAllocator; | 9 | use usb_device::bus::UsbBusAllocator; |
| 6 | 10 | ||
| 7 | pub struct UsbBus; | 11 | pub use embassy_hal_common::usb::*; |
| 8 | unsafe impl UsbPeripheral for UsbBus { | 12 | |
| 9 | // todo hardcoding | 13 | pub struct UsbBus<'d, T: Instance> { |
| 10 | const REGISTERS: *const () = crate::pac::USBD::ptr() as *const (); | 14 | phantom: PhantomData<&'d mut T>, |
| 11 | } | 15 | } |
| 12 | 16 | ||
| 13 | impl UsbBus { | 17 | unsafe impl<'d, T: Instance> UsbPeripheral for UsbBus<'d, T> { |
| 14 | // todo should it consume a USBD peripheral? | 18 | const REGISTERS: *const () = T::regs as *const (); |
| 15 | pub fn new() -> UsbBusAllocator<Usbd<UsbBus>> { | 19 | } |
| 20 | |||
| 21 | impl<'d, T: Instance> UsbBus<'d, T> { | ||
| 22 | pub fn new(_usb: impl Unborrow<Target = T> + 'd) -> UsbBusAllocator<Usbd<UsbBus<'d, T>>> { | ||
| 16 | unsafe { | 23 | unsafe { |
| 17 | (*crate::pac::USBD::ptr()).intenset.write(|w| { | 24 | (*pac::USBD::ptr()).intenset.write(|w| { |
| 18 | w.sof().set_bit(); | 25 | w.sof().set_bit(); |
| 19 | w.usbevent().set_bit(); | 26 | w.usbevent().set_bit(); |
| 20 | w.ep0datadone().set_bit(); | 27 | w.ep0datadone().set_bit(); |
| @@ -23,8 +30,35 @@ impl UsbBus { | |||
| 23 | }) | 30 | }) |
| 24 | }; | 31 | }; |
| 25 | 32 | ||
| 26 | Usbd::new(UsbBus) | 33 | Usbd::new(UsbBus { |
| 34 | phantom: PhantomData, | ||
| 35 | }) | ||
| 27 | } | 36 | } |
| 28 | } | 37 | } |
| 29 | 38 | ||
| 30 | unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} | 39 | unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} |
| 40 | |||
| 41 | pub(crate) mod sealed { | ||
| 42 | use super::*; | ||
| 43 | |||
| 44 | pub trait Instance { | ||
| 45 | fn regs() -> &'static pac::usbd::RegisterBlock; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static + Send { | ||
| 50 | type Interrupt: Interrupt; | ||
| 51 | } | ||
| 52 | |||
| 53 | macro_rules! impl_usb { | ||
| 54 | ($type:ident, $pac_type:ident, $irq:ident) => { | ||
| 55 | impl crate::usb::sealed::Instance for peripherals::$type { | ||
| 56 | fn regs() -> &'static pac::usbd::RegisterBlock { | ||
| 57 | unsafe { &*pac::$pac_type::ptr() } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | impl crate::usb::Instance for peripherals::$type { | ||
| 61 | type Interrupt = crate::interrupt::$irq; | ||
| 62 | } | ||
| 63 | }; | ||
| 64 | } | ||
diff --git a/examples/nrf/src/bin/usb_uart.rs b/examples/nrf/src/bin/usb_uart.rs index 1df37d4ba..fc7ee03b8 100644 --- a/examples/nrf/src/bin/usb_uart.rs +++ b/examples/nrf/src/bin/usb_uart.rs | |||
| @@ -21,11 +21,11 @@ use embassy_nrf::{interrupt, Peripherals}; | |||
| 21 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; | 21 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; |
| 22 | 22 | ||
| 23 | #[embassy::main] | 23 | #[embassy::main] |
| 24 | async fn main(_spawner: Spawner, _p: Peripherals) { | 24 | async fn main(_spawner: Spawner, p: Peripherals) { |
| 25 | let mut tx_buffer = [0u8; 1024]; | 25 | let mut tx_buffer = [0u8; 1024]; |
| 26 | let mut rx_buffer = [0u8; 640]; | 26 | let mut rx_buffer = [0u8; 640]; |
| 27 | 27 | ||
| 28 | let usb_bus = UsbBus::new(); | 28 | let usb_bus = UsbBus::new(p.USBD); |
| 29 | 29 | ||
| 30 | let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); | 30 | let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); |
| 31 | 31 | ||
