Skip to content

Commit 390ac1d

Browse files
committed
Merge branch 'master' into feature/control-buffer-args
2 parents fb7e445 + 6a44a84 commit 390ac1d

File tree

6 files changed

+112
-85
lines changed

6 files changed

+112
-85
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
## [0.3.2] - 2024-03-06
11+
1012
### Added
1113
* A new `log` feature can be enabled to provide logging and tracing information about the USB
1214
interface.
@@ -83,7 +85,8 @@ pipes. This makes it so that control pipes have configurable sizing.
8385

8486
This is the initial release to crates.io.
8587

86-
[Unreleased]: https://github.com/rust-embedded-community/usb-device/compare/v0.3.1...HEAD
88+
[Unreleased]: https://github.com/rust-embedded-community/usb-device/compare/v0.3.2...HEAD
89+
[0.3.2]: https://github.com/rust-embedded-community/usb-device/compare/v0.3.1...v0.3.2
8790
[0.3.1]: https://github.com/rust-embedded-community/usb-device/compare/v0.3.0...v0.3.1
8891
[0.3.0]: https://github.com/rust-embedded-community/usb-device/compare/v0.2.9...v0.3.0
8992
[0.2.9]: https://github.com/rust-embedded-community/usb-device/compare/v0.2.8...v0.2.9

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "usb-device"
33
description = "USB stack for embedded devices."
4-
version = "0.3.1"
4+
version = "0.3.2"
55
edition = "2018"
66
readme = "README.md"
77
keywords = ["no-std", "embedded", "usb"]

src/control_pipe.rs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,19 @@ impl<B: UsbBus> ControlPipe<'_, B> {
126126
None
127127
}
128128

129-
pub fn handle_out(&mut self) -> Option<Request> {
129+
pub fn handle_out(&mut self) -> Result<Option<Request>> {
130130
match self.state {
131131
ControlState::DataOut(req) => {
132132
let i = self.i;
133133
let count = match self.ep_out.read(&mut self.buf[i..]) {
134134
Ok(count) => count,
135-
Err(UsbError::WouldBlock) => return None,
136-
Err(_) => {
135+
Err(UsbError::WouldBlock) => return Ok(None),
136+
Err(_err) => {
137137
// Failed to read or buffer overflow (overflow is only possible if the host
138138
// sends more data than it indicated in the SETUP request)
139+
usb_debug!("Failed EP0 read: {:?}", _err);
139140
self.set_error();
140-
return None;
141+
return Ok(None);
141142
}
142143
};
143144

@@ -151,7 +152,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
151152
if self.i >= self.len {
152153
usb_debug!("Request OUT complete: {:?}", req);
153154
self.state = ControlState::CompleteOut;
154-
return Some(req);
155+
return Ok(Some(req));
155156
}
156157
}
157158
// The host may terminate a DATA stage early by sending a zero-length status packet
@@ -164,7 +165,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
164165
"Control transfer completed. Current state: {:?}",
165166
self.state
166167
);
167-
let _ = self.ep_out.read(&mut []);
168+
self.ep_out.read(&mut [])?;
168169
self.state = ControlState::Idle;
169170
}
170171
_ => {
@@ -173,28 +174,23 @@ impl<B: UsbBus> ControlPipe<'_, B> {
173174
"Discarding EP0 data due to unexpected state. Current state: {:?}",
174175
self.state
175176
);
176-
let _ = self.ep_out.read(&mut []);
177+
self.ep_out.read(&mut [])?;
177178

178179
// Unexpected OUT packet
179180
self.set_error()
180181
}
181182
}
182183

183-
None
184+
Ok(None)
184185
}
185186

186-
pub fn handle_in_complete(&mut self) -> bool {
187+
pub fn handle_in_complete(&mut self) -> Result<bool> {
187188
match self.state {
188189
ControlState::DataIn => {
189-
self.write_in_chunk();
190+
self.write_in_chunk()?;
190191
}
191192
ControlState::DataInZlp => {
192-
if self.ep_in.write(&[]).is_err() {
193-
// There isn't much we can do if the write fails, except to wait for another
194-
// poll or for the host to resend the request.
195-
return false;
196-
}
197-
193+
self.ep_in.write(&[])?;
198194
usb_trace!("wrote EP0: ZLP");
199195
self.state = ControlState::DataInLast;
200196
}
@@ -204,7 +200,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
204200
}
205201
ControlState::StatusIn => {
206202
self.state = ControlState::Idle;
207-
return true;
203+
return Ok(true);
208204
}
209205
ControlState::Idle => {
210206
// If we received a message on EP0 while sending the last portion of an IN
@@ -218,23 +214,14 @@ impl<B: UsbBus> ControlPipe<'_, B> {
218214
}
219215
};
220216

221-
false
217+
Ok(false)
222218
}
223219

224-
fn write_in_chunk(&mut self) {
220+
fn write_in_chunk(&mut self) -> Result<()> {
225221
let count = min(self.len - self.i, self.ep_in.max_packet_size() as usize);
226222

227223
let buffer = self.static_in_buf.unwrap_or(&self.buf);
228-
let count = match self.ep_in.write(&buffer[self.i..(self.i + count)]) {
229-
Ok(c) => c,
230-
// There isn't much we can do if the write fails, except to wait for another poll or for
231-
// the host to resend the request.
232-
Err(_err) => {
233-
usb_debug!("Failed to write EP0: {:?}", _err);
234-
return;
235-
}
236-
};
237-
224+
let count = self.ep_in.write(&buffer[self.i..(self.i + count)])?;
238225
usb_trace!("wrote EP0: {:?}", &buffer[self.i..(self.i + count)]);
239226

240227
self.i += count;
@@ -248,6 +235,8 @@ impl<B: UsbBus> ControlPipe<'_, B> {
248235
ControlState::DataInLast
249236
};
250237
}
238+
239+
Ok(())
251240
}
252241

253242
pub fn accept_out(&mut self) -> Result<()> {
@@ -259,7 +248,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
259248
}
260249
};
261250

262-
let _ = self.ep_in.write(&[]);
251+
self.ep_in.write(&[])?;
263252
self.state = ControlState::StatusIn;
264253
Ok(())
265254
}
@@ -301,7 +290,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
301290
self.len = min(data_len, req.length as usize);
302291
self.i = 0;
303292
self.state = ControlState::DataIn;
304-
self.write_in_chunk();
293+
self.write_in_chunk()?;
305294

306295
Ok(())
307296
}

src/descriptor/lang_id.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#[allow(missing_docs)]
44
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
5+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
56
pub struct LangID(u16);
67

78
impl From<LangID> for u16 {

0 commit comments

Comments
 (0)