aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src
diff options
context:
space:
mode:
authoralexmoon <[email protected]>2022-04-02 16:35:03 -0400
committerDario Nieuwenhuis <[email protected]>2022-04-06 05:38:11 +0200
commit2ce435dc341c0238392df5dab5db9b80db167117 (patch)
tree19e64218922a67601540261749af81d2d1d0aac4 /embassy-usb/src
parent99f95a33c30b08359fcd72123fea01f4de0903ec (diff)
Add basic device state handling for endpoints.
Diffstat (limited to 'embassy-usb/src')
-rw-r--r--embassy-usb/src/driver.rs12
-rw-r--r--embassy-usb/src/lib.rs7
2 files changed, 17 insertions, 2 deletions
diff --git a/embassy-usb/src/driver.rs b/embassy-usb/src/driver.rs
index 82b59bd1e..6eaa40b0d 100644
--- a/embassy-usb/src/driver.rs
+++ b/embassy-usb/src/driver.rs
@@ -72,6 +72,9 @@ pub trait Bus {
72 /// Sets the device USB address to `addr`. 72 /// Sets the device USB address to `addr`.
73 fn set_device_address(&mut self, addr: u8); 73 fn set_device_address(&mut self, addr: u8);
74 74
75 /// Sets the device configured state.
76 fn set_configured(&mut self, configured: bool);
77
75 /// Sets or clears the STALL condition for an endpoint. If the endpoint is an OUT endpoint, it 78 /// Sets or clears the STALL condition for an endpoint. If the endpoint is an OUT endpoint, it
76 /// should be prepared to receive data again. Only used during control transfers. 79 /// should be prepared to receive data again. Only used during control transfers.
77 fn set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool); 80 fn set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool);
@@ -105,6 +108,10 @@ pub trait Bus {
105} 108}
106 109
107pub trait Endpoint { 110pub trait Endpoint {
111 type WaitEnabledFuture<'a>: Future<Output = ()> + 'a
112 where
113 Self: 'a;
114
108 /// Get the endpoint address 115 /// Get the endpoint address
109 fn info(&self) -> &EndpointInfo; 116 fn info(&self) -> &EndpointInfo;
110 117
@@ -115,6 +122,9 @@ pub trait Endpoint {
115 /// Gets whether the STALL condition is set for an endpoint. 122 /// Gets whether the STALL condition is set for an endpoint.
116 fn is_stalled(&self) -> bool; 123 fn is_stalled(&self) -> bool;
117 124
125 /// Waits for the endpoint to be enabled.
126 fn wait_enabled(&mut self) -> Self::WaitEnabledFuture<'_>;
127
118 // TODO enable/disable? 128 // TODO enable/disable?
119} 129}
120 130
@@ -212,6 +222,7 @@ pub enum WriteError {
212 /// class shouldn't provide more data than the `max_packet_size` it specified when allocating 222 /// class shouldn't provide more data than the `max_packet_size` it specified when allocating
213 /// the endpoint. 223 /// the endpoint.
214 BufferOverflow, 224 BufferOverflow,
225 Disabled,
215} 226}
216 227
217#[derive(Copy, Clone, Eq, PartialEq, Debug)] 228#[derive(Copy, Clone, Eq, PartialEq, Debug)]
@@ -223,4 +234,5 @@ pub enum ReadError {
223 /// should use a buffer that is large enough for the `max_packet_size` it specified when 234 /// should use a buffer that is large enough for the `max_packet_size` it specified when
224 /// allocating the endpoint. 235 /// allocating the endpoint.
225 BufferOverflow, 236 BufferOverflow,
237 Disabled,
226} 238}
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index d2d3e5e0a..cf8d12539 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -162,18 +162,21 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
162 self.remote_wakeup_enabled = true; 162 self.remote_wakeup_enabled = true;
163 self.control.accept(stage) 163 self.control.accept(stage)
164 } 164 }
165 (Request::SET_ADDRESS, 1..=127) => { 165 (Request::SET_ADDRESS, addr @ 1..=127) => {
166 self.pending_address = req.value as u8; 166 self.pending_address = addr as u8;
167 self.bus.set_device_address(self.pending_address);
167 self.control.accept(stage) 168 self.control.accept(stage)
168 } 169 }
169 (Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => { 170 (Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => {
170 self.device_state = UsbDeviceState::Configured; 171 self.device_state = UsbDeviceState::Configured;
172 self.bus.set_configured(true);
171 self.control.accept(stage) 173 self.control.accept(stage)
172 } 174 }
173 (Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => match self.device_state { 175 (Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => match self.device_state {
174 UsbDeviceState::Default => self.control.accept(stage), 176 UsbDeviceState::Default => self.control.accept(stage),
175 _ => { 177 _ => {
176 self.device_state = UsbDeviceState::Addressed; 178 self.device_state = UsbDeviceState::Addressed;
179 self.bus.set_configured(false);
177 self.control.accept(stage) 180 self.control.accept(stage)
178 } 181 }
179 }, 182 },