Skip to content

Commit

Permalink
Defmt support for enums and structs
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
haata committed May 22, 2022
1 parent cd093fe commit e0b7a1f
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

...

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ license = "MIT"
authors = ["Matti Virkkunen <[email protected]>"]
repository = "https://github.com/mvirkkunen/usb-device"

[dependencies]
defmt = { version = "0.3", optional = true }

[dev-dependencies]
rusb = "0.8.0"
rand = "0.6.1"
Expand Down
2 changes: 2 additions & 0 deletions src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ impl<B: UsbBus> UsbBusAllocator<B> {

/// 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<InterfaceNumber> for u8 {
Expand All @@ -291,6 +292,7 @@ impl From<InterfaceNumber> 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 {
Expand Down
3 changes: 3 additions & 0 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/control_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -157,6 +158,7 @@ impl<B: UsbBus> 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<u8> for EndpointAddress {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e0b7a1f

Please sign in to comment.