aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Rosenthal <[email protected]>2021-12-13 17:50:08 -0700
committerJacob Rosenthal <[email protected]>2021-12-13 18:04:54 -0700
commit83a1237ea3f8b164749cb895b7bdb7a5696107f6 (patch)
tree93b4a873ebcaf88eadc35fd33e39c3b4f1817845
parente5dc63e8e99d1d818ed7ab3b13ffff3ee2c40a3d (diff)
stub out the embassy registers for usbd
-rw-r--r--embassy-nrf/src/chips/nrf52840.rs6
-rw-r--r--embassy-nrf/src/lib.rs23
-rw-r--r--embassy-nrf/src/usb.rs68
-rw-r--r--examples/nrf/src/bin/usb_uart.rs6
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;
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
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
164impl_usb!(USBD, USBD, USBD);
165
160impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); 166impl_uarte!(UARTE0, UARTE0, UARTE0_UART0);
161impl_uarte!(UARTE1, UARTE1, UARTE1); 167impl_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;
48pub mod timer; 48pub mod timer;
49pub mod twim; 49pub mod twim;
50pub mod uarte; 50pub mod uarte;
51//todo add nrf52833 nrf52840
52#[cfg(feature = "nrf52840")]
53pub mod usb;
51#[cfg(not(feature = "_nrf5340"))] 54#[cfg(not(feature = "_nrf5340"))]
52pub mod wdt; 55pub mod wdt;
53 56
@@ -73,26 +76,6 @@ pub(crate) use chip::pac;
73 76
74pub use chip::{peripherals, Peripherals}; 77pub use chip::{peripherals, Peripherals};
75 78
76#[cfg(any(feature = "nrf52820", feature = "nrf52833", feature = "nrf52840"))]
77pub 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
96pub mod interrupt { 79pub 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
3use core::marker::PhantomData;
4use embassy::util::Unborrow;
5
6use crate::interrupt::Interrupt;
7use crate::pac;
8use nrf_usbd::{UsbPeripheral, Usbd};
9use usb_device::bus::UsbBusAllocator;
10
11// todo using different type than Usb because T isnt Send
12pub struct UsbBus;
13unsafe impl UsbPeripheral for UsbBus {
14 // todo hardcoding
15 const REGISTERS: *const () = crate::pac::USBD::ptr() as *const ();
16}
17
18impl UsbBus {
19 pub fn new() -> UsbBusAllocator<Usbd<UsbBus>> {
20 Usbd::new(UsbBus)
21 }
22}
23
24unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {}
25
26pub struct Usb<'d, T: Instance> {
27 phantom: PhantomData<&'d mut T>,
28}
29
30impl<'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
45pub(crate) mod sealed {
46 use super::*;
47
48 pub trait Instance {
49 fn regs() -> &'static pac::usbd::RegisterBlock;
50 }
51}
52
53pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static {
54 type Interrupt: Interrupt;
55}
56
57macro_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;
17use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; 17use embassy::io::{AsyncBufReadExt, AsyncWriteExt};
18use embassy::time::{Duration, Timer}; 18use embassy::time::{Duration, Timer};
19use embassy_hal_common::usb::{State, Usb, UsbSerial}; 19use embassy_hal_common::usb::{State, Usb, UsbSerial};
20use embassy_nrf::usb::UsbBus; 20use embassy_nrf::usb::{Usb as UsbDevice, UsbBus};
21use embassy_nrf::{interrupt, Peripherals}; 21use embassy_nrf::{interrupt, Peripherals};
22use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; 22use usb_device::device::{UsbDeviceBuilder, UsbVidPid};
23 23
24#[embassy::main] 24#[embassy::main]
25async fn main(_spawner: Spawner, _p: Peripherals) { 25async 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);