ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
pca9505.cpp
Go to the documentation of this file.
1#include "pca9505.h"
3#include "esphome/core/log.h"
4
5namespace esphome::pca9505 {
6
7// Each register block holds one byte per port. Add the port number to reach a single
8// port, or set the auto-increment bit to transfer the whole block in one go.
9static constexpr uint8_t INPUT_REG = 0x00;
10static constexpr uint8_t OUTPUT_REG = 0x08;
11static constexpr uint8_t INVERT_REG = 0x10;
12static constexpr uint8_t CONFIG_REG = 0x18;
13static constexpr uint8_t MASK_REG = 0x20;
14static constexpr uint8_t AUTO_INCREMENT = 0x80;
15
16ESPHOME_LOG_TAG(TAG, "pca9505");
17
19 // Test to see if device exists
20 if (!this->read_bank_(0)) {
21 ESP_LOGE(TAG, "PCA9505 not detected at 0x%02X", this->address_);
22 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
23 return;
24 }
25
26 // No polarity inversion
27 this->write_all_banks_(INVERT_REG, 0x00);
28 // All outputs low, before anything is switched to being an output
29 this->write_all_banks_(OUTPUT_REG, 0x00);
30 // All inputs at initialization. The part sees a 1 as an input, so this is the
31 // inverse of config_, which is all zeroes here.
32 this->write_all_banks_(CONFIG_REG, 0xFF);
33
34 // Mask every interrupt. This must be written, not assumed: the chip keeps its
35 // registers through an MCU reboot, so a previous run may have left pins unmasked.
36 for (uint8_t &bank : this->mask_) {
37 bank = 0xFF;
38 }
39 this->write_all_banks_(MASK_REG, 0xFF);
40
42}
43
45 ESP_LOGCONFIG(TAG, "PCA9505:");
46 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
47 LOG_I2C_DEVICE(this)
48}
49
50bool PCA9505Component::digital_read_hw(uint8_t pin) { return this->read_bank_(pin / 8); }
51
52bool PCA9505Component::digital_read_cache(uint8_t pin) { return this->input_[pin / 8] & (1 << (pin % 8)); }
53
55 const uint8_t bank = pin / 8;
56 const uint8_t mask = 1 << (pin % 8);
57 assign_bits(this->output_[bank], mask, value);
58 this->write_bank_(OUTPUT_REG, bank, this->output_[bank]);
59}
60
62 const uint8_t bank = pin / 8;
63 const uint8_t mask = 1 << (pin % 8);
64 if (flags & gpio::FLAG_INPUT) {
65 this->config_[bank] &= ~mask;
66 // An input may raise INT.
67 this->mask_[bank] &= ~mask;
69 } else if (flags & gpio::FLAG_OUTPUT) {
70 this->config_[bank] |= mask;
71 // An output must never raise INT.
72 this->mask_[bank] |= mask;
73 }
74 // Invert as the part sees a 1 as an input
75 this->write_bank_(CONFIG_REG, bank, ~this->config_[bank]);
76 // Without an interrupt pin INT is unconnected, so the masks written at setup can stay
77 if (this->interrupt_pin_ != nullptr) {
78 this->write_bank_(MASK_REG, bank, this->mask_[bank]);
79 }
80}
81
82bool PCA9505Component::read_bank_(uint8_t bank) {
83 if (this->is_failed())
84 return false;
85
86 return this->status_comm_result(this->read_register(INPUT_REG + bank, &this->input_[bank], 1) == i2c::ERROR_OK,
87 LOG_STR("Failed to read input register"));
88}
89
90bool PCA9505Component::write_bank_(uint8_t reg, uint8_t bank, uint8_t value) {
91 if (this->is_failed())
92 return false;
93
94 return this->status_comm_result(this->write_register(reg + bank, &value, 1) == i2c::ERROR_OK,
95 LOG_STR("Failed to write register"));
96}
97
98bool PCA9505Component::write_all_banks_(uint8_t reg, uint8_t value) {
99 if (this->is_failed())
100 return false;
101
102 uint8_t values[BANK_COUNT];
103 for (uint8_t &bank : values) {
104 bank = value;
105 }
106 return this->status_comm_result(this->write_register(reg | AUTO_INCREMENT, values, sizeof(values)) == i2c::ERROR_OK,
107 LOG_STR("Failed to write register"));
108}
109
110} // namespace esphome::pca9505
111
112ESPHOME_EXPANDER_PIN_SUMMARY(esphome::pca9505::PCA9505Component, "%u via PCA9505")
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void enable_polling_loop_()
Call from pin_mode() for an input pin: without an interrupt pin the loop polls instead.
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:54
uint8_t address_
store the address of the device on the bus
Definition i2c.h:327
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:45
bool write_all_banks_(uint8_t reg, uint8_t value)
Definition pca9505.cpp:98
uint8_t output_[BANK_COUNT]
The output state per bank - 1 means HIGH, 0 means LOW.
Definition pca9505.h:37
bool read_bank_(uint8_t bank)
Definition pca9505.cpp:82
uint8_t config_[BANK_COUNT]
The pin config per bank - 1 means OUTPUT, 0 means INPUT.
Definition pca9505.h:35
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition pca9505.cpp:61
uint8_t mask_[BANK_COUNT]
Which pins may pull INT low - 1 means masked, matching the chip.
Definition pca9505.h:41
bool digital_read_cache(uint8_t pin) override
Definition pca9505.cpp:52
uint8_t input_[BANK_COUNT]
The input pin states per bank - 1 means HIGH, 0 means LOW.
Definition pca9505.h:39
bool write_bank_(uint8_t reg, uint8_t bank, uint8_t value)
Definition pca9505.cpp:90
void digital_write_hw(uint8_t pin, bool value) override
Definition pca9505.cpp:54
bool digital_read_hw(uint8_t pin) override
Definition pca9505.cpp:50
uint16_t flags
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "pca9505")
uint32_t value
Definition hal.cpp:41
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:1150