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