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