From e0b7a1fe3acc5a212ed30390836df215c65aafcc Mon Sep 17 00:00:00 2001 From: Jacob Alexander Date: Fri, 13 Aug 2021 23:07:08 -0700 Subject: [PATCH] Defmt support for enums and structs USB tends to be very timing sensitive, using defmt makes it possible to use debug prints with USB (will negatively affect timings if you use traditional debug prints and effect the results). --- CHANGELOG.md | 1 + Cargo.toml | 3 +++ src/bus.rs | 2 ++ src/control.rs | 3 +++ src/control_pipe.rs | 1 + src/device.rs | 1 + src/endpoint.rs | 2 ++ src/lib.rs | 2 ++ 8 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 789c3f3..cfe438b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed * Fixed an issue where USB devices were not enumerating on Windows ([#32](https://github.com/rust-embedded-community/usb-device/issues/82)) +* Add optional support for defmt ([#76](https://github.com/rust-embedded-community/usb-device/pull/76)) ... diff --git a/Cargo.toml b/Cargo.toml index d582a0f..7cbebf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,9 @@ license = "MIT" authors = ["Matti Virkkunen "] repository = "https://github.com/mvirkkunen/usb-device" +[dependencies] +defmt = { version = "0.3", optional = true } + [dev-dependencies] rusb = "0.8.0" rand = "0.6.1" diff --git a/src/bus.rs b/src/bus.rs index a6cd31c..e18f8d6 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -281,6 +281,7 @@ impl UsbBusAllocator { /// A handle for a USB interface that contains its number. #[derive(Copy, Clone, Eq, PartialEq)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub struct InterfaceNumber(u8); impl From for u8 { @@ -291,6 +292,7 @@ impl From for u8 { /// A handle for a USB string descriptor that contains its index. #[derive(Copy, Clone, Eq, PartialEq)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub struct StringIndex(u8); impl StringIndex { diff --git a/src/control.rs b/src/control.rs index a6bf245..a822457 100644 --- a/src/control.rs +++ b/src/control.rs @@ -4,6 +4,7 @@ use core::mem; /// Control request type. #[repr(u8)] #[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub enum RequestType { /// Request is a USB standard request. Usually handled by /// [`UsbDevice`](crate::device::UsbDevice). @@ -18,6 +19,7 @@ pub enum RequestType { /// Control request recipient. #[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub enum Recipient { /// Request is intended for the entire device. Device = 0, @@ -35,6 +37,7 @@ pub enum Recipient { /// A control request read from a SETUP packet. #[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub struct Request { /// Direction of the request. pub direction: UsbDirection, diff --git a/src/control_pipe.rs b/src/control_pipe.rs index 32fa6c7..877b910 100644 --- a/src/control_pipe.rs +++ b/src/control_pipe.rs @@ -5,6 +5,7 @@ use crate::{Result, UsbDirection, UsbError}; use core::cmp::min; #[derive(Debug)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] #[allow(unused)] enum ControlState { Idle, diff --git a/src/device.rs b/src/device.rs index 816e044..dc18511 100644 --- a/src/device.rs +++ b/src/device.rs @@ -12,6 +12,7 @@ use crate::{Result, UsbDirection}; /// In general class traffic is only possible in the `Configured` state. #[repr(u8)] #[derive(PartialEq, Eq, Copy, Clone, Debug)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub enum UsbDeviceState { /// The USB device has just been created or reset. Default, diff --git a/src/endpoint.rs b/src/endpoint.rs index c4758f4..e207d81 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -33,6 +33,7 @@ pub type EndpointIn<'a, B> = Endpoint<'a, B, In>; /// transfer bmAttributes transfer type bits. #[repr(u8)] #[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub enum EndpointType { /// Control endpoint. Used for device management. Only the host can initiate requests. Usually /// used only endpoint 0. @@ -157,6 +158,7 @@ impl Endpoint<'_, B, Out> { /// Type-safe endpoint address. #[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub struct EndpointAddress(u8); impl From for EndpointAddress { diff --git a/src/lib.rs b/src/lib.rs index d7744c9..7679fd3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,7 @@ /// A USB stack error. #[derive(Debug)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub enum UsbError { /// An operation would block because the device is currently busy or there is no data available. WouldBlock, @@ -76,6 +77,7 @@ pub enum UsbError { /// request types. #[repr(u8)] #[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))] pub enum UsbDirection { /// Host to device (OUT) Out = 0x00,