aboutsummaryrefslogtreecommitdiffstats
path: root/bootkbd
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2019-08-04 14:19:25 -0400
committerBrian Cully <bjc@kublai.com>2019-08-04 14:19:25 -0400
commite405c474f5e0e94288191223de7ae0f31ae0b15f (patch)
tree4ca89a92f0c868eb8feae272513c1e924b834adc /bootkbd
parentabd478d9425dd2d4acd5b58202b1a95b73ff217b (diff)
downloadsamd21-demo-e405c474f5e0e94288191223de7ae0f31ae0b15f.tar.gz
samd21-demo-e405c474f5e0e94288191223de7ae0f31ae0b15f.zip
Migrate everything over to separate libraries.
* `usb-host` is the crate containing the HAL traits. * `bootkbd` is a crate for a bare-bones boot protocol keyboard driver. * `samd21-host` is a crate with an implementation of `usb-host` for a SAMD21 platform, with an example for the trinket-m0 which uses the `bootkbd` driver to print keyboard reports.
Diffstat (limited to 'bootkbd')
-rw-r--r--bootkbd/Cargo.toml1
-rwxr-xr-x[-rw-r--r--]bootkbd/src/lib.rs40
2 files changed, 21 insertions, 20 deletions
diff --git a/bootkbd/Cargo.toml b/bootkbd/Cargo.toml
index 7b1b02d..5ae7bd4 100644
--- a/bootkbd/Cargo.toml
+++ b/bootkbd/Cargo.toml
@@ -8,4 +8,3 @@ license = "GPL-3.0-or-later"
[dependencies]
usb-host = { path = "../usb-host" }
log = "~0.4"
-
diff --git a/bootkbd/src/lib.rs b/bootkbd/src/lib.rs
index c143512..dde9e71 100644..100755
--- a/bootkbd/src/lib.rs
+++ b/bootkbd/src/lib.rs
@@ -1,8 +1,10 @@
+#![no_std]
+
use log::{debug, error, info};
use usb_host::{
- ConfigurationDescriptor, DescriptorType, DeviceDescriptor, Direction, Driver, Endpoint,
- Error as USBError, RequestCode, RequestDirection, RequestKind, RequestRecipient, RequestType,
- TransferType, USBHost, WValue,
+ ConfigurationDescriptor, DescriptorType, DeviceDescriptor, Direction, Driver, DriverError,
+ Endpoint, RequestCode, RequestDirection, RequestKind, RequestRecipient, RequestType,
+ TransferError, TransferType, USBHost, WValue,
};
use core::mem::{self, MaybeUninit};
@@ -21,6 +23,11 @@ const MAX_ENDPOINTS: usize = 2;
pub struct BootKeyboard {
devices: [Option<Device>; MAX_DEVICES],
}
+impl core::fmt::Debug for BootKeyboard {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ write!(f, "BootKeyboard")
+ }
+}
impl BootKeyboard {
pub fn new() -> Self {
@@ -35,14 +42,14 @@ impl Driver for BootKeyboard {
true
}
- fn add_device(&mut self, _device: DeviceDescriptor, address: u8) -> Result<(), USBError> {
+ fn add_device(&mut self, _device: DeviceDescriptor, address: u8) -> Result<(), DriverError> {
for i in 0..self.devices.len() {
if self.devices[i].is_none() {
self.devices[i] = Some(Device::new(address));
return Ok(());
}
}
- Err(USBError::Permanent("out of devices"))
+ Err(DriverError::Permanent(address, "out of devices"))
}
fn remove_device(&mut self, address: u8) {
@@ -56,20 +63,15 @@ impl Driver for BootKeyboard {
}
}
- fn tick_device(
- &mut self,
- address: u8,
- millis: usize,
- host: &mut dyn USBHost,
- ) -> Result<(), USBError> {
- for i in 0..self.devices.len() {
- if let Some(ref mut dev) = self.devices[i] {
- if dev.addr == address {
- return dev.fsm(millis, host);
+ fn tick(&mut self, millis: usize, host: &mut dyn USBHost) -> Result<(), DriverError> {
+ for d in &mut self.devices[..] {
+ if let Some(ref mut dev) = d {
+ if let Err(TransferError::Permanent(e)) = dev.fsm(millis, host) {
+ return Err(DriverError::Permanent(dev.addr, e));
}
}
}
- Err(USBError::Permanent("no device at address"))
+ Ok(())
}
}
@@ -110,7 +112,7 @@ impl Device {
}
}
- fn fsm(&mut self, millis: usize, host: &mut dyn USBHost) -> Result<(), USBError> {
+ fn fsm(&mut self, millis: usize, host: &mut dyn USBHost) -> Result<(), TransferError> {
// TODO: either we need another `control_transfer` that
// doesn't take data, or this `none` value needs to be put in
// the usb-host layer. None of these options are good.
@@ -252,8 +254,8 @@ impl Device {
let mut buf: [u8; 8] = [0; 8];
if let Some(ref mut ep) = self.endpoints[0] {
match host.in_transfer(ep, &mut buf) {
- Err(USBError::Permanent(msg)) => error!("reading report: {}", msg),
- Err(USBError::Retry(_)) => return Ok(()),
+ Err(TransferError::Permanent(msg)) => error!("reading report: {}", msg),
+ Err(TransferError::Retry(_)) => return Ok(()),
Ok(len) => info!("report: {} - {:?}", len, buf),
}
}