ESPHome  2024.12.2
qspi_dbi.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP_IDF
2 #include "qspi_dbi.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace qspi_dbi {
7 
8 void QspiDbi::setup() {
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_)
26  check_buffer_();
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  // Start addresses and widths/heights must be divisible by 2 (CASET/RASET restriction in datasheet)
37  if (this->x_low_ % 2 == 1) {
38  this->x_low_--;
39  }
40  if (this->x_high_ % 2 == 0) {
41  this->x_high_++;
42  }
43  if (this->y_low_ % 2 == 1) {
44  this->y_low_--;
45  }
46  if (this->y_high_ % 2 == 0) {
47  this->y_high_++;
48  }
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;
71  check_buffer_();
72  uint32_t pos = (y * this->width_) + x;
73  bool updated = false;
74  pos = pos * 2;
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
89  if (x < this->x_low_)
90  this->x_low_ = x;
91  if (y < this->y_low_)
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 
100 void 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  ESP_LOGCONFIG(TAG, "QSPI_DBI setup complete");
124 }
125 
126 void QspiDbi::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
127  ESP_LOGVV(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
128  uint8_t buf[4];
129  x1 += this->offset_x_;
130  x2 += this->offset_x_;
131  y1 += this->offset_y_;
132  y2 += this->offset_y_;
133  put16_be(buf, y1);
134  put16_be(buf + 2, y2);
135  this->write_command_(RASET, buf, sizeof buf);
136  put16_be(buf, x1);
137  put16_be(buf + 2, x2);
138  this->write_command_(CASET, buf, sizeof buf);
139 }
140 
141 void QspiDbi::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
142  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
143  if (!this->setup_complete_ || this->is_failed())
144  return;
145  if (w <= 0 || h <= 0)
146  return;
147  if (bitness != display::COLOR_BITNESS_565 || order != this->color_mode_ ||
148  big_endian != (this->bit_order_ == spi::BIT_ORDER_MSB_FIRST)) {
149  Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
150  return;
151  } else if (this->draw_from_origin_) {
152  auto stride = x_offset + w + x_pad;
153  for (int y = 0; y != h; y++) {
154  memcpy(this->buffer_ + ((y + y_start) * this->width_ + x_start) * 2,
155  ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2);
156  }
157  ptr = this->buffer_;
158  w = this->width_;
159  h += y_start;
160  x_start = 0;
161  y_start = 0;
162  x_offset = 0;
163  y_offset = 0;
164  }
165  this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
166 }
167 
168 void QspiDbi::write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
169  int x_pad) {
170  this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
171  this->enable();
172  // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
173  if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
174  // 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
175  this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, ptr, w * h * 2, 4);
176  } else {
177  auto stride = x_offset + w + x_pad;
178  uint16_t cmd = 0x2C00;
179  for (int y = 0; y != h; y++) {
180  this->write_cmd_addr_data(8, 0x32, 24, cmd, ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2, 4);
181  cmd = 0x3C00;
182  }
183  }
184  this->disable();
185 }
186 void QspiDbi::write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) {
187  ESP_LOGV(TAG, "Command %02X, length %d, bytes %s", cmd, len, format_hex_pretty(bytes, len).c_str());
188  this->enable();
189  this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len);
190  this->disable();
191 }
192 
193 void QspiDbi::write_sequence_(const std::vector<uint8_t> &vec) {
194  size_t index = 0;
195  while (index != vec.size()) {
196  if (vec.size() - index < 2) {
197  ESP_LOGE(TAG, "Malformed init sequence");
198  return;
199  }
200  uint8_t cmd = vec[index++];
201  uint8_t x = vec[index++];
202  if (x == DELAY_FLAG) {
203  ESP_LOGV(TAG, "Delay %dms", cmd);
204  delay(cmd);
205  } else {
206  uint8_t num_args = x & 0x7F;
207  if (vec.size() - index < num_args) {
208  ESP_LOGE(TAG, "Malformed init sequence");
209  return;
210  }
211  const auto *ptr = vec.data() + index;
212  this->write_command_(cmd, ptr, num_args);
213  index += num_args;
214  }
215  }
216 }
217 
219  ESP_LOGCONFIG("", "QSPI_DBI Display");
220  ESP_LOGCONFIG("", "Model: %s", this->model_);
221  ESP_LOGCONFIG(TAG, " Height: %u", this->height_);
222  ESP_LOGCONFIG(TAG, " Width: %u", this->width_);
223  LOG_PIN(" CS Pin: ", this->cs_);
224  LOG_PIN(" Reset Pin: ", this->reset_pin_);
225  ESP_LOGCONFIG(TAG, " SPI Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000));
226 }
227 
228 } // namespace qspi_dbi
229 } // namespace esphome
230 #endif
virtual void digital_write(bool value)=0
int get_width_internal() override
Definition: qspi_dbi.h:104
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:369
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition: qspi_dbi.cpp:126
bool is_failed() const
Definition: component.cpp:143
uint16_t x
Definition: tt21100.cpp:17
std::vector< std::vector< uint8_t > > init_sequences_
Definition: qspi_dbi.h:166
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:69
display::ColorOrder color_mode_
Definition: qspi_dbi.h:155
The most significant bit is transmitted/received first.
Definition: spi.h:42
void write_sequence_(const std::vector< uint8_t > &vec)
Definition: qspi_dbi.cpp:193
GPIOPin * cs_
Definition: spi.h:378
virtual void setup()=0
uint8_t h
Definition: bl0906.h:209
uint16_t y
Definition: tt21100.cpp:18
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:186
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:435
bool is_ready() const
Definition: component.cpp:144
void setup() override
Definition: qspi_dbi.cpp:8
void dump_config() override
Definition: qspi_dbi.cpp:218
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:168
int get_height_internal() override
Definition: qspi_dbi.h:105
void update() override
Definition: qspi_dbi.cpp:29
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition: qspi_dbi.cpp:65
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:141
SPIBitOrder bit_order_
Definition: spi.h:374
uint32_t data_rate_
Definition: spi.h:376
std::string size_t len
Definition: helpers.h:293
void reset_params_(bool ready=false)
Definition: qspi_dbi.cpp:100
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
stm32_cmd_t * cmd
Definition: stm32flash.h:96
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26