tca9534_driver_rs/
registers.rs

1/// TCA9534 register definitions
2///
3/// Based on TCA9534 datasheet: <https://www.ti.com/lit/ds/symlink/tca9534.pdf>
4
5/// Register enumeration
6#[derive(Debug, Copy, Clone)]
7pub enum Register {
8    /// Input port register (0x00) - Read only
9    /// Reflects the incoming logic levels of the pins, regardless of whether the pin is defined as an input or an output
10    InputPort = 0x00,
11    
12    /// Output port register (0x01) - Read/Write
13    /// The Output Port register shows the outgoing logic levels of the pins defined as outputs
14    OutputPort = 0x01,
15    
16    /// Polarity Inversion register (0x02) - Read/Write  
17    /// This register allows the user to invert the polarity of the Input Port register data
18    Polarity = 0x02,
19    
20    /// Configuration register (0x03) - Read/Write
21    /// The Configuration register configures the directions of the I/O pins
22    /// 1 = pin is configured as an input (default)
23    /// 0 = pin is configured as an output
24    Config = 0x03,
25}
26
27impl Register {
28    /// Get register address
29    pub fn addr(self) -> u8 {
30        self as u8
31    }
32}
33
34#[cfg(feature = "defmt")]
35impl defmt::Format for Register {
36    fn format(&self, fmt: defmt::Formatter) {
37        match *self {
38            Register::InputPort => defmt::write!(fmt, "InputPort"),
39            Register::OutputPort => defmt::write!(fmt, "OutputPort"),
40            Register::Polarity => defmt::write!(fmt, "Polarity"),
41            Register::Config => defmt::write!(fmt, "Config"),
42        }
43    }
44}
45
46/// Pin configuration (direction)
47#[derive(Debug, Copy, Clone, PartialEq)]
48pub enum PinConfig {
49    /// Pin configured as input (high impedance) - default
50    Input = 1,
51    /// Pin configured as output (can drive high or low)
52    Output = 0,
53}
54
55impl PinConfig {
56    /// Get pin config bit value
57    pub fn bits(self) -> u8 {
58        self as u8
59    }
60}
61
62#[cfg(feature = "defmt")]
63impl defmt::Format for PinConfig {
64    fn format(&self, fmt: defmt::Formatter) {
65        match *self {
66            PinConfig::Input => defmt::write!(fmt, "Input"),
67            PinConfig::Output => defmt::write!(fmt, "Output"),
68        }
69    }
70}
71
72/// Pin polarity setting
73#[derive(Debug, Copy, Clone, PartialEq)]
74pub enum PinPolarity {
75    /// Normal polarity (default) - GPIO register bit reflects same value on the input pin
76    Normal = 0,
77    /// Inverted polarity - GPIO register bit reflects inverted value on the input pin
78    Inverted = 1,
79}
80
81impl PinPolarity {
82    /// Get polarity bit value
83    pub fn bits(self) -> u8 {
84        self as u8
85    }
86}
87
88#[cfg(feature = "defmt")]
89impl defmt::Format for PinPolarity {
90    fn format(&self, fmt: defmt::Formatter) {
91        match *self {
92            PinPolarity::Normal => defmt::write!(fmt, "Normal"),
93            PinPolarity::Inverted => defmt::write!(fmt, "Inverted"),
94        }
95    }
96}
97
98/// Pin logic level
99#[derive(Debug, Copy, Clone, PartialEq)]
100pub enum PinLevel {
101    /// Logic low (0V)
102    Low = 0,
103    /// Logic high (VCC)
104    High = 1,
105}
106
107impl PinLevel {
108    /// Get level bit value
109    pub fn bits(self) -> u8 {
110        self as u8
111    }
112}
113
114#[cfg(feature = "defmt")]
115impl defmt::Format for PinLevel {
116    fn format(&self, fmt: defmt::Formatter) {
117        match *self {
118            PinLevel::Low => defmt::write!(fmt, "Low"),
119            PinLevel::High => defmt::write!(fmt, "High"),
120        }
121    }
122}
123
124/// Pin number type (0-7)
125pub type Pin = u8;
126
127/// Port value type (8-bit value representing all pins)
128pub type PortValue = u8;
129
130/// Configuration constants
131pub mod config {
132    /// All pins configured as inputs
133    pub const ALL_INPUTS: u8 = 0xFF;
134    
135    /// All pins configured as outputs
136    pub const ALL_OUTPUTS: u8 = 0x00;
137    
138    /// All pins normal polarity
139    pub const ALL_NORMAL_POLARITY: u8 = 0x00;
140    
141    /// All pins inverted polarity
142    pub const ALL_INVERTED_POLARITY: u8 = 0xFF;
143    
144    /// All outputs low
145    pub const ALL_OUTPUTS_LOW: u8 = 0x00;
146    
147    /// All outputs high
148    pub const ALL_OUTPUTS_HIGH: u8 = 0xFF;
149}
150
151/// Common I2C addresses for TCA9534 based on A2, A1, A0 pins
152pub mod addresses {
153    /// A2=0, A1=0, A0=0 (default)
154    pub const ADDR_000: u8 = 0x20;
155    /// A2=0, A1=0, A0=1
156    pub const ADDR_001: u8 = 0x21;
157    /// A2=0, A1=1, A0=0
158    pub const ADDR_010: u8 = 0x22;
159    /// A2=0, A1=1, A0=1
160    pub const ADDR_011: u8 = 0x23;
161    /// A2=1, A1=0, A0=0
162    pub const ADDR_100: u8 = 0x24;
163    /// A2=1, A1=0, A0=1
164    pub const ADDR_101: u8 = 0x25;
165    /// A2=1, A1=1, A0=0
166    pub const ADDR_110: u8 = 0x26;
167    /// A2=1, A1=1, A0=1
168    pub const ADDR_111: u8 = 0x27;
169}