aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/usb.rs
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2019-07-24 17:35:24 -0400
committerBrian Cully <bjc@kublai.com>2019-07-24 17:35:24 -0400
commit87015d0256e5c3888681da6d469e515b58efbb5f (patch)
treedcab0ddbe85a09b988808ce00729bf1cdd3927c9 /app/src/usb.rs
parentcd47fb1f4887ea863b7e90d476a8366634ce9980 (diff)
downloadsamd21-demo-87015d0256e5c3888681da6d469e515b58efbb5f.tar.gz
samd21-demo-87015d0256e5c3888681da6d469e515b58efbb5f.zip
Use log crate instead of hand-rolled solution.
* Remove my logging macros except *_now variants, since those are still handy. * Move log flush into RTC handler. This probably belongs in a separate timer at some point. * Change all old log messages to log crate's macros, except the Panic and HardFault calls.
Diffstat (limited to 'app/src/usb.rs')
-rw-r--r--app/src/usb.rs72
1 files changed, 34 insertions, 38 deletions
diff --git a/app/src/usb.rs b/app/src/usb.rs
index acb4457..a5788a1 100644
--- a/app/src/usb.rs
+++ b/app/src/usb.rs
@@ -1,12 +1,14 @@
mod pipe;
mod usbproto;
-use crate::{log, log_now, logln, logln_now, rtc};
use pipe::{DataBuf, PipeErr, PipeTable, USBPipeType, USBToken};
use rb::{Reader, RingBuffer, Writer};
use usbproto::*;
+use crate::rtc;
+
use embedded_hal::digital::v2::OutputPin;
+use log::{info, warn};
use trinket_m0::{
calibration::{usb_transn_cal, usb_transp_cal, usb_trim_cal},
clock::{ClockGenId, ClockSource, GenericClockController},
@@ -155,7 +157,7 @@ impl USBHost {
rc.host_enable_pin = Some(he_pin.into_open_drain_output(port));
}
- logln_now!("setting up usb clock");
+ info!("setting up usb clock");
pm.apbbmask.modify(|_, w| w.usb_().set_bit());
// Set up USB clock from 48MHz source on generic clock 6.
@@ -172,7 +174,7 @@ impl USBHost {
}
pub fn reset_periph(&mut self) {
- log!("resetting usb");
+ info!("resetting usb");
// Reset the USB peripheral and wait for sync.
self.usb.host().ctrla.write(|w| w.swrst().set_bit());
while self.usb.host().syncbusy.read().swrst().bit_is_set() {}
@@ -220,14 +222,14 @@ impl USBHost {
// Set VBUS OK to allow host operation.
self.usb.host().ctrlb.modify(|_, w| w.vbusok().set_bit());
- logln!("...done");
+ info!("...done");
}
pub fn task(&mut self) {
static mut LAST_EVENT: Event = Event::Error;
unsafe {
if LAST_EVENT != LATEST_EVENT {
- logln!("new event: {:?}", LATEST_EVENT);
+ info!("new event: {:?}", LATEST_EVENT);
}
}
@@ -257,12 +259,9 @@ impl USBHost {
let bits = self.usb.host().intflag.read().bits();
unsafe {
if LAST_CBITS != cbits || LAST_FLAGS != bits || LAST_TASK_STATE != self.task_state {
- logln!(
+ info!(
"cb: {:x}, f: {:x} changing state {:?} -> {:?}",
- cbits,
- bits,
- LAST_TASK_STATE,
- self.task_state,
+ cbits, bits, LAST_TASK_STATE, self.task_state,
);
}
LAST_CBITS = cbits;
@@ -271,7 +270,7 @@ impl USBHost {
};
if let Some(_event) = self.events.shift() {
- // logln!("Found event: {:?}", event);
+ // info!("Found event: {:?}", event);
// self.task_state = match event {
// Event::None => TaskState::Detached(DetachedState::Illegal),
// Event::Detached => {
@@ -342,7 +341,7 @@ impl USBHost {
AttachedState::WaitResetComplete => {
if self.usb.host().intflag.read().rst().bit_is_set() {
- logln!("reset was sent");
+ info!("reset was sent");
self.usb.host().intflag.write(|w| w.rst().set_bit());
// Make sure we always have a control pipe set up.
@@ -375,7 +374,7 @@ impl USBHost {
self.task_state = match self.configure_dev(0, 0, low_speed) {
Ok(_) => TaskState::Steady(SteadyState::Running),
Err(e) => {
- logln!("Enumeration error: {:?}", e);
+ warn!("Enumeration error: {:?}", e);
TaskState::Steady(SteadyState::Error)
}
}
@@ -403,14 +402,11 @@ impl USBHost {
)?;
let desc = vol_descr.get();
- logln_now!(
+ info!(
" -- len: {}, ver: {:04x}, bMaxPacketSize: {}, bNumConfigurations: {}",
- desc.b_length,
- desc.bcd_usb,
- desc.b_max_packet_size,
- desc.b_num_configurations
+ desc.b_length, desc.bcd_usb, desc.b_max_packet_size, desc.b_num_configurations
);
- logln_now!(" -- vid: {:x}, pid: {:x}", desc.id_vendor, desc.id_product);
+ info!(" -- vid: {:x}, pid: {:x}", desc.id_vendor, desc.id_product);
// Assign address to this device and:
// - Stash bMaxPacketSize
@@ -425,12 +421,12 @@ impl USBHost {
0,
None,
)?;
- logln_now!(" -- address set");
+ info!(" -- address set");
// Delay according to §9.2.6.3 of USB 2.0
let until = rtc::millis() + 300;
while rtc::millis() < until {}
- logln_now!("getting config");
+ info!("getting config");
let tmp: USBConfigurationDescriptor = Default::default();
//let vol_descr = ::vcell::VolatileCell::new(tmp);
self.control_req(
@@ -444,10 +440,10 @@ impl USBHost {
)?;
//let desc = vol_descr.get();
- logln_now!("cdesc.len: {}", tmp.b_length);
+ info!("cdesc.len: {}", tmp.b_length);
// Once addressed, SET_CONFIGURATION(0)
- logln_now!("+++ setting configuration");
+ info!("+++ setting configuration");
let conf: u8 = 0;
self.control_req(
new_address,
@@ -458,7 +454,7 @@ impl USBHost {
0,
None,
)?;
- logln_now!(" -- configuration set");
+ info!(" -- configuration set");
// Now we should be able to access it normally.
@@ -502,13 +498,13 @@ impl USBHost {
if let Some(b) = buf {
match bm_request_type.direction() {
USBSetupDirection::DeviceToHost => {
- logln_now!("buf0: {:?}", &b);
+ info!("buf0: {:?}", &b);
pipe.in_transfer(&b, NAK_LIMIT)?;
- logln_now!("buf1: {:?}", &b);
+ info!("buf1: {:?}", &b);
}
USBSetupDirection::HostToDevice => {
- logln_now!("Should OUT for {}b", b.len);
+ info!("Should OUT for {}b", b.len);
}
}
}
@@ -532,7 +528,7 @@ impl USBHost {
// TODO: should probably make `pipe.send` have optional
// `DataBuf`, rather than exposing `dispatch_retries`.
- logln_now!("dispatching status stage");
+ info!("dispatching status stage");
pipe.dispatch_retries(token, NAK_LIMIT)?;
Ok(())
}
@@ -556,36 +552,36 @@ pub fn handler(usbp: usize, events: &mut EventWriter) {
let usb: &mut USB = unsafe { core::mem::transmute(usbp) };
let flags = usb.host().intflag.read();
- logln!("USB - {:x}", flags.bits());
+ info!("USB - {:x}", flags.bits());
let mut unshift_event = |e: Event| {
unsafe { LATEST_EVENT = e };
if let Err(_) = events.unshift(e) {
- logln!("Couldn't write USB event to queue.");
+ info!("Couldn't write USB event to queue.");
}
};
if flags.hsof().bit_is_set() {
- logln!(" +hsof");
+ info!(" +hsof");
usb.host().intflag.write(|w| w.hsof().set_bit());
unshift_event(Event::Attached);
}
if flags.rst().bit_is_set() {
// We seem to get this whenever a device attaches/detaches.
- logln!(" +rst");
+ info!(" +rst");
usb.host().intflag.write(|w| w.rst().set_bit());
unshift_event(Event::Detached);
}
if flags.uprsm().bit_is_set() {
- logln!(" +uprsm");
+ info!(" +uprsm");
usb.host().intflag.write(|w| w.uprsm().set_bit());
unshift_event(Event::Detached);
}
if flags.dnrsm().bit_is_set() {
- logln!(" +dnrsm");
+ info!(" +dnrsm");
usb.host().intflag.write(|w| w.dnrsm().set_bit());
unshift_event(Event::Detached);
}
@@ -593,19 +589,19 @@ pub fn handler(usbp: usize, events: &mut EventWriter) {
if flags.wakeup().bit_is_set() {
// §32.8.5.8 - since VBUSOK is set, then this happens when a
// device is connected.
- logln!(" +wakeup");
+ info!(" +wakeup");
usb.host().intflag.write(|w| w.wakeup().set_bit());
unshift_event(Event::Attached);
}
if flags.ramacer().bit_is_set() {
- logln!(" +ramacer");
+ info!(" +ramacer");
usb.host().intflag.write(|w| w.ramacer().set_bit());
unshift_event(Event::Detached);
}
if flags.dconn().bit_is_set() {
- logln!(" +dconn");
+ info!(" +dconn");
usb.host().intflag.write(|w| w.dconn().set_bit());
usb.host().intenclr.write(|w| w.dconn().set_bit());
usb.host().intflag.write(|w| w.ddisc().set_bit());
@@ -615,7 +611,7 @@ pub fn handler(usbp: usize, events: &mut EventWriter) {
}
if flags.ddisc().bit_is_set() {
- logln!(" +ddisc");
+ info!(" +ddisc");
usb.host().intflag.write(|w| w.ddisc().set_bit());
usb.host().intenclr.write(|w| w.ddisc().set_bit());
// // Stop reset signal, in case of disconnection during reset