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 <jacobrosenthal@gmail.com>
This commit is contained in:
bors[bot]
2022-01-04 07:41:54 +00:00
committed by GitHub
11 changed files with 259 additions and 1 deletions

View File

@ -59,6 +59,8 @@ rand_core = "0.6.3"
fixed = "1.10.0"
embedded-storage = "0.2.0"
cfg-if = "1.0.0"
nrf-usbd = {version = "0.1.1"}
usb-device = "0.2.8"
nrf52805-pac = { version = "0.10.1", optional = true, features = [ "rt" ] }
nrf52810-pac = { version = "0.10.1", optional = true, features = [ "rt" ] }

View File

@ -7,6 +7,9 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
pub const FLASH_SIZE: usize = 256 * 1024;
embassy_hal_common::peripherals! {
// USB
USBD,
// RTC
RTC0,
RTC1,
@ -122,6 +125,8 @@ embassy_hal_common::peripherals! {
TEMP,
}
impl_usb!(USBD, USBD, USBD);
impl_uarte!(UARTE0, UARTE0, UARTE0_UART0);
impl_spim!(TWISPI0, SPIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0);

View File

@ -7,6 +7,9 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
pub const FLASH_SIZE: usize = 512 * 1024;
embassy_hal_common::peripherals! {
// USB
USBD,
// RTC
RTC0,
RTC1,
@ -154,6 +157,8 @@ embassy_hal_common::peripherals! {
TEMP,
}
impl_usb!(USBD, USBD, USBD);
impl_uarte!(UARTE0, UARTE0, UARTE0_UART0);
impl_uarte!(UARTE1, UARTE1, UARTE1);

View File

@ -7,6 +7,9 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
pub const FLASH_SIZE: usize = 1024 * 1024;
embassy_hal_common::peripherals! {
// USB
USBD,
// RTC
RTC0,
RTC1,
@ -157,6 +160,8 @@ embassy_hal_common::peripherals! {
TEMP,
}
impl_usb!(USBD, USBD, USBD);
impl_uarte!(UARTE0, UARTE0, UARTE0_UART0);
impl_uarte!(UARTE1, UARTE1, UARTE1);

View File

@ -211,6 +211,9 @@ pub const EASY_DMA_SIZE: usize = (1 << 16) - 1;
pub const FORCE_COPY_BUFFER_SIZE: usize = 1024;
embassy_hal_common::peripherals! {
// USB
USBD,
// RTC
RTC0,
RTC1,
@ -342,6 +345,8 @@ embassy_hal_common::peripherals! {
P1_15,
}
impl_usb!(USBD, USBD, USBD);
impl_uarte!(UARTETWISPI0, UARTE0, SERIAL0);
impl_uarte!(UARTETWISPI1, UARTE1, SERIAL1);
impl_uarte!(UARTETWISPI2, UARTE2, SERIAL2);

View File

@ -47,6 +47,13 @@ pub mod temp;
pub mod timer;
pub mod twim;
pub mod uarte;
#[cfg(any(
feature = "_nrf5340-app",
feature = "nrf52820",
feature = "nrf52833",
feature = "nrf52840"
))]
pub mod usb;
#[cfg(not(feature = "_nrf5340"))]
pub mod wdt;

65
embassy-nrf/src/usb.rs Normal file
View File

@ -0,0 +1,65 @@
#![macro_use]
use crate::interrupt::Interrupt;
use crate::pac;
use core::marker::PhantomData;
use embassy::util::Unborrow;
use nrf_usbd::{UsbPeripheral, Usbd};
use usb_device::bus::UsbBusAllocator;
pub use embassy_hal_common::usb::*;
pub struct UsbBus<'d, T: Instance> {
phantom: PhantomData<&'d mut T>,
}
unsafe impl<'d, T: Instance> UsbPeripheral for UsbBus<'d, T> {
// todo how to use T::regs
const REGISTERS: *const () = pac::USBD::ptr() as *const ();
}
impl<'d, T: Instance> UsbBus<'d, T> {
pub fn new(_usb: impl Unborrow<Target = T> + 'd) -> UsbBusAllocator<Usbd<UsbBus<'d, T>>> {
let r = T::regs();
r.intenset.write(|w| {
w.sof().set_bit();
w.usbevent().set_bit();
w.ep0datadone().set_bit();
w.ep0setup().set_bit();
w.usbreset().set_bit()
});
Usbd::new(UsbBus {
phantom: PhantomData,
})
}
}
unsafe impl embassy_hal_common::usb::USBInterrupt for crate::interrupt::USBD {}
pub(crate) mod sealed {
use super::*;
pub trait Instance {
fn regs() -> &'static pac::usbd::RegisterBlock;
}
}
pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static + Send {
type Interrupt: Interrupt;
}
macro_rules! impl_usb {
($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::usb::sealed::Instance for peripherals::$type {
fn regs() -> &'static pac::usbd::RegisterBlock {
unsafe { &*pac::$pac_type::ptr() }
}
}
impl crate::usb::Instance for peripherals::$type {
type Interrupt = crate::interrupt::$irq;
}
};
}