Skip to content

Commit 74b797e

Browse files
committed
Cleanup examples
1 parent d93fc5e commit 74b797e

File tree

4 files changed

+10
-96
lines changed

4 files changed

+10
-96
lines changed

examples/usb/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ authors = ["Jonas Schievink <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
cortex-m = "0.6.2"
98
cortex-m-rt = "0.6.12"
10-
cortex-m-semihosting = "0.3.5"
119
panic-semihosting = "0.5.3"
1210
nrf52840-pac = "0.9.0"
1311
usb-device = "0.2.7"

examples/usb/examples/serial.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,13 @@ use nrf52840_pac::Peripherals;
1010
use usb_device::device::{UsbDeviceBuilder, UsbVidPid};
1111
use usbd_serial::{SerialPort, USB_CLASS_CDC};
1212

13-
1413
#[entry]
1514
fn main() -> ! {
1615
let periph = Peripherals::take().unwrap();
17-
while !periph
18-
.POWER
19-
.usbregstatus
20-
.read()
21-
.vbusdetect()
22-
.is_vbus_present()
23-
{}
24-
25-
// wait until USB 3.3V supply is stable
26-
while !periph
27-
.POWER
28-
.events_usbpwrrdy
29-
.read()
30-
.events_usbpwrrdy()
31-
.bit_is_clear()
32-
{}
33-
3416
let clocks = Clocks::new(periph.CLOCK);
3517
let clocks = clocks.enable_ext_hfosc();
3618

37-
let usbd = periph.USBD;
38-
39-
let usb_bus = Usbd::new(usbd, &clocks);
19+
let usb_bus = Usbd::new(periph.USBD, &clocks);
4020
let mut serial = SerialPort::new(&usb_bus);
4121

4222
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))

examples/usb/examples/test_class.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,10 @@ use usb_device::test_class::TestClass;
1212
#[entry]
1313
fn main() -> ! {
1414
let periph = Peripherals::take().unwrap();
15-
while !periph
16-
.POWER
17-
.usbregstatus
18-
.read()
19-
.vbusdetect()
20-
.is_vbus_present()
21-
{}
22-
23-
// wait until USB 3.3V supply is stable
24-
while !periph
25-
.POWER
26-
.events_usbpwrrdy
27-
.read()
28-
.events_usbpwrrdy()
29-
.bit_is_clear()
30-
{}
31-
3215
let clocks = Clocks::new(periph.CLOCK);
3316
let clocks = clocks.enable_ext_hfosc();
3417

35-
let usbd = periph.USBD;
36-
let usb_bus = Usbd::new(usbd, &clocks);
18+
let usb_bus = Usbd::new(periph.USBD, &clocks);
3719

3820
let mut test = TestClass::new(&usb_bus);
3921

examples/usb/src/main.rs

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,25 @@
33

44
use panic_semihosting as _;
55

6-
use cortex_m::peripheral::SCB;
76
use cortex_m_rt::entry;
8-
use cortex_m_semihosting::hprintln;
9-
use nrf52840_hal::gpio::{p0, p1, Level};
107
use nrf52840_hal::prelude::*;
118
use nrf52840_hal::timer::{OneShot, Timer};
129
use nrf52840_hal::usbd::Usbd;
1310
use nrf52840_hal::clocks::Clocks;
14-
use nrf52840_pac::{interrupt, Peripherals, TIMER0};
11+
use nrf52840_pac::{Peripherals, TIMER0};
1512
use usb_device::device::{UsbDeviceBuilder, UsbDeviceState, UsbVidPid};
1613
use usbd_serial::{SerialPort, USB_CLASS_CDC};
1714

18-
#[interrupt]
19-
fn TIMER0() {
20-
SCB::sys_reset();
21-
}
22-
2315
#[entry]
2416
fn main() -> ! {
2517
let periph = Peripherals::take().unwrap();
26-
while !periph
27-
.POWER
28-
.usbregstatus
29-
.read()
30-
.vbusdetect()
31-
.is_vbus_present()
32-
{}
33-
34-
// wait until USB 3.3V supply is stable
35-
while !periph
36-
.POWER
37-
.events_usbpwrrdy
38-
.read()
39-
.events_usbpwrrdy()
40-
.bit_is_clear()
41-
{}
42-
4318
let clocks = Clocks::new(periph.CLOCK);
4419
let clocks = clocks.enable_ext_hfosc();
4520

46-
let mut timer = Timer::one_shot(periph.TIMER0);
47-
let usbd = periph.USBD;
48-
let p0 = p0::Parts::new(periph.P0);
49-
let p1 = p1::Parts::new(periph.P1);
50-
51-
let mut led = p0.p0_23.into_push_pull_output(Level::High);
21+
let mut timer = Timer::periodic(periph.TIMER0);
22+
timer.start(Timer::<TIMER0, OneShot>::TICKS_PER_SECOND);
5223

53-
let btn = p1.p1_00.into_pullup_input();
54-
while btn.is_high().unwrap() {}
55-
56-
timer.enable_interrupt();
57-
timer.start(Timer::<TIMER0, OneShot>::TICKS_PER_SECOND * 3);
58-
59-
led.set_low().unwrap();
60-
61-
let usb_bus = Usbd::new(usbd, &clocks);
24+
let usb_bus = Usbd::new(periph.USBD, &clocks);
6225
let mut serial = SerialPort::new(&usb_bus);
6326

6427
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
@@ -67,21 +30,12 @@ fn main() -> ! {
6730
.max_packet_size_0(64) // (makes control transfers 8x faster)
6831
.build();
6932

70-
hprintln!("<start>").ok();
71-
let mut state = UsbDeviceState::Default;
7233
loop {
73-
if !usb_dev.poll(&mut [&mut serial]) {
74-
continue;
75-
}
76-
77-
let new_state = usb_dev.state();
78-
if new_state != state {
79-
hprintln!("{:?} {:#x}", new_state, usb_dev.bus().device_address()).ok();
80-
state = new_state;
34+
usb_dev.poll(&mut [&mut serial]);
8135

82-
if state == UsbDeviceState::Configured {
83-
serial.write(b"Hello, world!\n").unwrap();
84-
serial.flush().unwrap();
36+
if usb_dev.state() == UsbDeviceState::Configured && serial.dtr() {
37+
if timer.wait().is_ok() {
38+
serial.write(b"Hello, world!\n").ok();
8539
}
8640
}
8741
}

0 commit comments

Comments
 (0)