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