diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-09-26 12:29:27 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-09-26 12:29:27 +0200 |
| commit | 7f7c14b7bce5b84eb27c8122535a96a6f0e5dd77 (patch) | |
| tree | c7481fd07b616128718301de7aafea553cc8dd6f | |
| parent | a9efbf18c62186de0581ec72cca90a69340a02a3 (diff) | |
usb: split driver trait to separate crate.
| -rw-r--r-- | embassy-nrf/src/usb.rs | 17 | ||||
| -rw-r--r-- | embassy-rp/src/usb.rs | 27 | ||||
| -rw-r--r-- | embassy-stm32/src/usb/usb.rs | 35 | ||||
| -rw-r--r-- | embassy-usb-driver/Cargo.toml | 16 | ||||
| -rw-r--r-- | embassy-usb-driver/src/lib.rs (renamed from embassy-usb/src/driver.rs) | 107 | ||||
| -rw-r--r-- | embassy-usb/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-usb/src/builder.rs | 11 | ||||
| -rw-r--r-- | embassy-usb/src/control.rs | 5 | ||||
| -rw-r--r-- | embassy-usb/src/descriptor.rs | 7 | ||||
| -rw-r--r-- | embassy-usb/src/descriptor_reader.rs | 2 | ||||
| -rw-r--r-- | embassy-usb/src/lib.rs | 18 | ||||
| -rw-r--r-- | embassy-usb/src/types.rs | 105 |
12 files changed, 189 insertions, 165 deletions
diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index 0685d419c..20510eb49 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs | |||
| @@ -10,8 +10,9 @@ use cortex_m::peripheral::NVIC; | |||
| 10 | use embassy_hal_common::{into_ref, PeripheralRef}; | 10 | use embassy_hal_common::{into_ref, PeripheralRef}; |
| 11 | use embassy_sync::waitqueue::AtomicWaker; | 11 | use embassy_sync::waitqueue::AtomicWaker; |
| 12 | pub use embassy_usb; | 12 | pub use embassy_usb; |
| 13 | use embassy_usb::driver::{self, EndpointError, Event, Unsupported}; | 13 | use embassy_usb::driver::{ |
| 14 | use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection}; | 14 | self, Direction, EndpointAddress, EndpointError, EndpointInfo, EndpointType, Event, Unsupported, |
| 15 | }; | ||
| 15 | use pac::usbd::RegisterBlock; | 16 | use pac::usbd::RegisterBlock; |
| 16 | 17 | ||
| 17 | use crate::interrupt::{Interrupt, InterruptExt}; | 18 | use crate::interrupt::{Interrupt, InterruptExt}; |
| @@ -243,7 +244,7 @@ impl<'d, T: Instance, P: UsbSupply + 'd> driver::Driver<'d> for Driver<'d, T, P> | |||
| 243 | interval: u8, | 244 | interval: u8, |
| 244 | ) -> Result<Self::EndpointIn, driver::EndpointAllocError> { | 245 | ) -> Result<Self::EndpointIn, driver::EndpointAllocError> { |
| 245 | let index = self.alloc_in.allocate(ep_type)?; | 246 | let index = self.alloc_in.allocate(ep_type)?; |
| 246 | let ep_addr = EndpointAddress::from_parts(index, UsbDirection::In); | 247 | let ep_addr = EndpointAddress::from_parts(index, Direction::In); |
| 247 | Ok(Endpoint::new(EndpointInfo { | 248 | Ok(Endpoint::new(EndpointInfo { |
| 248 | addr: ep_addr, | 249 | addr: ep_addr, |
| 249 | ep_type, | 250 | ep_type, |
| @@ -259,7 +260,7 @@ impl<'d, T: Instance, P: UsbSupply + 'd> driver::Driver<'d> for Driver<'d, T, P> | |||
| 259 | interval: u8, | 260 | interval: u8, |
| 260 | ) -> Result<Self::EndpointOut, driver::EndpointAllocError> { | 261 | ) -> Result<Self::EndpointOut, driver::EndpointAllocError> { |
| 261 | let index = self.alloc_out.allocate(ep_type)?; | 262 | let index = self.alloc_out.allocate(ep_type)?; |
| 262 | let ep_addr = EndpointAddress::from_parts(index, UsbDirection::Out); | 263 | let ep_addr = EndpointAddress::from_parts(index, Direction::Out); |
| 263 | Ok(Endpoint::new(EndpointInfo { | 264 | Ok(Endpoint::new(EndpointInfo { |
| 264 | addr: ep_addr, | 265 | addr: ep_addr, |
| 265 | ep_type, | 266 | ep_type, |
| @@ -428,8 +429,8 @@ impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> { | |||
| 428 | let regs = T::regs(); | 429 | let regs = T::regs(); |
| 429 | let i = ep_addr.index(); | 430 | let i = ep_addr.index(); |
| 430 | match ep_addr.direction() { | 431 | match ep_addr.direction() { |
| 431 | UsbDirection::Out => regs.halted.epout[i].read().getstatus().is_halted(), | 432 | Direction::Out => regs.halted.epout[i].read().getstatus().is_halted(), |
| 432 | UsbDirection::In => regs.halted.epin[i].read().getstatus().is_halted(), | 433 | Direction::In => regs.halted.epin[i].read().getstatus().is_halted(), |
| 433 | } | 434 | } |
| 434 | } | 435 | } |
| 435 | 436 | ||
| @@ -442,7 +443,7 @@ impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> { | |||
| 442 | debug!("endpoint_set_enabled {:?} {}", ep_addr, enabled); | 443 | debug!("endpoint_set_enabled {:?} {}", ep_addr, enabled); |
| 443 | 444 | ||
| 444 | match ep_addr.direction() { | 445 | match ep_addr.direction() { |
| 445 | UsbDirection::In => { | 446 | Direction::In => { |
| 446 | let mut was_enabled = false; | 447 | let mut was_enabled = false; |
| 447 | regs.epinen.modify(|r, w| { | 448 | regs.epinen.modify(|r, w| { |
| 448 | let mut bits = r.bits(); | 449 | let mut bits = r.bits(); |
| @@ -466,7 +467,7 @@ impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> { | |||
| 466 | 467 | ||
| 467 | In::waker(i).wake(); | 468 | In::waker(i).wake(); |
| 468 | } | 469 | } |
| 469 | UsbDirection::Out => { | 470 | Direction::Out => { |
| 470 | regs.epouten.modify(|r, w| { | 471 | regs.epouten.modify(|r, w| { |
| 471 | let mut bits = r.bits(); | 472 | let mut bits = r.bits(); |
| 472 | if enabled { | 473 | if enabled { |
diff --git a/embassy-rp/src/usb.rs b/embassy-rp/src/usb.rs index a7ec5fb79..ce473b21d 100644 --- a/embassy-rp/src/usb.rs +++ b/embassy-rp/src/usb.rs | |||
| @@ -7,8 +7,9 @@ use core::task::Poll; | |||
| 7 | use atomic_polyfill::compiler_fence; | 7 | use atomic_polyfill::compiler_fence; |
| 8 | use embassy_hal_common::into_ref; | 8 | use embassy_hal_common::into_ref; |
| 9 | use embassy_sync::waitqueue::AtomicWaker; | 9 | use embassy_sync::waitqueue::AtomicWaker; |
| 10 | use embassy_usb::driver::{self, EndpointAllocError, EndpointError, Event, Unsupported}; | 10 | use embassy_usb::driver::{ |
| 11 | use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection}; | 11 | self, Direction, EndpointAddress, EndpointAllocError, EndpointError, EndpointInfo, EndpointType, Event, Unsupported, |
| 12 | }; | ||
| 12 | 13 | ||
| 13 | use crate::interrupt::{Interrupt, InterruptExt}; | 14 | use crate::interrupt::{Interrupt, InterruptExt}; |
| 14 | use crate::{pac, peripherals, Peripheral, RegExt}; | 15 | use crate::{pac, peripherals, Peripheral, RegExt}; |
| @@ -204,8 +205,8 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 204 | ); | 205 | ); |
| 205 | 206 | ||
| 206 | let alloc = match D::dir() { | 207 | let alloc = match D::dir() { |
| 207 | UsbDirection::Out => &mut self.ep_out, | 208 | Direction::Out => &mut self.ep_out, |
| 208 | UsbDirection::In => &mut self.ep_in, | 209 | Direction::In => &mut self.ep_in, |
| 209 | }; | 210 | }; |
| 210 | 211 | ||
| 211 | let index = alloc.iter_mut().enumerate().find(|(i, ep)| { | 212 | let index = alloc.iter_mut().enumerate().find(|(i, ep)| { |
| @@ -254,7 +255,7 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 254 | }; | 255 | }; |
| 255 | 256 | ||
| 256 | match D::dir() { | 257 | match D::dir() { |
| 257 | UsbDirection::Out => unsafe { | 258 | Direction::Out => unsafe { |
| 258 | T::dpram().ep_out_control(index - 1).write(|w| { | 259 | T::dpram().ep_out_control(index - 1).write(|w| { |
| 259 | w.set_enable(false); | 260 | w.set_enable(false); |
| 260 | w.set_buffer_address(addr); | 261 | w.set_buffer_address(addr); |
| @@ -262,7 +263,7 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 262 | w.set_endpoint_type(ep_type_reg); | 263 | w.set_endpoint_type(ep_type_reg); |
| 263 | }) | 264 | }) |
| 264 | }, | 265 | }, |
| 265 | UsbDirection::In => unsafe { | 266 | Direction::In => unsafe { |
| 266 | T::dpram().ep_in_control(index - 1).write(|w| { | 267 | T::dpram().ep_in_control(index - 1).write(|w| { |
| 267 | w.set_enable(false); | 268 | w.set_enable(false); |
| 268 | w.set_buffer_address(addr); | 269 | w.set_buffer_address(addr); |
| @@ -429,14 +430,14 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 429 | 430 | ||
| 430 | let n = ep_addr.index(); | 431 | let n = ep_addr.index(); |
| 431 | match ep_addr.direction() { | 432 | match ep_addr.direction() { |
| 432 | UsbDirection::In => unsafe { | 433 | Direction::In => unsafe { |
| 433 | T::dpram().ep_in_control(n - 1).modify(|w| w.set_enable(enabled)); | 434 | T::dpram().ep_in_control(n - 1).modify(|w| w.set_enable(enabled)); |
| 434 | T::dpram().ep_in_buffer_control(ep_addr.index()).write(|w| { | 435 | T::dpram().ep_in_buffer_control(ep_addr.index()).write(|w| { |
| 435 | w.set_pid(0, true); // first packet is DATA0, but PID is flipped before | 436 | w.set_pid(0, true); // first packet is DATA0, but PID is flipped before |
| 436 | }); | 437 | }); |
| 437 | EP_IN_WAKERS[n].wake(); | 438 | EP_IN_WAKERS[n].wake(); |
| 438 | }, | 439 | }, |
| 439 | UsbDirection::Out => unsafe { | 440 | Direction::Out => unsafe { |
| 440 | T::dpram().ep_out_control(n - 1).modify(|w| w.set_enable(enabled)); | 441 | T::dpram().ep_out_control(n - 1).modify(|w| w.set_enable(enabled)); |
| 441 | 442 | ||
| 442 | T::dpram().ep_out_buffer_control(ep_addr.index()).write(|w| { | 443 | T::dpram().ep_out_buffer_control(ep_addr.index()).write(|w| { |
| @@ -474,14 +475,14 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 474 | } | 475 | } |
| 475 | 476 | ||
| 476 | trait Dir { | 477 | trait Dir { |
| 477 | fn dir() -> UsbDirection; | 478 | fn dir() -> Direction; |
| 478 | fn waker(i: usize) -> &'static AtomicWaker; | 479 | fn waker(i: usize) -> &'static AtomicWaker; |
| 479 | } | 480 | } |
| 480 | 481 | ||
| 481 | pub enum In {} | 482 | pub enum In {} |
| 482 | impl Dir for In { | 483 | impl Dir for In { |
| 483 | fn dir() -> UsbDirection { | 484 | fn dir() -> Direction { |
| 484 | UsbDirection::In | 485 | Direction::In |
| 485 | } | 486 | } |
| 486 | 487 | ||
| 487 | #[inline] | 488 | #[inline] |
| @@ -492,8 +493,8 @@ impl Dir for In { | |||
| 492 | 493 | ||
| 493 | pub enum Out {} | 494 | pub enum Out {} |
| 494 | impl Dir for Out { | 495 | impl Dir for Out { |
| 495 | fn dir() -> UsbDirection { | 496 | fn dir() -> Direction { |
| 496 | UsbDirection::Out | 497 | Direction::Out |
| 497 | } | 498 | } |
| 498 | 499 | ||
| 499 | #[inline] | 500 | #[inline] |
diff --git a/embassy-stm32/src/usb/usb.rs b/embassy-stm32/src/usb/usb.rs index e5ee1181c..39809a3e1 100644 --- a/embassy-stm32/src/usb/usb.rs +++ b/embassy-stm32/src/usb/usb.rs | |||
| @@ -9,8 +9,9 @@ use atomic_polyfill::{AtomicBool, AtomicU8}; | |||
| 9 | use embassy_hal_common::into_ref; | 9 | use embassy_hal_common::into_ref; |
| 10 | use embassy_sync::waitqueue::AtomicWaker; | 10 | use embassy_sync::waitqueue::AtomicWaker; |
| 11 | use embassy_time::{block_for, Duration}; | 11 | use embassy_time::{block_for, Duration}; |
| 12 | use embassy_usb::driver::{self, EndpointAllocError, EndpointError, Event, Unsupported}; | 12 | use embassy_usb::driver::{ |
| 13 | use embassy_usb::types::{EndpointAddress, EndpointInfo, EndpointType, UsbDirection}; | 13 | self, Direction, EndpointAddress, EndpointAllocError, EndpointError, EndpointInfo, EndpointType, Event, Unsupported, |
| 14 | }; | ||
| 14 | use pac::common::{Reg, RW}; | 15 | use pac::common::{Reg, RW}; |
| 15 | use pac::usb::vals::{EpType, Stat}; | 16 | use pac::usb::vals::{EpType, Stat}; |
| 16 | 17 | ||
| @@ -279,8 +280,8 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 279 | } | 280 | } |
| 280 | let used = ep.used_out || ep.used_in; | 281 | let used = ep.used_out || ep.used_in; |
| 281 | let used_dir = match D::dir() { | 282 | let used_dir = match D::dir() { |
| 282 | UsbDirection::Out => ep.used_out, | 283 | Direction::Out => ep.used_out, |
| 283 | UsbDirection::In => ep.used_in, | 284 | Direction::In => ep.used_in, |
| 284 | }; | 285 | }; |
| 285 | !used || (ep.ep_type == ep_type && !used_dir) | 286 | !used || (ep.ep_type == ep_type && !used_dir) |
| 286 | }); | 287 | }); |
| @@ -293,7 +294,7 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 293 | ep.ep_type = ep_type; | 294 | ep.ep_type = ep_type; |
| 294 | 295 | ||
| 295 | let buf = match D::dir() { | 296 | let buf = match D::dir() { |
| 296 | UsbDirection::Out => { | 297 | Direction::Out => { |
| 297 | assert!(!ep.used_out); | 298 | assert!(!ep.used_out); |
| 298 | ep.used_out = true; | 299 | ep.used_out = true; |
| 299 | 300 | ||
| @@ -312,7 +313,7 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 312 | _phantom: PhantomData, | 313 | _phantom: PhantomData, |
| 313 | } | 314 | } |
| 314 | } | 315 | } |
| 315 | UsbDirection::In => { | 316 | Direction::In => { |
| 316 | assert!(!ep.used_in); | 317 | assert!(!ep.used_in); |
| 317 | ep.used_in = true; | 318 | ep.used_in = true; |
| 318 | 319 | ||
| @@ -504,7 +505,7 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 504 | // This can race, so do a retry loop. | 505 | // This can race, so do a retry loop. |
| 505 | let reg = T::regs().epr(ep_addr.index() as _); | 506 | let reg = T::regs().epr(ep_addr.index() as _); |
| 506 | match ep_addr.direction() { | 507 | match ep_addr.direction() { |
| 507 | UsbDirection::In => { | 508 | Direction::In => { |
| 508 | loop { | 509 | loop { |
| 509 | let r = unsafe { reg.read() }; | 510 | let r = unsafe { reg.read() }; |
| 510 | match r.stat_tx() { | 511 | match r.stat_tx() { |
| @@ -523,7 +524,7 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 523 | } | 524 | } |
| 524 | EP_IN_WAKERS[ep_addr.index()].wake(); | 525 | EP_IN_WAKERS[ep_addr.index()].wake(); |
| 525 | } | 526 | } |
| 526 | UsbDirection::Out => { | 527 | Direction::Out => { |
| 527 | loop { | 528 | loop { |
| 528 | let r = unsafe { reg.read() }; | 529 | let r = unsafe { reg.read() }; |
| 529 | match r.stat_rx() { | 530 | match r.stat_rx() { |
| @@ -549,8 +550,8 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 549 | let regs = T::regs(); | 550 | let regs = T::regs(); |
| 550 | let epr = unsafe { regs.epr(ep_addr.index() as _).read() }; | 551 | let epr = unsafe { regs.epr(ep_addr.index() as _).read() }; |
| 551 | match ep_addr.direction() { | 552 | match ep_addr.direction() { |
| 552 | UsbDirection::In => epr.stat_tx() == Stat::STALL, | 553 | Direction::In => epr.stat_tx() == Stat::STALL, |
| 553 | UsbDirection::Out => epr.stat_rx() == Stat::STALL, | 554 | Direction::Out => epr.stat_rx() == Stat::STALL, |
| 554 | } | 555 | } |
| 555 | } | 556 | } |
| 556 | 557 | ||
| @@ -560,7 +561,7 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 560 | let reg = T::regs().epr(ep_addr.index() as _); | 561 | let reg = T::regs().epr(ep_addr.index() as _); |
| 561 | trace!("EPR before: {:04x}", unsafe { reg.read() }.0); | 562 | trace!("EPR before: {:04x}", unsafe { reg.read() }.0); |
| 562 | match ep_addr.direction() { | 563 | match ep_addr.direction() { |
| 563 | UsbDirection::In => { | 564 | Direction::In => { |
| 564 | loop { | 565 | loop { |
| 565 | let want_stat = match enabled { | 566 | let want_stat = match enabled { |
| 566 | false => Stat::DISABLED, | 567 | false => Stat::DISABLED, |
| @@ -576,7 +577,7 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 576 | } | 577 | } |
| 577 | EP_IN_WAKERS[ep_addr.index()].wake(); | 578 | EP_IN_WAKERS[ep_addr.index()].wake(); |
| 578 | } | 579 | } |
| 579 | UsbDirection::Out => { | 580 | Direction::Out => { |
| 580 | loop { | 581 | loop { |
| 581 | let want_stat = match enabled { | 582 | let want_stat = match enabled { |
| 582 | false => Stat::DISABLED, | 583 | false => Stat::DISABLED, |
| @@ -616,14 +617,14 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> { | |||
| 616 | } | 617 | } |
| 617 | 618 | ||
| 618 | trait Dir { | 619 | trait Dir { |
| 619 | fn dir() -> UsbDirection; | 620 | fn dir() -> Direction; |
| 620 | fn waker(i: usize) -> &'static AtomicWaker; | 621 | fn waker(i: usize) -> &'static AtomicWaker; |
| 621 | } | 622 | } |
| 622 | 623 | ||
| 623 | pub enum In {} | 624 | pub enum In {} |
| 624 | impl Dir for In { | 625 | impl Dir for In { |
| 625 | fn dir() -> UsbDirection { | 626 | fn dir() -> Direction { |
| 626 | UsbDirection::In | 627 | Direction::In |
| 627 | } | 628 | } |
| 628 | 629 | ||
| 629 | #[inline] | 630 | #[inline] |
| @@ -634,8 +635,8 @@ impl Dir for In { | |||
| 634 | 635 | ||
| 635 | pub enum Out {} | 636 | pub enum Out {} |
| 636 | impl Dir for Out { | 637 | impl Dir for Out { |
| 637 | fn dir() -> UsbDirection { | 638 | fn dir() -> Direction { |
| 638 | UsbDirection::Out | 639 | Direction::Out |
| 639 | } | 640 | } |
| 640 | 641 | ||
| 641 | #[inline] | 642 | #[inline] |
diff --git a/embassy-usb-driver/Cargo.toml b/embassy-usb-driver/Cargo.toml new file mode 100644 index 000000000..b525df337 --- /dev/null +++ b/embassy-usb-driver/Cargo.toml | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | [package] | ||
| 2 | name = "embassy-usb-driver" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
| 7 | |||
| 8 | [package.metadata.embassy_docs] | ||
| 9 | src_base = "https://github.com/embassy-rs/embassy/blob/embassy-usb-driver-v$VERSION/embassy-usb/src/" | ||
| 10 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-usb-driver/src/" | ||
| 11 | features = ["defmt"] | ||
| 12 | target = "thumbv7em-none-eabi" | ||
| 13 | |||
| 14 | [dependencies] | ||
| 15 | defmt = { version = "0.3", optional = true } | ||
| 16 | log = { version = "0.4.14", optional = true } \ No newline at end of file | ||
diff --git a/embassy-usb/src/driver.rs b/embassy-usb-driver/src/lib.rs index 7888f1639..051190a48 100644 --- a/embassy-usb/src/driver.rs +++ b/embassy-usb-driver/src/lib.rs | |||
| @@ -1,6 +1,111 @@ | |||
| 1 | #![no_std] | ||
| 2 | |||
| 1 | use core::future::Future; | 3 | use core::future::Future; |
| 2 | 4 | ||
| 3 | use super::types::*; | 5 | /// Direction of USB traffic. Note that in the USB standard the direction is always indicated from |
| 6 | /// the perspective of the host, which is backward for devices, but the standard directions are used | ||
| 7 | /// for consistency. | ||
| 8 | /// | ||
| 9 | /// The values of the enum also match the direction bit used in endpoint addresses and control | ||
| 10 | /// request types. | ||
| 11 | #[repr(u8)] | ||
| 12 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 13 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 14 | pub enum Direction { | ||
| 15 | /// Host to device (OUT) | ||
| 16 | Out = 0x00, | ||
| 17 | /// Device to host (IN) | ||
| 18 | In = 0x80, | ||
| 19 | } | ||
| 20 | |||
| 21 | impl From<u8> for Direction { | ||
| 22 | fn from(value: u8) -> Self { | ||
| 23 | unsafe { core::mem::transmute(value & 0x80) } | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | /// USB endpoint transfer type. The values of this enum can be directly cast into `u8` to get the | ||
| 28 | /// transfer bmAttributes transfer type bits. | ||
| 29 | #[repr(u8)] | ||
| 30 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 31 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 32 | pub enum EndpointType { | ||
| 33 | /// Control endpoint. Used for device management. Only the host can initiate requests. Usually | ||
| 34 | /// used only endpoint 0. | ||
| 35 | Control = 0b00, | ||
| 36 | /// Isochronous endpoint. Used for time-critical unreliable data. Not implemented yet. | ||
| 37 | Isochronous = 0b01, | ||
| 38 | /// Bulk endpoint. Used for large amounts of best-effort reliable data. | ||
| 39 | Bulk = 0b10, | ||
| 40 | /// Interrupt endpoint. Used for small amounts of time-critical reliable data. | ||
| 41 | Interrupt = 0b11, | ||
| 42 | } | ||
| 43 | |||
| 44 | /// Type-safe endpoint address. | ||
| 45 | #[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
| 46 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 47 | pub struct EndpointAddress(u8); | ||
| 48 | |||
| 49 | impl From<u8> for EndpointAddress { | ||
| 50 | #[inline] | ||
| 51 | fn from(addr: u8) -> EndpointAddress { | ||
| 52 | EndpointAddress(addr) | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | impl From<EndpointAddress> for u8 { | ||
| 57 | #[inline] | ||
| 58 | fn from(addr: EndpointAddress) -> u8 { | ||
| 59 | addr.0 | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | impl EndpointAddress { | ||
| 64 | const INBITS: u8 = Direction::In as u8; | ||
| 65 | |||
| 66 | /// Constructs a new EndpointAddress with the given index and direction. | ||
| 67 | #[inline] | ||
| 68 | pub fn from_parts(index: usize, dir: Direction) -> Self { | ||
| 69 | EndpointAddress(index as u8 | dir as u8) | ||
| 70 | } | ||
| 71 | |||
| 72 | /// Gets the direction part of the address. | ||
| 73 | #[inline] | ||
| 74 | pub fn direction(&self) -> Direction { | ||
| 75 | if (self.0 & Self::INBITS) != 0 { | ||
| 76 | Direction::In | ||
| 77 | } else { | ||
| 78 | Direction::Out | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | /// Returns true if the direction is IN, otherwise false. | ||
| 83 | #[inline] | ||
| 84 | pub fn is_in(&self) -> bool { | ||
| 85 | (self.0 & Self::INBITS) != 0 | ||
| 86 | } | ||
| 87 | |||
| 88 | /// Returns true if the direction is OUT, otherwise false. | ||
| 89 | #[inline] | ||
| 90 | pub fn is_out(&self) -> bool { | ||
| 91 | (self.0 & Self::INBITS) == 0 | ||
| 92 | } | ||
| 93 | |||
| 94 | /// Gets the index part of the endpoint address. | ||
| 95 | #[inline] | ||
| 96 | pub fn index(&self) -> usize { | ||
| 97 | (self.0 & !Self::INBITS) as usize | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 102 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 103 | pub struct EndpointInfo { | ||
| 104 | pub addr: EndpointAddress, | ||
| 105 | pub ep_type: EndpointType, | ||
| 106 | pub max_packet_size: u16, | ||
| 107 | pub interval: u8, | ||
| 108 | } | ||
| 4 | 109 | ||
| 5 | /// Driver for a specific USB peripheral. Implement this to add support for a new hardware | 110 | /// Driver for a specific USB peripheral. Implement this to add support for a new hardware |
| 6 | /// platform. | 111 | /// platform. |
diff --git a/embassy-usb/Cargo.toml b/embassy-usb/Cargo.toml index 8cad4d314..660ecc8cc 100644 --- a/embassy-usb/Cargo.toml +++ b/embassy-usb/Cargo.toml | |||
| @@ -9,8 +9,12 @@ src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-usb/s | |||
| 9 | features = ["defmt"] | 9 | features = ["defmt"] |
| 10 | target = "thumbv7em-none-eabi" | 10 | target = "thumbv7em-none-eabi" |
| 11 | 11 | ||
| 12 | [features] | ||
| 13 | defmt = ["dep:defmt", "embassy-usb-driver/defmt"] | ||
| 14 | |||
| 12 | [dependencies] | 15 | [dependencies] |
| 13 | embassy-futures = { version = "0.1.0", path = "../embassy-futures" } | 16 | embassy-futures = { version = "0.1.0", path = "../embassy-futures" } |
| 17 | embassy-usb-driver = { version = "0.1.0", path = "../embassy-usb-driver" } | ||
| 14 | 18 | ||
| 15 | defmt = { version = "0.3", optional = true } | 19 | defmt = { version = "0.3", optional = true } |
| 16 | log = { version = "0.4.14", optional = true } | 20 | log = { version = "0.4.14", optional = true } |
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs index 6be88bc76..87a8333bb 100644 --- a/embassy-usb/src/builder.rs +++ b/embassy-usb/src/builder.rs | |||
| @@ -1,11 +1,10 @@ | |||
| 1 | use heapless::Vec; | 1 | use heapless::Vec; |
| 2 | 2 | ||
| 3 | use super::control::ControlHandler; | 3 | use crate::control::ControlHandler; |
| 4 | use super::descriptor::{BosWriter, DescriptorWriter}; | 4 | use crate::descriptor::{BosWriter, DescriptorWriter}; |
| 5 | use super::driver::{Driver, Endpoint}; | 5 | use crate::driver::{Driver, Endpoint, EndpointType}; |
| 6 | use super::types::*; | 6 | use crate::types::*; |
| 7 | use super::{DeviceStateHandler, UsbDevice, MAX_INTERFACE_COUNT}; | 7 | use crate::{DeviceStateHandler, Interface, UsbDevice, MAX_INTERFACE_COUNT, STRING_INDEX_CUSTOM_START}; |
| 8 | use crate::{Interface, STRING_INDEX_CUSTOM_START}; | ||
| 9 | 8 | ||
| 10 | #[derive(Debug, Copy, Clone)] | 9 | #[derive(Debug, Copy, Clone)] |
| 11 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 10 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
diff --git a/embassy-usb/src/control.rs b/embassy-usb/src/control.rs index 3e5749a01..9e0dee888 100644 --- a/embassy-usb/src/control.rs +++ b/embassy-usb/src/control.rs | |||
| @@ -1,7 +1,8 @@ | |||
| 1 | //! USB control data types. | 1 | //! USB control data types. |
| 2 | use core::mem; | 2 | use core::mem; |
| 3 | 3 | ||
| 4 | use super::types::*; | 4 | use crate::driver::Direction; |
| 5 | use crate::types::StringIndex; | ||
| 5 | 6 | ||
| 6 | /// Control request type. | 7 | /// Control request type. |
| 7 | #[repr(u8)] | 8 | #[repr(u8)] |
| @@ -42,7 +43,7 @@ pub enum Recipient { | |||
| 42 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 43 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 43 | pub struct Request { | 44 | pub struct Request { |
| 44 | /// Direction of the request. | 45 | /// Direction of the request. |
| 45 | pub direction: UsbDirection, | 46 | pub direction: Direction, |
| 46 | /// Type of the request. | 47 | /// Type of the request. |
| 47 | pub request_type: RequestType, | 48 | pub request_type: RequestType, |
| 48 | /// Recipient of the request. | 49 | /// Recipient of the request. |
diff --git a/embassy-usb/src/descriptor.rs b/embassy-usb/src/descriptor.rs index b94a4b161..497f03196 100644 --- a/embassy-usb/src/descriptor.rs +++ b/embassy-usb/src/descriptor.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use super::builder::Config; | 1 | use crate::builder::Config; |
| 2 | use super::types::*; | 2 | use crate::driver::EndpointInfo; |
| 3 | use super::CONFIGURATION_VALUE; | 3 | use crate::types::*; |
| 4 | use crate::CONFIGURATION_VALUE; | ||
| 4 | 5 | ||
| 5 | /// Standard descriptor types | 6 | /// Standard descriptor types |
| 6 | #[allow(missing_docs)] | 7 | #[allow(missing_docs)] |
diff --git a/embassy-usb/src/descriptor_reader.rs b/embassy-usb/src/descriptor_reader.rs index 0a12b566c..d64bcb73b 100644 --- a/embassy-usb/src/descriptor_reader.rs +++ b/embassy-usb/src/descriptor_reader.rs | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | use crate::descriptor::descriptor_type; | 1 | use crate::descriptor::descriptor_type; |
| 2 | use crate::types::EndpointAddress; | 2 | use crate::driver::EndpointAddress; |
| 3 | 3 | ||
| 4 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] | 4 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 5 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 5 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs index 6f58c953c..e1a99cfae 100644 --- a/embassy-usb/src/lib.rs +++ b/embassy-usb/src/lib.rs | |||
| @@ -4,23 +4,23 @@ | |||
| 4 | // This mod MUST go first, so that the others see its macros. | 4 | // This mod MUST go first, so that the others see its macros. |
| 5 | pub(crate) mod fmt; | 5 | pub(crate) mod fmt; |
| 6 | 6 | ||
| 7 | pub use embassy_usb_driver as driver; | ||
| 8 | |||
| 7 | mod builder; | 9 | mod builder; |
| 8 | pub mod control; | 10 | pub mod control; |
| 9 | pub mod descriptor; | 11 | pub mod descriptor; |
| 10 | mod descriptor_reader; | 12 | mod descriptor_reader; |
| 11 | pub mod driver; | ||
| 12 | pub mod types; | 13 | pub mod types; |
| 13 | 14 | ||
| 14 | use embassy_futures::select::{select, Either}; | 15 | use embassy_futures::select::{select, Either}; |
| 15 | use heapless::Vec; | 16 | use heapless::Vec; |
| 16 | 17 | ||
| 17 | pub use self::builder::{Builder, Config}; | 18 | pub use crate::builder::{Builder, Config}; |
| 18 | use self::control::*; | 19 | use crate::control::*; |
| 19 | use self::descriptor::*; | 20 | use crate::descriptor::*; |
| 20 | use self::driver::{Bus, Driver, Event}; | ||
| 21 | use self::types::*; | ||
| 22 | use crate::descriptor_reader::foreach_endpoint; | 21 | use crate::descriptor_reader::foreach_endpoint; |
| 23 | use crate::driver::ControlPipe; | 22 | use crate::driver::{Bus, ControlPipe, Direction, Driver, EndpointAddress, Event}; |
| 23 | use crate::types::*; | ||
| 24 | 24 | ||
| 25 | /// The global state of the USB device. | 25 | /// The global state of the USB device. |
| 26 | /// | 26 | /// |
| @@ -250,8 +250,8 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 250 | trace!("control request: {:?}", req); | 250 | trace!("control request: {:?}", req); |
| 251 | 251 | ||
| 252 | match req.direction { | 252 | match req.direction { |
| 253 | UsbDirection::In => self.handle_control_in(req).await, | 253 | Direction::In => self.handle_control_in(req).await, |
| 254 | UsbDirection::Out => self.handle_control_out(req).await, | 254 | Direction::Out => self.handle_control_out(req).await, |
| 255 | } | 255 | } |
| 256 | 256 | ||
| 257 | if self.inner.set_address_pending { | 257 | if self.inner.set_address_pending { |
diff --git a/embassy-usb/src/types.rs b/embassy-usb/src/types.rs index b8717ffa9..aeab063d1 100644 --- a/embassy-usb/src/types.rs +++ b/embassy-usb/src/types.rs | |||
| @@ -1,108 +1,3 @@ | |||
| 1 | /// Direction of USB traffic. Note that in the USB standard the direction is always indicated from | ||
| 2 | /// the perspective of the host, which is backward for devices, but the standard directions are used | ||
| 3 | /// for consistency. | ||
| 4 | /// | ||
| 5 | /// The values of the enum also match the direction bit used in endpoint addresses and control | ||
| 6 | /// request types. | ||
| 7 | #[repr(u8)] | ||
| 8 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 9 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 10 | pub enum UsbDirection { | ||
| 11 | /// Host to device (OUT) | ||
| 12 | Out = 0x00, | ||
| 13 | /// Device to host (IN) | ||
| 14 | In = 0x80, | ||
| 15 | } | ||
| 16 | |||
| 17 | impl From<u8> for UsbDirection { | ||
| 18 | fn from(value: u8) -> Self { | ||
| 19 | unsafe { core::mem::transmute(value & 0x80) } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | /// USB endpoint transfer type. The values of this enum can be directly cast into `u8` to get the | ||
| 24 | /// transfer bmAttributes transfer type bits. | ||
| 25 | #[repr(u8)] | ||
| 26 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 27 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 28 | pub enum EndpointType { | ||
| 29 | /// Control endpoint. Used for device management. Only the host can initiate requests. Usually | ||
| 30 | /// used only endpoint 0. | ||
| 31 | Control = 0b00, | ||
| 32 | /// Isochronous endpoint. Used for time-critical unreliable data. Not implemented yet. | ||
| 33 | Isochronous = 0b01, | ||
| 34 | /// Bulk endpoint. Used for large amounts of best-effort reliable data. | ||
| 35 | Bulk = 0b10, | ||
| 36 | /// Interrupt endpoint. Used for small amounts of time-critical reliable data. | ||
| 37 | Interrupt = 0b11, | ||
| 38 | } | ||
| 39 | |||
| 40 | /// Type-safe endpoint address. | ||
| 41 | #[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
| 42 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 43 | pub struct EndpointAddress(u8); | ||
| 44 | |||
| 45 | impl From<u8> for EndpointAddress { | ||
| 46 | #[inline] | ||
| 47 | fn from(addr: u8) -> EndpointAddress { | ||
| 48 | EndpointAddress(addr) | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | impl From<EndpointAddress> for u8 { | ||
| 53 | #[inline] | ||
| 54 | fn from(addr: EndpointAddress) -> u8 { | ||
| 55 | addr.0 | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | impl EndpointAddress { | ||
| 60 | const INBITS: u8 = UsbDirection::In as u8; | ||
| 61 | |||
| 62 | /// Constructs a new EndpointAddress with the given index and direction. | ||
| 63 | #[inline] | ||
| 64 | pub fn from_parts(index: usize, dir: UsbDirection) -> Self { | ||
| 65 | EndpointAddress(index as u8 | dir as u8) | ||
| 66 | } | ||
| 67 | |||
| 68 | /// Gets the direction part of the address. | ||
| 69 | #[inline] | ||
| 70 | pub fn direction(&self) -> UsbDirection { | ||
| 71 | if (self.0 & Self::INBITS) != 0 { | ||
| 72 | UsbDirection::In | ||
| 73 | } else { | ||
| 74 | UsbDirection::Out | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | /// Returns true if the direction is IN, otherwise false. | ||
| 79 | #[inline] | ||
| 80 | pub fn is_in(&self) -> bool { | ||
| 81 | (self.0 & Self::INBITS) != 0 | ||
| 82 | } | ||
| 83 | |||
| 84 | /// Returns true if the direction is OUT, otherwise false. | ||
| 85 | #[inline] | ||
| 86 | pub fn is_out(&self) -> bool { | ||
| 87 | (self.0 & Self::INBITS) == 0 | ||
| 88 | } | ||
| 89 | |||
| 90 | /// Gets the index part of the endpoint address. | ||
| 91 | #[inline] | ||
| 92 | pub fn index(&self) -> usize { | ||
| 93 | (self.0 & !Self::INBITS) as usize | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 98 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 99 | pub struct EndpointInfo { | ||
| 100 | pub addr: EndpointAddress, | ||
| 101 | pub ep_type: EndpointType, | ||
| 102 | pub max_packet_size: u16, | ||
| 103 | pub interval: u8, | ||
| 104 | } | ||
| 105 | |||
| 106 | /// A handle for a USB interface that contains its number. | 1 | /// A handle for a USB interface that contains its number. |
| 107 | #[derive(Copy, Clone, Eq, PartialEq)] | 2 | #[derive(Copy, Clone, Eq, PartialEq)] |
| 108 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 3 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
