diff options
| -rw-r--r-- | embassy-nrf/src/chips/nrf52840.rs | 6 | ||||
| -rw-r--r-- | embassy-nrf/src/lib.rs | 23 | ||||
| -rw-r--r-- | embassy-nrf/src/usb.rs | 68 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb_uart.rs | 6 |
4 files changed, 81 insertions, 22 deletions
diff --git a/embassy-nrf/src/chips/nrf52840.rs b/embassy-nrf/src/chips/nrf52840.rs index f5b90cd5a..5aea06524 100644 --- a/embassy-nrf/src/chips/nrf52840.rs +++ b/embassy-nrf/src/chips/nrf52840.rs | |||
| @@ -7,6 +7,10 @@ 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 | |||
| 11 | // USB | ||
| 12 | USBD, | ||
| 13 | |||
| 10 | // RTC | 14 | // RTC |
| 11 | RTC0, | 15 | RTC0, |
| 12 | RTC1, | 16 | RTC1, |
| @@ -157,6 +161,8 @@ embassy_hal_common::peripherals! { | |||
| 157 | TEMP, | 161 | TEMP, |
| 158 | } | 162 | } |
| 159 | 163 | ||
| 164 | impl_usb!(USBD, USBD, USBD); | ||
| 165 | |||
| 160 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); | 166 | impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); |
| 161 | impl_uarte!(UARTE1, UARTE1, UARTE1); | 167 | impl_uarte!(UARTE1, UARTE1, UARTE1); |
| 162 | 168 | ||
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 13e7803a8..7434905e6 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -48,6 +48,9 @@ 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 | ||
| 52 | #[cfg(feature = "nrf52840")] | ||
| 53 | pub mod usb; | ||
| 51 | #[cfg(not(feature = "_nrf5340"))] | 54 | #[cfg(not(feature = "_nrf5340"))] |
| 52 | pub mod wdt; | 55 | pub mod wdt; |
| 53 | 56 | ||
| @@ -73,26 +76,6 @@ pub(crate) use chip::pac; | |||
| 73 | 76 | ||
| 74 | pub use chip::{peripherals, Peripherals}; | 77 | pub use chip::{peripherals, Peripherals}; |
| 75 | 78 | ||
| 76 | #[cfg(any(feature = "nrf52820", feature = "nrf52833", feature = "nrf52840"))] | ||
| 77 | pub mod usb { | ||
| 78 | |||
| 79 | use nrf_usbd::{UsbPeripheral, Usbd}; | ||
| 80 | use usb_device::bus::UsbBusAllocator; | ||
| 81 | |||
| 82 | pub struct UsbBus; | ||
| 83 | unsafe impl UsbPeripheral for UsbBus { | ||
| 84 | const REGISTERS: *const () = crate::pac::USBD::ptr() as *const (); | ||
| 85 | } | ||
| 86 | |||
| 87 | impl UsbBus { | ||
| 88 | pub fn new() -> UsbBusAllocator<Usbd<UsbBus>> { | ||
| 89 | Usbd::new(UsbBus) | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} | ||
| 94 | } | ||
| 95 | |||
| 96 | pub mod interrupt { | 79 | pub mod interrupt { |
| 97 | pub use crate::chip::irqs::*; | 80 | pub use crate::chip::irqs::*; |
| 98 | pub use cortex_m::interrupt::{CriticalSection, Mutex}; | 81 | pub use cortex_m::interrupt::{CriticalSection, Mutex}; |
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs new file mode 100644 index 000000000..ca1d656a1 --- /dev/null +++ b/embassy-nrf/src/usb.rs | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | #![macro_use] | ||
| 2 | |||
| 3 | use core::marker::PhantomData; | ||
| 4 | use embassy::util::Unborrow; | ||
| 5 | |||
| 6 | use crate::interrupt::Interrupt; | ||
| 7 | use crate::pac; | ||
| 8 | use nrf_usbd::{UsbPeripheral, Usbd}; | ||
| 9 | use usb_device::bus::UsbBusAllocator; | ||
| 10 | |||
| 11 | // todo using different type than Usb because T isnt Send | ||
| 12 | pub struct UsbBus; | ||
| 13 | unsafe impl UsbPeripheral for UsbBus { | ||
| 14 | // todo hardcoding | ||
| 15 | const REGISTERS: *const () = crate::pac::USBD::ptr() as *const (); | ||
| 16 | } | ||
| 17 | |||
| 18 | impl UsbBus { | ||
| 19 | pub fn new() -> UsbBusAllocator<Usbd<UsbBus>> { | ||
| 20 | Usbd::new(UsbBus) | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} | ||
| 25 | |||
| 26 | pub struct Usb<'d, T: Instance> { | ||
| 27 | phantom: PhantomData<&'d mut T>, | ||
| 28 | } | ||
| 29 | |||
| 30 | impl<'d, T: Instance> Usb<'d, T> { | ||
| 31 | #[allow(unused_unsafe)] | ||
| 32 | pub fn new(_usb: impl Unborrow<Target = T> + 'd) -> Self { | ||
| 33 | let r = T::regs(); | ||
| 34 | |||
| 35 | Self { | ||
| 36 | phantom: PhantomData, | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | fn on_interrupt(_: *mut ()) { | ||
| 41 | let r = T::regs(); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | pub(crate) mod sealed { | ||
| 46 | use super::*; | ||
| 47 | |||
| 48 | pub trait Instance { | ||
| 49 | fn regs() -> &'static pac::usbd::RegisterBlock; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static { | ||
| 54 | type Interrupt: Interrupt; | ||
| 55 | } | ||
| 56 | |||
| 57 | macro_rules! impl_usb { | ||
| 58 | ($type:ident, $pac_type:ident, $irq:ident) => { | ||
| 59 | impl crate::usb::sealed::Instance for peripherals::$type { | ||
| 60 | fn regs() -> &'static pac::usbd::RegisterBlock { | ||
| 61 | unsafe { &*pac::$pac_type::ptr() } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | impl crate::usb::Instance for peripherals::$type { | ||
| 65 | type Interrupt = crate::interrupt::$irq; | ||
| 66 | } | ||
| 67 | }; | ||
| 68 | } | ||
diff --git a/examples/nrf/src/bin/usb_uart.rs b/examples/nrf/src/bin/usb_uart.rs index 383edb348..902075dfc 100644 --- a/examples/nrf/src/bin/usb_uart.rs +++ b/examples/nrf/src/bin/usb_uart.rs | |||
| @@ -17,15 +17,17 @@ use embassy::executor::Spawner; | |||
| 17 | use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; | 17 | use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; |
| 18 | use embassy::time::{Duration, Timer}; | 18 | use embassy::time::{Duration, Timer}; |
| 19 | use embassy_hal_common::usb::{State, Usb, UsbSerial}; | 19 | use embassy_hal_common::usb::{State, Usb, UsbSerial}; |
| 20 | use embassy_nrf::usb::UsbBus; | 20 | use embassy_nrf::usb::{Usb as UsbDevice, UsbBus}; |
| 21 | use embassy_nrf::{interrupt, Peripherals}; | 21 | use embassy_nrf::{interrupt, Peripherals}; |
| 22 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; | 22 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; |
| 23 | 23 | ||
| 24 | #[embassy::main] | 24 | #[embassy::main] |
| 25 | async fn main(_spawner: Spawner, _p: Peripherals) { | 25 | async fn main(_spawner: Spawner, p: Peripherals) { |
| 26 | let mut tx_buffer = [0u8; 1024]; | 26 | let mut tx_buffer = [0u8; 1024]; |
| 27 | let mut rx_buffer = [0u8; 640]; | 27 | let mut rx_buffer = [0u8; 640]; |
| 28 | 28 | ||
| 29 | let _usb_dev = UsbDevice::new(p.USBD); | ||
| 30 | |||
| 29 | let usb_bus = UsbBus::new(); | 31 | let usb_bus = UsbBus::new(); |
| 30 | 32 | ||
| 31 | let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); | 33 | let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); |
