ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ch423.cpp
Go to the documentation of this file.
1#include "ch423.h"
2#include "esphome/core/log.h"
4
5namespace esphome::ch423 {
6
7static constexpr uint8_t CH423_REG_SYS = 0x24; // Set system parameters (0x48 >> 1)
8static constexpr uint8_t CH423_SYS_IO_OE = 0x01; // IO output enable
9static constexpr uint8_t CH423_SYS_OD_EN = 0x04; // Open drain enable for OC pins
10static constexpr uint8_t CH423_REG_IO = 0x30; // Write/read IO7-IO0 (0x60 >> 1)
11static constexpr uint8_t CH423_REG_IO_RD = 0x26; // Read IO7-IO0 (0x4D >> 1, rounded down)
12static constexpr uint8_t CH423_REG_OCL = 0x22; // Write OC7-OC0 (0x44 >> 1)
13static constexpr uint8_t CH423_REG_OCH = 0x23; // Write OC15-OC8 (0x46 >> 1)
14
15ESPHOME_LOG_TAG(TAG, "ch423");
16
18 // set outputs before mode
19 this->write_outputs_();
20 // Set system parameters and check for errors
21 bool success = this->write_reg_(CH423_REG_SYS, this->sys_params_);
22 // Only read inputs if pins are configured for input (IO_OE not set)
23 if (success && !(this->sys_params_ & CH423_SYS_IO_OE)) {
24 success = this->read_inputs_();
25 }
26 if (!success) {
27 this->mark_failed(LOG_STR("CH423 not detected"));
28 return;
29 }
30
31 ESP_LOGCONFIG(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
32 this->status_has_error());
33}
34
36 // Clear all the previously read flags.
37 this->pin_read_flags_ = 0x00;
38}
39
40void CH423Component::dump_config() { ESP_LOGCONFIG(TAG, "CH423:"); }
41
43 if (pin < 8) {
45 this->sys_params_ |= CH423_SYS_IO_OE;
46 }
47 } else if (pin >= 8 && pin < 24) {
49 this->sys_params_ |= CH423_SYS_OD_EN;
50 }
51 }
52}
53
55 if (this->pin_read_flags_ == 0 || this->pin_read_flags_ & (1 << pin)) {
56 // Read values on first access or in case it's being read again in the same loop
57 this->read_inputs_();
58 }
59
60 this->pin_read_flags_ |= (1 << pin);
61 return (this->input_bits_ & (1 << pin)) != 0;
62}
63
64void CH423Component::digital_write(uint8_t pin, bool value) {
65 if (value) {
66 this->output_bits_ |= (1 << pin);
67 } else {
68 this->output_bits_ &= ~(1 << pin);
69 }
70 this->write_outputs_();
71}
72
74 if (this->is_failed()) {
75 return false;
76 }
77 // reading inputs requires IO_OE to be 0
78 if (this->sys_params_ & CH423_SYS_IO_OE) {
79 return false;
80 }
81 uint8_t result = this->read_reg_(CH423_REG_IO_RD);
82 this->input_bits_ = result;
84 return true;
85}
86
87// Write a register. Can't use the standard write_byte() method because there is no single pre-configured i2c address.
88bool CH423Component::write_reg_(uint8_t reg, uint8_t value) {
89 auto err = this->bus_->write_readv(reg, &value, 1, nullptr, 0);
90 if (err != i2c::ERROR_OK) {
91 char buf[64];
92 ESPHOME_snprintf_P(buf, sizeof(buf), ESPHOME_PSTR("write failed for register 0x%X, error %d"), reg, err);
93 this->status_set_warning(buf);
94 return false;
95 }
97 return true;
98}
99
100uint8_t CH423Component::read_reg_(uint8_t reg) {
101 uint8_t value;
102 auto err = this->bus_->write_readv(reg, nullptr, 0, &value, 1);
103 if (err != i2c::ERROR_OK) {
104 char buf[64];
105 ESPHOME_snprintf_P(buf, sizeof(buf), ESPHOME_PSTR("read failed for register 0x%X, error %d"), reg, err);
106 this->status_set_warning(buf);
107 return 0;
108 }
109 this->status_clear_warning();
110 return value;
111}
112
114 bool success = true;
115 // Write IO7-IO0
116 success &= this->write_reg_(CH423_REG_IO, static_cast<uint8_t>(this->output_bits_));
117 // Write OC7-OC0
118 success &= this->write_reg_(CH423_REG_OCL, static_cast<uint8_t>(this->output_bits_ >> 8));
119 // Write OC15-OC8
120 success &= this->write_reg_(CH423_REG_OCH, static_cast<uint8_t>(this->output_bits_ >> 16));
121 return success;
122}
123
125
127bool CH423GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) ^ this->inverted_; }
128
129void CH423GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value ^ this->inverted_); }
130size_t CH423GPIOPin::dump_summary(char *buffer, size_t len) const {
131 return snprintf(buffer, len, "EXIO%u via CH423", this->pin_);
132}
134 flags_ = flags;
135 this->parent_->pin_mode(this->pin_, flags);
136}
137
138} // namespace esphome::ch423
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
bool status_has_warning() const
Definition component.h:280
bool status_has_error() const
Definition component.h:282
void status_clear_warning()
Definition component.h:291
uint8_t read_reg_(uint8_t reg)
Definition ch423.cpp:100
void digital_write(uint8_t pin, bool value)
Helper function to write the value of a pin.
Definition ch423.cpp:64
bool write_reg_(uint8_t reg, uint8_t value)
Definition ch423.cpp:88
void setup() override
Check i2c availability and setup masks.
Definition ch423.cpp:17
uint8_t pin_read_flags_
Flags to check if read previously during this loop.
Definition ch423.h:36
uint8_t input_bits_
Copy of last read values.
Definition ch423.h:38
uint8_t sys_params_
System parameters.
Definition ch423.h:40
bool digital_read(uint8_t pin)
Helper function to read the value of a pin.
Definition ch423.cpp:54
float get_setup_priority() const override
Definition ch423.cpp:124
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition ch423.cpp:42
void dump_config() override
Definition ch423.cpp:40
void loop() override
Poll for input changes periodically.
Definition ch423.cpp:35
uint32_t output_bits_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition ch423.h:34
void digital_write(bool value) override
Definition ch423.cpp:129
void set_flags(gpio::Flags flags)
Definition ch423.cpp:133
CH423Component * parent_
Definition ch423.h:60
size_t dump_summary(char *buffer, size_t len) const override
Definition ch423.cpp:130
bool digital_read() override
Definition ch423.cpp:127
void pin_mode(gpio::Flags flags) override
Definition ch423.cpp:126
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:271
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
uint16_t flags
uint32_t buffer
Definition gif.cpp:50
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
const void size_t len
Definition hal.h:68