ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
tca9555.cpp
Go to the documentation of this file.
1#include "tca9555.h"
3#include "esphome/core/log.h"
4
5static const uint8_t TCA9555_INPUT_PORT_REGISTER_0 = 0x00;
6static const uint8_t TCA9555_INPUT_PORT_REGISTER_1 = 0x01;
7static const uint8_t TCA9555_OUTPUT_PORT_REGISTER_0 = 0x02;
8static const uint8_t TCA9555_OUTPUT_PORT_REGISTER_1 = 0x03;
9static const uint8_t TCA9555_POLARITY_REGISTER_0 = 0x04;
10static const uint8_t TCA9555_POLARITY_REGISTER_1 = 0x05;
11static const uint8_t TCA9555_CONFIGURATION_PORT_0 = 0x06;
12static const uint8_t TCA9555_CONFIGURATION_PORT_1 = 0x07;
13
15
16ESPHOME_LOG_TAG(TAG, "tca9555");
17
19 if (!this->read_gpio_modes_()) {
20 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
21 return;
22 }
23 if (!this->read_gpio_outputs_()) {
24 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
25 return;
26 }
27
29}
31 ESP_LOGCONFIG(TAG, "TCA9555:");
32 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
33 LOG_I2C_DEVICE(this)
34}
36 if (flags == gpio::FLAG_INPUT) {
37 // Set mode mask bit
38 this->mode_mask_ |= 1 << pin;
40 } else if (flags == gpio::FLAG_OUTPUT) {
41 // Clear mode mask bit
42 this->mode_mask_ &= ~(1 << pin);
43 }
44 // Write GPIO to enable input mode
45 this->write_gpio_modes_();
46}
47
49 if (this->is_failed())
50 return false;
51 uint16_t value;
52 if (!this->status_comm_result(this->read_byte_16_le(TCA9555_OUTPUT_PORT_REGISTER_0, &value),
53 LOG_STR("Failed to read output register")))
54 return false;
55 this->output_mask_ = value;
56 return true;
57}
58
60 if (this->is_failed())
61 return false;
62 uint16_t value;
63 if (!this->status_comm_result(this->read_byte_16_le(TCA9555_CONFIGURATION_PORT_0, &value),
64 LOG_STR("Failed to read mode register")))
65 return false;
66 this->mode_mask_ = value;
67 return true;
68}
70 if (this->is_failed())
71 return false;
72 uint8_t data;
73 uint8_t bank_number = pin < 8 ? 0 : 1;
74 uint8_t register_to_read = bank_number ? TCA9555_INPUT_PORT_REGISTER_1 : TCA9555_INPUT_PORT_REGISTER_0;
75 if (!this->read_bytes(register_to_read, &data, 1)) {
76 this->status_set_warning(LOG_STR("Failed to read input register"));
77 return false;
78 }
79 uint8_t second_half = this->input_mask_ >> 8;
80 uint8_t first_half = this->input_mask_;
81 if (bank_number) {
82 this->input_mask_ = (data << 8) | (uint16_t(first_half) << 0);
83 } else {
84 this->input_mask_ = (uint16_t(second_half) << 8) | (data << 0);
85 }
86
88 return true;
89}
90
91void TCA9555Component::digital_write_hw(uint8_t pin, bool value) {
92 if (this->is_failed())
93 return;
94
95 assign_bits(this->output_mask_, 1 << pin, value);
96
97 this->status_comm_result(this->write_byte_16_le(TCA9555_OUTPUT_PORT_REGISTER_0, this->output_mask_),
98 LOG_STR("Failed to write output register"));
99}
100
102 if (this->is_failed())
103 return false;
104 return this->status_comm_result(this->write_byte_16_le(TCA9555_CONFIGURATION_PORT_0, this->mode_mask_),
105 LOG_STR("Failed to write mode register"));
106}
107
108bool TCA9555Component::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
109
110} // namespace esphome::tca9555
111
112ESPHOME_EXPANDER_PIN_SUMMARY(esphome::tca9555::TCA9555Component, "%u via TCA9555")
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void status_clear_warning()
Definition component.h:296
void enable_polling_loop_()
Call from pin_mode() for an input pin: without an interrupt pin the loop polls instead.
bool write_byte_16_le(uint8_t a_register, uint16_t data) const
Write a 16-bit value least significant byte first.
Definition i2c.cpp:106
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
bool read_byte_16_le(uint8_t a_register, uint16_t *data)
Read a 16-bit value stored least significant byte first.
Definition i2c.h:296
bool digital_read_hw(uint8_t pin) override
Definition tca9555.cpp:69
bool digital_read_cache(uint8_t pin) override
Definition tca9555.cpp:108
uint16_t mode_mask_
Mask for the pin mode - 1 means output, 0 means input.
Definition tca9555.h:28
void setup() override
Check i2c availability and setup masks.
Definition tca9555.cpp:18
uint16_t input_mask_
The state read in digital_read_hw - 1 means HIGH, 0 means LOW.
Definition tca9555.h:32
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition tca9555.h:30
void digital_write_hw(uint8_t pin, bool value) override
Definition tca9555.cpp:91
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition tca9555.cpp:35
uint16_t flags
ESPHOME_LOG_TAG(TAG, "tca9555")
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1072