Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod pdinfo;
pub mod pdo;
pub mod type_c;
pub mod ucsi;
pub mod usb;
pub mod vdm;

use core::hash::Hash;
Expand Down
3 changes: 2 additions & 1 deletion src/ucsi/lpm/get_alternate_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use bitfield::bitfield;
use super::Recipient;
use crate::ucsi::lpm::InvalidRecipient;
use crate::ucsi::{CommandHeaderRaw, COMMAND_LEN};
use crate::vdm::{AltModeId, Svid};
use crate::vdm::structured::Svid;
use crate::vdm::AltModeId;

/// Data length for the GET_ALTERNATE_MODES command response
pub const RESPONSE_DATA_LEN: usize = 12;
Expand Down
37 changes: 37 additions & 0 deletions src/usb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Universal Serial Bus (USB) related types and traits from the USB 2.0 and 3.2
//! specifications.

/// A Binary-Coded Decimal (BCD) format as defined by the USB 2.0 specification.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Bcd(pub u16);

impl Bcd {
/// Parse the BCD value into its major, minor, and subminor components in the
/// format `jj.m.n` where
/// - `jj` is the major version (2 nibbles)
/// - `m` is the minor version (1 nibble)
/// - `n` is the subminor version (1 nibble)
pub const fn jjmn(&self) -> (u8, u8, u8) {
Comment thread
asasine marked this conversation as resolved.
let jj = (self.0 >> 8) as u8;
let m = ((self.0 >> 4) & 0xF) as u8;
let n = (self.0 & 0xF) as u8;
(jj, m, n)
}
}

/// The USB Product ID as assigned by the USB-IF.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ProductId(pub u16);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn bcd_jjmn() {
assert_eq!(Bcd(0x1234).jjmn(), (0x12, 0x3, 0x4));
assert_eq!(Bcd(0xFEDC).jjmn(), (0xFE, 0xD, 0xC));
}
}
11 changes: 6 additions & 5 deletions src/vdm.rs → src/vdm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//! Vendor Defined Messages (VDM) allow vendors to exchange information outside of that defined by the PD spec.
//!
//! See PD spect 6.4.4 Vendor Defined Message.

pub mod structured;

pub const DATA_OBJ_SIZE: usize = 4;
pub const MAX_VDOS: usize = 6;
pub const MAX_NUM_DATA_OBJECTS: usize = 7;
Expand Down Expand Up @@ -25,11 +31,6 @@ pub enum Cmd {
SvidCmdStart = 16,
}

/// Standard or Vendor ID (SVID) newtype, see PD spec 6.4.4.2.1
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Svid(pub u16);

/// Altmode ID newtype for discover modes command and others, see PD spec 6.4.4.3.3
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
Expand Down
Loading
Loading