Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to usb-device 0.3 #25

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
[Unreleased]
------------

**BREAKING** Update to `usb-device` 0.3. By adopting this release, you're
required to use `usb-device` 0.3 and its compatible packages.

[0.2.2] 2023-09-15
------------------

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exclude = [
bitflags = "1.2"
cortex-m = "0.7"
ral-registers = "0.1"
usb-device = "0.2"
usb-device = "0.3"

[dependencies.log]
optional = true
Expand Down
14 changes: 7 additions & 7 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,29 @@ mod test {

#[test]
fn allocate_entire_buffer() {
static mut BUFFER: [u8; 32] = [0; 32];
let mut alloc = unsafe { Allocator::new(&mut BUFFER) };
let mut buffer: [u8; 32] = [0; 32];
let mut alloc = unsafe { Allocator::from_buffer(&mut buffer) };
let ptr = alloc.allocate(32);
assert!(ptr.is_some());
assert_eq!(ptr.unwrap().ptr, unsafe { BUFFER.as_mut_ptr() });
assert_eq!(ptr.unwrap().ptr, buffer.as_mut_ptr());

let ptr = alloc.allocate(1);
assert!(ptr.is_none());
}

#[test]
fn allocate_partial_buffers() {
static mut BUFFER: [u8; 32] = [0; 32];
let mut alloc = unsafe { Allocator::new(&mut BUFFER) };
let mut buffer: [u8; 32] = [0; 32];
let mut alloc = unsafe { Allocator::from_buffer(&mut buffer) };

let ptr = alloc.allocate(7);
assert!(ptr.is_some());
assert_eq!(ptr.unwrap().ptr, unsafe { BUFFER.as_mut_ptr().add(32 - 7) });
assert_eq!(ptr.unwrap().ptr, unsafe { buffer.as_mut_ptr().add(32 - 7) });

let ptr = alloc.allocate(7);
assert!(ptr.is_some());
assert_eq!(ptr.unwrap().ptr, unsafe {
BUFFER.as_mut_ptr().add(32 - 14)
buffer.as_mut_ptr().add(32 - 14)
});

let ptr = alloc.allocate(19);
Expand Down
6 changes: 4 additions & 2 deletions src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub use super::driver::Speed;
/// use usb_device::prelude::*;
/// let bus_allocator = usb_device::bus::UsbBusAllocator::new(bus_adapter);
/// let mut device = UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x5824, 0x27dd))
/// .product("imxrt-usbd")
/// .strings(&[StringDescriptors::default().product("imxrt-usbd")]).unwrap()
/// // Other builder methods...
/// .build();
///
Expand Down Expand Up @@ -181,7 +181,9 @@ impl BusAdapter {
buffer,
state,
speed,
Some(cortex_m::interrupt::CriticalSection::new()),
// Safety: see the above API docs. Caller knows that we're faking our
// Sync capability.
Some(unsafe { cortex_m::interrupt::CriticalSection::new() }),
)
}

Expand Down
18 changes: 14 additions & 4 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl Endpoint {
let endptctrl = endpoint_control::register(usb, self.address.index());
match self.address.direction() {
UsbDirection::In => {
ral::modify_reg!(endpoint_control, &endptctrl, ENDPTCTRL, TXE: 0, TXT: EndpointType::Bulk as u32)
ral::modify_reg!(endpoint_control, &endptctrl, ENDPTCTRL, TXE: 0, TXT: into_raw_endpoint_type(EndpointType::Bulk))
}
UsbDirection::Out => {
ral::modify_reg!(endpoint_control, &endptctrl, ENDPTCTRL, RXE: 0, RXT: EndpointType::Bulk as u32)
ral::modify_reg!(endpoint_control, &endptctrl, ENDPTCTRL, RXE: 0, RXT: into_raw_endpoint_type(EndpointType::Bulk))
}
}
}
Expand Down Expand Up @@ -248,10 +248,10 @@ impl Endpoint {
let endptctrl = endpoint_control::register(usb, self.address.index());
match self.address.direction() {
UsbDirection::In => {
ral::modify_reg!(endpoint_control, &endptctrl, ENDPTCTRL, TXE: 1, TXR: 1, TXT: self.kind as u32)
ral::modify_reg!(endpoint_control, &endptctrl, ENDPTCTRL, TXE: 1, TXR: 1, TXT: into_raw_endpoint_type(self.kind))
}
UsbDirection::Out => {
ral::modify_reg!(endpoint_control, &endptctrl, ENDPTCTRL, RXE: 1, RXR: 1, RXT: self.kind as u32)
ral::modify_reg!(endpoint_control, &endptctrl, ENDPTCTRL, RXE: 1, RXR: 1, RXT: into_raw_endpoint_type(self.kind))
}
}
}
Expand Down Expand Up @@ -284,3 +284,13 @@ impl Endpoint {
}
}
}

/// Converts the endpoint type into its ENDPTCTRL endpoint type
/// enumerations.
///
/// See the ENDPTCTRL register documentation in the reference manual.
fn into_raw_endpoint_type(ep_type: EndpointType) -> u32 {
// Bits 0..1 represent the transfer type for the endpoint,
// and it's compatible with ENDPTCTRL's enumerated values.
(ep_type.to_bm_attributes() & 0b11u8).into()
}
2 changes: 1 addition & 1 deletion src/gpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
//! let bus_allocator = usb_device::bus::UsbBusAllocator::new(bus_adapter);
//!
//! let mut device = UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x5824, 0x27dd))
//! .product("imxrt-usbd")
//! .strings(&[StringDescriptors::default().product("imxrt-usbd")]).unwrap()
//! .build();
//!
//! // You can still access the timer through the bus() method on
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! [`usb-device`]: https://crates.io/crates/usb-device

#![no_std]
#![warn(unsafe_op_in_unsafe_fn)]

#[macro_use]
mod log;
Expand Down
Loading