ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ch423.cpp
Go to the documentation of this file.
1#include "ch423.h"
3#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 (!this->setup_check(success, LOG_STR("CH423 not detected")))
27 return;
28
29 ESP_LOGCONFIG(TAG, "Initialization complete. Warning: %d, Error: %d", this->status_has_warning(),
30 this->status_has_error());
31}
32
34 // Clear all the previously read flags.
35 this->pin_read_flags_ = 0x00;
36}
37
38void CH423Component::dump_config() { ESP_LOGCONFIG(TAG, "CH423:"); }
39
41 if (pin < 8) {
43 this->sys_params_ |= CH423_SYS_IO_OE;
44 }
45 } else if (pin >= 8 && pin < 24) {
47 this->sys_params_ |= CH423_SYS_OD_EN;
48 }
49 }
50}
51
53 if (this->pin_read_flags_ == 0 || this->pin_read_flags_ & (1 << pin)) {
54 // Read values on first access or in case it's being read again in the same loop
55 this->read_inputs_();
56 }
57
58 this->pin_read_flags_ |= (1 << pin);
59 return (this->input_bits_ & (1 << pin)) != 0;
60}
61
62void CH423Component::digital_write(uint8_t pin, bool value) {
63 assign_bits(this->output_bits_, 1 << pin, value);
64 this->write_outputs_();
65}
66
68 if (this->is_failed()) {
69 return false;
70 }
71 // reading inputs requires IO_OE to be 0
72 if (this->sys_params_ & CH423_SYS_IO_OE) {
73 return false;
74 }
75 uint8_t result;
76 if (!this->status_comm_result(this->read_reg_(CH423_REG_IO_RD, &result)))
77 return false;
78 this->input_bits_ = result;
79 return true;
80}
81
82// Write a register. Can't use the standard write_byte() method because there is no single pre-configured i2c address.
83// Both only set the warning; the caller clears it after its last step.
84bool CH423Component::write_reg_(uint8_t reg, uint8_t value) {
85 return this->status_comm_check(this->bus_->write_readv(reg, &value, 1, nullptr, 0) == i2c::ERROR_OK);
86}
87
88bool CH423Component::read_reg_(uint8_t reg, uint8_t *value) {
89 return this->status_comm_check(this->bus_->write_readv(reg, nullptr, 0, value, 1) == i2c::ERROR_OK);
90}
91
93 bool success = true;
94 // Write IO7-IO0
95 success &= this->write_reg_(CH423_REG_IO, static_cast<uint8_t>(this->output_bits_));
96 // Write OC7-OC0
97 success &= this->write_reg_(CH423_REG_OCL, static_cast<uint8_t>(this->output_bits_ >> 8));
98 // Write OC15-OC8
99 success &= this->write_reg_(CH423_REG_OCH, static_cast<uint8_t>(this->output_bits_ >> 16));
100 return this->status_comm_result(success);
101}
102
104
105} // namespace esphome::ch423
106
107ESPHOME_EXPANDER_PIN_SUMMARY(esphome::ch423::CH423Component, "EXIO%u via CH423")
bool is_failed() const
Definition component.h:288
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
bool status_has_warning() const
Definition component.h:294
bool status_has_error() const
Definition component.h:296
bool status_comm_check(bool ok)
Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
Definition component.h:309
void digital_write(uint8_t pin, bool value)
Helper function to write the value of a pin.
Definition ch423.cpp:62
bool write_reg_(uint8_t reg, uint8_t value)
Definition ch423.cpp:84
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:37
bool read_reg_(uint8_t reg, uint8_t *value)
Definition ch423.cpp:88
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:52
float get_setup_priority() const override
Definition ch423.cpp:103
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition ch423.cpp:40
void dump_config() override
Definition ch423.cpp:38
void loop() override
Poll for input changes periodically.
Definition ch423.cpp:33
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:338
uint16_t flags
ESPHOME_LOG_TAG(TAG, "ch423")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:42
uint32_t value
Definition hal.cpp:59
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:1334
esp_err_t result