10 static const char *
const TAG =
"weikai_i2c";
15 hex_buffer[(3 * 32) + 1] = 0;
16 for (
size_t i = 0; i <
length; i++) {
17 snprintf(&hex_buffer[3 * (i % 32)],
sizeof(hex_buffer),
"%02X ", data[i]);
19 ESP_LOGVV(TAG,
" %s", hex_buffer);
24 hex_buffer[3 * (length % 32) + 2] = 0;
25 ESP_LOGVV(TAG,
" %s", hex_buffer);
29 static const char *
const REG_TO_STR_P0[16] = {
"GENA",
"GRST",
"GMUT",
"SPAGE",
"SCR",
"LCR",
"FCR",
"SIER",
30 "SIFR",
"TFCNT",
"RFCNT",
"FSR",
"LSR",
"FDAT",
"FWCR",
"RS485"};
31 static const char *
const REG_TO_STR_P1[16] = {
"GENA",
"GRST",
"GMUT",
"SPAGE",
"BAUD1",
"BAUD0",
"PRES",
"RFTL",
32 "TFTL",
"FWTH",
"FWTL",
"XON1",
"XOFF1",
"SADR",
"SAEN",
"RTSDLY"};
33 using namespace weikai;
41 return page1 ? REG_TO_STR_P1[reg & 0x0F] : REG_TO_STR_P0[reg & 0x0F];
60 uint8_t
const addr = base_address | channel << 1 | fifo << 0;
72 auto error = comp_i2c->
read_register(this->register_, &value, 1);
74 this->comp_->status_clear_warning();
75 ESP_LOGVV(TAG,
"WeikaiRegisterI2C::read_reg() @%02X reg=%s ch=%u I2C_code:%d, buf=%02X", address,
76 reg_to_str(this->register_, comp_i2c->
page1()), this->channel_, (
int) error, value);
78 this->comp_->status_set_warning();
79 ESP_LOGE(TAG,
"WeikaiRegisterI2C::read_reg() @%02X reg=%s ch=%u I2C_code:%d, buf=%02X", address,
80 reg_to_str(this->register_, comp_i2c->
page1()), this->channel_, (
int) error, value);
89 auto error = comp_i2c->
read(data, length);
91 this->comp_->status_clear_warning();
92 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 93 ESP_LOGVV(TAG,
"WeikaiRegisterI2C::read_fifo() @%02X ch=%d I2C_code:%d len=%d buffer", address, this->channel_,
98 this->comp_->status_set_warning();
99 ESP_LOGE(TAG,
"WeikaiRegisterI2C::read_fifo() @%02X reg=N/A ch=%d I2C_code:%d len=%d buf=%02X...", address,
100 this->channel_, (
int) error, length, data[0]);
108 auto error = comp_i2c->
write_register(this->register_, &value, 1);
110 this->comp_->status_clear_warning();
111 ESP_LOGVV(TAG,
"WK2168Reg::write_reg() @%02X reg=%s ch=%d I2C_code:%d buf=%02X", address,
112 reg_to_str(this->register_, comp_i2c->
page1()), this->channel_, (
int) error, value);
114 this->comp_->status_set_warning();
115 ESP_LOGE(TAG,
"WK2168Reg::write_reg() @%02X reg=%s ch=%d I2C_code:%d buf=%d", address,
116 reg_to_str(this->register_, comp_i2c->
page1()), this->channel_, (
int) error, value);
124 auto error = comp_i2c->
write(data, length);
126 this->comp_->status_clear_warning();
127 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 128 ESP_LOGVV(TAG,
"WK2168Reg::write_fifo() @%02X ch=%d I2C_code:%d len=%d buffer", address, this->channel_,
129 (
int) error, length);
133 this->comp_->status_set_warning();
134 ESP_LOGE(TAG,
"WK2168Reg::write_fifo() @%02X reg=N/A, ch=%d I2C_code:%d len=%d, buf=%02X...", address,
135 this->channel_, (
int) error, length, data[0]);
144 this->base_address_ = this->address_;
145 ESP_LOGCONFIG(TAG,
"Setting up wk2168_i2c: %s with %d UARTs at @%02X ...", this->get_name(), this->children_.size(),
146 this->base_address_);
154 this->page1_ =
false;
157 for (
auto *child : this->children_) {
158 child->setup_channel();
163 ESP_LOGCONFIG(TAG,
"Initialization of %s with %d UARTs completed", this->get_name(), this->children_.size());
164 ESP_LOGCONFIG(TAG,
" Crystal: %" PRIu32, this->crystal_);
166 ESP_LOGCONFIG(TAG,
" Test mode: %d", test_mode_);
167 ESP_LOGCONFIG(TAG,
" Transfer buffer size: %d",
XFER_MAX_SIZE);
168 this->address_ = this->base_address_;
169 LOG_I2C_DEVICE(
this);
171 for (
auto *child : this->children_) {
172 child->dump_channel();
constexpr uint8_t GRST_C2RST
Channel 2 soft reset (0: not reset, 1: reset)
void dump_config() override
constexpr uint8_t GRST_C4RST
Channel 4 soft reset (0: not reset, 1: reset)
constexpr uint8_t WKREG_GENA
Global Control Register - 00 0000.
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
uint8_t read_reg() const override
uint8_t base_address_
base address of I2C device
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
constexpr uint8_t WKREG_GRST
Global Reset Register - 00 0001.
constexpr uint8_t GENA_C2EN
Channel 2 enable clock (0: disable, 1: enable)
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
void write_reg(uint8_t value) override
No error found during execution of method.
WeiKai component family - classes declaration.
The WeikaiComponentI2C class stores the information to the WeiKai component connected through an I2C ...
constexpr uint8_t GENA_C3EN
Channel 3 enable clock (0: disable, 1: enable)
constexpr uint8_t WKREG_GPDAT
Global GPIO data register - 11 0001.
void write_fifo(uint8_t *data, size_t length) override
constexpr uint8_t GRST_C1RST
Channel 1 soft reset (0: not reset, 1: reset)
void print_buffer(const uint8_t *data, size_t length)
Display a buffer in hexadecimal format (32 hex values / line).
constexpr uint8_t GRST_C3RST
Channel 3 soft reset (0: not reset, 1: reset)
Implementation of SPI Controller mode.
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
constexpr uint8_t WKREG_SPAGE
Global Page register c0/c1 0011.
constexpr uint8_t WKREG_GPDIR
Global GPIO direction register - 10 0001.
const char * reg_to_str(int reg, bool page1)
void read_fifo(uint8_t *data, size_t length) const override
constexpr uint8_t GENA_C1EN
Channel 1 enable clock (0: disable, 1: enable)
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
uint8_t i2c_address(uint8_t base_address, uint8_t channel, RegType fifo)
Computes the I²C bus's address used to access the component.
constexpr uint8_t GENA_C4EN
Channel 4 enable clock (0: disable, 1: enable)