-
Notifications
You must be signed in to change notification settings - Fork 139
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
USB support #295
Merged
Merged
USB support #295
Changes from 38 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
0fbc358
wip
jonas-schievink 86e84a8
errata: use volatile operations
japaric 261eeae
4 byte align bulk/interrupt endpoint buffers
japaric faf0cb3
don't return PollResult::Data when there's no data
japaric 2c3693b
EP0: shoehorn status stage
japaric b9e0eb4
make bulk reads works
japaric 714b4f8
demand a Clocks token
japaric 788ff0f
remove stray print statement
japaric b84300b
make the example compile again
japaric d1d70b4
remove patch
japaric 7fb2998
rebase fixes
japaric 5b216e2
Rebase #144, formerly #133 to pull in USB for nrf52480
wez 1c87602
fixup CI for newly add usb test
wez f36a7e0
Reset shorts on SETUP to prevent spurious status stages
Disasm fa4011e
Introduce EP0State
Disasm c6d6aeb
Rewrite read/write logic
Disasm b938651
Add hack to detect interrupted IN transfers
Disasm 1b3e38b
Inhibit SetAddress response
Disasm e78f3d0
Apply suggestions from code review
Disasm a7a7e84
Replace unsafe borrow with get() and set()
Disasm 84ccc05
Issue a status stage when the transfer data is fully written
Disasm 5d0c0eb
Don't prepare DMA transfer in read_control_setup
Disasm 048e51d
Don't prepare DMA transfers in reset
Disasm 7ad844a
Remove backing buffers
Disasm 48b50e6
Track IN endpoint state
Disasm 6ffd9e1
Remove redundant clear operation
Disasm 922ced5
Check EPDATASTATUS inside write()
Disasm 07b01ec
Merge remote-tracking branch 'upstream/master' into usb
Disasm 4f33ab8
Cleanup
Disasm 2e469df
Implement is_stalled()
Disasm f55b541
Add serial and test_class examples
Disasm d93fc5e
Fix warning
Disasm 74b797e
Cleanup examples
Disasm 9ce50b5
Fix USB demo
6ede424
Clean up Cargo features, support nRF52833
dbe0b5a
Add a 1ms delay to `force_reset`
844b782
Use nrf-usbd
jonas-schievink 3ea49b7
Use crates.io version of nrf-usbd
jonas-schievink 9185196
Address review comments
jonas-schievink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "usb" | ||
version = "0.1.0" | ||
authors = ["Jonas Schievink <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
cortex-m-rt = "0.6.12" | ||
panic-semihosting = "0.5.3" | ||
nrf52840-pac = "0.9.0" | ||
usb-device = "0.2.7" | ||
usbd-serial = "0.1.0" | ||
|
||
[dependencies.nrf52840-hal] | ||
version = "0.12.0" | ||
path = "../../nrf52840-hal" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
[default.probe] | ||
# USB vendor ID | ||
# usb_vid = "1337" | ||
# USB product ID | ||
# usb_pid = "1337" | ||
# Serial number | ||
# serial = "12345678" | ||
# The protocol to be used for communicating with the target. | ||
protocol = "Swd" | ||
# The speed in kHz of the data link to the target. | ||
# speed = 1337 | ||
|
||
[default.flashing] | ||
# Whether or not the target should be flashed. | ||
enabled = true | ||
# Whether or not the target should be halted after reset. | ||
# DEPRECATED, moved to reset section | ||
halt_afterwards = false | ||
# Whether or not bytes erased but not rewritten with data from the ELF | ||
# should be restored with their contents before erasing. | ||
restore_unwritten_bytes = false | ||
# The path where an SVG of the assembled flash layout should be written to. | ||
# flash_layout_output_path = "out.svg" | ||
|
||
[default.reset] | ||
# Whether or not the target should be reset. | ||
# When flashing is enabled as well, the target will be reset after flashing. | ||
enabled = true | ||
# Whether or not the target should be halted after reset. | ||
halt_afterwards = false | ||
|
||
[default.general] | ||
# The chip name of the chip to be debugged. | ||
chip = "nRF52840" | ||
# A list of chip descriptions to be loaded during runtime. | ||
chip_descriptions = [] | ||
# The default log level to be used. Possible values are one of: | ||
# "OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" | ||
log_level = "WARN" | ||
|
||
[default.rtt] | ||
# Whether or not an RTTUI should be opened after flashing. | ||
# This is exclusive and cannot be used with GDB at the moment. | ||
enabled = true | ||
# A list of channel associations to be displayed. If left empty, all channels are displayed. | ||
channels = [ | ||
# { up = 0, down = 0, name = "name", format = "String" } | ||
] | ||
# The duration in ms for which the logger should retry to attach to RTT. | ||
timeout = 3000 | ||
# Whether timestamps in the RTTUI are enabled | ||
show_timestamps = true | ||
# Whether to save rtt history buffer on exit. | ||
log_enabled = false | ||
# Where to save rtt history buffer relative to manifest path. | ||
log_path = "./logs" | ||
|
||
[default.gdb] | ||
# Whether or not a GDB server should be opened after flashing. | ||
# This is exclusive and cannot be used with RTT at the moment. | ||
enabled = false | ||
# The connection string in host:port format wher the GDB server will open a socket. | ||
# gdb_connection_string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# USB examples | ||
|
||
This demo provides two binaries: | ||
|
||
- `serial`: a USB serial "echo" example. | ||
- `test_class`: exposes the test device from the `usb-device` crate. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use panic_semihosting as _; | ||
|
||
use cortex_m_rt::entry; | ||
use nrf52840_hal::clocks::Clocks; | ||
use nrf52840_hal::usbd::{UsbPeripheral, Usbd}; | ||
use nrf52840_pac::Peripherals; | ||
use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; | ||
use usbd_serial::{SerialPort, USB_CLASS_CDC}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let periph = Peripherals::take().unwrap(); | ||
let clocks = Clocks::new(periph.CLOCK); | ||
let clocks = clocks.enable_ext_hfosc(); | ||
|
||
let usb_bus = Usbd::new(UsbPeripheral::new(periph.USBD, &clocks)); | ||
let mut serial = SerialPort::new(&usb_bus); | ||
|
||
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) | ||
.manufacturer("Fake company") | ||
.product("Serial port") | ||
.serial_number("TEST") | ||
.device_class(USB_CLASS_CDC) | ||
.max_packet_size_0(64) // (makes control transfers 8x faster) | ||
.build(); | ||
|
||
loop { | ||
if !usb_dev.poll(&mut [&mut serial]) { | ||
continue; | ||
} | ||
|
||
let mut buf = [0u8; 64]; | ||
|
||
match serial.read(&mut buf) { | ||
Ok(count) if count > 0 => { | ||
// Echo back in upper case | ||
for c in buf[0..count].iter_mut() { | ||
if 0x61 <= *c && *c <= 0x7a { | ||
*c &= !0x20; | ||
} | ||
} | ||
|
||
let mut write_offset = 0; | ||
while write_offset < count { | ||
match serial.write(&buf[write_offset..count]) { | ||
Ok(len) if len > 0 => { | ||
write_offset += len; | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use panic_semihosting as _; | ||
|
||
use cortex_m_rt::entry; | ||
use nrf52840_hal::clocks::Clocks; | ||
use nrf52840_hal::usbd::{UsbPeripheral, Usbd}; | ||
use nrf52840_pac::Peripherals; | ||
use usb_device::test_class::TestClass; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let periph = Peripherals::take().unwrap(); | ||
let clocks = Clocks::new(periph.CLOCK); | ||
let clocks = clocks.enable_ext_hfosc(); | ||
|
||
let usb_bus = Usbd::new(UsbPeripheral::new(periph.USBD, &clocks)); | ||
|
||
let mut test = TestClass::new(&usb_bus); | ||
|
||
let mut usb_dev = { test.make_device(&usb_bus) }; | ||
|
||
loop { | ||
if usb_dev.poll(&mut [&mut test]) { | ||
test.poll(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::clocks::ExternalOscillator; | ||
use crate::pac::USBD; | ||
use crate::Clocks; | ||
|
||
pub use nrf_usbd::Usbd; | ||
|
||
#[allow(dead_code)] // fields are unused and only hold ownership | ||
pub struct UsbPeripheral<'a> { | ||
usbd: USBD, | ||
clocks: &'a (), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
impl<'a> UsbPeripheral<'a> { | ||
pub fn new<L, LSTAT>(usbd: USBD, _clocks: &'a Clocks<ExternalOscillator, L, LSTAT>) -> Self { | ||
Self { usbd, clocks: &() } | ||
} | ||
} | ||
|
||
unsafe impl<'a> nrf_usbd::UsbPeripheral for UsbPeripheral<'a> { | ||
const REGISTERS: *const () = USBD::ptr() as *const _; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit: I think if you name the field
_clocks
the warning goes away