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

Create PDO/RDO definitions #2

Merged
merged 7 commits into from
Feb 19, 2025
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
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"rust-lang.rust-analyzer",
"tamasfe.even-better-toml",
],
}
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
defmt = { version = "0.3", optional = true }
embedded-hal-async = "1.0.0"
bitfield = "0.18.1"

[features]
default = []
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]

pub mod asynchronous;
pub mod pdo;

/// Port ID new type
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
147 changes: 147 additions & 0 deletions src/pdo/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//! Power data object (PDO) definitions
//! This module defines source and sink PDOs. Each PDO type has a corresponding *Raw and *Data struct.
//! The raw struct just provides a structured version of the raw PDO data, while the data struct provides
//! a type-safe version.
use crate::PdError;

mod rdo;
pub mod sink;
pub mod source;

pub use rdo::Rdo;

/// 10 mA unit
pub const MA10_UNIT: u16 = 10;
/// 50 mA unit
pub const MA50_UNIT: u16 = 50;
/// 20 mV unit
pub const MV20_UNIT: u16 = 20;
/// 50 mV unit
pub const MV50_UNIT: u16 = 50;
/// 100 mV unit
pub const MV100_UNIT: u16 = 100;
/// 250 mV unit
pub const MW250_UNIT: u32 = 250;
/// 1000 mW unit
pub const MW1000_UNIT: u32 = 1000;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum PdoKind {
Fixed,
Battery,
Variable,
Augmented,
}

impl From<u32> for PdoKind {
fn from(pdo: u32) -> Self {
const PDO_KIND_SHIFT: u8 = 30;
PdoKind::from((pdo >> PDO_KIND_SHIFT) as u8)
}
}

impl From<u8> for PdoKind {
fn from(value: u8) -> Self {
const PDO_KIND_MASK: u8 = 0x3;
match value & PDO_KIND_MASK {
0x0 => PdoKind::Fixed,
0x1 => PdoKind::Battery,
0x2 => PdoKind::Variable,
0x3 => PdoKind::Augmented,
_ => unreachable!(),
}
}
}

impl From<PdoKind> for u8 {
fn from(value: PdoKind) -> Self {
match value {
PdoKind::Fixed => 0x0,
PdoKind::Battery => 0x1,
PdoKind::Variable => 0x2,
PdoKind::Augmented => 0x3,
}
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ApdoKind {
/// SPR Programable power supply
SprPps,
/// EPR Adjustable voltage supply
EprAvs,
/// SPR Adjustable voltage supply
SprAvs,
}

impl From<ApdoKind> for u8 {
fn from(value: ApdoKind) -> u8 {
match value {
ApdoKind::SprPps => 0x0,
ApdoKind::EprAvs => 0x1,
ApdoKind::SprAvs => 0x2,
}
}
}

impl TryFrom<u8> for ApdoKind {
type Error = PdError;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x0 => Ok(ApdoKind::SprPps),
0x1 => Ok(ApdoKind::EprAvs),
0x2 => Ok(ApdoKind::SprAvs),
_ => Err(PdError::InvalidParams),
}
}
}

impl TryFrom<u32> for ApdoKind {
type Error = PdError;

fn try_from(value: u32) -> Result<Self, Self::Error> {
const APDO_KIND_SHIFT: u8 = 28;
const APDO_KIND_MASK: u32 = 0x3;
match (value >> APDO_KIND_SHIFT) & APDO_KIND_MASK {
0x0 => Ok(ApdoKind::SprPps),
0x1 => Ok(ApdoKind::EprAvs),
0x2 => Ok(ApdoKind::SprAvs),
_ => Err(PdError::InvalidParams),
}
}
}

/// Common PDO trait
pub trait Common {
/// Get the PDO kind
fn kind(&self) -> PdoKind;
/// Get the APDO kind
fn apdo_kind(&self) -> Option<ApdoKind>;
}

/// Top-level PDO type
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Pdo {
Source(source::Pdo),
Sink(sink::Pdo),
}

impl Common for Pdo {
fn kind(&self) -> PdoKind {
match self {
Pdo::Source(pdo) => pdo.kind(),
Pdo::Sink(pdo) => pdo.kind(),
}
}

fn apdo_kind(&self) -> Option<ApdoKind> {
match self {
Pdo::Source(pdo) => pdo.apdo_kind(),
Pdo::Sink(pdo) => pdo.apdo_kind(),
}
}
}
Loading