ESPHome 2026.4.3
Loading...
Searching...
No Matches
mcp23xxx_base.h
Go to the documentation of this file.
1#pragma once
2
5#include "esphome/core/hal.h"
6
7namespace esphome {
8namespace mcp23xxx_base {
9
11
12template<uint8_t N> class MCP23XXXBase : public Component, public gpio_expander::CachedGpioExpander<uint8_t, N> {
13 public:
14 virtual void pin_mode(uint8_t pin, gpio::Flags flags);
15 virtual void pin_interrupt_mode(uint8_t pin, MCP23XXXInterruptMode interrupt_mode);
16
17 void set_open_drain_ints(const bool value) { this->open_drain_ints_ = value; }
20 float get_setup_priority() const override { return setup_priority::IO; }
21
22 void loop() override {
23 this->reset_pin_cache_();
24 // Only disable the loop once INT has actually gone HIGH. Input transitions that straddle the
25 // I2C read leave INT asserted without re-firing a falling edge, which would strand us with
26 // stale state forever; keep looping until the line is released so we self-heal.
27 if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read()) {
28 this->disable_loop();
29 }
30 }
31
32 protected:
33 // No need to clear latched interrupts before attaching the ISR — if INT is
34 // already low the ISR fires immediately, loop runs, cache invalidates, and
35 // the GPIO read clears the latch. One harmless extra read at most.
37 if (this->interrupt_pin_ != nullptr) {
38 this->interrupt_pin_->setup();
40 this->set_invalidate_on_read_(false);
41 }
42 // Disable loop until an input pin is configured via pin_mode()
43 // For interrupt-driven mode, loop is re-enabled by the ISR
44 // For polling mode, loop is re-enabled when pin_mode() registers an input pin
45 this->disable_loop();
46 }
47 static void IRAM_ATTR gpio_intr(MCP23XXXBase *arg) { arg->enable_loop_soon_any_context(); }
48
49 // read a given register
50 virtual bool read_reg(uint8_t reg, uint8_t *value) = 0;
51 // write a value to a given register
52 virtual bool write_reg(uint8_t reg, uint8_t value) = 0;
53 // update registers with given pin value.
54 virtual void update_reg(uint8_t pin, bool pin_value, uint8_t reg_a) = 0;
55
58};
59
60template<uint8_t N> class MCP23XXXGPIOPin : public GPIOPin {
61 public:
62 void setup() override;
63 void pin_mode(gpio::Flags flags) override;
64 bool digital_read() override;
65 void digital_write(bool value) override;
66 size_t dump_summary(char *buffer, size_t len) const override;
67
68 void set_parent(MCP23XXXBase<N> *parent) { parent_ = parent; }
69 void set_pin(uint8_t pin) { pin_ = pin; }
70 void set_inverted(bool inverted) { inverted_ = inverted; }
72 void set_interrupt_mode(MCP23XXXInterruptMode interrupt_mode) { interrupt_mode_ = interrupt_mode; }
73
74 gpio::Flags get_flags() const override { return this->flags_; }
75
76 protected:
78 uint8_t pin_;
82};
83
84} // namespace mcp23xxx_base
85} // namespace esphome
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
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
A class to cache the read state of a GPIO expander.
Definition cached_gpio.h:29
InternalGPIOPin * get_interrupt_pin() const
virtual void pin_interrupt_mode(uint8_t pin, MCP23XXXInterruptMode interrupt_mode)
void set_interrupt_pin(InternalGPIOPin *pin)
static void IRAM_ATTR gpio_intr(MCP23XXXBase *arg)
virtual bool read_reg(uint8_t reg, uint8_t *value)=0
void set_open_drain_ints(const bool value)
virtual void update_reg(uint8_t pin, bool pin_value, uint8_t reg_a)=0
virtual void pin_mode(uint8_t pin, gpio::Flags flags)
virtual bool write_reg(uint8_t reg, uint8_t value)=0
float get_setup_priority() const override
void digital_write(bool value) override
void pin_mode(gpio::Flags flags) override
size_t dump_summary(char *buffer, size_t len) const override
void set_parent(MCP23XXXBase< N > *parent)
void set_interrupt_mode(MCP23XXXInterruptMode interrupt_mode)
gpio::Flags get_flags() const override
uint16_t flags
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:51
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