ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
weikai_spi.cpp
Go to the documentation of this file.
1
5
6#include "weikai_spi.h"
7
8namespace esphome {
9namespace weikai_spi {
10using namespace weikai;
11static const char *const TAG = "weikai_spi";
12
16uint32_t elapsed_ms(uint32_t &last_time) {
17 uint32_t e = millis() - last_time;
18 last_time = millis();
19 return e;
20};
21
25const char *p2s(uart::UARTParityOptions parity) {
26 using namespace uart;
27 switch (parity) {
28 case UART_CONFIG_PARITY_NONE:
29 return "NONE";
30 case UART_CONFIG_PARITY_EVEN:
31 return "EVEN";
32 case UART_CONFIG_PARITY_ODD:
33 return "ODD";
34 default:
35 return "UNKNOWN";
36 }
37}
38
40void print_buffer(const uint8_t *data, size_t length) {
41 char hex_buffer[100];
42 hex_buffer[(3 * 32) + 1] = 0;
43 for (size_t i = 0; i < length; i++) {
44 snprintf(&hex_buffer[3 * (i % 32)], sizeof(hex_buffer), "%02X ", data[i]);
45 if (i % 32 == 31) {
46 ESP_LOGVV(TAG, " %s", hex_buffer);
47 }
48 }
49 if (length % 32) {
50 // null terminate if incomplete line
51 hex_buffer[3 * (length % 32) + 2] = 0;
52 ESP_LOGVV(TAG, " %s", hex_buffer);
53 }
54}
55
56static const char *const REG_TO_STR_P0[16] = {"GENA", "GRST", "GMUT", "SPAGE", "SCR", "LCR", "FCR", "SIER",
57 "SIFR", "TFCNT", "RFCNT", "FSR", "LSR", "FDAT", "FWCR", "RS485"};
58static const char *const REG_TO_STR_P1[16] = {"GENA", "GRST", "GMUT", "SPAGE", "BAUD1", "BAUD0", "PRES", "RFTL",
59 "TFTL", "FWTH", "FWTL", "XON1", "XOFF1", "SADR", "SAEN", "RTSDLY"};
60
61// method to print a register value as text: used in the log messages ...
62const char *reg_to_str(int reg, bool page1) {
63 if (reg == WKREG_GPDAT) {
64 return "GPDAT";
65 } else if (reg == WKREG_GPDIR) {
66 return "GPDIR";
67 } else {
68 return page1 ? REG_TO_STR_P1[reg & 0x0F] : REG_TO_STR_P0[reg & 0x0F];
69 }
70}
71
72enum RegType { REG = 0, FIFO = 1 };
73enum CmdType { WRITE_CMD = 0, READ_CMD = 1 };
74
89inline static uint8_t cmd_byte(RegType fifo, CmdType transfer_type, uint8_t channel, uint8_t reg) {
90 return (fifo << 7 | transfer_type << 6 | channel << 4 | reg << 0);
91}
92
94// The WeikaiRegisterSPI methods
97 auto *spi_comp = static_cast<WeikaiComponentSPI *>(this->comp_);
98 uint8_t cmd = cmd_byte(REG, READ_CMD, this->channel_, this->register_);
99 spi_comp->enable();
100 spi_comp->write_byte(cmd);
101 uint8_t val = spi_comp->read_byte();
102 spi_comp->disable();
103 char bin_buf[9];
104 ESP_LOGVV(TAG, "WeikaiRegisterSPI::read_reg() cmd=%s(%02X) reg=%s ch=%d buf=%02X", format_bin_to(bin_buf, cmd), cmd,
105 reg_to_str(this->register_, this->comp_->page1()), this->channel_, val);
106 return val;
107}
108
109void WeikaiRegisterSPI::read_fifo(uint8_t *data, size_t length) const {
110 auto *spi_comp = static_cast<WeikaiComponentSPI *>(this->comp_);
111 uint8_t cmd = cmd_byte(FIFO, READ_CMD, this->channel_, this->register_);
112 spi_comp->enable();
113 spi_comp->write_byte(cmd);
114 spi_comp->read_array(data, length);
115 spi_comp->disable();
116#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
117 char bin_buf[9];
118 ESP_LOGVV(TAG, "WeikaiRegisterSPI::read_fifo() cmd=%s(%02X) ch=%d len=%d buffer", format_bin_to(bin_buf, cmd), cmd,
119 this->channel_, length);
120 print_buffer(data, length);
121#endif
122}
123
124void WeikaiRegisterSPI::write_reg(uint8_t value) {
125 auto *spi_comp = static_cast<WeikaiComponentSPI *>(this->comp_);
126 uint8_t buf[2]{cmd_byte(REG, WRITE_CMD, this->channel_, this->register_), value};
127 spi_comp->enable();
128 spi_comp->write_array(buf, 2);
129 spi_comp->disable();
130 char bin_buf[9];
131 ESP_LOGVV(TAG, "WeikaiRegisterSPI::write_reg() cmd=%s(%02X) reg=%s ch=%d buf=%02X", format_bin_to(bin_buf, buf[0]),
132 buf[0], reg_to_str(this->register_, this->comp_->page1()), this->channel_, buf[1]);
133}
134
135void WeikaiRegisterSPI::write_fifo(uint8_t *data, size_t length) {
136 auto *spi_comp = static_cast<WeikaiComponentSPI *>(this->comp_);
137 uint8_t cmd = cmd_byte(FIFO, WRITE_CMD, this->channel_, this->register_);
138 spi_comp->enable();
139 spi_comp->write_byte(cmd);
140 spi_comp->write_array(data, length);
141 spi_comp->disable();
142
143#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
144 char bin_buf[9];
145 ESP_LOGVV(TAG, "WeikaiRegisterSPI::write_fifo() cmd=%s(%02X) ch=%d len=%d buffer", format_bin_to(bin_buf, cmd), cmd,
146 this->channel_, length);
147 print_buffer(data, length);
148#endif
149}
150
152// The WeikaiComponentSPI methods
155 using namespace weikai;
156 ESP_LOGCONFIG(TAG, "Setup %s (%d UARTs)", this->get_name(), this->children_.size());
157 this->spi_setup();
158 // enable all channels
160 // reset all channels
162 // initialize the spage register to page 0
163 this->reg(WKREG_SPAGE, 0) = 0;
164 this->page1_ = false;
165
166 // we setup our children channels
167 for (auto *child : this->children_) {
168 child->setup_channel();
169 }
170}
171
173 ESP_LOGCONFIG(TAG,
174 "Initialization of %s with %d UARTs completed\n"
175 " Crystal: %" PRIu32,
176 this->get_name(), this->children_.size(), this->crystal_);
177 if (test_mode_) {
178 ESP_LOGCONFIG(TAG,
179 " Test mode: %d\n"
180 " Transfer buffer size: %d",
182 }
183 LOG_PIN(" CS Pin: ", this->cs_);
184
185 for (auto *child : this->children_) {
186 child->dump_channel();
187 }
188}
189
190} // namespace weikai_spi
191} // namespace esphome
int test_mode_
test mode value (0 -> no tests)
Definition weikai.h:261
bool page1_
set to true when in "page1 mode"
Definition weikai.h:262
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
Definition weikai.h:263
const char * get_name()
Get the name of the component.
Definition weikai.h:215
uint8_t register_
address of the register
Definition weikai.h:185
uint8_t channel_
channel for this register
Definition weikai.h:186
WeikaiComponent *const comp_
pointer to our parent (aggregation)
Definition weikai.h:184
The WeikaiComponentSPI class stores the information to the WeiKai component connected through an SPI ...
Definition weikai_spi.h:37
weikai::WeikaiRegister & reg(uint8_t reg, uint8_t channel) override
Definition weikai_spi.h:39
void write_reg(uint8_t value) override
uint8_t read_reg() const override
void write_fifo(uint8_t *data, size_t length) 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:127
constexpr uint8_t GRST_C4RST
Channel 4 soft reset (0: not reset, 1: reset)
Definition wk_reg_def.h:56
constexpr uint8_t WKREG_GPDIR
Global GPIO direction register - 10 0001.
Definition wk_reg_def.h:85
constexpr uint8_t WKREG_GPDAT
Global GPIO data register - 11 0001.
Definition wk_reg_def.h:99
constexpr uint8_t GRST_C2RST
Channel 2 soft reset (0: not reset, 1: reset)
Definition wk_reg_def.h:60
constexpr uint8_t WKREG_GRST
Global Reset Register - 00 0001.
Definition wk_reg_def.h:54
constexpr uint8_t GRST_C3RST
Channel 3 soft reset (0: not reset, 1: reset)
Definition wk_reg_def.h:58
constexpr uint8_t GENA_C2EN
Channel 2 enable clock (0: disable, 1: enable)
Definition wk_reg_def.h:38
constexpr uint8_t GRST_C1RST
Channel 1 soft reset (0: not reset, 1: reset)
Definition wk_reg_def.h:62
constexpr uint8_t GENA_C3EN
Channel 3 enable clock (0: disable, 1: enable)
Definition wk_reg_def.h:36
constexpr uint8_t GENA_C4EN
Channel 4 enable clock (0: disable, 1: enable)
Definition wk_reg_def.h:34
constexpr uint8_t WKREG_GENA
Global Control Register - 00 0000.
Definition wk_reg_def.h:32
constexpr uint8_t GENA_C1EN
Channel 1 enable clock (0: disable, 1: enable)
Definition wk_reg_def.h:40
mopeka_std_values val[4]
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
Definition weikai.h:33
const char * reg_to_str(int reg, bool page1)
Definition weikai.cpp:63
const char * p2s(uart::UARTParityOptions parity)
Converts the parity enum value to a C string.
Definition weikai.cpp:26
void print_buffer(const uint8_t *data, size_t length)
Display a buffer in hexadecimal format (32 hex values / line) for debug.
Definition weikai.cpp:41
uint32_t elapsed_ms(uint32_t &last_time)
measure the time elapsed between two calls
Definition weikai.cpp:17
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
char * format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as binary string to buffer.
Definition helpers.cpp:426
uint16_t length
Definition tt21100.cpp:0