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