aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src/lib.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-03-09 23:06:27 +0100
committerDario Nieuwenhuis <[email protected]>2022-04-06 05:38:11 +0200
commit77ceced036d574c7d67259b85e1d61b96e82d0d3 (patch)
tree4f1a250f92d59922ae7ceda0779aae16cfa767bb /embassy-usb/src/lib.rs
parent37598a5b3792ec1b763b5c16fe422c9e1347d7d6 (diff)
Working CDC-ACM host->device
Diffstat (limited to 'embassy-usb/src/lib.rs')
-rw-r--r--embassy-usb/src/lib.rs64
1 files changed, 47 insertions, 17 deletions
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index 397db96c4..33f3d4712 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -9,11 +9,13 @@ mod control;
9pub mod descriptor; 9pub mod descriptor;
10pub mod driver; 10pub mod driver;
11pub mod types; 11pub mod types;
12mod util;
12 13
13use self::control::*; 14use self::control::*;
14use self::descriptor::*; 15use self::descriptor::*;
15use self::driver::*; 16use self::driver::*;
16use self::types::*; 17use self::types::*;
18use self::util::*;
17 19
18pub use self::builder::Config; 20pub use self::builder::Config;
19pub use self::builder::UsbDeviceBuilder; 21pub use self::builder::UsbDeviceBuilder;
@@ -47,7 +49,7 @@ pub const CONFIGURATION_VALUE: u8 = 1;
47pub const DEFAULT_ALTERNATE_SETTING: u8 = 0; 49pub const DEFAULT_ALTERNATE_SETTING: u8 = 0;
48 50
49pub struct UsbDevice<'d, D: Driver<'d>> { 51pub struct UsbDevice<'d, D: Driver<'d>> {
50 driver: D::Bus, 52 bus: D::Bus,
51 control_in: D::EndpointIn, 53 control_in: D::EndpointIn,
52 control_out: D::EndpointOut, 54 control_out: D::EndpointOut,
53 55
@@ -93,7 +95,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
93 let driver = driver.enable(); 95 let driver = driver.enable();
94 96
95 Self { 97 Self {
96 driver, 98 bus: driver,
97 config, 99 config,
98 control_in, 100 control_in,
99 control_out, 101 control_out,
@@ -108,20 +110,47 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
108 } 110 }
109 111
110 pub async fn run(&mut self) { 112 pub async fn run(&mut self) {
113 let mut buf = [0; 8];
114
111 loop { 115 loop {
112 let mut buf = [0; 8]; 116 let control_fut = self.control_out.read(&mut buf);
113 let n = self.control_out.read(&mut buf).await.unwrap(); 117 let bus_fut = self.bus.poll();
114 assert_eq!(n, 8); 118 match select(bus_fut, control_fut).await {
115 let req = Request::parse(&buf).unwrap(); 119 Either::Left(evt) => match evt {
116 info!("setup request: {:x}", req); 120 Event::Reset => {
117 121 self.bus.reset();
118 // Now that we have properly parsed the setup packet, ensure the end-point is no longer in 122
119 // a stalled state. 123 self.device_state = UsbDeviceState::Default;
120 self.control_out.set_stalled(false); 124 self.remote_wakeup_enabled = false;
121 125 self.pending_address = 0;
122 match req.direction { 126
123 UsbDirection::In => self.handle_control_in(req).await, 127 // TODO
124 UsbDirection::Out => self.handle_control_out(req).await, 128 //self.control.reset();
129 //for cls in classes {
130 // cls.reset();
131 //}
132 }
133 Event::Resume => {}
134 Event::Suspend => {
135 self.bus.suspend();
136 self.device_state = UsbDeviceState::Suspend;
137 }
138 },
139 Either::Right(n) => {
140 let n = n.unwrap();
141 assert_eq!(n, 8);
142 let req = Request::parse(&buf).unwrap();
143 info!("control request: {:x}", req);
144
145 // Now that we have properly parsed the setup packet, ensure the end-point is no longer in
146 // a stalled state.
147 self.control_out.set_stalled(false);
148
149 match req.direction {
150 UsbDirection::In => self.handle_control_in(req).await,
151 UsbDirection::Out => self.handle_control_out(req).await,
152 }
153 }
125 } 154 }
126 } 155 }
127 } 156 }
@@ -205,7 +234,8 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
205 } 234 }
206 235
207 (Recipient::Endpoint, Request::SET_FEATURE, Request::FEATURE_ENDPOINT_HALT) => { 236 (Recipient::Endpoint, Request::SET_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
208 //self.bus.set_stalled(((req.index as u8) & 0x8f).into(), true); 237 self.bus
238 .set_stalled(((req.index as u8) & 0x8f).into(), true);
209 self.control_out_accept(req).await; 239 self.control_out_accept(req).await;
210 } 240 }
211 241
@@ -266,7 +296,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
266 (Recipient::Endpoint, Request::GET_STATUS) => { 296 (Recipient::Endpoint, Request::GET_STATUS) => {
267 let ep_addr: EndpointAddress = ((req.index as u8) & 0x8f).into(); 297 let ep_addr: EndpointAddress = ((req.index as u8) & 0x8f).into();
268 let mut status: u16 = 0x0000; 298 let mut status: u16 = 0x0000;
269 if self.driver.is_stalled(ep_addr) { 299 if self.bus.is_stalled(ep_addr) {
270 status |= 0x0001; 300 status |= 0x0001;
271 } 301 }
272 self.control_in_accept(req, &status.to_le_bytes()).await; 302 self.control_in_accept(req, &status.to_le_bytes()).await;