ESPHome 2026.11.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
6#include <algorithm>
7#include <cstring>
8
10
11// Maximum bytes to log in verbose hex output
12static constexpr size_t QSPI_DBI_MAX_LOG_BYTES = 64;
13
15 this->spi_setup();
16 if (this->enable_pin_ != nullptr) {
17 this->enable_pin_->setup();
18 this->enable_pin_->digital_write(true);
19 }
20 if (this->reset_pin_ != nullptr) {
21 this->reset_pin_->setup();
22 this->reset_pin_->digital_write(true);
23 delay(5);
24 this->reset_pin_->digital_write(false);
25 delay(5);
26 this->reset_pin_->digital_write(true);
27 }
28 this->set_timeout(120, [this] { this->write_command_(SLEEP_OUT); });
29 this->set_timeout(240, [this] { this->write_init_sequence_(); });
30 if (this->draw_from_origin_)
32}
33
35 if (!this->setup_complete_) {
36 return;
37 }
38 this->updating_ = true;
39 this->do_update_();
40 this->updating_ = false;
41 if (this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
42 return;
43 // Some chips require that the drawing window be aligned on certain boundaries
44 auto dr = this->draw_rounding_;
45 this->x_low_ = this->x_low_ / dr * dr;
46 this->y_low_ = this->y_low_ / dr * dr;
47 this->x_high_ = (this->x_high_ + dr) / dr * dr - 1;
48 this->y_high_ = (this->y_high_ + dr) / dr * dr - 1;
49 if (this->draw_from_origin_) {
50 this->x_low_ = 0;
51 this->y_low_ = 0;
52 this->x_high_ = this->width_ - 1;
53 }
54 int w = this->x_high_ - this->x_low_ + 1;
55 int h = this->y_high_ - this->y_low_ + 1;
56 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_, this->y_low_,
57 this->width_ - w - this->x_low_);
58 // invalidate watermarks
59 this->x_low_ = this->width_;
60 this->y_low_ = this->height_;
61 this->x_high_ = 0;
62 this->y_high_ = 0;
63}
64
66 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
67 return;
68 }
69 if (this->is_failed())
70 return;
72 uint32_t pos = (y * this->width_) + x;
73 bool updated = false;
74 pos = pos * 2;
75 uint16_t new_color = display::ColorUtil::color_to_565(color);
76 if (this->buffer_[pos] != static_cast<uint8_t>(new_color >> 8)) {
77 this->buffer_[pos] = static_cast<uint8_t>(new_color >> 8);
78 updated = true;
79 }
80 pos = pos + 1;
81 new_color = new_color & 0xFF;
82
83 if (this->buffer_[pos] != new_color) {
84 this->buffer_[pos] = new_color;
85 updated = true;
86 }
87 if (updated) {
88 // low and high watermark may speed up drawing from buffer
90 this->x_low_ = x;
92 this->y_low_ = y;
93 if (x > this->x_high_)
94 this->x_high_ = x;
95 if (y > this->y_high_)
96 this->y_high_ = y;
97 }
98}
99
100void QspiDbi::reset_params_(bool ready) {
101 if (!ready && !this->is_ready())
102 return;
103 this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
104 // custom x/y transform and color order
105 uint8_t mad = this->color_mode_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
106 if (this->swap_xy_)
107 mad |= MADCTL_MV;
108 if (this->mirror_x_)
109 mad |= MADCTL_MX;
110 if (this->mirror_y_)
111 mad |= MADCTL_MY;
112 this->write_command_(MADCTL_CMD, mad);
113 this->write_command_(BRIGHTNESS, this->brightness_);
114 this->write_command_(DISPLAY_ON);
115}
116
118 for (const auto &seq : this->init_sequences_) {
119 this->write_sequence_(seq);
120 }
121 this->reset_params_(true);
122 this->setup_complete_ = true;
123}
124
125void QspiDbi::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
126 ESP_LOGVV(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
127 uint8_t buf[4];
128 x1 += this->offset_x_;
129 x2 += this->offset_x_;
130 y1 += this->offset_y_;
131 y2 += this->offset_y_;
132 put16_be(buf, y1);
133 put16_be(buf + 2, y2);
134 this->write_command_(RASET, buf, sizeof buf);
135 put16_be(buf, x1);
136 put16_be(buf + 2, x2);
137 this->write_command_(CASET, buf, sizeof buf);
138}
139
140void QspiDbi::draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr,
141 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset,
142 int x_pad) {
143 if (!this->setup_complete_ || this->is_failed())
144 return;
145 if (w <= 0 || h <= 0)
146 return;
149 Display::draw_pixels_internal(x_start, y_start, w, h, ptr, bitness, big_endian, x_offset, y_offset, x_pad);
150 return;
151 } else if (this->updating_) {
152 // Inside update() the block goes into the buffer, or the flush at its end would paint over it
154 return;
155 } else if (this->draw_from_origin_) {
156 auto stride = x_offset + w + x_pad;
157 for (int y = 0; y != h; y++) {
158 memcpy(this->buffer_ + ((y + y_start) * this->width_ + x_start) * 2,
159 ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2);
160 }
161 ptr = this->buffer_;
162 w = this->width_;
163 h += y_start;
164 x_start = 0;
165 y_start = 0;
166 x_offset = 0;
167 y_offset = 0;
168 }
169 this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
170}
171
172void QspiDbi::copy_to_buffer_(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness,
173 bool big_endian, int x_offset, int y_offset, int x_pad) {
174 // The buffer holds big endian RGB565
175 if (!big_endian) {
176 Display::draw_pixels_internal(x_start, y_start, w, h, ptr, bitness, big_endian, x_offset, y_offset, x_pad);
177 return;
178 }
179 const int first_col = std::max(0, -x_start), end_col = std::min(w, static_cast<int>(this->width_) - x_start);
180 const int first_row = std::max(0, -y_start), end_row = std::min(h, static_cast<int>(this->height_) - y_start);
181 if (first_col >= end_col || first_row >= end_row)
182 return;
183 this->check_buffer_();
184 if (this->buffer_ == nullptr)
185 return;
186 const size_t stride = x_offset + w + x_pad;
187 for (int y = first_row; y != end_row; y++) {
188 memcpy(this->buffer_ + ((y_start + y) * this->width_ + x_start + first_col) * 2,
189 ptr + ((y_offset + y) * stride + x_offset + first_col) * 2, (end_col - first_col) * 2);
190 }
191 this->x_low_ = std::min<int>(this->x_low_, x_start + first_col);
192 this->y_low_ = std::min<int>(this->y_low_, y_start + first_row);
193 this->x_high_ = std::max<int>(this->x_high_, x_start + end_col - 1);
194 this->y_high_ = std::max<int>(this->y_high_, y_start + end_row - 1);
195}
196
197void QspiDbi::write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
198 int x_pad) {
199 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
200 this->enable();
201 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
202 if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
203 // 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
204 this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, ptr, w * h * 2, 4);
205 } else {
206 auto stride = x_offset + w + x_pad;
207 this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, nullptr, 0, 4);
208 for (int y = 0; y != h; y++) {
209 this->write_cmd_addr_data(0, 0, 0, 0, ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2, 4);
210 }
211 }
212 this->disable();
213}
214void QspiDbi::write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) {
215#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
216 char hex_buf[format_hex_pretty_size(QSPI_DBI_MAX_LOG_BYTES)];
217#endif
218 ESP_LOGV(TAG, "Command %02X, length %d, bytes %s", cmd, len,
219 format_hex_pretty_to(hex_buf, sizeof(hex_buf), bytes, len));
220 this->enable();
221 this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len);
222 this->disable();
223}
224
225void QspiDbi::write_sequence_(const std::vector<uint8_t> &vec) {
226 size_t index = 0;
227 while (index != vec.size()) {
228 if (vec.size() - index < 2) {
229 ESP_LOGE(TAG, "Malformed init sequence");
230 return;
231 }
232 uint8_t cmd = vec[index++];
233 uint8_t x = vec[index++];
234 if (x == DELAY_FLAG) {
235 ESP_LOGV(TAG, "Delay %dms", cmd);
236 delay(cmd);
237 } else {
238 uint8_t num_args = x & 0x7F;
239 if (vec.size() - index < num_args) {
240 ESP_LOGE(TAG, "Malformed init sequence");
241 return;
242 }
243 const auto *ptr = vec.data() + index;
244 this->write_command_(cmd, ptr, num_args);
245 index += num_args;
246 }
247 }
248}
249
251 ESP_LOGCONFIG("", "QSPI_DBI Display");
252 ESP_LOGCONFIG("", "Model: %s", this->model_);
253 ESP_LOGCONFIG(TAG,
254 " Height: %u\n"
255 " Width: %u\n"
256 " Draw rounding: %u\n"
257 " SPI Data rate: %uMHz",
258 this->height_, this->width_, this->draw_rounding_, (unsigned) (this->data_rate_ / 1000000));
259 LOG_PIN(" CS Pin: ", this->cs_);
260 LOG_PIN(" Reset Pin: ", this->reset_pin_);
261}
262
263} // namespace esphome::qspi_dbi
264#endif
uint8_t h
Definition bl0906.h:2
bool is_failed() const
Definition component.h:274
bool is_ready() const
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
virtual void setup()=0
virtual void digital_write(bool value)=0
int int int const uint8_t ColorOrder ColorBitness bitness
Definition display.h:392
bool is_clipping() const
Definition display.h:805
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int int int x_pad
Definition display.h:392
int int int const uint8_t * ptr
Definition display.h:391
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
Definition display.h:392
DisplayRotation rotation_
Definition display.h:863
void write_sequence_(const std::vector< uint8_t > &vec)
Definition qspi_dbi.cpp:225
int get_width_internal() override
Definition qspi_dbi.h:102
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition qspi_dbi.cpp:125
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:197
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
Definition qspi_dbi.cpp:140
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:214
display::ColorOrder color_mode_
Definition qspi_dbi.h:156
void dump_config() override
Definition qspi_dbi.cpp:250
void setup() override
Definition qspi_dbi.cpp:14
void copy_to_buffer_(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Definition qspi_dbi.cpp:172
void reset_params_(bool ready=false)
Definition qspi_dbi.cpp:100
std::vector< std::vector< uint8_t > > init_sequences_
Definition qspi_dbi.h:169
int get_height_internal() override
Definition qspi_dbi.h:103
void update() override
Definition qspi_dbi.cpp:34
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition qspi_dbi.cpp:65
uint32_t data_rate_
Definition spi.h:436
SPIBitOrder bit_order_
Definition spi.h:434
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:500
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:136
@ BIT_ORDER_MSB_FIRST
The most significant bit is transmitted/received first.
Definition spi.h:50
const void size_t len
Definition hal.h:68
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:498
size_t size_t pos
Definition helpers.h:1255
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:1609
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