Skip to content

Commit 2bdc364

Browse files
committed
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). - Adds CI check for the defmt feature
1 parent 80e9ec0 commit 2bdc364

File tree

9 files changed

+20
-0
lines changed

9 files changed

+20
-0
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ jobs:
4242
with:
4343
command: check
4444
args: --features control-buffer-256
45+
46+
- uses: actions-rs/cargo@v1
47+
with:
48+
command: check
49+
args: --features defmt

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Fixed
1111
* Fixed an issue where USB devices were not enumerating on Windows ([#32](https://github.com/rust-embedded-community/usb-device/issues/82))
12+
* Add optional support for defmt ([#76](https://github.com/rust-embedded-community/usb-device/pull/76))
1213

1314
...
1415

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ license = "MIT"
99
authors = ["Matti Virkkunen <[email protected]>"]
1010
repository = "https://github.com/mvirkkunen/usb-device"
1111

12+
[dependencies]
13+
defmt = { version = "0.3", optional = true }
14+
1215
[dev-dependencies]
1316
rusb = "0.8.0"
1417
rand = "0.6.1"

src/bus.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ impl<B: UsbBus> UsbBusAllocator<B> {
281281

282282
/// A handle for a USB interface that contains its number.
283283
#[derive(Copy, Clone, Eq, PartialEq)]
284+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
284285
pub struct InterfaceNumber(u8);
285286

286287
impl From<InterfaceNumber> for u8 {
@@ -291,6 +292,7 @@ impl From<InterfaceNumber> for u8 {
291292

292293
/// A handle for a USB string descriptor that contains its index.
293294
#[derive(Copy, Clone, Eq, PartialEq)]
295+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
294296
pub struct StringIndex(u8);
295297

296298
impl StringIndex {

src/control.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::mem;
44
/// Control request type.
55
#[repr(u8)]
66
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
7+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
78
pub enum RequestType {
89
/// Request is a USB standard request. Usually handled by
910
/// [`UsbDevice`](crate::device::UsbDevice).
@@ -18,6 +19,7 @@ pub enum RequestType {
1819

1920
/// Control request recipient.
2021
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
22+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2123
pub enum Recipient {
2224
/// Request is intended for the entire device.
2325
Device = 0,
@@ -35,6 +37,7 @@ pub enum Recipient {
3537

3638
/// A control request read from a SETUP packet.
3739
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
40+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
3841
pub struct Request {
3942
/// Direction of the request.
4043
pub direction: UsbDirection,

src/control_pipe.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{Result, UsbDirection, UsbError};
55
use core::cmp::min;
66

77
#[derive(Debug)]
8+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
89
#[allow(unused)]
910
enum ControlState {
1011
Idle,

src/device.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{Result, UsbDirection};
1212
/// In general class traffic is only possible in the `Configured` state.
1313
#[repr(u8)]
1414
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
15+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1516
pub enum UsbDeviceState {
1617
/// The USB device has just been created or reset.
1718
Default,

src/endpoint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub type EndpointIn<'a, B> = Endpoint<'a, B, In>;
3333
/// transfer bmAttributes transfer type bits.
3434
#[repr(u8)]
3535
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
36+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
3637
pub enum EndpointType {
3738
/// Control endpoint. Used for device management. Only the host can initiate requests. Usually
3839
/// used only endpoint 0.
@@ -157,6 +158,7 @@ impl<B: UsbBus> Endpoint<'_, B, Out> {
157158

158159
/// Type-safe endpoint address.
159160
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
161+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
160162
pub struct EndpointAddress(u8);
161163

162164
impl From<u8> for EndpointAddress {

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
/// A USB stack error.
4040
#[derive(Debug)]
41+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4142
pub enum UsbError {
4243
/// An operation would block because the device is currently busy or there is no data available.
4344
WouldBlock,
@@ -76,6 +77,7 @@ pub enum UsbError {
7677
/// request types.
7778
#[repr(u8)]
7879
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
80+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7981
pub enum UsbDirection {
8082
/// Host to device (OUT)
8183
Out = 0x00,

0 commit comments

Comments
 (0)