ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
weikai_i2c.cpp
Go to the documentation of this file.
1
5
6#include "weikai_i2c.h"
7
9ESPHOME_LOG_TAG(TAG, "weikai_i2c");
10
11using namespace weikai;
12enum RegType { REG = 0, FIFO = 1 };
13
19inline uint8_t i2c_address(uint8_t base_address, uint8_t channel, RegType fifo) {
20 // the address of the device is:
21 // +----+----+----+----+----+----+----+----+
22 // | 0 | A1 | A0 | 1 | 0 | C1 | C0 | F |
23 // +----+----+----+----+----+----+----+----+
24 // where:
25 // - A1,A0 is the address read from A1,A0 switch
26 // - C1,C0 is the channel number (in practice only 00 or 01)
27 // - F is: 0 when accessing register, one when accessing FIFO
28 uint8_t const addr = base_address | channel << 1 | fifo << 0;
29 return addr;
30}
31
33// The WeikaiRegisterI2C methods
36 uint8_t value = 0x00;
37 WeikaiComponentI2C *comp_i2c = static_cast<WeikaiComponentI2C *>(this->comp_);
38 uint8_t address = i2c_address(comp_i2c->base_address_, this->channel_, REG);
39 comp_i2c->set_i2c_address(address);
40 auto error = comp_i2c->read_register(this->register_, &value, 1);
41 if (error == i2c::NO_ERROR) {
43 ESP_LOGVV(TAG, "WeikaiRegisterI2C::read_reg() @%02X reg=%s ch=%u I2C_code:%d, buf=%02X", address,
44 LOG_STR_ARG(reg_to_str(this->register_, comp_i2c->page1())), this->channel_, (int) error, value);
45 } else { // error
47 ESP_LOGE(TAG, "WeikaiRegisterI2C::read_reg() @%02X reg=%s ch=%u I2C_code:%d, buf=%02X", address,
48 LOG_STR_ARG(reg_to_str(this->register_, comp_i2c->page1())), this->channel_, (int) error, value);
49 }
50 return value;
51}
52
53void WeikaiRegisterI2C::read_fifo(uint8_t *data, size_t length) const {
54 WeikaiComponentI2C *comp_i2c = static_cast<WeikaiComponentI2C *>(this->comp_);
55 uint8_t address = i2c_address(comp_i2c->base_address_, this->channel_, FIFO);
56 comp_i2c->set_i2c_address(address);
57 auto error = comp_i2c->read(data, length);
58 if (error == i2c::NO_ERROR) {
60#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
61 ESP_LOGVV(TAG, "WeikaiRegisterI2C::read_fifo() @%02X ch=%d I2C_code:%d len=%d buffer", address, this->channel_,
62 (int) error, length);
63 print_buffer(TAG, data, length);
64#endif
65 } else { // error
67 ESP_LOGE(TAG, "WeikaiRegisterI2C::read_fifo() @%02X reg=N/A ch=%d I2C_code:%d len=%d buf=%02X", address,
68 this->channel_, (int) error, length, data[0]);
69 }
70}
71
72void WeikaiRegisterI2C::write_reg(uint8_t value) {
73 WeikaiComponentI2C *comp_i2c = static_cast<WeikaiComponentI2C *>(this->comp_);
74 uint8_t address = i2c_address(comp_i2c->base_address_, this->channel_, REG); // update the i2c bus
75 comp_i2c->set_i2c_address(address);
76 auto error = comp_i2c->write_register(this->register_, &value, 1);
77 if (error == i2c::NO_ERROR) {
79 ESP_LOGVV(TAG, "WK2168Reg::write_reg() @%02X reg=%s ch=%d I2C_code:%d buf=%02X", address,
80 LOG_STR_ARG(reg_to_str(this->register_, comp_i2c->page1())), this->channel_, (int) error, value);
81 } else { // error
83 ESP_LOGE(TAG, "WK2168Reg::write_reg() @%02X reg=%s ch=%d I2C_code:%d buf=%d", address,
84 LOG_STR_ARG(reg_to_str(this->register_, comp_i2c->page1())), this->channel_, (int) error, value);
85 }
86}
87
88void WeikaiRegisterI2C::write_fifo(uint8_t *data, size_t length) {
89 WeikaiComponentI2C *comp_i2c = static_cast<WeikaiComponentI2C *>(this->comp_);
90 uint8_t address = i2c_address(comp_i2c->base_address_, this->channel_, FIFO); // set fifo flag
91 comp_i2c->set_i2c_address(address);
92 auto error = comp_i2c->write(data, length);
93 if (error == i2c::NO_ERROR) {
95#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
96 ESP_LOGVV(TAG, "WK2168Reg::write_fifo() @%02X ch=%d I2C_code:%d len=%d buffer", address, this->channel_,
97 (int) error, length);
98 print_buffer(TAG, data, length);
99#endif
100 } else { // error
101 this->comp_->status_set_warning();
102 ESP_LOGE(TAG, "WK2168Reg::write_fifo() @%02X reg=N/A, ch=%d I2C_code:%d len=%d, buf=%02X", address, this->channel_,
103 (int) error, length, data[0]);
104 }
105}
106
108// The WeikaiComponentI2C methods
111 // before any manipulation we store the address to base_address_ for future use
112 this->base_address_ = this->address_;
113 ESP_LOGCONFIG(TAG, "Setup %s (%d UARTs) @ 0x%02X", this->get_name(), this->children_.size(), this->base_address_);
114
115 // enable all channels
117 // reset all channels
119 // initialize the spage register to page 0
120 this->reg(WKREG_SPAGE, 0) = 0;
121 this->page1_ = false;
122
123 // we setup our children channels
124 for (auto *child : this->children_) {
125 child->setup_channel();
126 }
127}
128
130 ESP_LOGCONFIG(TAG,
131 "Initialization of %s with %d UARTs completed\n"
132 " Crystal: %" PRIu32,
133 this->get_name(), this->children_.size(), this->crystal_);
134 if (test_mode_) {
135 ESP_LOGCONFIG(TAG,
136 " Test mode: %d\n"
137 " Transfer buffer size: %d",
139 }
140 this->address_ = this->base_address_; // we restore the base_address before display (less confusing)
141 LOG_I2C_DEVICE(this);
142
143 for (auto *child : this->children_) {
144 child->dump_channel();
145 }
146}
147
148} // namespace esphome::weikai_i2c
uint8_t address
Definition bl0906.h:4
void status_clear_warning()
Definition component.h:296
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:37
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
uint8_t address_
store the address of the device on the bus
Definition i2c.h:327
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:151
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:28
int test_mode_
test mode value (0 -> no tests)
Definition weikai.h:189
bool page1_
set to true when in "page1 mode"
Definition weikai.h:190
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
Definition weikai.h:191
const char * get_name()
Get the name of the component.
Definition weikai.h:143
uint8_t register_
address of the register
Definition weikai.h:113
uint8_t channel_
channel for this register
Definition weikai.h:114
WeikaiComponent *const comp_
pointer to our parent (aggregation)
Definition weikai.h:112
The WeikaiComponentI2C class stores the information to the WeiKai component connected through an I2C ...
Definition weikai_i2c.h:41
weikai::WeikaiRegister & reg(uint8_t reg, uint8_t channel) override
Definition weikai_i2c.h:43
uint8_t base_address_
base address of I2C device
Definition weikai_i2c.h:55
void write_fifo(uint8_t *data, size_t length) override
uint8_t read_reg() const override
void write_reg(uint8_t value) override
void read_fifo(uint8_t *data, size_t length) const override
constexpr uint8_t WKREG_SPAGE
Global Page register c0/c1 0011.
Definition wk_reg_def.h:126
constexpr uint8_t GRST_C4RST
Channel 4 soft reset (0: not reset, 1: reset)
Definition wk_reg_def.h:55
constexpr uint8_t GRST_C2RST
Channel 2 soft reset (0: not reset, 1: reset)
Definition wk_reg_def.h:59
constexpr uint8_t WKREG_GRST
Global Reset Register - 00 0001.
Definition wk_reg_def.h:53
constexpr uint8_t GRST_C3RST
Channel 3 soft reset (0: not reset, 1: reset)
Definition wk_reg_def.h:57
constexpr uint8_t GENA_C2EN
Channel 2 enable clock (0: disable, 1: enable)
Definition wk_reg_def.h:37
constexpr uint8_t GRST_C1RST
Channel 1 soft reset (0: not reset, 1: reset)
Definition wk_reg_def.h:61
constexpr uint8_t GENA_C3EN
Channel 3 enable clock (0: disable, 1: enable)
Definition wk_reg_def.h:35
constexpr uint8_t GENA_C4EN
Channel 4 enable clock (0: disable, 1: enable)
Definition wk_reg_def.h:33
constexpr uint8_t WKREG_GENA
Global Control Register - 00 0000.
Definition wk_reg_def.h:31
constexpr uint8_t GENA_C1EN
Channel 1 enable clock (0: disable, 1: enable)
Definition wk_reg_def.h:39
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:14
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.
const LogString * reg_to_str(int reg, bool page1)
Name of a register for the log messages.
Definition weikai.cpp:62
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
Definition weikai.h:33
void print_buffer(const char *tag, const uint8_t *data, size_t length, int level)
Display a buffer in hexadecimal format (32 hex values / line) for debug.
Definition weikai.cpp:41
ESPHOME_LOG_TAG(TAG, "weikai")
uint16_t length
Definition tt21100.cpp:0
WeiKai component family - classes declaration.