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