aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Rosenthal <[email protected]>2021-12-15 10:11:00 -0700
committerJacob Rosenthal <[email protected]>2021-12-15 10:23:19 -0700
commit61f12324ff987cc1b8bbd8fe3e7096bd7285485d (patch)
treef8a933b292e0f2c8dfb4a87b2bb64aa35aa715d1
parent1c0a3688a478cd05067c879751180097e3abf491 (diff)
enable USB peripheral for relevant chips
-rw-r--r--embassy-nrf/src/chips/nrf52820.rs3
-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.rs8
-rw-r--r--embassy-nrf/src/usb.rs54
-rw-r--r--examples/nrf/src/bin/usb_uart.rs4
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;
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,
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 7434905e6..3cd1768c1 100644
--- a/embassy-nrf/src/lib.rs
+++ b/embassy-nrf/src/lib.rs
@@ -48,8 +48,12 @@ 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 51#[cfg(any(
52#[cfg(feature = "nrf52840")] 52 feature = "_nrf5340-app",
53 feature = "nrf52820",
54 feature = "nrf52833",
55 feature = "nrf52840"
56))]
53pub mod usb; 57pub mod usb;
54#[cfg(not(feature = "_nrf5340"))] 58#[cfg(not(feature = "_nrf5340"))]
55pub mod wdt; 59pub 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
3pub use embassy_hal_common::usb::*; 3use crate::interrupt::Interrupt;
4use crate::pac;
5
6use core::marker::PhantomData;
7use embassy::util::Unborrow;
4use nrf_usbd::{UsbPeripheral, Usbd}; 8use nrf_usbd::{UsbPeripheral, Usbd};
5use usb_device::bus::UsbBusAllocator; 9use usb_device::bus::UsbBusAllocator;
6 10
7pub struct UsbBus; 11pub use embassy_hal_common::usb::*;
8unsafe impl UsbPeripheral for UsbBus { 12
9 // todo hardcoding 13pub 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
13impl UsbBus { 17unsafe 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
21impl<'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
30unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {} 39unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {}
40
41pub(crate) mod sealed {
42 use super::*;
43
44 pub trait Instance {
45 fn regs() -> &'static pac::usbd::RegisterBlock;
46 }
47}
48
49pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static + Send {
50 type Interrupt: Interrupt;
51}
52
53macro_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};
21use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; 21use usb_device::device::{UsbDeviceBuilder, UsbVidPid};
22 22
23#[embassy::main] 23#[embassy::main]
24async fn main(_spawner: Spawner, _p: Peripherals) { 24async 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