aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2019-07-25 09:40:31 -0400
committerBrian Cully <bjc@kublai.com>2019-07-25 10:14:46 -0400
commit7966be1419d3eaa088ed83f95282383182585640 (patch)
tree3b7b09eb768d47e327035301f53f3bd23b7596f2
parent5b48a5c2f5526f088187fa341f64368994c9f517 (diff)
downloadsamd21-demo-7966be1419d3eaa088ed83f95282383182585640.tar.gz
samd21-demo-7966be1419d3eaa088ed83f95282383182585640.zip
Move USB host stuff to separate crate.
-rw-r--r--app/Cargo.lock13
-rw-r--r--app/Cargo.toml5
-rw-r--r--app/src/logger.rs2
-rwxr-xr-xapp/src/main.rs7
-rw-r--r--usbh/Cargo.lock269
-rw-r--r--usbh/Cargo.toml15
-rwxr-xr-x[-rw-r--r--]usbh/src/lib.rs (renamed from app/src/usb.rs)53
-rw-r--r--usbh/src/pipe.rs (renamed from app/src/usb/pipe.rs)33
-rw-r--r--usbh/src/pipe/addr.rs (renamed from app/src/usb/pipe/addr.rs)0
-rw-r--r--usbh/src/pipe/ctrl_pipe.rs (renamed from app/src/usb/pipe/ctrl_pipe.rs)0
-rw-r--r--usbh/src/pipe/ext_reg.rs (renamed from app/src/usb/pipe/ext_reg.rs)0
-rw-r--r--usbh/src/pipe/pck_size.rs (renamed from app/src/usb/pipe/pck_size.rs)0
-rw-r--r--usbh/src/pipe/status_bk.rs (renamed from app/src/usb/pipe/status_bk.rs)0
-rw-r--r--usbh/src/pipe/status_pipe.rs (renamed from app/src/usb/pipe/status_pipe.rs)0
-rw-r--r--usbh/src/usbproto.rs (renamed from app/src/usb/usbproto.rs)0
15 files changed, 358 insertions, 39 deletions
diff --git a/app/Cargo.lock b/app/Cargo.lock
index 6cabc74..92a66e8 100644
--- a/app/Cargo.lock
+++ b/app/Cargo.lock
@@ -262,7 +262,7 @@ dependencies = [
"smart-leds 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"smart-leds-trait 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"trinket_m0 0.3.0",
- "vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "usbh 0.1.0",
]
[[package]]
@@ -331,6 +331,17 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "usbh"
+version = "0.1.0"
+dependencies = [
+ "atsamd-hal 0.5.0",
+ "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rb 0.1.0",
+ "vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "vcell"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/app/Cargo.toml b/app/Cargo.toml
index d73dad3..98e0642 100644
--- a/app/Cargo.toml
+++ b/app/Cargo.toml
@@ -7,6 +7,9 @@ readme = "../README.md"
license = "GPL-3.0-or-later"
[dependencies]
+rb = { path = "../rb" }
+usbh = { path = "../usbh" }
+
trinket_m0 = { path = "../../atsamd/boards/trinket_m0" }
panic-halt = "~0.2"
smart-leds = "~0.2"
@@ -19,8 +22,6 @@ embedded-hal = "~0.2"
clint = { path = "../../clint" }
#cortex-m-semihosting = "~0.3"
bare-metal = "~0.2"
-rb = { path = "../rb" }
-vcell = "~0.1"
log = "~0.4"
[features]
diff --git a/app/src/logger.rs b/app/src/logger.rs
index 7e90572..140eebe 100644
--- a/app/src/logger.rs
+++ b/app/src/logger.rs
@@ -1,5 +1,3 @@
-use crate::logln_now;
-
use rb::{Reader, RingBuffer, Writer};
use core::cell::UnsafeCell;
diff --git a/app/src/main.rs b/app/src/main.rs
index b9f626e..792f339 100755
--- a/app/src/main.rs
+++ b/app/src/main.rs
@@ -8,7 +8,6 @@ mod dotstar;
mod logger;
mod macros;
mod rtc;
-mod usb;
//#[allow(unused)]
//use panic_halt;
@@ -17,7 +16,7 @@ use clint::HandlerArray;
use cortex_m::asm::wfi;
use cortex_m_rt::{entry, exception, ExceptionFrame};
use embedded_hal::digital::v2::OutputPin;
-use log::{error, info, LevelFilter};
+use log::{info, LevelFilter};
use smart_leds::colors;
use smart_leds_trait::SmartLedsWrite;
use trinket_m0::{
@@ -29,6 +28,7 @@ use trinket_m0::{
time::*,
CorePeripherals, Peripherals,
};
+use usbh::USBHost;
static HANDLERS: HandlerArray = HandlerArray::new();
@@ -98,7 +98,7 @@ fn main() -> ! {
let mut rtc_handler = rtc::setup(peripherals.RTC, &mut clocks);
info!("setting up usb host");
- let (mut usb_host, mut usb_handler) = usb::USBHost::new(
+ let (mut usb_host, mut usb_handler) = USBHost::new(
peripherals.USB,
pins.usb_sof,
pins.usb_dm,
@@ -107,6 +107,7 @@ fn main() -> ! {
&mut pins.port,
&mut clocks,
&mut peripherals.PM,
+ &rtc::millis,
);
info!("setting up handlers");
diff --git a/usbh/Cargo.lock b/usbh/Cargo.lock
new file mode 100644
index 0000000..5b973da
--- /dev/null
+++ b/usbh/Cargo.lock
@@ -0,0 +1,269 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "aligned"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "aligned"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "as-slice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "as-slice"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "atsamd-hal"
+version = "0.5.0"
+dependencies = [
+ "atsamd21e18a 0.4.0",
+ "bitfield 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "paste 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "atsamd21e18a"
+version = "0.4.0"
+dependencies = [
+ "bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "bare-metal"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "bitfield"
+version = "0.13.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "cfg-if"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "cortex-m"
+version = "0.5.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "aligned 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cortex-m 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "cortex-m"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "aligned 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "embedded-hal"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "generic-array"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "log"
+version = "0.4.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "nb"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "paste"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "paste-impl 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "proc-macro-hack 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "paste-impl"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro-hack 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.15.42 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "proc-macro-hack"
+version = "0.5.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.15.42 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "0.4.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "quote"
+version = "0.6.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rb"
+version = "0.1.0"
+
+[[package]]
+name = "rustc_version"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "semver"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "semver-parser"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "stable_deref_trait"
+version = "1.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "syn"
+version = "0.15.42"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "typenum"
+version = "1.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "unicode-xid"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "usbh"
+version = "0.1.0"
+dependencies = [
+ "atsamd-hal 0.5.0",
+ "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rb 0.1.0",
+ "vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "vcell"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "void"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "volatile-register"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[metadata]
+"checksum aligned 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d39da9b88ae1a81c03c9c082b8db83f1d0e93914126041962af61034ab44c4a5"
+"checksum aligned 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a316c7ea8e1e9ece54862c992def5a7ac14de9f5832b69d71760680efeeefa"
+"checksum as-slice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "293dac66b274fab06f95e7efb05ec439a6b70136081ea522d270bc351ae5bb27"
+"checksum bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a3caf393d93b2d453e80638d0674597020cef3382ada454faacd43d1a55a735a"
+"checksum bitfield 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)" = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719"
+"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
+"checksum cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0b159a1e8306949579de3698c841dba58058197b65c60807194e4fa1e7a554"
+"checksum cortex-m 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f3c18719fdc57db65668bfc977db9a0fa1a41d718c5d9cd4f652c9d4b0e0956a"
+"checksum embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4908a155094da7723c2d60d617b820061e3b4efcc3d9e293d206a5a76c170b"
+"checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec"
+"checksum log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c275b6ad54070ac2d665eef9197db647b32239c9d244bfb6f041a766d00da5b3"
+"checksum nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b1411551beb3c11dedfb0a90a0fa256b47d28b9ec2cdff34c25a2fa59e45dbdc"
+"checksum paste 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1f4a4a1c555c6505821f9d58b8779d0f630a6b7e4e1be24ba718610acf01fa79"
+"checksum paste-impl 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "26e796e623b8b257215f27e6c80a5478856cae305f5b59810ff9acdaa34570e6"
+"checksum proc-macro-hack 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "982a35d1194084ba319d65c4a68d24ca28f5fdb5b8bc20899e4eef8641ea5178"
+"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
+"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
+"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
+"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
+"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
+"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
+"checksum syn 0.15.42 (registry+https://github.com/rust-lang/crates.io-index)" = "eadc09306ca51a40555dd6fc2b415538e9e18bc9f870e47b1a524a79fe2dcf5e"
+"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169"
+"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
+"checksum vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45c297f0afb6928cd08ab1ff9d95e99392595ea25ae1b5ecf822ff8764e57a0d"
+"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
+"checksum volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286"
diff --git a/usbh/Cargo.toml b/usbh/Cargo.toml
new file mode 100644
index 0000000..327772b
--- /dev/null
+++ b/usbh/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "usbh"
+version = "0.1.0"
+authors = ["Brian Cully <bjc@kublai.com>"]
+edition = "2018"
+
+[dependencies]
+rb = { path = "../rb" }
+atsamd-hal = { path = "../../atsamd/hal" }
+embedded-hal = "~0.2"
+log = "~0.4"
+vcell = "~0.1"
+
+[features]
+default = ["atsamd-hal/samd21e18a"]
diff --git a/app/src/usb.rs b/usbh/src/lib.rs
index a5788a1..4e6b8bf 100644..100755
--- a/app/src/usb.rs
+++ b/usbh/src/lib.rs
@@ -1,3 +1,5 @@
+#![no_std]
+
mod pipe;
mod usbproto;
@@ -5,16 +7,14 @@ 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::{
+use atsamd_hal::{
calibration::{usb_transn_cal, usb_transp_cal, usb_trim_cal},
clock::{ClockGenId, ClockSource, GenericClockController},
gpio::{self, Floating, Input, OpenDrain, Output},
- PM, USB,
+ target_device::{PM, USB},
};
+use embedded_hal::digital::v2::OutputPin;
+use log::{info, warn};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Event {
@@ -110,7 +110,10 @@ impl USBDeviceAddress {
}
}
-pub struct USBHost {
+pub struct USBHost<F>
+where
+ F: Fn() -> usize + 'static,
+{
usb: USB,
events: EventReader,
@@ -126,9 +129,15 @@ pub struct USBHost {
_dm_pad: gpio::Pa24<gpio::PfG>,
_dp_pad: gpio::Pa25<gpio::PfG>,
host_enable_pin: Option<gpio::Pa28<Output<OpenDrain>>>,
+
+ // To get current milliseconds.
+ millis: &'static F,
}
-impl USBHost {
+impl<F> USBHost<F>
+where
+ F: Fn() -> usize + 'static,
+{
pub fn new(
usb: USB,
sof_pin: gpio::Pa23<Input<Floating>>,
@@ -138,6 +147,7 @@ impl USBHost {
port: &mut gpio::Port,
clocks: &mut GenericClockController,
pm: &mut PM,
+ millis: &'static F,
) -> (Self, impl FnMut()) {
let (eventr, mut eventw) = unsafe { EVENTS.split() };
let mut rc = Self {
@@ -151,6 +161,8 @@ impl USBHost {
_dm_pad: dm_pin.into_function_g(port),
_dp_pad: dp_pin.into_function_g(port),
host_enable_pin: None,
+
+ millis: millis,
};
if let Some(he_pin) = host_enable_pin {
@@ -245,7 +257,7 @@ impl USBHost {
}
Event::Attached => {
if let TaskState::Detached(_) = self.task_state {
- self.delay = rtc::millis() + SETTLE_DELAY;
+ self.delay = (self.millis)() + SETTLE_DELAY;
TaskState::Attached(AttachedState::WaitForSettle)
} else {
self.task_state
@@ -282,7 +294,7 @@ impl USBHost {
// }
// Event::Attached => {
// if let TaskState::Detached(_) = self.task_state {
- // self.delay = rtc::millis() + SETTLE_DELAY;
+ // self.delay = self.millis() + SETTLE_DELAY;
// TaskState::Attached(AttachedState::WaitForSettle)
// } else {
// self.task_state
@@ -333,7 +345,7 @@ impl USBHost {
fn attached_fsm(&mut self, s: AttachedState) {
match s {
AttachedState::WaitForSettle => {
- if rtc::millis() >= self.delay {
+ if (self.millis)() >= self.delay {
self.usb.host().ctrlb.modify(|_, w| w.busreset().set_bit());
self.task_state = TaskState::Attached(AttachedState::WaitResetComplete);
}
@@ -351,7 +363,7 @@ impl USBHost {
// immediately after reset according to §32.6.3.3.
self.usb.host().ctrlb.modify(|_, w| w.sofe().set_bit());
// USB spec requires 20ms of SOF after bus reset.
- self.delay = rtc::millis() + 20;
+ self.delay = (self.millis)() + 20;
self.task_state = TaskState::Attached(AttachedState::WaitSOF);
}
}
@@ -359,7 +371,7 @@ impl USBHost {
AttachedState::WaitSOF => {
if self.usb.host().intflag.read().hsof().bit_is_set() {
self.usb.host().intflag.write(|w| w.hsof().set_bit());
- if rtc::millis() >= self.delay {
+ if (self.millis)() >= self.delay {
self.task_state = TaskState::Steady(SteadyState::Configuring);
}
}
@@ -423,8 +435,8 @@ impl USBHost {
)?;
info!(" -- address set");
// Delay according to §9.2.6.3 of USB 2.0
- let until = rtc::millis() + 300;
- while rtc::millis() < until {}
+ let until = (self.millis)() + 300;
+ while (self.millis)() < until {}
info!("getting config");
let tmp: USBConfigurationDescriptor = Default::default();
@@ -490,7 +502,12 @@ impl USBHost {
},
};
let mut pipe = self.pipe_table.pipe_for(self.usb.host_mut(), addr, ep);
- pipe.send(USBToken::Setup, &DataBuf::from(&setup_packet), NAK_LIMIT)?;
+ pipe.send(
+ USBToken::Setup,
+ &DataBuf::from(&setup_packet),
+ NAK_LIMIT,
+ self.millis,
+ )?;
/*
* Data stage.
@@ -499,7 +516,7 @@ impl USBHost {
match bm_request_type.direction() {
USBSetupDirection::DeviceToHost => {
info!("buf0: {:?}", &b);
- pipe.in_transfer(&b, NAK_LIMIT)?;
+ pipe.in_transfer(&b, NAK_LIMIT, self.millis)?;
info!("buf1: {:?}", &b);
}
@@ -529,7 +546,7 @@ impl USBHost {
// TODO: should probably make `pipe.send` have optional
// `DataBuf`, rather than exposing `dispatch_retries`.
info!("dispatching status stage");
- pipe.dispatch_retries(token, NAK_LIMIT)?;
+ pipe.dispatch_retries(token, NAK_LIMIT, self.millis)?;
Ok(())
}
diff --git a/app/src/usb/pipe.rs b/usbh/src/pipe.rs
index 2efb29b..75923ba 100644
--- a/app/src/usb/pipe.rs
+++ b/usbh/src/pipe.rs
@@ -12,15 +12,10 @@ use pck_size::PckSize;
use status_bk::StatusBk;
use status_pipe::StatusPipe;
-use crate::rtc;
-
use core::convert::TryInto;
use log::info;
-// FIXME: this stuff is needed for PipeRegs, and while tied to samd,
-// it shouldn't be tied to trinket_m0. Will need to figure out a
-// better layout for this.
-use trinket_m0::usb::{
+use atsamd_hal::target_device::usb::{
self,
host::{BINTERVAL, PCFG, PINTFLAG, PSTATUS, PSTATUSCLR, PSTATUSSET},
};
@@ -97,6 +92,7 @@ impl Pipe<'_, '_> {
token: USBToken,
buf: &DataBuf,
nak_limit: usize,
+ millis: &dyn Fn() -> usize,
) -> Result<(), PipeErr> {
// Data needs to be word aligned.
assert!((buf.ptr as u32) & 0x3 == 0);
@@ -120,10 +116,15 @@ impl Pipe<'_, '_> {
//unsafe { w.multi_packet_size().bits(0) }
});
- self.dispatch_retries(token, nak_limit)
+ self.dispatch_retries(token, nak_limit, millis)
}
- pub(crate) fn in_transfer(&mut self, buf: &DataBuf, nak_limit: usize) -> Result<(), PipeErr> {
+ pub(crate) fn in_transfer(
+ &mut self,
+ buf: &DataBuf,
+ nak_limit: usize,
+ millis: &dyn Fn() -> usize,
+ ) -> Result<(), PipeErr> {
// Data needs to be word aligned.
assert!((buf.ptr as u32) & 0x3 == 0);
// byte_count section of register is 14 bits.
@@ -152,27 +153,28 @@ impl Pipe<'_, '_> {
// w.bits(1)
//});
- self.dispatch_retries(USBToken::In, nak_limit)
+ self.dispatch_retries(USBToken::In, nak_limit, millis)
}
pub(crate) fn dispatch_retries(
&mut self,
token: USBToken,
retries: usize,
+ millis: &dyn Fn() -> usize,
) -> Result<(), PipeErr> {
assert!(retries > 0);
info!("initial regs");
self.log_regs();
- let until = rtc::millis() + USB_TIMEOUT;
+ let until = millis() + USB_TIMEOUT;
let mut last_result: Result<(), PipeErr> = Err(PipeErr::SWTimeout);
let mut naks = 0;
while naks <= retries {
info!("p{}: dispatch {:?} retry {}", self.num, token, naks);
self.dispatch_packet(token);
- last_result = self.dispatch_result(token, until);
+ last_result = self.dispatch_result(token, until, millis);
match last_result {
Ok(_) => return Ok(()),
// FIXME: handle datatoggle
@@ -238,8 +240,13 @@ impl Pipe<'_, '_> {
self.regs.statusclr.write(|w| w.pfreeze().set_bit());
}
- fn dispatch_result(&mut self, token: USBToken, until: usize) -> Result<(), PipeErr> {
- while rtc::millis() <= until {
+ fn dispatch_result(
+ &mut self,
+ token: USBToken,
+ until: usize,
+ millis: &dyn Fn() -> usize,
+ ) -> Result<(), PipeErr> {
+ while millis() <= until {
if self.is_transfer_complete(token)? {
return Ok(());
} else if self.regs.intflag.read().stall().bit_is_set() {
diff --git a/app/src/usb/pipe/addr.rs b/usbh/src/pipe/addr.rs
index 8b92177..8b92177 100644
--- a/app/src/usb/pipe/addr.rs
+++ b/usbh/src/pipe/addr.rs
diff --git a/app/src/usb/pipe/ctrl_pipe.rs b/usbh/src/pipe/ctrl_pipe.rs
index 63390df..63390df 100644
--- a/app/src/usb/pipe/ctrl_pipe.rs
+++ b/usbh/src/pipe/ctrl_pipe.rs
diff --git a/app/src/usb/pipe/ext_reg.rs b/usbh/src/pipe/ext_reg.rs
index 023ce9f..023ce9f 100644
--- a/app/src/usb/pipe/ext_reg.rs
+++ b/usbh/src/pipe/ext_reg.rs
diff --git a/app/src/usb/pipe/pck_size.rs b/usbh/src/pipe/pck_size.rs
index 5133005..5133005 100644
--- a/app/src/usb/pipe/pck_size.rs
+++ b/usbh/src/pipe/pck_size.rs
diff --git a/app/src/usb/pipe/status_bk.rs b/usbh/src/pipe/status_bk.rs
index 4ddb420..4ddb420 100644
--- a/app/src/usb/pipe/status_bk.rs
+++ b/usbh/src/pipe/status_bk.rs
diff --git a/app/src/usb/pipe/status_pipe.rs b/usbh/src/pipe/status_pipe.rs
index 4f8eb41..4f8eb41 100644
--- a/app/src/usb/pipe/status_pipe.rs
+++ b/usbh/src/pipe/status_pipe.rs
diff --git a/app/src/usb/usbproto.rs b/usbh/src/usbproto.rs
index 9c1dbcb..9c1dbcb 100644
--- a/app/src/usb/usbproto.rs
+++ b/usbh/src/usbproto.rs