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    /// Wait for availability of VBus voltage.
37    fn wait_for_vbus(&self) -> impl Future<Output = ()>;
38
39    /// Receive a packet.
40    fn receive(&mut self, buffer: &mut [u8]) -> impl Future<Output = Result<usize, DriverRxError>>;
41
42    /// Transmit a packet.
43    fn transmit(&mut self, data: &[u8]) -> impl Future<Output = Result<(), DriverTxError>>;
44
45    /// Transmit a hard reset signal.
46    fn transmit_hard_reset(&mut self) -> impl Future<Output = Result<(), DriverTxError>>;
47}