ESPHome 2026.5.0-dev
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 if (this->interrupt_pin_ != nullptr) {
66 this->disable_loop();
67 }
68}
69
71 if (this->has_pullup_) {
72 ESP_LOGCONFIG(TAG, "PCAL6416A:");
73 } else {
74 ESP_LOGCONFIG(TAG, "PCA6416A:");
75 }
76 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
77 LOG_I2C_DEVICE(this)
78 if (this->is_failed()) {
79 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
80 }
81}
82
84 uint8_t reg_addr = pin < 8 ? PCA6416A_INPUT0 : PCA6416A_INPUT1;
85 uint8_t value = 0;
86 if (!this->read_register_(reg_addr, &value)) {
87 return false;
88 }
89
90 // Update the appropriate part of input_mask_
91 if (pin < 8) {
92 this->input_mask_ = (this->input_mask_ & 0xFF00) | value;
93 } else {
94 this->input_mask_ = (this->input_mask_ & 0x00FF) | (uint16_t(value) << 8);
95 }
96 return true;
97}
98
99bool PCA6416AComponent::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
100
101void PCA6416AComponent::digital_write_hw(uint8_t pin, bool value) {
102 uint8_t reg_addr = pin < 8 ? PCA6416A_OUTPUT0 : PCA6416A_OUTPUT1;
103 this->update_register_(pin, value, reg_addr);
104}
105
107 uint8_t io_dir = pin < 8 ? PCA6416A_CONFIG0 : PCA6416A_CONFIG1;
108 uint8_t pull_en = pin < 8 ? PCAL6416A_PULL_EN0 : PCAL6416A_PULL_EN1;
109 uint8_t pull_dir = pin < 8 ? PCAL6416A_PULL_DIR0 : PCAL6416A_PULL_DIR1;
110 if (flags == gpio::FLAG_INPUT) {
111 this->update_register_(pin, true, io_dir);
112 if (has_pullup_) {
113 this->update_register_(pin, true, pull_dir);
114 this->update_register_(pin, false, pull_en);
115 }
116 if (this->interrupt_pin_ == nullptr) {
117 this->enable_loop();
118 }
119 } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
120 this->update_register_(pin, true, io_dir);
121 if (has_pullup_) {
122 this->update_register_(pin, true, pull_dir);
123 this->update_register_(pin, true, pull_en);
124 } else {
125 ESP_LOGW(TAG, "Your PCA6416A does not support pull-up resistors");
126 }
127 if (this->interrupt_pin_ == nullptr) {
128 this->enable_loop();
129 }
130 } else if (flags == gpio::FLAG_OUTPUT) {
131 this->update_register_(pin, false, io_dir);
132 }
133}
134
135bool PCA6416AComponent::read_register_(uint8_t reg, uint8_t *value) {
136 if (this->is_failed()) {
137 ESP_LOGD(TAG, "Device marked failed");
138 return false;
139 }
140
141 this->last_error_ = this->read_register(reg, value, 1);
142 if (this->last_error_ != i2c::ERROR_OK) {
143 this->status_set_warning();
144 ESP_LOGE(TAG, "read_register_(): I2C I/O error: %d", (int) this->last_error_);
145 return false;
146 }
147
148 this->status_clear_warning();
149 return true;
150}
151
152bool PCA6416AComponent::write_register_(uint8_t reg, uint8_t value) {
153 if (this->is_failed()) {
154 ESP_LOGD(TAG, "Device marked failed");
155 return false;
156 }
157
158 this->last_error_ = this->write_register(reg, &value, 1);
159 if (this->last_error_ != i2c::ERROR_OK) {
160 this->status_set_warning();
161 ESP_LOGE(TAG, "write_register_(): I2C I/O error: %d", (int) this->last_error_);
162 return false;
163 }
164
165 this->status_clear_warning();
166 return true;
167}
168
169void PCA6416AComponent::update_register_(uint8_t pin, bool pin_value, uint8_t reg_addr) {
170 uint8_t bit = pin % 8;
171 uint8_t reg_value = 0;
172 if (reg_addr == PCA6416A_OUTPUT0) {
173 reg_value = this->output_0_;
174 } else if (reg_addr == PCA6416A_OUTPUT1) {
175 reg_value = this->output_1_;
176 } else {
177 this->read_register_(reg_addr, &reg_value);
178 }
179
180 if (pin_value) {
181 reg_value |= 1 << bit;
182 } else {
183 reg_value &= ~(1 << bit);
184 }
185
186 this->write_register_(reg_addr, reg_value);
187
188 if (reg_addr == PCA6416A_OUTPUT0) {
189 this->output_0_ = reg_value;
190 } else if (reg_addr == PCA6416A_OUTPUT1) {
191 this->output_1_ = reg_value;
192 }
193}
194
196
199bool PCA6416AGPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
200void PCA6416AGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
201size_t PCA6416AGPIOPin::dump_summary(char *buffer, size_t len) const {
202 return buf_append_printf(buffer, len, 0, "%u via PCA6416A", this->pin_);
203}
204
205} // namespace pca6416a
206} // 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
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:101
bool write_register_(uint8_t reg, uint8_t value)
Definition pca6416a.cpp:152
static void IRAM_ATTR gpio_intr(PCA6416AComponent *arg)
Definition pca6416a.cpp:61
bool read_register_(uint8_t reg, uint8_t *value)
Definition pca6416a.cpp:135
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:169
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:106
void setup() override
Check i2c availability and setup masks.
Definition pca6416a.cpp:26
float get_setup_priority() const override
Definition pca6416a.cpp:195
bool digital_read_hw(uint8_t pin) override
Definition pca6416a.cpp:83
bool digital_read_cache(uint8_t pin) override
Definition pca6416a.cpp:99
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:201
void digital_write(bool value) override
Definition pca6416a.cpp:200
PCA6416AComponent * parent_
Definition pca6416a.h:69
void pin_mode(gpio::Flags flags) override
Definition pca6416a.cpp:198
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