Skip to main content

usbpd_traits/
lib.rs

1//! USB PD library traits.
2//!
3//! Provides a driver trait that allows to add support for various USB PD PHYs.
4#![cfg_attr(not(test), no_std)]
5#![warn(missing_docs)]
6use core::future::Future;
7
8/// Receive Error.
9#[derive(Debug, Clone, Copy)]
10#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11pub enum DriverRxError {
12    /// Received message discarded, e.g. due to CRC errors.
13    Discarded,
14
15    /// Hard Reset received before or during reception.
16    HardReset,
17}
18
19/// Transmit Error.
20#[derive(Debug, Clone, Copy)]
21#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22pub enum DriverTxError {
23    /// Concurrent receive in progress or excessive noise on the line.
24    Discarded,
25
26    /// Hard Reset received before or during transmission.
27    HardReset,
28}
29
30/// Driver trait, through which the protocol layer talks to the PHY.
31pub trait Driver {
32    /// If this is `true`, the protocol layer will not send its own
33    /// GoodCRC messages and will instead rely on the hardware.
34    const HAS_AUTO_GOOD_CRC: bool = false;
35
36    /// If this is `true`, the hardware automatically retries transmission
37    /// when no GoodCRC is received. The protocol layer will skip its own
38    /// retry loop and call driver.transmit() directly. On success, it
39    /// maintains protocol state (TX message counter) without calling
40    /// wait_for_good_crc(), since the hardware already verified GoodCRC.
41    const HAS_AUTO_RETRY: bool = false;
42
43    /// Wait for availability of VBus voltage.
44    fn wait_for_vbus(&mut self) -> impl Future<Output = ()>;
45
46    /// Receive a packet.
47    fn receive(&mut self, buffer: &mut [u8]) -> impl Future<Output = Result<usize, DriverRxError>>;
48
49    /// Transmit a packet.
50    fn transmit(&mut self, data: &[u8]) -> impl Future<Output = Result<(), DriverTxError>>;
51
52    /// Transmit a hard reset signal.
53    fn transmit_hard_reset(&mut self) -> impl Future<Output = Result<(), DriverTxError>>;
54}