ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pca9554.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
8
9namespace esphome::pca9554 {
10
12 public:
13 explicit PCA9554Component(uint8_t pin_count) : pin_count_(pin_count), reg_width_((pin_count + 7) / 8) {}
14
16 void setup() override;
18 void pin_mode(uint8_t pin, gpio::Flags flags);
19
20 void dump_config() override;
21
22#ifdef USE_PCA9554_PCAL
24 void set_pcal() { this->pcal_ = true; }
26 void set_latch(uint8_t pin) { this->latch_ |= 1U << pin; }
28 void set_drive_strength(uint8_t pin, uint8_t strength) {
29 const uint8_t shift = 2 * pin;
30 this->drive_ = (this->drive_ & ~(3UL << shift)) | (uint32_t(strength) << shift);
31 }
32#endif
33
34 protected:
35 bool read_inputs_();
36 bool write_register_(uint8_t reg, uint16_t value) { return this->write_address_(reg * this->reg_width_, value); }
37 bool write_address_(uint8_t address, uint16_t value);
38#ifdef USE_PCA9554_PCAL
39 bool write_pcal_register_(uint8_t reg, uint16_t value);
44 if (this->pcal_dirty_)
45 this->write_pcal_shadows_();
46 }
48 void update_pcal_register_(uint8_t reg, uint16_t &shadow, uint16_t mask, uint16_t value);
49#endif
50
51 // Virtual methods from CachedGpioExpander
52 bool digital_read_hw(uint8_t pin) override;
53 bool digital_read_cache(uint8_t pin) override;
54 void digital_write_hw(uint8_t pin, bool value) override;
55
56#ifdef USE_PCA9554_PCAL
58 uint32_t drive_{0xFFFFFFFF};
59#endif
61 uint16_t config_mask_{0x00};
63 uint16_t output_mask_{0x00};
65 uint16_t input_mask_{0x00};
66#ifdef USE_PCA9554_PCAL
67 // Shadows of the PCAL registers. setup() writes them, so every part starts with the pulls disconnected
68 // whatever its power-on state is (the PCAL9555 powers up with pull-ups enabled).
69 uint16_t pupd_enable_{0x0000};
70 uint16_t pupd_select_{0xFFFF};
71 uint16_t latch_{0x0000};
72 uint16_t interrupt_mask_{0xFFFF};
73#endif
75 const uint8_t pin_count_;
77 const uint8_t reg_width_;
78#ifdef USE_PCA9554_PCAL
79 bool pcal_{false};
80 bool pcal_dirty_{false}; // a PCAL write failed; retried after the next good read or output write
81#endif
82};
83
85
86} // namespace esphome::pca9554
87
uint8_t address
Definition bl0906.h:4
Base for expanders that cache reads per loop. With an interrupt pin the cache stays valid until INT f...
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
bool write_pcal_shadows_()
Write every PCAL shadow to the chip; false when any write failed.
Definition pca9554.cpp:159
bool digital_read_cache(uint8_t pin) override
Definition pca9554.cpp:75
void set_latch(uint8_t pin)
Only updates the shadow, which setup() writes.
Definition pca9554.h:26
const uint8_t reg_width_
width of registers
Definition pca9554.h:77
bool write_register_(uint8_t reg, uint16_t value)
Definition pca9554.h:36
uint32_t drive_
Drive strength shadow, two bits per pin; the low reg_width_ bytes go to OUTPUT_DRIVE_0.
Definition pca9554.h:58
void retry_pcal_shadows_()
Retry the shadows when an earlier PCAL write failed.
Definition pca9554.h:43
uint16_t output_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition pca9554.h:63
void digital_write_hw(uint8_t pin, bool value) override
Definition pca9554.cpp:80
bool write_address_(uint8_t address, uint16_t value)
Definition pca9554.cpp:139
const uint8_t pin_count_
number of bits the expander has
Definition pca9554.h:75
void set_drive_strength(uint8_t pin, uint8_t strength)
Only updates the shadow, which setup() writes. 0 is a quarter of the full drive strength,...
Definition pca9554.h:28
void update_pcal_register_(uint8_t reg, uint16_t &shadow, uint16_t mask, uint16_t value)
Changes the masked bits of a shadow and writes the register only when it changed.
Definition pca9554.cpp:171
bool digital_read_hw(uint8_t pin) override
Definition pca9554.cpp:70
uint16_t input_mask_
The state of the actual input pins - 1 means HIGH, 0 means LOW.
Definition pca9554.h:65
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition pca9554.cpp:90
PCA9554Component(uint8_t pin_count)
Definition pca9554.h:13
uint16_t config_mask_
Mask for the pin config - 1 means OUTPUT, 0 means INPUT.
Definition pca9554.h:61
bool write_pcal_register_(uint8_t reg, uint16_t value)
Definition pca9554.cpp:152
void setup() override
Check i2c availability and setup masks.
Definition pca9554.cpp:26
void set_pcal()
The PCAL parts add pull resistors, input latches, an interrupt mask and drive strength.
Definition pca9554.h:24
uint16_t flags
ESPHOME_EXPANDER_PIN_SUMMARY_DECL(esphome::pca9554::PCA9554Component)
static void uint32_t