Skip to content

Commit 4b29184

Browse files
committed
usb-driver: Add enumerations and allocator for isochronous endpoints.
Copied from rust-embedded-community/usb-device#60
1 parent f6ea7f1 commit 4b29184

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

embassy-usb-driver/src/lib.rs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,70 @@ pub enum Direction {
1818
In,
1919
}
2020

21+
/// Isochronous transfers employ one of three synchronization schemes. See USB 2.0 spec 5.12.4.1.
22+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
23+
pub enum IsochronousSynchronizationType {
24+
/// Synchronization is not implemented for this endpoint.
25+
NoSynchronization,
26+
/// Source and Sink sample clocks are free running.
27+
Asynchronous,
28+
/// Source sample clock is locked to Sink, Sink sample clock is locked to data flow.
29+
Adaptive,
30+
/// Source and Sink sample clocks are locked to USB SOF.
31+
Synchronous,
32+
}
33+
34+
/// Intended use of an isochronous endpoint, see USB 2.0 spec sections 5.12 and 9.6.6.
35+
/// Associations between data and feedback endpoints are described in section 9.6.6.
36+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
37+
pub enum IsochronousUsageType {
38+
/// Endpoint is used for isochronous data.
39+
Data,
40+
/// Feedback for synchronization.
41+
Feedback,
42+
/// Endpoint is data and provides implicit feedback for synchronization.
43+
ImplicitFeedbackData,
44+
}
45+
2146
/// USB endpoint transfer type. The values of this enum can be directly cast into `u8` to get the
2247
/// transfer bmAttributes transfer type bits.
23-
#[repr(u8)]
2448
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
2549
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2650
pub enum EndpointType {
2751
/// Control endpoint. Used for device management. Only the host can initiate requests. Usually
2852
/// used only endpoint 0.
29-
Control = 0b00,
53+
Control,
3054
/// Isochronous endpoint. Used for time-critical unreliable data. Not implemented yet.
31-
Isochronous = 0b01,
55+
Isochronous((IsochronousSynchronizationType, IsochronousUsageType)),
3256
/// Bulk endpoint. Used for large amounts of best-effort reliable data.
33-
Bulk = 0b10,
57+
Bulk,
3458
/// Interrupt endpoint. Used for small amounts of time-critical reliable data.
35-
Interrupt = 0b11,
59+
Interrupt,
60+
}
61+
62+
impl EndpointType {
63+
/// Format EndpointType for use in bmAttributes transfer type field USB 2.0 spec section 9.6.6
64+
pub fn to_bm_attributes(&self) -> u8 {
65+
match self {
66+
EndpointType::Control => 0b00,
67+
EndpointType::Isochronous((sync_type, usage_type)) => {
68+
let sync_bits = match sync_type {
69+
IsochronousSynchronizationType::NoSynchronization => 0b00,
70+
IsochronousSynchronizationType::Asynchronous => 0b01,
71+
IsochronousSynchronizationType::Adaptive => 0b10,
72+
IsochronousSynchronizationType::Synchronous => 0b11,
73+
};
74+
let usage_bits = match usage_type {
75+
IsochronousUsageType::Data => 0b00,
76+
IsochronousUsageType::Feedback => 0b01,
77+
IsochronousUsageType::ImplicitFeedbackData => 0b10,
78+
};
79+
(usage_bits << 4) | (sync_bits << 2) | 0b01
80+
}
81+
EndpointType::Bulk => 0b10,
82+
EndpointType::Interrupt => 0b11,
83+
}
84+
}
3685
}
3786

3887
/// Type-safe endpoint address.

0 commit comments

Comments
 (0)