aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-09-26 12:35:33 +0200
committerDario Nieuwenhuis <[email protected]>2022-09-26 12:35:33 +0200
commitf4f58249722bc656a13865e06535d208440c3e4a (patch)
treee5fd41f7c32bb479791d2ec16327e938efce0501
parent7f7c14b7bce5b84eb27c8122535a96a6f0e5dd77 (diff)
usb: do not allow converting Directon to/from u8
-rw-r--r--embassy-usb-driver/src/lib.rs11
-rw-r--r--embassy-usb/src/control.rs2
2 files changed, 3 insertions, 10 deletions
diff --git a/embassy-usb-driver/src/lib.rs b/embassy-usb-driver/src/lib.rs
index 051190a48..fc29786fc 100644
--- a/embassy-usb-driver/src/lib.rs
+++ b/embassy-usb-driver/src/lib.rs
@@ -8,20 +8,13 @@ use core::future::Future;
8/// 8///
9/// The values of the enum also match the direction bit used in endpoint addresses and control 9/// The values of the enum also match the direction bit used in endpoint addresses and control
10/// request types. 10/// request types.
11#[repr(u8)]
12#[derive(Copy, Clone, Eq, PartialEq, Debug)] 11#[derive(Copy, Clone, Eq, PartialEq, Debug)]
13#[cfg_attr(feature = "defmt", derive(defmt::Format))] 12#[cfg_attr(feature = "defmt", derive(defmt::Format))]
14pub enum Direction { 13pub enum Direction {
15 /// Host to device (OUT) 14 /// Host to device (OUT)
16 Out = 0x00, 15 Out,
17 /// Device to host (IN) 16 /// Device to host (IN)
18 In = 0x80, 17 In,
19}
20
21impl From<u8> for Direction {
22 fn from(value: u8) -> Self {
23 unsafe { core::mem::transmute(value & 0x80) }
24 }
25} 18}
26 19
27/// USB endpoint transfer type. The values of this enum can be directly cast into `u8` to get the 20/// USB endpoint transfer type. The values of this enum can be directly cast into `u8` to get the
diff --git a/embassy-usb/src/control.rs b/embassy-usb/src/control.rs
index 9e0dee888..d6d0c6565 100644
--- a/embassy-usb/src/control.rs
+++ b/embassy-usb/src/control.rs
@@ -106,7 +106,7 @@ impl Request {
106 let recipient = rt & 0b11111; 106 let recipient = rt & 0b11111;
107 107
108 Request { 108 Request {
109 direction: rt.into(), 109 direction: if rt & 0x80 == 0 { Direction::Out } else { Direction::In },
110 request_type: unsafe { mem::transmute((rt >> 5) & 0b11) }, 110 request_type: unsafe { mem::transmute((rt >> 5) & 0b11) },
111 recipient: if recipient <= 3 { 111 recipient: if recipient <= 3 {
112 unsafe { mem::transmute(recipient) } 112 unsafe { mem::transmute(recipient) }