Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] USB support #133

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"examples/rtfm-demo",
"examples/spi-demo",
"examples/twi-ssd1306",
"examples/usb",
]

[profile.dev]
Expand All @@ -26,3 +27,9 @@ debug = true
lto = true
opt-level = "s"

[patch.crates-io.usb-device]
git = "https://github.com/jonas-schievink/usb-device.git"
branch = "inhibit-setaddr-resp"

[patch.crates-io.panic-semihosting]
path = "../panic-semihosting"
2 changes: 2 additions & 0 deletions examples/usb/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = 'arm-none-eabi-gdb'
13 changes: 13 additions & 0 deletions examples/usb/.gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# disable "are you sure you want to quit?"
define hook-quit
set confirm off
end

target remote :3333

# print demangled symbols by default
set print asm-demangle on

monitor arm semihosting enable
load
cont
18 changes: 18 additions & 0 deletions examples/usb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "usb"
version = "0.1.0"
authors = ["Jonas Schievink <[email protected]>"]
edition = "2018"

[dependencies]
cortex-m = "0.6.2"
cortex-m-rt = "0.6.12"
cortex-m-semihosting = "0.3.5"
panic-semihosting = "0.5.3"
nrf52840-pac = "0.8.0"
usb-device = "0.2.5"
usbd-serial = "0.1.0"

[dependencies.nrf52840-hal]
version = "0.8.0"
path = "../../nrf52840-hal"
14 changes: 14 additions & 0 deletions examples/usb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# spi-demo
SPIM demonstation code.
Connect a resistor between pin 22 and 23 on the demo board to feed MOSI directly back to MISO
If all tests pass all four Led (Led1 to Led4) will light up, in case of error only at least one of the Led will remain turned off.


## HW connections
Pin Connecton
P0.24 SPIclk
P0.23 MOSI
P0.22 MISO

This is designed for nRF52-DK board:
https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52-DK
72 changes: 72 additions & 0 deletions examples/usb/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#![no_std]
#![no_main]

use panic_semihosting as _;

use cortex_m::peripheral::SCB;
use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use nrf52840_hal::gpio::{p0, p1, Level};
use nrf52840_hal::prelude::*;
use nrf52840_hal::timer::{OneShot, Timer};
use nrf52840_hal::usbd::Usbd;
use nrf52840_pac::{interrupt, Peripherals, TIMER0};
use usb_device::device::{UsbDeviceBuilder, UsbDeviceState, UsbVidPid};
use usbd_serial::{SerialPort, USB_CLASS_CDC};

#[interrupt]
fn TIMER0() {
SCB::sys_reset();
}

#[entry]
fn main() -> ! {
static mut EP_BUF: [u8; 256] = [0; 256];

let core = cortex_m::Peripherals::take().unwrap();
let periph = Peripherals::take().unwrap();
while !periph
.POWER
.usbregstatus
.read()
.vbusdetect()
.is_vbus_present()
{}

let mut nvic = core.NVIC;
let mut timer = Timer::one_shot(periph.TIMER0);
let usbd = periph.USBD;
let p0 = p0::Parts::new(periph.P0);
let p1 = p1::Parts::new(periph.P1);

let mut led = p0.p0_23.into_push_pull_output(Level::High);
let btn = p1.p1_00.into_pullup_input();
while btn.is_high().unwrap() {}

timer.enable_interrupt(Some(&mut nvic));
timer.start(Timer::<TIMER0, OneShot>::TICKS_PER_SECOND * 3);

led.set_low().unwrap();

let usb_bus = Usbd::new_alloc(usbd, EP_BUF);
let mut serial = SerialPort::new(&usb_bus);

let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
.product("nRF52840 Serial Port Demo")
.device_class(USB_CLASS_CDC)
.build();

hprintln!("<start>").ok();
let mut state = UsbDeviceState::Default;
loop {
if !usb_dev.poll(&mut [&mut serial]) {
continue;
}

let new_state = usb_dev.state();
if new_state != state {
hprintln!("{:?} {:#x}", new_state, usb_dev.bus().device_address()).ok();
state = new_state;
}
}
}
6 changes: 5 additions & 1 deletion nrf52-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ version = "1.0.2"
default-features = false
version = "0.2.2"

[dependencies.usb-device]
version = "0.2.5"
optional = true

[dependencies.nrf52810-pac]
optional = true
version = "0.8.0"
Expand All @@ -56,5 +60,5 @@ doc = []
default = ["52832"]
52810 = ["nrf52810-pac"]
52832 = ["nrf52832-pac"]
52840 = ["nrf52840-pac"]
52840 = ["nrf52840-pac", "usb-device"]
9160 = ["nrf9160-pac"]
2 changes: 2 additions & 0 deletions nrf52-hal-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub mod time;
pub mod timer;
pub mod twim;
pub mod uarte;
#[cfg(feature = "52840")]
pub mod usbd;

pub mod prelude {
pub use crate::hal::digital::v2::*;
Expand Down
57 changes: 57 additions & 0 deletions nrf52-hal-common/src/usbd/errata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// Writes `val` to `addr`. Used to apply Errata workarounds.
unsafe fn poke(addr: u32, val: u32) {
*(addr as *mut u32) = val;
}

/// Reads 32 bits from `addr`.
unsafe fn peek(addr: u32) -> u32 {
*(addr as *mut u32)
}

Comment on lines +1 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be volatile operations ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought I made them volatile...

pub fn pre_enable() {
// Works around Erratum 187 on chip revisions 1 and 2.
unsafe {
poke(0x4006EC00, 0x00009375);
poke(0x4006ED14, 0x00000003);
poke(0x4006EC00, 0x00009375);
}

pre_wakeup();
}

pub fn post_enable() {
post_wakeup();

// Works around Erratum 187 on chip revisions 1 and 2.
unsafe {
poke(0x4006EC00, 0x00009375);
poke(0x4006ED14, 0x00000000);
poke(0x4006EC00, 0x00009375);
}
}

pub fn pre_wakeup() {
// Works around Erratum 171 on chip revisions 1 and 2.

unsafe {
if peek(0x4006EC00) == 0x00000000 {
poke(0x4006EC00, 0x00009375);
}

poke(0x4006EC14, 0x000000C0);
poke(0x4006EC00, 0x00009375);
}
}

pub fn post_wakeup() {
// Works around Erratum 171 on chip revisions 1 and 2.

unsafe {
if peek(0x4006EC00) == 0x00000000 {
poke(0x4006EC00, 0x00009375);
}

poke(0x4006EC14, 0x00000000);
poke(0x4006EC00, 0x00009375);
}
}
Loading