ESPHome 2026.5.0
Loading...
Searching...
No Matches
weikai.cpp
Go to the documentation of this file.
1
5
6#include "weikai.h"
8
9namespace esphome::weikai {
10
11static const char *const TAG = "weikai";
12
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 };
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 ? "correctly" : "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 if (value) {
214 this->output_state_ |= (1 << pin);
215 } else {
216 this->output_state_ &= ~(1 << pin);
217 }
218 char bin_buf[9];
219 ESP_LOGVV(TAG, "writing output pin %d with %d out_state %s", pin, uint8_t(value),
220 format_bin_to(bin_buf, this->output_state_));
221 this->reg(WKREG_GPDAT, 0) = this->output_state_;
222}
223
225 if (flags == gpio::FLAG_INPUT) {
226 this->pin_config_ &= ~(1 << pin); // clear bit (input mode)
227 } else {
228 if (flags == gpio::FLAG_OUTPUT) {
229 this->pin_config_ |= 1 << pin; // set bit (output mode)
230 } else {
231 ESP_LOGE(TAG, "pin %d direction invalid", pin);
232 }
233 }
234 char bin_buf[9];
235 ESP_LOGVV(TAG, "setting pin %d direction to %d pin_config=%s", pin, flags, format_bin_to(bin_buf, this->pin_config_));
236 this->reg(WKREG_GPDIR, 0) = this->pin_config_; // TODO check ~
237}
238
240 ESP_LOGCONFIG(TAG, "Setting GPIO pin %d mode to %s", this->pin_,
241 flags_ == gpio::FLAG_INPUT ? "Input"
242 : this->flags_ == gpio::FLAG_OUTPUT ? "Output"
243 : "NOT SPECIFIED");
244 this->pin_mode(this->flags_);
245}
246
247size_t WeikaiGPIOPin::dump_summary(char *buffer, size_t len) const {
248 return buf_append_printf(buffer, len, 0, "%u via WeiKai %s", this->pin_, this->parent_->get_name());
249}
250
252// The WeikaiChannel methods
255 ESP_LOGCONFIG(TAG, " Setting up UART %s:%s", this->parent_->get_name(), this->get_channel_name());
256 // we enable transmit and receive on this channel
257 if (this->check_channel_down()) {
258 ESP_LOGCONFIG(TAG, " Error channel %s not working", this->get_channel_name());
259 }
260 this->reset_fifo_();
261 this->receive_buffer_.clear();
262 this->set_line_param_();
263 this->set_baudrate_();
264}
265
267 ESP_LOGCONFIG(TAG,
268 " UART %s\n"
269 " Baud rate: %" PRIu32 " Bd\n"
270 " Data bits: %u\n"
271 " Stop bits: %u\n"
272 " Parity: %s",
273 this->get_channel_name(), this->baud_rate_, this->data_bits_, this->stop_bits_, p2s(this->parity_));
274}
275
277 // enable transmission and reception
278 this->reg(WKREG_SCR) = SCR_RXEN | SCR_TXEN;
279 // we reset and enable transmit and receive FIFO
281}
282
284 this->data_bits_ = 8; // always equal to 8 for WeiKai (cant be changed)
285 uint8_t lcr = 0;
286 if (this->stop_bits_ == 2)
287 lcr |= LCR_STPL;
288 switch (this->parity_) { // parity selection settings
290 lcr |= (LCR_PAEN | LCR_PAR_ODD);
291 break;
293 lcr |= (LCR_PAEN | LCR_PAR_EVEN);
294 break;
295 default:
296 break; // no parity 000x
297 }
298 this->reg(WKREG_LCR) = lcr; // write LCR
299 char bin_buf[9];
300 ESP_LOGV(TAG, " line config: %d data_bits, %d stop_bits, parity %s register [%s]", this->data_bits_,
301 this->stop_bits_, p2s(this->parity_), format_bin_to(bin_buf, lcr));
302}
303
305 if (this->baud_rate_ > this->parent_->crystal_ / 16) {
306 baud_rate_ = this->parent_->crystal_ / 16;
307 ESP_LOGE(TAG, " Requested baudrate too high for crystal=%" PRIu32 " Hz. Has been reduced to %" PRIu32 " Bd",
308 this->parent_->crystal_, this->baud_rate_);
309 };
310 uint16_t const val_int = this->parent_->crystal_ / (this->baud_rate_ * 16) - 1;
311 uint16_t val_dec = (this->parent_->crystal_ % (this->baud_rate_ * 16)) / (this->baud_rate_ * 16);
312 uint8_t const baud_high = (uint8_t) (val_int >> 8);
313 uint8_t const baud_low = (uint8_t) (val_int & 0xFF);
314 while (val_dec > 0x0A)
315 val_dec /= 0x0A;
316 uint8_t const baud_dec = (uint8_t) (val_dec);
317
318 this->parent_->page1_ = true; // switch to page 1
319 this->reg(WKREG_SPAGE) = 1;
320 this->reg(WKREG_BRH) = baud_high;
321 this->reg(WKREG_BRL) = baud_low;
322 this->reg(WKREG_BRD) = baud_dec;
323 this->parent_->page1_ = false; // switch back to page 0
324 this->reg(WKREG_SPAGE) = 0;
325
326 ESP_LOGV(TAG, " Crystal=%" PRId32 " baudrate=%" PRId32 " => registers [%d %d %d]", this->parent_->crystal_,
327 this->baud_rate_, baud_high, baud_low, baud_dec);
328}
329
331
333 size_t tfcnt = this->reg(WKREG_TFCNT);
334 if (tfcnt == 0) {
335 uint8_t const fsr = this->reg(WKREG_FSR);
336 if (fsr & FSR_TFFULL) {
337 char bin_buf[9];
338 ESP_LOGVV(TAG, "tx FIFO full FSR=%s", format_bin_to(bin_buf, fsr));
339 tfcnt = FIFO_SIZE;
340 }
341 }
342 ESP_LOGVV(TAG, "tx FIFO contains %d bytes", tfcnt);
343 return tfcnt;
344}
345
347 size_t available = this->reg(WKREG_RFCNT);
348 uint8_t const fsr = this->reg(WKREG_FSR);
349 if (fsr & (FSR_RFOE | FSR_RFLB | FSR_RFFE | FSR_RFPE)) {
350 char bin_buf[9];
351 if (fsr & FSR_RFOE)
352 ESP_LOGE(TAG, "Receive data overflow FSR=%s", format_bin_to(bin_buf, fsr));
353 if (fsr & FSR_RFLB)
354 ESP_LOGE(TAG, "Receive line break FSR=%s", format_bin_to(bin_buf, fsr));
355 if (fsr & FSR_RFFE)
356 ESP_LOGE(TAG, "Receive frame error FSR=%s", format_bin_to(bin_buf, fsr));
357 if (fsr & FSR_RFPE)
358 ESP_LOGE(TAG, "Receive parity error FSR=%s", format_bin_to(bin_buf, fsr));
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
396bool WeikaiChannel::peek_byte(uint8_t *buffer) {
397 auto available = this->receive_buffer_.count();
398 if (!available)
400 return this->receive_buffer_.peek(*buffer);
401}
402
404 size_t available = this->receive_buffer_.count();
405 if (!available)
407 return available;
408}
409
410bool WeikaiChannel::read_array(uint8_t *buffer, size_t length) {
411 bool status = true;
412 auto available = this->receive_buffer_.count();
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 this->receive_buffer_.pop(buffer[i]);
421 }
422 ESP_LOGVV(TAG, "read_array(ch=%d buffer[0]=%02X, length=%d): status %s", this->channel_, *buffer, length,
423 status ? "OK" : "ERROR");
424 return status;
425}
426
427void WeikaiChannel::write_array(const uint8_t *buffer, size_t length) {
428 if (length > XFER_MAX_SIZE) {
429 ESP_LOGE(TAG, "Write_array: invalid call - requested %d bytes but max size %d", length, XFER_MAX_SIZE);
431 }
432 this->reg(0).write_fifo(const_cast<uint8_t *>(buffer), length);
433}
434
436 uint32_t const start_time = millis();
437 while (this->tx_fifo_is_not_empty_()) { // wait until buffer empty
438 if (millis() - start_time > 200) {
439 ESP_LOGW(TAG, "WARNING flush timeout - still %d bytes not sent after 200 ms", this->tx_in_fifo_());
441 }
442 yield(); // reschedule our thread to avoid blocking
443 }
445}
446
448 size_t total = 0;
449 size_t to_transfer;
450 size_t free;
451 while ((to_transfer = this->rx_in_fifo_()) && (free = this->receive_buffer_.free())) {
452 // while bytes in fifo and some room in the buffer we transfer
453 if (to_transfer > XFER_MAX_SIZE)
454 to_transfer = XFER_MAX_SIZE; // we can only do so much
455 if (to_transfer > free)
456 to_transfer = free; // we'll do the rest next time
457 if (to_transfer) {
458 uint8_t data[to_transfer];
459 this->reg(0).read_fifo(data, to_transfer);
460 for (size_t i = 0; i < to_transfer; i++)
461 this->receive_buffer_.push(data[i]);
462 total += to_transfer;
463 }
464 } // while work to do
465 return total;
466}
467
469// TEST COMPONENT
470//
471#ifdef TEST_COMPONENT
474
478class Increment {
479 public:
481 Increment() : i_(0) {}
485 uint8_t operator()() { return i_++; }
486
487 private:
488 uint8_t i_;
489};
490
493void print_buffer(std::vector<uint8_t> buffer) {
494 char hex_buffer[100];
495 hex_buffer[(3 * 32) + 1] = 0;
496 for (size_t i = 0; i < buffer.size(); i++) {
497 snprintf(&hex_buffer[3 * (i % 32)], sizeof(hex_buffer), "%02X ", buffer[i]);
498 if (i % 32 == 31)
499 ESP_LOGI(TAG, " %s", hex_buffer);
500 }
501 if (buffer.size() % 32) {
502 // null terminate if incomplete line
503 hex_buffer[3 * (buffer.size() % 32) + 1] = 0;
504 ESP_LOGI(TAG, " %s", hex_buffer);
505 }
506}
507
510 auto start_exec = micros();
511 std::vector<uint8_t> output_buffer(XFER_MAX_SIZE);
512 generate(output_buffer.begin(), output_buffer.end(), Increment()); // fill with incrementing number
513 size_t to_send = RING_BUFFER_SIZE;
514 while (to_send) {
515 this->write_array(&output_buffer[0], XFER_MAX_SIZE); // we send the buffer
516 this->flush();
517 to_send -= XFER_MAX_SIZE;
518 }
519 ESP_LOGV(TAG, "%s => sent %d bytes - exec time %d µs", message, RING_BUFFER_SIZE, micros() - start_exec);
520}
521
524 auto start_exec = micros();
525 bool status = true;
526 size_t received = 0;
527 std::vector<uint8_t> buffer(RING_BUFFER_SIZE);
528
529 // we wait until we have received all the bytes
530 uint32_t const start_time = millis();
531 status = true;
532 while (received < RING_BUFFER_SIZE) {
533 while (XFER_MAX_SIZE > this->available()) {
534 this->xfer_fifo_to_buffer_();
535 if (millis() - start_time > 1500) {
536 ESP_LOGE(TAG, "uart_receive_test_() timeout: only %d bytes received", this->available());
537 break;
538 }
539 yield(); // reschedule our thread to avoid blocking
540 }
541 status = this->read_array(&buffer[received], XFER_MAX_SIZE) && status;
542 received += XFER_MAX_SIZE;
543 }
544
545 uint8_t peek_value = 0;
546 this->peek_byte(&peek_value);
547 if (peek_value != 0) {
548 ESP_LOGE(TAG, "Peek first byte value error");
549 status = false;
550 }
551
552 for (size_t i = 0; i < RING_BUFFER_SIZE; i++) {
553 if (buffer[i] != i % XFER_MAX_SIZE) {
554 ESP_LOGE(TAG, "Read buffer contains error b=%x i=%x", buffer[i], i % XFER_MAX_SIZE);
555 print_buffer(buffer);
556 status = false;
557 break;
558 }
559 }
560
561 ESP_LOGV(TAG, "%s => received %d bytes status %s - exec time %d µs", message, received, status ? "OK" : "ERROR",
562 micros() - start_exec);
563 return status;
564}
565
567#endif
568
569} // 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:205
size_t free()
returns the number of free positions in the buffer
Definition weikai.h:109
bool push(const T item)
pushes an item at the tail of the fifo
Definition weikai.h:64
bool peek(T &item)
return the value of the item at fifo's head without removing it
Definition weikai.h:88
void clear()
clear the buffer content
Definition weikai.h:112
bool pop(T &item)
return and remove the item at head of the fifo
Definition weikai.h:76
size_t count()
return the number of item in the ring buffer
Definition weikai.h:105
size_t available() override
Returns the number of bytes in the receive buffer.
Definition weikai.cpp:403
virtual void dump_channel()
dump channel information
Definition weikai.cpp:266
virtual void setup_channel()
Setup the channel.
Definition weikai.cpp:254
WeikaiRegister & reg(uint8_t reg)
Factory method to create a WeikaiRegister proxy object.
Definition weikai.h:326
void set_line_param_()
set the line parameters
Definition weikai.cpp:283
void set_baudrate_()
set the baud rate
Definition weikai.cpp:304
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:447
virtual bool check_channel_down()
check if channel is alive
Definition weikai.cpp:377
uart::UARTFlushResult flush() override
Flush the output fifo.
Definition weikai.cpp:435
const char * get_channel_name()
Get the channel name.
Definition weikai.h:315
WeikaiComponent * parent_
our WK2168component parent
Definition weikai.h:436
WKRingBuffer< uint8_t, RING_BUFFER_SIZE > receive_buffer_
the buffer where we store temporarily the bytes received
Definition weikai.h:435
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:276
bool tx_fifo_is_not_empty_()
test if transmit buffer is not empty in the status register (optimization)
Definition weikai.cpp:330
uint8_t channel_
our Channel number
Definition weikai.h:437
size_t rx_in_fifo_()
Returns the number of bytes in the receive fifo.
Definition weikai.cpp:346
size_t tx_in_fifo_()
Returns the number of bytes in the transmit fifo.
Definition weikai.cpp:332
void write_array(const uint8_t *buffer, size_t length) override
Writes a specified number of bytes to a serial port.
Definition weikai.cpp:427
int test_mode_
test mode value (0 -> no tests)
Definition weikai.h:260
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:258
uint32_t crystal_
crystal value;
Definition weikai.h:259
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:257
bool page1_
set to true when in "page1 mode"
Definition weikai.h:261
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
Definition weikai.h:262
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:224
uint8_t pin_config_
pin config mask: 1 means OUTPUT, 0 means INPUT
Definition weikai.h:256
const char * get_name()
Get the name of the component.
Definition weikai.h:214
void pin_mode(gpio::Flags flags) override
Definition weikai.h:280
size_t dump_summary(char *buffer, size_t len) const override
Definition weikai.cpp:247
WeikaiComponent * parent_
Definition weikai.h:285
WeikaiRegister objects acts as proxies to access remote register independently of the bus type.
Definition weikai.h:137
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 char * message
Definition component.cpp:35
void yield(void)
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: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:523
void uart_send_test_(char *message)
Test the write_array() method.
Definition weikai.cpp:509
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]
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_INPUT
Definition gpio.h:27
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:9
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
Definition weikai.h:32
constexpr size_t RING_BUFFER_SIZE
size of the ring buffer set to size of the FIFO
Definition weikai.h:41
const char * reg_to_str(int reg, bool page1)
Definition weikai.cpp:62
const char * p2s(uart::UARTParityOptions parity)
Converts the parity enum value to a C string.
Definition weikai.cpp:25
constexpr size_t FIFO_SIZE
size of the internal WeiKai FIFO
Definition weikai.h:38
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:40
uint32_t elapsed_ms(uint32_t &last_time)
measure the time elapsed between two calls
Definition weikai.cpp:16
std::string size_t len
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
void HOT delay(uint32_t ms)
Definition hal.cpp:82
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
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:378
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
WeiKai component family - classes declaration.