10using namespace weikai;
11static const char *
const TAG =
"weikai_spi";
16inline std::string
i2s(uint8_t
val) {
return std::bitset<8>(
val).to_string(); }
18#define I2S2CS(val) (i2s(val).c_str())
24 uint32_t e =
millis() - last_time;
35 case UART_CONFIG_PARITY_NONE:
37 case UART_CONFIG_PARITY_EVEN:
39 case UART_CONFIG_PARITY_ODD:
49 hex_buffer[(3 * 32) + 1] = 0;
50 for (
size_t i = 0; i <
length; i++) {
51 snprintf(&hex_buffer[3 * (i % 32)],
sizeof(hex_buffer),
"%02X ", data[i]);
53 ESP_LOGVV(TAG,
" %s", hex_buffer);
58 hex_buffer[3 * (
length % 32) + 2] = 0;
59 ESP_LOGVV(TAG,
" %s", hex_buffer);
63static const char *
const REG_TO_STR_P0[16] = {
"GENA",
"GRST",
"GMUT",
"SPAGE",
"SCR",
"LCR",
"FCR",
"SIER",
64 "SIFR",
"TFCNT",
"RFCNT",
"FSR",
"LSR",
"FDAT",
"FWCR",
"RS485"};
65static const char *
const REG_TO_STR_P1[16] = {
"GENA",
"GRST",
"GMUT",
"SPAGE",
"BAUD1",
"BAUD0",
"PRES",
"RFTL",
66 "TFTL",
"FWTH",
"FWTL",
"XON1",
"XOFF1",
"SADR",
"SAEN",
"RTSDLY"};
75 return page1 ? REG_TO_STR_P1[reg & 0x0F] : REG_TO_STR_P0[reg & 0x0F];
96inline static uint8_t cmd_byte(
RegType fifo,
CmdType transfer_type, uint8_t channel, uint8_t reg) {
97 return (fifo << 7 | transfer_type << 6 | channel << 4 | reg << 0);
107 spi_comp->write_byte(cmd);
108 uint8_t
val = spi_comp->read_byte();
110 ESP_LOGVV(TAG,
"WeikaiRegisterSPI::read_reg() cmd=%s(%02X) reg=%s ch=%d buf=%02X", I2S2CS(cmd), cmd,
119 spi_comp->write_byte(cmd);
120 spi_comp->read_array(data,
length);
122#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
123 ESP_LOGVV(TAG,
"WeikaiRegisterSPI::read_fifo() cmd=%s(%02X) ch=%d len=%d buffer", I2S2CS(cmd), cmd, this->
channel_,
133 spi_comp->write_array(buf, 2);
135 ESP_LOGVV(TAG,
"WeikaiRegisterSPI::write_reg() cmd=%s(%02X) reg=%s ch=%d buf=%02X", I2S2CS(buf[0]), buf[0],
143 spi_comp->write_byte(cmd);
144 spi_comp->write_array(data,
length);
147#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
148 ESP_LOGVV(TAG,
"WeikaiRegisterSPI::write_fifo() cmd=%s(%02X) ch=%d len=%d buffer", I2S2CS(cmd), cmd, this->
channel_,
158 using namespace weikai;
159 ESP_LOGCONFIG(TAG,
"Setting up wk2168_spi: %s with %d UARTs...", this->
get_name(), this->
children_.size());
171 child->setup_channel();
176 ESP_LOGCONFIG(TAG,
"Initialization of %s with %d UARTs completed", this->
get_name(), this->
children_.size());
177 ESP_LOGCONFIG(TAG,
" Crystal: %" PRIu32
"", this->
crystal_);
179 ESP_LOGCONFIG(TAG,
" Test mode: %d",
test_mode_);
180 ESP_LOGCONFIG(TAG,
" Transfer buffer size: %d",
XFER_MAX_SIZE);
181 LOG_PIN(
" CS Pin: ", this->
cs_);
184 child->dump_channel();
void spi_setup() override
int test_mode_
test mode value (0 -> no tests)
uint32_t crystal_
crystal value;
bool page1_
set to true when in "page1 mode"
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
const char * get_name()
Get the name of the component.
uint8_t register_
address of the register
uint8_t channel_
channel for this register
WeikaiComponent *const comp_
pointer to our parent (aggregation)
The WeikaiComponentSPI class stores the information to the WeiKai component connected through an SPI ...
void dump_config() override
weikai::WeikaiRegister & reg(uint8_t reg, uint8_t channel) override
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.
constexpr uint8_t GRST_C4RST
Channel 4 soft reset (0: not reset, 1: reset)
constexpr uint8_t WKREG_GPDIR
Global GPIO direction register - 10 0001.
constexpr uint8_t WKREG_GPDAT
Global GPIO data register - 11 0001.
constexpr uint8_t GRST_C2RST
Channel 2 soft reset (0: not reset, 1: reset)
constexpr uint8_t WKREG_GRST
Global Reset Register - 00 0001.
constexpr uint8_t GRST_C3RST
Channel 3 soft reset (0: not reset, 1: reset)
constexpr uint8_t GENA_C2EN
Channel 2 enable clock (0: disable, 1: enable)
constexpr uint8_t GRST_C1RST
Channel 1 soft reset (0: not reset, 1: reset)
constexpr uint8_t GENA_C3EN
Channel 3 enable clock (0: disable, 1: enable)
constexpr uint8_t GENA_C4EN
Channel 4 enable clock (0: disable, 1: enable)
constexpr uint8_t WKREG_GENA
Global Control Register - 00 0000.
constexpr uint8_t GENA_C1EN
Channel 1 enable clock (0: disable, 1: enable)
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
const char * reg_to_str(int reg, bool page1)
const char * p2s(uart::UARTParityOptions parity)
Converts the parity enum value to a C string.
std::string i2s(uint8_t val)
convert an int to binary representation as C++ std::string
void print_buffer(const uint8_t *data, size_t length)
Display a buffer in hexadecimal format (32 hex values / line) for debug.
uint32_t elapsed_ms(uint32_t &last_time)
measure the time elapsed between two calls
Providing packet encoding functions for exchanging data with a remote host.
uint32_t IRAM_ATTR HOT millis()