ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
qspi_dbi.cpp
Go to the documentation of this file.
1#if defined(USE_ESP32) && defined(USE_ESP32_VARIANT_ESP32S3)
2#include "qspi_dbi.h"
4#include "esphome/core/log.h"
5
7
8// Maximum bytes to log in verbose hex output
9static constexpr size_t QSPI_DBI_MAX_LOG_BYTES = 64;
10
12 this->spi_setup();
13 if (this->enable_pin_ != nullptr) {
14 this->enable_pin_->setup();
15 this->enable_pin_->digital_write(true);
16 }
17 if (this->reset_pin_ != nullptr) {
18 this->reset_pin_->setup();
19 this->reset_pin_->digital_write(true);
20 delay(5);
21 this->reset_pin_->digital_write(false);
22 delay(5);
23 this->reset_pin_->digital_write(true);
24 }
25 this->set_timeout(120, [this] { this->write_command_(SLEEP_OUT); });
26 this->set_timeout(240, [this] { this->write_init_sequence_(); });
27 if (this->draw_from_origin_)
29}
30
32 if (!this->setup_complete_) {
33 return;
34 }
35 this->do_update_();
36 if (this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
37 return;
38 // Some chips require that the drawing window be aligned on certain boundaries
39 auto dr = this->draw_rounding_;
40 this->x_low_ = this->x_low_ / dr * dr;
41 this->y_low_ = this->y_low_ / dr * dr;
42 this->x_high_ = (this->x_high_ + dr) / dr * dr - 1;
43 this->y_high_ = (this->y_high_ + dr) / dr * dr - 1;
44 if (this->draw_from_origin_) {
45 this->x_low_ = 0;
46 this->y_low_ = 0;
47 this->x_high_ = this->width_ - 1;
48 }
49 int w = this->x_high_ - this->x_low_ + 1;
50 int h = this->y_high_ - this->y_low_ + 1;
51 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_, this->y_low_,
52 this->width_ - w - this->x_low_);
53 // invalidate watermarks
54 this->x_low_ = this->width_;
55 this->y_low_ = this->height_;
56 this->x_high_ = 0;
57 this->y_high_ = 0;
58}
59
61 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
62 return;
63 }
64 if (this->is_failed())
65 return;
67 uint32_t pos = (y * this->width_) + x;
68 bool updated = false;
69 pos = pos * 2;
71 if (this->buffer_[pos] != static_cast<uint8_t>(new_color >> 8)) {
72 this->buffer_[pos] = static_cast<uint8_t>(new_color >> 8);
73 updated = true;
74 }
75 pos = pos + 1;
76 new_color = new_color & 0xFF;
77
78 if (this->buffer_[pos] != new_color) {
79 this->buffer_[pos] = new_color;
80 updated = true;
81 }
82 if (updated) {
83 // low and high watermark may speed up drawing from buffer
85 this->x_low_ = x;
87 this->y_low_ = y;
88 if (x > this->x_high_)
89 this->x_high_ = x;
90 if (y > this->y_high_)
91 this->y_high_ = y;
92 }
93}
94
95void QspiDbi::reset_params_(bool ready) {
96 if (!ready && !this->is_ready())
97 return;
98 this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
99 // custom x/y transform and color order
100 uint8_t mad = this->color_mode_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
101 if (this->swap_xy_)
102 mad |= MADCTL_MV;
103 if (this->mirror_x_)
104 mad |= MADCTL_MX;
105 if (this->mirror_y_)
106 mad |= MADCTL_MY;
107 this->write_command_(MADCTL_CMD, mad);
108 this->write_command_(BRIGHTNESS, this->brightness_);
109 this->write_command_(DISPLAY_ON);
110}
111
113 for (const auto &seq : this->init_sequences_) {
114 this->write_sequence_(seq);
115 }
116 this->reset_params_(true);
117 this->setup_complete_ = true;
118}
119
120void QspiDbi::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
121 ESP_LOGVV(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
122 uint8_t buf[4];
123 x1 += this->offset_x_;
124 x2 += this->offset_x_;
125 y1 += this->offset_y_;
126 y2 += this->offset_y_;
127 put16_be(buf, y1);
128 put16_be(buf + 2, y2);
129 this->write_command_(RASET, buf, sizeof buf);
130 put16_be(buf, x1);
131 put16_be(buf + 2, x2);
132 this->write_command_(CASET, buf, sizeof buf);
133}
134
135void QspiDbi::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
136 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
137 if (!this->setup_complete_ || this->is_failed())
138 return;
139 if (w <= 0 || h <= 0)
140 return;
141 if (bitness != display::COLOR_BITNESS_565 || order != this->color_mode_ ||
142 big_endian != (this->bit_order_ == spi::BIT_ORDER_MSB_FIRST)) {
143 Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
144 return;
145 } else if (this->draw_from_origin_) {
146 auto stride = x_offset + w + x_pad;
147 for (int y = 0; y != h; y++) {
148 memcpy(this->buffer_ + ((y + y_start) * this->width_ + x_start) * 2,
149 ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2);
150 }
151 ptr = this->buffer_;
152 w = this->width_;
153 h += y_start;
154 x_start = 0;
155 y_start = 0;
156 x_offset = 0;
157 y_offset = 0;
158 }
159 this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
160}
161
162void QspiDbi::write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
163 int x_pad) {
164 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
165 this->enable();
166 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
167 if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
168 // we could deal here with a non-zero y_offset, but if x_offset is zero, y_offset probably will be so don't bother
169 this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, ptr, w * h * 2, 4);
170 } else {
171 auto stride = x_offset + w + x_pad;
172 this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, nullptr, 0, 4);
173 for (int y = 0; y != h; y++) {
174 this->write_cmd_addr_data(0, 0, 0, 0, ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2, 4);
175 }
176 }
177 this->disable();
178}
179void QspiDbi::write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) {
180#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
181 char hex_buf[format_hex_pretty_size(QSPI_DBI_MAX_LOG_BYTES)];
182#endif
183 ESP_LOGV(TAG, "Command %02X, length %d, bytes %s", cmd, len,
184 format_hex_pretty_to(hex_buf, sizeof(hex_buf), bytes, len));
185 this->enable();
186 this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len);
187 this->disable();
188}
189
190void QspiDbi::write_sequence_(const std::vector<uint8_t> &vec) {
191 size_t index = 0;
192 while (index != vec.size()) {
193 if (vec.size() - index < 2) {
194 ESP_LOGE(TAG, "Malformed init sequence");
195 return;
196 }
197 uint8_t cmd = vec[index++];
198 uint8_t x = vec[index++];
199 if (x == DELAY_FLAG) {
200 ESP_LOGV(TAG, "Delay %dms", cmd);
201 delay(cmd);
202 } else {
203 uint8_t num_args = x & 0x7F;
204 if (vec.size() - index < num_args) {
205 ESP_LOGE(TAG, "Malformed init sequence");
206 return;
207 }
208 const auto *ptr = vec.data() + index;
209 this->write_command_(cmd, ptr, num_args);
210 index += num_args;
211 }
212 }
213}
214
216 ESP_LOGCONFIG("", "QSPI_DBI Display");
217 ESP_LOGCONFIG("", "Model: %s", this->model_);
218 ESP_LOGCONFIG(TAG,
219 " Height: %u\n"
220 " Width: %u\n"
221 " Draw rounding: %u\n"
222 " SPI Data rate: %uMHz",
223 this->height_, this->width_, this->draw_rounding_, (unsigned) (this->data_rate_ / 1000000));
224 LOG_PIN(" CS Pin: ", this->cs_);
225 LOG_PIN(" Reset Pin: ", this->reset_pin_);
226}
227
228} // namespace esphome::qspi_dbi
229#endif
uint8_t h
Definition bl0906.h:2
bool is_failed() const
Definition component.h:272
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
bool is_ready() const
virtual void setup()=0
virtual void digital_write(bool value)=0
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void write_sequence_(const std::vector< uint8_t > &vec)
Definition qspi_dbi.cpp:190
void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
Definition qspi_dbi.cpp:135
int get_width_internal() override
Definition qspi_dbi.h:101
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition qspi_dbi.cpp:120
void write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset, int x_pad)
Definition qspi_dbi.cpp:162
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
the RM67162 in quad SPI mode seems to work like this (not in the datasheet, this is deduced from the ...
Definition qspi_dbi.cpp:179
display::ColorOrder color_mode_
Definition qspi_dbi.h:153
void dump_config() override
Definition qspi_dbi.cpp:215
void setup() override
Definition qspi_dbi.cpp:11
void reset_params_(bool ready=false)
Definition qspi_dbi.cpp:95
std::vector< std::vector< uint8_t > > init_sequences_
Definition qspi_dbi.h:165
int get_height_internal() override
Definition qspi_dbi.h:102
void update() override
Definition qspi_dbi.cpp:31
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition qspi_dbi.cpp:60
uint32_t data_rate_
Definition spi.h:412
SPIBitOrder bit_order_
Definition spi.h:410
void write_cmd_addr_data(size_t cmd_bits, uint32_t cmd, size_t addr_bits, uint32_t address, const uint8_t *data, size_t length, uint8_t bus_width=1)
Definition spi.h:473
@ BIT_ORDER_MSB_FIRST
The most significant bit is transmitted/received first.
Definition spi.h:44
const void size_t len
Definition hal.h:64
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:340
size_t size_t pos
Definition helpers.h:1038
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1386
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t seq
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6