ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
weikai_spi.cpp
Go to the documentation of this file.
1
5
6#include "weikai_spi.h"
7
9using namespace weikai;
10ESPHOME_LOG_TAG(TAG, "weikai_spi");
11
16 uint32_t e = millis() - last_time;
17 last_time = millis();
18 return e;
19};
20
24const char *p2s(uart::UARTParityOptions parity) {
25 using namespace uart;
26 switch (parity) {
27 case UART_CONFIG_PARITY_NONE:
28 return "NONE";
29 case UART_CONFIG_PARITY_EVEN:
30 return "EVEN";
31 case UART_CONFIG_PARITY_ODD:
32 return "ODD";
33 default:
34 return "UNKNOWN";
35 }
36}
37
38enum RegType { REG = 0, FIFO = 1 };
39enum CmdType { WRITE_CMD = 0, READ_CMD = 1 };
40
55inline static uint8_t cmd_byte(RegType fifo, CmdType transfer_type, uint8_t channel, uint8_t reg) {
56 return (fifo << 7 | transfer_type << 6 | channel << 4 | reg << 0);
57}
58
60// The WeikaiRegisterSPI methods
63 auto *spi_comp = static_cast<WeikaiComponentSPI *>(this->comp_);
64 uint8_t cmd = cmd_byte(REG, READ_CMD, this->channel_, this->register_);
65 spi_comp->enable();
66 spi_comp->write_byte(cmd);
67 uint8_t val = spi_comp->read_byte();
68 spi_comp->disable();
69 char bin_buf[9];
70 ESP_LOGVV(TAG, "WeikaiRegisterSPI::read_reg() cmd=%s(%02X) reg=%s ch=%d buf=%02X", format_bin_to(bin_buf, cmd), cmd,
71 LOG_STR_ARG(reg_to_str(this->register_, this->comp_->page1())), this->channel_, val);
72 return val;
73}
74
75void WeikaiRegisterSPI::read_fifo(uint8_t *data, size_t length) const {
76 auto *spi_comp = static_cast<WeikaiComponentSPI *>(this->comp_);
77 uint8_t cmd = cmd_byte(FIFO, READ_CMD, this->channel_, this->register_);
78 spi_comp->enable();
79 spi_comp->write_byte(cmd);
80 spi_comp->read_array(data, length);
81 spi_comp->disable();
82#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
83 char bin_buf[9];
84 ESP_LOGVV(TAG, "WeikaiRegisterSPI::read_fifo() cmd=%s(%02X) ch=%d len=%d buffer", format_bin_to(bin_buf, cmd), cmd,
85 this->channel_, length);
86 print_buffer(TAG, data, length);
87#endif
88}
89
90void WeikaiRegisterSPI::write_reg(uint8_t value) {
91 auto *spi_comp = static_cast<WeikaiComponentSPI *>(this->comp_);
92 uint8_t buf[2]{cmd_byte(REG, WRITE_CMD, this->channel_, this->register_), value};
93 spi_comp->enable();
94 spi_comp->write_array(buf, 2);
95 spi_comp->disable();
96 char bin_buf[9];
97 ESP_LOGVV(TAG, "WeikaiRegisterSPI::write_reg() cmd=%s(%02X) reg=%s ch=%d buf=%02X", format_bin_to(bin_buf, buf[0]),
98 buf[0], LOG_STR_ARG(reg_to_str(this->register_, this->comp_->page1())), this->channel_, buf[1]);
99}
100
101void WeikaiRegisterSPI::write_fifo(uint8_t *data, size_t length) {
102 auto *spi_comp = static_cast<WeikaiComponentSPI *>(this->comp_);
103 uint8_t cmd = cmd_byte(FIFO, WRITE_CMD, this->channel_, this->register_);
104 spi_comp->enable();
105 spi_comp->write_byte(cmd);
106 spi_comp->write_array(data, length);
107 spi_comp->disable();
108
109#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
110 char bin_buf[9];
111 ESP_LOGVV(TAG, "WeikaiRegisterSPI::write_fifo() cmd=%s(%02X) ch=%d len=%d buffer", format_bin_to(bin_buf, cmd), cmd,
112 this->channel_, length);
113 print_buffer(TAG, data, length);
114#endif
115}
116
118// The WeikaiComponentSPI methods
121 using namespace weikai;
122 ESP_LOGCONFIG(TAG, "Setup %s (%d UARTs)", this->get_name(), this->children_.size());
123 this->spi_setup();
124 // enable all channels
126 // reset all channels
128 // initialize the spage register to page 0
129 this->reg(WKREG_SPAGE, 0) = 0;
130 this->page1_ = false;
131
132 // we setup our children channels
133 for (auto *child : this->children_) {
134 child->setup_channel();
135 }
136}
137
139 ESP_LOGCONFIG(TAG,
140 "Initialization of %s with %d UARTs completed\n"
141 " Crystal: %" PRIu32,
142 this->get_name(), this->children_.size(), this->crystal_);
143 if (test_mode_) {
144 ESP_LOGCONFIG(TAG,
145 " Test mode: %d\n"
146 " Transfer buffer size: %d",
148 }
149 LOG_PIN(" CS Pin: ", this->cs_);
150
151 for (auto *child : this->children_) {
152 child->dump_channel();
153 }
154}
155
156} // namespace esphome::weikai_spi
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 WeikaiComponentSPI class stores the information to the WeiKai component connected through an SPI ...
Definition weikai_spi.h:36
weikai::WeikaiRegister & reg(uint8_t reg, uint8_t channel) override
Definition weikai_spi.h:38
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: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
mopeka_std_values val[3]
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
const char * p2s(uart::UARTParityOptions parity)
Converts the parity enum value to a C string.
Definition weikai.cpp:26
ESPHOME_LOG_TAG(TAG, "weikai")
uint32_t elapsed_ms(uint32_t &last_time)
measure the time elapsed between two calls
Definition weikai.cpp:17
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