Skip to content

Commit

Permalink
RTIC USB example WIP. Terminates on usb.init. Part of mciantyre#64.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchmindtree committed Jul 5, 2020
1 parent 8cf06cf commit f647e5d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
50 changes: 50 additions & 0 deletions examples/rtic_usb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![no_std]
#![no_main]

use embedded_hal::digital::v2::ToggleableOutputPin;
use panic_halt as _;
use teensy4_bsp as bsp;

#[rtic::app(device = teensy4_bsp, peripherals = true)]
const APP: () = {
struct Resources {
led: bsp::LED,
}

#[init]
fn init(mut cx: init::Context) -> init::LateResources {
init_delay();

// Initialize the USB stack with the default logging settings.
let usb_rx = cx.device.usb.init(bsp::usb::LoggingConfig {
filters: &[("usb", None)],
..Default::default()
});

let led = bsp::configure_led(&mut cx.device.gpr, cx.device.pins.p13);
init::LateResources { led }
}

#[task(binds = USB_OTG1, resources = [led])]
fn usb(cx: usb::Context) {
cx.resources.led.toggle().unwrap();
}

#[idle]
fn idle(_cx: idle::Context) -> ! {
loop {
core::sync::atomic::spin_loop_hint();
}
}
};

// If we reach WFI on teensy 4.0 too quickly it seems to halt. Here we wait a short while in `init`
// to avoid this issue. The issue only appears to occur when rebooting the device (via the button),
// however there appears to be no issue when power cycling the device.
//
// TODO: Investigate exactly why this appears to be necessary.
fn init_delay() {
for _ in 0..10_000_000 {
core::sync::atomic::spin_loop_hint();
}
}
5 changes: 4 additions & 1 deletion src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
//!
//! [`log`]: https://crates.io/crates/log
use crate::interrupt; // bring in interrupt variants for #[interrupt] macro
use core::fmt;
use teensy4_usb_sys as usbsys;

#[cfg(not(feature = "rtic"))]
use crate::interrupt; // bring in interrupt variants for #[interrupt] macro

/// Logging configuration
///
/// Allows a user to specify certain configurations of the logging
Expand Down Expand Up @@ -86,6 +88,7 @@ impl USB {
}
}

#[cfg(not(feature = "rtic"))]
#[crate::rt::interrupt]
fn USB_OTG1() {
unsafe {
Expand Down

0 comments on commit f647e5d

Please sign in to comment.