Skip to main content

zencan_common/
pdo.rs

1//! Definitions and data types related to PDOs
2
3use crate::CanId;
4
5/// Represents a PDO mapping
6///
7/// Each mapping specifies one sub-object to be included in the PDO data bytes.
8#[derive(Clone, Copy, Debug, PartialEq)]
9#[cfg_attr(
10    feature = "std",
11    derive(serde::Deserialize),
12    serde(deny_unknown_fields)
13)]
14pub struct PdoMapping {
15    /// The object index
16    pub index: u16,
17    /// The object sub index
18    pub sub: u8,
19    /// The size of the object to map, in **bits**
20    pub size: u8,
21}
22
23impl PdoMapping {
24    /// Convert a PdoMapping object to the u32 representation stored in the PdoMapping object
25    pub fn to_object_value(&self) -> u32 {
26        ((self.index as u32) << 16) | ((self.sub as u32) << 8) | (self.size as u32)
27    }
28
29    /// Create a PdoMapping object from the raw u32 representation stored in the PdoMapping object
30    pub fn from_object_value(value: u32) -> Self {
31        let index = (value >> 16) as u16;
32        let sub = ((value >> 8) & 0xff) as u8;
33        let size = (value & 0xff) as u8;
34        Self { index, sub, size }
35    }
36}
37
38/// Represents a PDO Communications Parameter Object
39#[derive(Clone, Copy, Debug, PartialEq)]
40pub struct PdoCommParameter {
41    /// Indicates the PDO is valid / enabled
42    ///
43    /// Note: The bit in the object itself is inverted -- it's 1 when the PDO is not valid
44    pub valid: bool,
45    /// True if RTR is allowed on this PDO
46    pub rtr_disabled: bool,
47    /// The COB-ID used to send or receive the PDO
48    pub cob_id: CanId,
49    /// The transmission type for the PDO
50    ///
51    /// It specifies when a PDO is sent or latched:
52    ///
53    /// - 0: Sent in response to sync, but only after an application specific event (e.g. it may be
54    ///   sent when the value changes, but not when it has not)
55    /// - 1 - 240: Sent in response to every Nth sync
56    /// - 254: Event driven (application to send it whenever it wants)
57    pub transmission_type: u8,
58}