ESPHome 2026.4.3
Loading...
Searching...
No Matches
pca6416a.cpp
Go to the documentation of this file.
1#include "pca6416a.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace pca6416a {
6
23
24static const char *const TAG = "pca6416a";
25
27 // Test to see if device exists
28 uint8_t value;
29 if (!this->read_register_(PCA6416A_INPUT0, &value)) {
30 ESP_LOGE(TAG, "PCA6416A not available under 0x%02X", this->address_);
31 this->mark_failed();
32 return;
33 }
34
35 // Test to see if the device supports pull-up resistors
36 if (this->read_register(PCAL6416A_PULL_EN0, &value, 1) == i2c::ERROR_OK) {
37 this->has_pullup_ = true;
38 }
39
40 // No polarity inversion
43 // Set all pins to input
46 // Read current output register state
49
50 ESP_LOGD(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
51 this->status_has_error());
52
53 if (this->interrupt_pin_ != nullptr) {
54 this->interrupt_pin_->setup();
56 this->set_invalidate_on_read_(false);
57 }
58 this->disable_loop();
59}
60
63 // Invalidate cache at the start of each loop
64 this->reset_pin_cache_();
65 // Only disable the loop once INT has actually gone HIGH. Input transitions that straddle the
66 // I2C read leave INT asserted without re-firing a falling edge, which would strand us with
67 // stale state forever; keep looping until the line is released so we self-heal.
68 if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read()) {
69 this->disable_loop();
70 }
71}
72
74 if (this->has_pullup_) {
75 ESP_LOGCONFIG(TAG, "PCAL6416A:");
76 } else {
77 ESP_LOGCONFIG(TAG, "PCA6416A:");
78 }
79 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
80 LOG_I2C_DEVICE(this)
81 if (this->is_failed()) {
82 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
83 }
84}
85
87 uint8_t reg_addr = pin < 8 ? PCA6416A_INPUT0 : PCA6416A_INPUT1;
88 uint8_t value = 0;
89 if (!this->read_register_(reg_addr, &value)) {
90 return false;
91 }
92
93 // Update the appropriate part of input_mask_
94 if (pin < 8) {
95 this->input_mask_ = (this->input_mask_ & 0xFF00) | value;
96 } else {
97 this->input_mask_ = (this->input_mask_ & 0x00FF) | (uint16_t(value) << 8);
98 }
99 return true;
100}
101
102bool PCA6416AComponent::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
103
104void PCA6416AComponent::digital_write_hw(uint8_t pin, bool value) {
105 uint8_t reg_addr = pin < 8 ? PCA6416A_OUTPUT0 : PCA6416A_OUTPUT1;
106 this->update_register_(pin, value, reg_addr);
107}
108
110 uint8_t io_dir = pin < 8 ? PCA6416A_CONFIG0 : PCA6416A_CONFIG1;
111 uint8_t pull_en = pin < 8 ? PCAL6416A_PULL_EN0 : PCAL6416A_PULL_EN1;
112 uint8_t pull_dir = pin < 8 ? PCAL6416A_PULL_DIR0 : PCAL6416A_PULL_DIR1;
113 if (flags == gpio::FLAG_INPUT) {
114 this->update_register_(pin, true, io_dir);
115 if (has_pullup_) {
116 this->update_register_(pin, true, pull_dir);
117 this->update_register_(pin, false, pull_en);
118 }
119 if (this->interrupt_pin_ == nullptr) {
120 this->enable_loop();
121 }
122 } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
123 this->update_register_(pin, true, io_dir);
124 if (has_pullup_) {
125 this->update_register_(pin, true, pull_dir);
126 this->update_register_(pin, true, pull_en);
127 } else {
128 ESP_LOGW(TAG, "Your PCA6416A does not support pull-up resistors");
129 }
130 if (this->interrupt_pin_ == nullptr) {
131 this->enable_loop();
132 }
133 } else if (flags == gpio::FLAG_OUTPUT) {
134 this->update_register_(pin, false, io_dir);
135 }
136}
137
138bool PCA6416AComponent::read_register_(uint8_t reg, uint8_t *value) {
139 if (this->is_failed()) {
140 ESP_LOGD(TAG, "Device marked failed");
141 return false;
142 }
143
144 this->last_error_ = this->read_register(reg, value, 1);
145 if (this->last_error_ != i2c::ERROR_OK) {
146 this->status_set_warning();
147 ESP_LOGE(TAG, "read_register_(): I2C I/O error: %d", (int) this->last_error_);
148 return false;
149 }
150
151 this->status_clear_warning();
152 return true;
153}
154
155bool PCA6416AComponent::write_register_(uint8_t reg, uint8_t value) {
156 if (this->is_failed()) {
157 ESP_LOGD(TAG, "Device marked failed");
158 return false;
159 }
160
161 this->last_error_ = this->write_register(reg, &value, 1);
162 if (this->last_error_ != i2c::ERROR_OK) {
163 this->status_set_warning();
164 ESP_LOGE(TAG, "write_register_(): I2C I/O error: %d", (int) this->last_error_);
165 return false;
166 }
167
168 this->status_clear_warning();
169 return true;
170}
171
172void PCA6416AComponent::update_register_(uint8_t pin, bool pin_value, uint8_t reg_addr) {
173 uint8_t bit = pin % 8;
174 uint8_t reg_value = 0;
175 if (reg_addr == PCA6416A_OUTPUT0) {
176 reg_value = this->output_0_;
177 } else if (reg_addr == PCA6416A_OUTPUT1) {
178 reg_value = this->output_1_;
179 } else {
180 this->read_register_(reg_addr, &reg_value);
181 }
182
183 if (pin_value) {
184 reg_value |= 1 << bit;
185 } else {
186 reg_value &= ~(1 << bit);
187 }
188
189 this->write_register_(reg_addr, reg_value);
190
191 if (reg_addr == PCA6416A_OUTPUT0) {
192 this->output_0_ = reg_value;
193 } else if (reg_addr == PCA6416A_OUTPUT1) {
194 this->output_1_ = reg_value;
195 }
196}
197
199
202bool PCA6416AGPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
203void PCA6416AGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
204size_t PCA6416AGPIOPin::dump_summary(char *buffer, size_t len) const {
205 return buf_append_printf(buffer, len, 0, "%u via PCA6416A", this->pin_);
206}
207
208} // namespace pca6416a
209} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void enable_loop()
Enable this component's loop.
Definition component.h:258
bool status_has_warning() const
Definition component.h:290
bool status_has_error() const
Definition component.h:292
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Definition component.h:306
virtual void setup()=0
virtual bool digital_read()=0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:107
bool digital_read(P pin)
Read the state of the given pin.
Definition cached_gpio.h:37
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:34
uint8_t address_
store the address of the device on the bus
Definition i2c.h:270
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:25
void digital_write_hw(uint8_t pin, bool value) override
Definition pca6416a.cpp:104
bool write_register_(uint8_t reg, uint8_t value)
Definition pca6416a.cpp:155
static void IRAM_ATTR gpio_intr(PCA6416AComponent *arg)
Definition pca6416a.cpp:61
bool read_register_(uint8_t reg, uint8_t *value)
Definition pca6416a.cpp:138
esphome::i2c::ErrorCode last_error_
Storage for last I2C error seen.
Definition pca6416a.h:46
void update_register_(uint8_t pin, bool pin_value, uint8_t reg_addr)
Definition pca6416a.cpp:172
uint16_t input_mask_
Cache for input values (16-bit combined for both banks)
Definition pca6416a.h:44
bool has_pullup_
Only the PCAL6416A has pull-up resistors.
Definition pca6416a.h:48
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition pca6416a.cpp:109
void setup() override
Check i2c availability and setup masks.
Definition pca6416a.cpp:26
float get_setup_priority() const override
Definition pca6416a.cpp:198
bool digital_read_hw(uint8_t pin) override
Definition pca6416a.cpp:86
bool digital_read_cache(uint8_t pin) override
Definition pca6416a.cpp:102
uint8_t output_0_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition pca6416a.h:41
size_t dump_summary(char *buffer, size_t len) const override
Definition pca6416a.cpp:204
void digital_write(bool value) override
Definition pca6416a.cpp:203
PCA6416AComponent * parent_
Definition pca6416a.h:69
void pin_mode(gpio::Flags flags) override
Definition pca6416a.cpp:201
uint16_t flags
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:51
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_PULLUP
Definition gpio.h:30
@ FLAG_INPUT
Definition gpio.h:27
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:38
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:1045