ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ch423.cpp
Go to the documentation of this file.
1#include "ch423.h"
3#include "esphome/core/log.h"
5
6namespace esphome::ch423 {
7
8static constexpr uint8_t CH423_REG_SYS = 0x24; // Set system parameters (0x48 >> 1)
9static constexpr uint8_t CH423_SYS_IO_OE = 0x01; // IO output enable
10static constexpr uint8_t CH423_SYS_OD_EN = 0x04; // Open drain enable for OC pins
11static constexpr uint8_t CH423_REG_IO = 0x30; // Write/read IO7-IO0 (0x60 >> 1)
12static constexpr uint8_t CH423_REG_IO_RD = 0x26; // Read IO7-IO0 (0x4D >> 1, rounded down)
13static constexpr uint8_t CH423_REG_OCL = 0x22; // Write OC7-OC0 (0x44 >> 1)
14static constexpr uint8_t CH423_REG_OCH = 0x23; // Write OC15-OC8 (0x46 >> 1)
15
16ESPHOME_LOG_TAG(TAG, "ch423");
17
19 // set outputs before mode
20 this->write_outputs_();
21 // Set system parameters and check for errors
22 bool success = this->write_reg_(CH423_REG_SYS, this->sys_params_);
23 // Only read inputs if pins are configured for input (IO_OE not set)
24 if (success && !(this->sys_params_ & CH423_SYS_IO_OE)) {
25 success = this->read_inputs_();
26 }
27 if (!success) {
28 this->mark_failed(LOG_STR("CH423 not detected"));
29 return;
30 }
31
32 ESP_LOGCONFIG(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
33 this->status_has_error());
34}
35
37 // Clear all the previously read flags.
38 this->pin_read_flags_ = 0x00;
39}
40
41void CH423Component::dump_config() { ESP_LOGCONFIG(TAG, "CH423:"); }
42
44 if (pin < 8) {
46 this->sys_params_ |= CH423_SYS_IO_OE;
47 }
48 } else if (pin >= 8 && pin < 24) {
50 this->sys_params_ |= CH423_SYS_OD_EN;
51 }
52 }
53}
54
56 if (this->pin_read_flags_ == 0 || this->pin_read_flags_ & (1 << pin)) {
57 // Read values on first access or in case it's being read again in the same loop
58 this->read_inputs_();
59 }
60
61 this->pin_read_flags_ |= (1 << pin);
62 return (this->input_bits_ & (1 << pin)) != 0;
63}
64
65void CH423Component::digital_write(uint8_t pin, bool value) {
66 assign_bits(this->output_bits_, 1 << pin, value);
67 this->write_outputs_();
68}
69
71 if (this->is_failed()) {
72 return false;
73 }
74 // reading inputs requires IO_OE to be 0
75 if (this->sys_params_ & CH423_SYS_IO_OE) {
76 return false;
77 }
78 uint8_t result = this->read_reg_(CH423_REG_IO_RD);
79 this->input_bits_ = result;
81 return true;
82}
83
84// Write a register. Can't use the standard write_byte() method because there is no single pre-configured i2c address.
85bool CH423Component::write_reg_(uint8_t reg, uint8_t value) {
86 auto err = this->bus_->write_readv(reg, &value, 1, nullptr, 0);
87 if (err != i2c::ERROR_OK) {
88 char buf[64];
89 ESPHOME_snprintf_P(buf, sizeof(buf), ESPHOME_PSTR("write failed for register 0x%X, error %d"), reg, err);
90 this->status_set_warning(buf);
91 return false;
92 }
94 return true;
95}
96
97uint8_t CH423Component::read_reg_(uint8_t reg) {
98 uint8_t value;
99 auto err = this->bus_->write_readv(reg, nullptr, 0, &value, 1);
100 if (err != i2c::ERROR_OK) {
101 char buf[64];
102 ESPHOME_snprintf_P(buf, sizeof(buf), ESPHOME_PSTR("read failed for register 0x%X, error %d"), reg, err);
103 this->status_set_warning(buf);
104 return 0;
105 }
106 this->status_clear_warning();
107 return value;
108}
109
111 bool success = true;
112 // Write IO7-IO0
113 success &= this->write_reg_(CH423_REG_IO, static_cast<uint8_t>(this->output_bits_));
114 // Write OC7-OC0
115 success &= this->write_reg_(CH423_REG_OCL, static_cast<uint8_t>(this->output_bits_ >> 8));
116 // Write OC15-OC8
117 success &= this->write_reg_(CH423_REG_OCH, static_cast<uint8_t>(this->output_bits_ >> 16));
118 return success;
119}
120
122
123} // namespace esphome::ch423
124
125ESPHOME_EXPANDER_PIN_SUMMARY(esphome::ch423::CH423Component, "EXIO%u via CH423")
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool status_has_warning() const
Definition component.h:293
bool status_has_error() const
Definition component.h:295
void status_clear_warning()
Definition component.h:309
uint8_t read_reg_(uint8_t reg)
Definition ch423.cpp:97
void digital_write(uint8_t pin, bool value)
Helper function to write the value of a pin.
Definition ch423.cpp:65
bool write_reg_(uint8_t reg, uint8_t value)
Definition ch423.cpp:85
void setup() override
Check i2c availability and setup masks.
Definition ch423.cpp:18
uint8_t pin_read_flags_
Flags to check if read previously during this loop.
Definition ch423.h:37
uint8_t input_bits_
Copy of last read values.
Definition ch423.h:39
uint8_t sys_params_
System parameters.
Definition ch423.h:41
bool digital_read(uint8_t pin)
Helper function to read the value of a pin.
Definition ch423.cpp:55
float get_setup_priority() const override
Definition ch423.cpp:121
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition ch423.cpp:43
void dump_config() override
Definition ch423.cpp:41
void loop() override
Poll for input changes periodically.
Definition ch423.cpp:36
uint32_t output_bits_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition ch423.h:35
virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)=0
This virtual method writes bytes to an I2CBus from an array, then reads bytes into an array of ReadBu...
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:325
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
uint16_t flags
ESPHOME_LOG_TAG(TAG, "ch423")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:41
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