ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
weikai.cpp
Go to the documentation of this file.
1
5
6#include "weikai.h"
9
10namespace esphome::weikai {
11
12ESPHOME_LOG_TAG(TAG, "weikai");
13
18 uint32_t e = millis() - last_time;
19 last_time = millis();
20 return e;
21};
22
26const char *p2s(uart::UARTParityOptions parity) {
27 using namespace uart;
28 switch (parity) {
29 case UART_CONFIG_PARITY_NONE:
30 return "NONE";
31 case UART_CONFIG_PARITY_EVEN:
32 return "EVEN";
33 case UART_CONFIG_PARITY_ODD:
34 return "ODD";
35 default:
36 return "UNKNOWN";
37 }
38}
39
41void print_buffer(const char *tag, const uint8_t *data, size_t length, int level) {
42 char hex_buffer[100];
43 hex_buffer[(3 * 32) + 1] = 0;
44 for (size_t i = 0; i < length; i++) {
45 snprintf(&hex_buffer[3 * (i % 32)], sizeof(hex_buffer), "%02X ", data[i]);
46 if (i % 32 == 31) {
47 esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(" %s"), hex_buffer);
48 }
49 }
50 if (length % 32) {
51 // null terminate if incomplete line
52 hex_buffer[3 * (length % 32) + 2] = 0;
53 esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(" %s"), hex_buffer);
54 }
55}
56
57PROGMEM_STRING_TABLE(RegNamesPage0, "GENA", "GRST", "GMUT", "SPAGE", "SCR", "LCR", "FCR", "SIER", "SIFR", "TFCNT",
58 "RFCNT", "FSR", "LSR", "FDAT", "FWCR", "RS485");
59PROGMEM_STRING_TABLE(RegNamesPage1, "GENA", "GRST", "GMUT", "SPAGE", "BAUD1", "BAUD0", "PRES", "RFTL", "TFTL", "FWTH",
60 "FWTL", "XON1", "XOFF1", "SADR", "SAEN", "RTSDLY");
61
62const LogString *reg_to_str(int reg, bool page1) {
63 if (reg == WKREG_GPDAT) {
64 return LOG_STR("GPDAT");
65 } else if (reg == WKREG_GPDIR) {
66 return LOG_STR("GPDIR");
67 } else {
68 return page1 ? RegNamesPage1::get_log_str(reg & 0x0F, 0) : RegNamesPage0::get_log_str(reg & 0x0F, 0);
69 }
70}
71
72enum RegType { REG = 0, FIFO = 1 };
73
75// The WeikaiRegister methods
78 write_reg(value);
79 return *this;
80}
81
83 value &= read_reg();
84 write_reg(value);
85 return *this;
86}
87
89 value |= read_reg();
90 write_reg(value);
91 return *this;
92}
93
95// The WeikaiComponent methods
98 if (!this->is_in_loop_state())
99 return;
100
101 // If there are some bytes in the receive FIFO we transfers them to the ring buffers
102 size_t transferred = 0;
103 for (auto *child : this->children_) {
104 // we look if some characters has been received in the fifo
105 transferred += child->xfer_fifo_to_buffer_();
106 }
107 if (transferred > 0) {
108 ESP_LOGV(TAG, "transferred %d bytes from fifo to buffer", transferred);
109 }
110
111#ifdef TEST_COMPONENT
112 static uint32_t loop_time = 0;
113 static uint32_t loop_count = 0;
114 uint32_t time = 0;
115
116 if (test_mode_ == 1) { // test component in loopback
117 ESP_LOGI(TAG, "Component loop %" PRIu32 " for %s : %" PRIu32 " ms since last call", loop_count++, this->get_name(),
118 millis() - loop_time);
119 loop_time = millis();
120 char message[64];
121 elapsed_ms(time); // set time to now
122 for (int i = 0; i < this->children_.size(); i++) {
123 if (i != ((loop_count - 1) % this->children_.size())) // we do only one per loop
124 continue;
125 snprintf(message, sizeof(message), "%s:%s", this->get_name(), children_[i]->get_channel_name());
126 children_[i]->uart_send_test_(message);
127 uint32_t const start_time = millis();
128 while (children_[i]->tx_fifo_is_not_empty_()) { // wait until buffer empty
129 if (millis() - start_time > 1500) {
130 ESP_LOGE(TAG, "timeout while flushing - %d bytes left in buffer", children_[i]->tx_in_fifo_());
131 break;
132 }
133 yield(); // reschedule our thread to avoid blocking
134 }
135 bool status = children_[i]->uart_receive_test_(message);
136 ESP_LOGI(TAG, "Test %s => send/received %u bytes %s - execution time %" PRIu32 " ms", message, RING_BUFFER_SIZE,
137 status ? LOG_STR_LITERAL("correctly") : LOG_STR_LITERAL("with error"), elapsed_ms(time));
138 }
139 }
140
141 if (this->test_mode_ == 2) { // test component in echo mode
142 for (auto *child : this->children_) {
143 uint8_t data = 0;
144 if (child->available()) {
145 child->read_byte(&data);
146 ESP_LOGI(TAG, "echo mode: read -> send %02X", data);
147 child->write_byte(data);
148 }
149 }
150 }
151 if (test_mode_ == 3) {
153 }
154
155 if (test_mode_ == 4) {
157 }
158#endif
159}
160
161#if defined(TEST_COMPONENT)
163 static bool init_input{false};
164 static uint8_t state{0};
165 uint8_t value;
166 char bin_buf[9]; // 8 binary digits + null
167 if (!init_input) {
168 init_input = true;
169 // set all pins in input mode
170 this->reg(WKREG_GPDIR, 0) = 0x00;
171 ESP_LOGI(TAG, "initializing all pins to input mode");
172 state = this->reg(WKREG_GPDAT, 0);
173 ESP_LOGI(TAG, "initial input data state = %02X (%s)", state, format_bin_to(bin_buf, state));
174 }
175 value = this->reg(WKREG_GPDAT, 0);
176 if (value != state) {
177 ESP_LOGI(TAG, "Input data changed from %02X to %02X (%s)", state, value, format_bin_to(bin_buf, value));
178 state = value;
179 }
180}
181
183 static bool init_output{false};
184 static uint8_t state{0};
185 char bin_buf[9]; // 8 binary digits + null
186 if (!init_output) {
187 init_output = true;
188 // set all pins in output mode
189 this->reg(WKREG_GPDIR, 0) = 0xFF;
190 ESP_LOGI(TAG, "initializing all pins to output mode");
191 this->reg(WKREG_GPDAT, 0) = state;
192 ESP_LOGI(TAG, "setting all outputs to 0");
193 }
194 state = ~state;
195 this->reg(WKREG_GPDAT, 0) = state;
196 ESP_LOGI(TAG, "Flipping all outputs to %02X (%s)", state, format_bin_to(bin_buf, state));
197 delay(100); // NOLINT
198}
199#endif
200
202// The WeikaiGPIOPin methods
205 this->input_state_ = this->reg(WKREG_GPDAT, 0);
206 char bin_buf[9];
207 ESP_LOGVV(TAG, "reading input pin %u = %u in_state %s", pin, this->input_state_ & (1 << pin),
208 format_bin_to(bin_buf, this->input_state_));
209 return this->input_state_ & (1 << pin);
210}
211
212void WeikaiComponent::write_pin_val_(uint8_t pin, bool value) {
213 assign_bits(this->output_state_, 1 << pin, value);
214 char bin_buf[9];
215 ESP_LOGVV(TAG, "writing output pin %d with %d out_state %s", pin, uint8_t(value),
216 format_bin_to(bin_buf, this->output_state_));
217 this->reg(WKREG_GPDAT, 0) = this->output_state_;
218}
219
221 if (flags == gpio::FLAG_INPUT) {
222 this->pin_config_ &= ~(1 << pin); // clear bit (input mode)
223 } else {
224 if (flags == gpio::FLAG_OUTPUT) {
225 this->pin_config_ |= 1 << pin; // set bit (output mode)
226 } else {
227 ESP_LOGE(TAG, "pin %d direction invalid", pin);
228 }
229 }
230 char bin_buf[9];
231 ESP_LOGVV(TAG, "setting pin %d direction to %d pin_config=%s", pin, flags, format_bin_to(bin_buf, this->pin_config_));
232 this->reg(WKREG_GPDIR, 0) = this->pin_config_; // TODO check ~
233}
234
236 ESP_LOGCONFIG(TAG, "Setting GPIO pin %d mode to %s", this->pin_,
237 this->flags_ == gpio::FLAG_INPUT ? LOG_STR_LITERAL("Input")
238 : this->flags_ == gpio::FLAG_OUTPUT ? LOG_STR_LITERAL("Output")
239 : LOG_STR_LITERAL("NOT SPECIFIED"));
240 this->pin_mode(this->flags_);
241}
242
243size_t WeikaiGPIOPin::dump_summary(char *buffer, size_t len) const {
244 return buf_append_printf(buffer, len, 0, "%u via WeiKai %s", this->pin_, this->parent_->get_name());
245}
246
248// The WeikaiChannel methods
251 ESP_LOGCONFIG(TAG, " Setting up UART %s:%s", this->parent_->get_name(), this->get_channel_name());
252 // we enable transmit and receive on this channel
253 if (this->check_channel_down_()) {
254 ESP_LOGCONFIG(TAG, " Error channel %s not working", this->get_channel_name());
255 }
256 this->reset_fifo_();
257 this->receive_buffer_.clear();
258 this->set_line_param_();
259 this->set_baudrate_();
260}
261
263 ESP_LOGCONFIG(TAG,
264 " UART %s\n"
265 " Baud rate: %" PRIu32 " Bd\n"
266 " Data bits: %u\n"
267 " Stop bits: %u\n"
268 " Parity: %s",
269 this->get_channel_name(), this->baud_rate_, this->data_bits_, this->stop_bits_, p2s(this->parity_));
270}
271
273 // enable transmission and reception
274 this->reg(WKREG_SCR) = SCR_RXEN | SCR_TXEN;
275 // we reset and enable transmit and receive FIFO
277}
278
280 this->data_bits_ = 8; // always equal to 8 for WeiKai (cant be changed)
281 uint8_t lcr = 0;
282 if (this->stop_bits_ == 2)
283 lcr |= LCR_STPL;
284 switch (this->parity_) { // parity selection settings
286 lcr |= (LCR_PAEN | LCR_PAR_ODD);
287 break;
289 lcr |= (LCR_PAEN | LCR_PAR_EVEN);
290 break;
291 default:
292 break; // no parity 000x
293 }
294 this->reg(WKREG_LCR) = lcr; // write LCR
295 char bin_buf[9];
296 ESP_LOGV(TAG, " line config: %d data_bits, %d stop_bits, parity %s register [%s]", this->data_bits_,
297 this->stop_bits_, p2s(this->parity_), format_bin_to(bin_buf, lcr));
298}
299
301 if (this->baud_rate_ > this->parent_->crystal_ / 16) {
302 baud_rate_ = this->parent_->crystal_ / 16;
303 ESP_LOGE(TAG, " Requested baudrate too high for crystal=%" PRIu32 " Hz. Has been reduced to %" PRIu32 " Bd",
304 this->parent_->crystal_, this->baud_rate_);
305 };
306 uint16_t const val_int = this->parent_->crystal_ / (this->baud_rate_ * 16) - 1;
307 uint16_t val_dec = (this->parent_->crystal_ % (this->baud_rate_ * 16)) / (this->baud_rate_ * 16);
308 uint8_t const baud_high = (uint8_t) (val_int >> 8);
309 uint8_t const baud_low = (uint8_t) (val_int & 0xFF);
310 while (val_dec > 0x0A)
311 val_dec /= 0x0A;
312 uint8_t const baud_dec = (uint8_t) (val_dec);
313
314 this->parent_->page1_ = true; // switch to page 1
315 this->reg(WKREG_SPAGE) = 1;
316 this->reg(WKREG_BRH) = baud_high;
317 this->reg(WKREG_BRL) = baud_low;
318 this->reg(WKREG_BRD) = baud_dec;
319 this->parent_->page1_ = false; // switch back to page 0
320 this->reg(WKREG_SPAGE) = 0;
321
322 ESP_LOGV(TAG, " Crystal=%" PRId32 " baudrate=%" PRId32 " => registers [%d %d %d]", this->parent_->crystal_,
323 this->baud_rate_, baud_high, baud_low, baud_dec);
324}
325
327
329 size_t tfcnt = this->reg(WKREG_TFCNT);
330 if (tfcnt == 0) {
331 uint8_t const fsr = this->reg(WKREG_FSR);
332 if (fsr & FSR_TFFULL) {
333 char bin_buf[9];
334 ESP_LOGVV(TAG, "tx FIFO full FSR=%s", format_bin_to(bin_buf, fsr));
335 tfcnt = FIFO_SIZE;
336 }
337 }
338 ESP_LOGVV(TAG, "tx FIFO contains %d bytes", tfcnt);
339 return tfcnt;
340}
341
343 size_t available = this->reg(WKREG_RFCNT);
344 uint8_t const fsr = this->reg(WKREG_FSR);
345 if (fsr & (FSR_RFOE | FSR_RFLB | FSR_RFFE | FSR_RFPE)) {
346 char bin_buf[9];
347 if (fsr & FSR_RFOE) {
348 ESP_LOGE(TAG, "Receive data overflow FSR=%s", format_bin_to(bin_buf, fsr));
349 }
350 if (fsr & FSR_RFLB) {
351 ESP_LOGE(TAG, "Receive line break FSR=%s", format_bin_to(bin_buf, fsr));
352 }
353 if (fsr & FSR_RFFE) {
354 ESP_LOGE(TAG, "Receive frame error FSR=%s", format_bin_to(bin_buf, fsr));
355 }
356 if (fsr & FSR_RFPE) {
357 ESP_LOGE(TAG, "Receive parity error FSR=%s", format_bin_to(bin_buf, fsr));
358 }
359 }
360 if ((available == 0) && (fsr & FSR_RFDAT)) {
361 // here we should be very careful because we can have something like this:
362 // - at time t0 we read RFCNT=0 because nothing yet received
363 // - at time t0+delta we might read FIFO not empty because one byte has just been received
364 // - so to be sure we need to do another read of RFCNT and if it is still zero -> buffer full
365 available = this->reg(WKREG_RFCNT);
366 if (available == 0) { // still zero ?
367 char bin_buf[9];
368 ESP_LOGV(TAG, "rx FIFO is full FSR=%s", format_bin_to(bin_buf, fsr));
370 }
371 }
372 char bin_buf2[9];
373 ESP_LOGVV(TAG, "rx FIFO contain %d bytes - FSR status=%s", available, format_bin_to(bin_buf2, fsr));
374 return available;
375}
376
378 // to check if we channel is up we write to the LCR W/R register
379 // note that this will put a break on the tx line for few ms
380 WeikaiRegister &lcr = this->reg(WKREG_LCR);
381 lcr = 0x3F;
382 uint8_t val = lcr;
383 if (val != 0x3F) {
384 ESP_LOGE(TAG, "R/W of register failed expected 0x3F received 0x%02X", val);
385 return true;
386 }
387 lcr = 0;
388 val = lcr;
389 if (val != 0x00) {
390 ESP_LOGE(TAG, "R/W of register failed expected 0x00 received 0x%02X", val);
391 return true;
392 }
393 return false;
394}
395
397 if (this->receive_buffer_.empty() && this->xfer_fifo_to_buffer_() == 0)
398 return false;
399 *buffer = this->receive_buffer_.front();
400 return true;
401}
402
404 size_t available = this->receive_buffer_.size();
405 if (!available)
407 return available;
408}
409
411 bool status = true;
412 size_t available = this->receive_buffer_.size();
413 if (length > available) {
414 ESP_LOGW(TAG, "read_array: buffer underflow requested %d bytes only %d bytes available", length, available);
416 status = false;
417 }
418 // retrieve the bytes from ring buffer
419 for (size_t i = 0; i < length; i++) {
420 buffer[i] = this->receive_buffer_.front();
421 this->receive_buffer_.pop();
422 }
423 ESP_LOGVV(TAG, "read_array(ch=%d buffer[0]=%02X, length=%d): status %s", this->channel_, *buffer, length,
424 status ? LOG_STR_LITERAL("OK") : LOG_STR_LITERAL("ERROR"));
425 return status;
426}
427
428void WeikaiChannel::write_array(const uint8_t *buffer, size_t length) {
429 if (length > XFER_MAX_SIZE) {
430 ESP_LOGE(TAG, "Write_array: invalid call - requested %d bytes but max size %d", length, XFER_MAX_SIZE);
432 }
433 this->reg(0).write_fifo(const_cast<uint8_t *>(buffer), length);
434}
435
437 uint32_t const start_time = millis();
438 while (this->tx_fifo_is_not_empty_()) { // wait until buffer empty
439 if (millis() - start_time > 200) {
440 ESP_LOGW(TAG, "WARNING flush timeout - still %d bytes not sent after 200 ms", this->tx_in_fifo_());
442 }
443 yield(); // reschedule our thread to avoid blocking
444 }
446}
447
449 size_t total = 0;
450 while (true) {
451 size_t to_transfer = this->rx_in_fifo_();
452 size_t free = RING_BUFFER_SIZE - this->receive_buffer_.size();
453 if (to_transfer == 0 || free == 0) {
454 break;
455 }
456 // while bytes in fifo and some room in the buffer we transfer
457 if (to_transfer > XFER_MAX_SIZE)
458 to_transfer = XFER_MAX_SIZE; // we can only do so much
459 if (to_transfer > free)
460 to_transfer = free; // we'll do the rest next time
461 if (to_transfer) {
462 uint8_t data[to_transfer];
463 this->reg(0).read_fifo(data, to_transfer);
464 for (size_t i = 0; i < to_transfer; i++)
465 this->receive_buffer_.push(data[i]);
466 total += to_transfer;
467 }
468 } // while work to do
469 return total;
470}
471
473// TEST COMPONENT
474//
475#ifdef TEST_COMPONENT
478
482class Increment {
483 public:
485 Increment() : i_(0) {}
489 uint8_t operator()() { return i_++; }
490
491 private:
492 uint8_t i_;
493};
494
497 auto start_exec = micros();
498 std::vector<uint8_t> output_buffer(XFER_MAX_SIZE);
499 generate(output_buffer.begin(), output_buffer.end(), Increment()); // fill with incrementing number
500 size_t to_send = RING_BUFFER_SIZE;
501 while (to_send) {
502 this->write_array(&output_buffer[0], XFER_MAX_SIZE); // we send the buffer
503 this->flush();
504 to_send -= XFER_MAX_SIZE;
505 }
506 ESP_LOGV(TAG, "%s => sent %d bytes - exec time %d µs", message, RING_BUFFER_SIZE, micros() - start_exec);
507}
508
511 auto start_exec = micros();
512 bool status = true;
513 size_t received = 0;
514 std::vector<uint8_t> buffer(RING_BUFFER_SIZE);
515
516 // we wait until we have received all the bytes
517 uint32_t const start_time = millis();
518 status = true;
519 while (received < RING_BUFFER_SIZE) {
520 while (XFER_MAX_SIZE > this->available()) {
521 this->xfer_fifo_to_buffer_();
522 if (millis() - start_time > 1500) {
523 ESP_LOGE(TAG, "uart_receive_test_() timeout: only %d bytes received", this->available());
524 break;
525 }
526 yield(); // reschedule our thread to avoid blocking
527 }
528 status = this->read_array(&buffer[received], XFER_MAX_SIZE) && status;
529 received += XFER_MAX_SIZE;
530 }
531
532 uint8_t peek_value = 0;
533 this->peek_byte(&peek_value);
534 if (peek_value != 0) {
535 ESP_LOGE(TAG, "Peek first byte value error");
536 status = false;
537 }
538
539 for (size_t i = 0; i < RING_BUFFER_SIZE; i++) {
540 if (buffer[i] != i % XFER_MAX_SIZE) {
541 ESP_LOGE(TAG, "Read buffer contains error b=%x i=%x", buffer[i], i % XFER_MAX_SIZE);
542 print_buffer(TAG, buffer.data(), buffer.size(), ESPHOME_LOG_LEVEL_INFO);
543 status = false;
544 break;
545 }
546 }
547
548 ESP_LOGV(TAG, "%s => received %d bytes status %s - exec time %d µs", message, received,
549 status ? LOG_STR_LITERAL("OK") : LOG_STR_LITERAL("ERROR"), micros() - start_exec);
550 return status;
551}
552
554#endif
555
556} // namespace esphome::weikai
uint8_t status
Definition bl0942.h:8
bool is_in_loop_state() const
Check if this component has completed setup and is in the loop state.
Definition component.h:207
bool push(const T &value)
Definition helpers.h:424
index_type size() const
Definition helpers.h:443
void clear()
Clear all elements (reset to empty)
Definition helpers.h:447
size_t available() override
Returns the number of bytes in the receive buffer.
Definition weikai.cpp:403
void dump_channel()
dump channel information
Definition weikai.cpp:262
void setup_channel()
Setup the channel.
Definition weikai.cpp:250
WeikaiRegister & reg(uint8_t reg)
Factory method to create a WeikaiRegister proxy object.
Definition weikai.h:255
void set_line_param_()
set the line parameters
Definition weikai.cpp:279
void set_baudrate_()
set the baud rate
Definition weikai.cpp:300
bool check_channel_down_()
check if channel is alive
Definition weikai.cpp:377
bool peek_byte(uint8_t *buffer) override
Reads the first byte in FIFO without removing it.
Definition weikai.cpp:396
size_t xfer_fifo_to_buffer_()
transfer bytes from the weikai internal FIFO to the buffer (if any)
Definition weikai.cpp:448
uart::UARTFlushResult flush() override
Flush the output fifo.
Definition weikai.cpp:436
const char * get_channel_name()
Get the channel name.
Definition weikai.h:244
WeikaiComponent * parent_
our WK2168component parent
Definition weikai.h:374
bool read_array(uint8_t *buffer, size_t length) override
Reads a specified number of bytes from a serial port.
Definition weikai.cpp:410
void reset_fifo_()
reset the weikai internal FIFO
Definition weikai.cpp:272
bool tx_fifo_is_not_empty_()
test if transmit buffer is not empty in the status register (optimization)
Definition weikai.cpp:326
StaticRingBuffer< uint8_t, RING_BUFFER_SIZE > receive_buffer_
the buffer where we store temporarily the bytes received
Definition weikai.h:373
uint8_t channel_
our Channel number
Definition weikai.h:375
size_t rx_in_fifo_()
Returns the number of bytes in the receive fifo.
Definition weikai.cpp:342
size_t tx_in_fifo_()
Returns the number of bytes in the transmit fifo.
Definition weikai.cpp:328
void write_array(const uint8_t *buffer, size_t length) override
Writes a specified number of bytes to a serial port.
Definition weikai.cpp:428
int test_mode_
test mode value (0 -> no tests)
Definition weikai.h:189
virtual WeikaiRegister & reg(uint8_t reg, uint8_t channel)=0
Factory method to create a Register object.
uint8_t input_state_
input pin states: 1 means HIGH, 0 means LOW
Definition weikai.h:187
uint32_t crystal_
crystal value;
Definition weikai.h:188
void write_pin_val_(uint8_t pin, bool value)
Helper method to write the value of a pin.
Definition weikai.cpp:212
bool read_pin_val_(uint8_t pin)
Helper method to read the value of a pin.
Definition weikai.cpp:204
uint8_t output_state_
output state: 1 means HIGH, 0 means LOW
Definition weikai.h:186
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
void loop() override
override the Component loop()
Definition weikai.cpp:97
void set_pin_direction_(uint8_t pin, gpio::Flags flags)
Helper method to set the pin mode of a pin.
Definition weikai.cpp:220
uint8_t pin_config_
pin config mask: 1 means OUTPUT, 0 means INPUT
Definition weikai.h:185
const char * get_name()
Get the name of the component.
Definition weikai.h:143
void pin_mode(gpio::Flags flags) override
Definition weikai.h:209
size_t dump_summary(char *buffer, size_t len) const override
Definition weikai.cpp:243
WeikaiComponent * parent_
Definition weikai.h:214
WeikaiRegister objects acts as proxies to access remote register independently of the bus type.
Definition weikai.h:66
virtual void write_reg(uint8_t value)=0
writes the register
WeikaiRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition weikai.cpp:88
WeikaiRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition weikai.cpp:82
virtual void write_fifo(uint8_t *data, size_t length)=0
write an array of bytes to the transmitter fifo
WeikaiRegister & operator=(uint8_t value)
overloads the = operator.
Definition weikai.cpp:77
virtual uint8_t read_reg() const =0
reads the register
virtual void read_fifo(uint8_t *data, size_t length) const =0
read an array of bytes from the receiver fifo
const LogString * message
Definition component.cpp:35
void yield(void)
uint16_t flags
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
constexpr uint8_t FSR_TFDAT
Transmitter FIFO count (0: empty, 1: not empty)
Definition wk_reg_def.h:252
constexpr uint8_t FCR_RFRST
Receiver FIFO reset.
Definition wk_reg_def.h:192
constexpr uint8_t FSR_RFPE
Receiver Parity Error (0: no PE, 1: PE)
Definition wk_reg_def.h:248
constexpr uint8_t WKREG_RFCNT
Receiver FIFO count - c0/c1 1010.
Definition wk_reg_def.h:218
constexpr uint8_t SCR_RXEN
receiving control (0: enable, 1: disable)
Definition wk_reg_def.h:144
constexpr uint8_t WKREG_FCR
FIFO Control Register - c0/c1 0110.
Definition wk_reg_def.h:184
constexpr uint8_t LCR_STPL
Stop length (0: 1 bit, 1: 2 bits)
Definition wk_reg_def.h:170
constexpr uint8_t FCR_TFRST
Transmitter FIFO reset.
Definition wk_reg_def.h:190
constexpr uint8_t SCR_TXEN
transmission control (0: enable, 1: disable)
Definition wk_reg_def.h:142
constexpr uint8_t FCR_TFEN
Transmitter FIFO enable.
Definition wk_reg_def.h:186
constexpr uint8_t FSR_RFOE
Receiver FIFO Overflow Error (0: no OE, 1: OE)
Definition wk_reg_def.h:242
constexpr uint8_t WKREG_FSR
FIFO Status Register - c0/c1 1011.
Definition wk_reg_def.h:240
constexpr uint8_t FCR_RFEN
Receiver FIFO enable.
Definition wk_reg_def.h:188
constexpr uint8_t FSR_RFFE
Receiver FIFO Frame Error (0: no FE, 1: FE)
Definition wk_reg_def.h:246
constexpr uint8_t LCR_PAR_ODD
Parity odd.
Definition wk_reg_def.h:164
constexpr uint8_t FSR_RFDAT
Receiver FIFO count (0: empty, 1: not empty)
Definition wk_reg_def.h:250
constexpr uint8_t WKREG_TFCNT
Transmitter FIFO Count - c0/c1 1001.
Definition wk_reg_def.h:208
constexpr uint8_t FSR_TFFULL
Transmitter FIFO full (0: not full, 1: full)
Definition wk_reg_def.h:254
constexpr uint8_t WKREG_LCR
Line Configuration Register - c0/c1 0101.
Definition wk_reg_def.h:158
constexpr uint8_t WKREG_SCR
Serial Control Register - c0/c1 0100.
Definition wk_reg_def.h:140
constexpr uint8_t FSR_RFLB
Receiver FIFO Line Break (0: no LB, 1: LB)
Definition wk_reg_def.h:244
constexpr uint8_t LCR_PAR_EVEN
Parity even.
Definition wk_reg_def.h:166
constexpr uint8_t LCR_PAEN
Parity enable (0: no check, 1: check)
Definition wk_reg_def.h:160
constexpr uint8_t WKREG_SPAGE
Global Page register c0/c1 0011.
Definition wk_reg_def.h:126
constexpr uint8_t WKREG_BRH
Baud rate configuration register: high byte - c0/c1 0100.
Definition wk_reg_def.h:279
constexpr uint8_t WKREG_BRL
Baud rate configuration register: low byte - c0/c1 0101.
Definition wk_reg_def.h:289
constexpr uint8_t WKREG_BRD
Baud rate configuration register decimal part - c0/c1 0110.
Definition wk_reg_def.h:292
bool uart_receive_test_(char *message)
Test the read_array() method.
Definition weikai.cpp:510
void uart_send_test_(char *message)
Test the write_array() method.
Definition weikai.cpp:496
constexpr uint8_t WKREG_GPDIR
Global GPIO direction register - 10 0001.
Definition wk_reg_def.h:84
constexpr uint8_t WKREG_GPDAT
Global GPIO data register - 11 0001.
Definition wk_reg_def.h:98
mopeka_std_values val[3]
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_SUCCESS
Confirmed: all bytes left the TX FIFO.
@ UART_FLUSH_RESULT_TIMEOUT
Confirmed: timed out before TX completed.
When the TEST_COMPONENT flag is defined we include some auto-test methods.
Definition weikai.cpp:10
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
constexpr size_t RING_BUFFER_SIZE
size of the ring buffer set to size of the FIFO
Definition weikai.h:42
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
const char * p2s(uart::UARTParityOptions parity)
Converts the parity enum value to a C string.
Definition weikai.cpp:26
PROGMEM_STRING_TABLE(RegNamesPage0, "GENA", "GRST", "GMUT", "SPAGE", "SCR", "LCR", "FCR", "SIER", "SIFR", "TFCNT", "RFCNT", "FSR", "LSR", "FDAT", "FWCR", "RS485")
constexpr size_t FIFO_SIZE
size of the internal WeiKai FIFO
Definition weikai.h:39
ESPHOME_LOG_TAG(TAG, "weikai")
uint32_t elapsed_ms(uint32_t &last_time)
measure the time elapsed between two calls
Definition weikai.cpp:17
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
Definition log.cpp:21
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:1072
const void size_t len
Definition hal.h:68
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.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:533
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
WeiKai component family - classes declaration.