MCP230xx open drain interrupt pins (#1243)

This commit is contained in:
Keith Burzinski 2021-02-13 03:07:11 -06:00 committed by GitHub
parent 81b512a7b3
commit b52f7cfe86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 7 deletions

View File

@ -2,7 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import pins from esphome import pins
from esphome.components import i2c from esphome.components import i2c
from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED, CONF_OPEN_DRAIN_INTERRUPT
DEPENDENCIES = ['i2c'] DEPENDENCIES = ['i2c']
MULTI_CONF = True MULTI_CONF = True
@ -20,6 +20,7 @@ MCP23008GPIOPin = mcp23008_ns.class_('MCP23008GPIOPin', cg.GPIOPin)
CONFIG_SCHEMA = cv.Schema({ CONFIG_SCHEMA = cv.Schema({
cv.Required(CONF_ID): cv.declare_id(MCP23008), cv.Required(CONF_ID): cv.declare_id(MCP23008),
cv.Optional(CONF_OPEN_DRAIN_INTERRUPT, default=False): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20)) }).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20))
@ -27,6 +28,7 @@ def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config) yield i2c.register_i2c_device(var, config)
cg.add(var.set_open_drain_ints(config[CONF_OPEN_DRAIN_INTERRUPT]))
CONF_MCP23008 = 'mcp23008' CONF_MCP23008 = 'mcp23008'

View File

@ -14,8 +14,10 @@ void MCP23008::setup() {
return; return;
} }
// all pins input if (this->open_drain_ints_) {
this->write_reg_(MCP23008_IODIR, 0xFF); // enable open-drain interrupt pins, 3.3V-safe
this->write_reg_(MCP23008_IOCON, 0x04);
}
} }
bool MCP23008::digital_read(uint8_t pin) { bool MCP23008::digital_read(uint8_t pin) {
uint8_t bit = pin % 8; uint8_t bit = pin % 8;

View File

@ -39,6 +39,8 @@ class MCP23008 : public Component, public i2c::I2CDevice {
void digital_write(uint8_t pin, bool value); void digital_write(uint8_t pin, bool value);
void pin_mode(uint8_t pin, uint8_t mode); void pin_mode(uint8_t pin, uint8_t mode);
void set_open_drain_ints(const bool value) { open_drain_ints_ = value; }
float get_setup_priority() const override; float get_setup_priority() const override;
protected: protected:
@ -50,6 +52,7 @@ class MCP23008 : public Component, public i2c::I2CDevice {
void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a); void update_reg_(uint8_t pin, bool pin_value, uint8_t reg_a);
uint8_t olat_{0x00}; uint8_t olat_{0x00};
bool open_drain_ints_;
}; };
class MCP23008GPIOPin : public GPIOPin { class MCP23008GPIOPin : public GPIOPin {

View File

@ -2,7 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import pins from esphome import pins
from esphome.components import i2c from esphome.components import i2c
from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED, CONF_OPEN_DRAIN_INTERRUPT
DEPENDENCIES = ['i2c'] DEPENDENCIES = ['i2c']
MULTI_CONF = True MULTI_CONF = True
@ -20,6 +20,7 @@ MCP23017GPIOPin = mcp23017_ns.class_('MCP23017GPIOPin', cg.GPIOPin)
CONFIG_SCHEMA = cv.Schema({ CONFIG_SCHEMA = cv.Schema({
cv.Required(CONF_ID): cv.declare_id(MCP23017), cv.Required(CONF_ID): cv.declare_id(MCP23017),
cv.Optional(CONF_OPEN_DRAIN_INTERRUPT, default=False): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20)) }).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20))
@ -27,6 +28,7 @@ def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config) yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config) yield i2c.register_i2c_device(var, config)
cg.add(var.set_open_drain_ints(config[CONF_OPEN_DRAIN_INTERRUPT]))
CONF_MCP23017 = 'mcp23017' CONF_MCP23017 = 'mcp23017'

View File

@ -14,9 +14,11 @@ void MCP23017::setup() {
return; return;
} }
// all pins input if (this->open_drain_ints_) {
this->write_reg_(MCP23017_IODIRA, 0xFF); // enable open-drain interrupt pins, 3.3V-safe
this->write_reg_(MCP23017_IODIRB, 0xFF); this->write_reg_(MCP23017_IOCONA, 0x04);
this->write_reg_(MCP23017_IOCONB, 0x04);
}
} }
bool MCP23017::digital_read(uint8_t pin) { bool MCP23017::digital_read(uint8_t pin) {
uint8_t bit = pin % 8; uint8_t bit = pin % 8;

View File

@ -51,6 +51,8 @@ class MCP23017 : public Component, public i2c::I2CDevice {
void digital_write(uint8_t pin, bool value); void digital_write(uint8_t pin, bool value);
void pin_mode(uint8_t pin, uint8_t mode); void pin_mode(uint8_t pin, uint8_t mode);
void set_open_drain_ints(const bool value) { open_drain_ints_ = value; }
float get_setup_priority() const override; float get_setup_priority() const override;
protected: protected:
@ -63,6 +65,7 @@ class MCP23017 : public Component, public i2c::I2CDevice {
uint8_t olat_a_{0x00}; uint8_t olat_a_{0x00};
uint8_t olat_b_{0x00}; uint8_t olat_b_{0x00};
bool open_drain_ints_;
}; };
class MCP23017GPIOPin : public GPIOPin { class MCP23017GPIOPin : public GPIOPin {

View File

@ -357,6 +357,7 @@ CONF_ON_VALUE = 'on_value'
CONF_ON_VALUE_RANGE = 'on_value_range' CONF_ON_VALUE_RANGE = 'on_value_range'
CONF_ONE = 'one' CONF_ONE = 'one'
CONF_OPEN_ACTION = 'open_action' CONF_OPEN_ACTION = 'open_action'
CONF_OPEN_DRAIN_INTERRUPT = 'open_drain_interrupt'
CONF_OPEN_DURATION = 'open_duration' CONF_OPEN_DURATION = 'open_duration'
CONF_OPEN_ENDSTOP = 'open_endstop' CONF_OPEN_ENDSTOP = 'open_endstop'
CONF_OPTIMISTIC = 'optimistic' CONF_OPTIMISTIC = 'optimistic'

View File

@ -1921,10 +1921,12 @@ pcf8574:
mcp23017: mcp23017:
- id: 'mcp23017_hub' - id: 'mcp23017_hub'
open_drain_interrupt: 'true'
mcp23008: mcp23008:
- id: 'mcp23008_hub' - id: 'mcp23008_hub'
address: 0x22 address: 0x22
open_drain_interrupt: 'true'
mcp23016: mcp23016:
- id: 'mcp23016_hub' - id: 'mcp23016_hub'