ESPHome 2025.5.0
Loading...
Searching...
No Matches
mipi_spi.cpp
Go to the documentation of this file.
1#include "mipi_spi.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace mipi_spi {
6
8 ESP_LOGCONFIG(TAG, "Setting up MIPI SPI");
9 this->spi_setup();
10 if (this->dc_pin_ != nullptr) {
11 this->dc_pin_->setup();
12 this->dc_pin_->digital_write(false);
13 }
14 for (auto *pin : this->enable_pins_) {
15 pin->setup();
16 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->bus_width_ = this->parent_->get_bus_width();
27
28 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
29 auto when = millis() + 120;
30 delay(10);
31 size_t index = 0;
32 auto &vec = this->init_sequence_;
33 while (index != vec.size()) {
34 if (vec.size() - index < 2) {
35 ESP_LOGE(TAG, "Malformed init sequence");
36 this->mark_failed();
37 return;
38 }
39 uint8_t cmd = vec[index++];
40 uint8_t x = vec[index++];
41 if (x == DELAY_FLAG) {
42 ESP_LOGD(TAG, "Delay %dms", cmd);
43 delay(cmd);
44 } else {
45 uint8_t num_args = x & 0x7F;
46 if (vec.size() - index < num_args) {
47 ESP_LOGE(TAG, "Malformed init sequence");
48 this->mark_failed();
49 return;
50 }
51 auto arg_byte = vec[index];
52 switch (cmd) {
53 case SLEEP_OUT: {
54 // are we ready, boots?
55 int duration = when - millis();
56 if (duration > 0) {
57 ESP_LOGD(TAG, "Sleep %dms", duration);
59 }
60 } break;
61
62 case INVERT_ON:
63 this->invert_colors_ = true;
64 break;
65 case MADCTL_CMD:
66 this->madctl_ = arg_byte;
67 break;
68 case PIXFMT:
69 this->pixel_mode_ = arg_byte & 0x11 ? PIXEL_MODE_16 : PIXEL_MODE_18;
70 break;
71 case BRIGHTNESS:
72 this->brightness_ = arg_byte;
73 break;
74
75 default:
76 break;
77 }
78 const auto *ptr = vec.data() + index;
79 ESP_LOGD(TAG, "Command %02X, length %d, byte %02X", cmd, num_args, arg_byte);
80 this->write_command_(cmd, ptr, num_args);
81 index += num_args;
82 if (cmd == SLEEP_OUT)
83 delay(10);
84 }
85 }
86 this->setup_complete_ = true;
87 if (this->draw_from_origin_)
89 ESP_LOGCONFIG(TAG, "MIPI SPI setup complete");
90}
91
93 if (!this->setup_complete_ || this->is_failed()) {
94 return;
95 }
96 this->do_update_();
97 if (this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
98 return;
99 ESP_LOGV(TAG, "x_low %d, y_low %d, x_high %d, y_high %d", this->x_low_, this->y_low_, this->x_high_, this->y_high_);
100 // Some chips require that the drawing window be aligned on certain boundaries
101 auto dr = this->draw_rounding_;
102 this->x_low_ = this->x_low_ / dr * dr;
103 this->y_low_ = this->y_low_ / dr * dr;
104 this->x_high_ = (this->x_high_ + dr) / dr * dr - 1;
105 this->y_high_ = (this->y_high_ + dr) / dr * dr - 1;
106 if (this->draw_from_origin_) {
107 this->x_low_ = 0;
108 this->y_low_ = 0;
109 this->x_high_ = this->width_ - 1;
110 }
111 int w = this->x_high_ - this->x_low_ + 1;
112 int h = this->y_high_ - this->y_low_ + 1;
113 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_, this->y_low_,
114 this->width_ - w - this->x_low_);
115 // invalidate watermarks
116 this->x_low_ = this->width_;
117 this->y_low_ = this->height_;
118 this->x_high_ = 0;
119 this->y_high_ = 0;
120}
121
122void MipiSpi::fill(Color color) {
123 if (!this->check_buffer_())
124 return;
125 this->x_low_ = 0;
126 this->y_low_ = 0;
127 this->x_high_ = this->get_width_internal() - 1;
128 this->y_high_ = this->get_height_internal() - 1;
129 switch (this->color_depth_) {
132 memset(this->buffer_, (uint8_t) new_color, this->buffer_bytes_);
133 break;
134 }
135 default: {
136 auto new_color = display::ColorUtil::color_to_565(color);
137 if (((uint8_t) (new_color >> 8)) == ((uint8_t) new_color)) {
138 // Upper and lower is equal can use quicker memset operation. Takes ~20ms.
139 memset(this->buffer_, (uint8_t) new_color, this->buffer_bytes_);
140 } else {
141 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
142 auto len = this->buffer_bytes_ / 2;
143 while (len--) {
144 *ptr_16++ = new_color;
145 }
146 }
147 }
148 }
149}
150
152 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
153 return;
154 }
155 if (!this->check_buffer_())
156 return;
157 size_t pos = (y * this->width_) + x;
158 switch (this->color_depth_) {
160 uint8_t new_color = display::ColorUtil::color_to_332(color);
161 if (this->buffer_[pos] == new_color)
162 return;
163 this->buffer_[pos] = new_color;
164 break;
165 }
166
168 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
169 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
170 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
171 uint16_t new_color = hi_byte | (lo_byte << 8); // big endian
172 if (ptr_16[pos] == new_color)
173 return;
174 ptr_16[pos] = new_color;
175 break;
176 }
177 default:
178 return;
179 }
180 // low and high watermark may speed up drawing from buffer
182 this->x_low_ = x;
184 this->y_low_ = y;
185 if (x > this->x_high_)
186 this->x_high_ = x;
187 if (y > this->y_high_)
188 this->y_high_ = y;
189}
190
192 if (!this->is_ready())
193 return;
194 this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
195 if (this->brightness_.has_value())
196 this->write_command_(BRIGHTNESS, this->brightness_.value());
197}
198
200 size_t index = 0;
201 auto &vec = this->init_sequence_;
202 while (index != vec.size()) {
203 if (vec.size() - index < 2) {
204 ESP_LOGE(TAG, "Malformed init sequence");
205 this->mark_failed();
206 return;
207 }
208 uint8_t cmd = vec[index++];
209 uint8_t x = vec[index++];
210 if (x == DELAY_FLAG) {
211 ESP_LOGV(TAG, "Delay %dms", cmd);
212 delay(cmd);
213 } else {
214 uint8_t num_args = x & 0x7F;
215 if (vec.size() - index < num_args) {
216 ESP_LOGE(TAG, "Malformed init sequence");
217 this->mark_failed();
218 return;
219 }
220 const auto *ptr = vec.data() + index;
221 this->write_command_(cmd, ptr, num_args);
222 index += num_args;
223 }
224 }
225 this->setup_complete_ = true;
226 ESP_LOGCONFIG(TAG, "MIPI SPI setup complete");
227}
228
229void MipiSpi::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
230 ESP_LOGVV(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
231 uint8_t buf[4];
232 x1 += this->offset_width_;
233 x2 += this->offset_width_;
234 y1 += this->offset_height_;
235 y2 += this->offset_height_;
236 put16_be(buf, y1);
237 put16_be(buf + 2, y2);
238 this->write_command_(RASET, buf, sizeof buf);
239 put16_be(buf, x1);
240 put16_be(buf + 2, x2);
241 this->write_command_(CASET, buf, sizeof buf);
242}
243
244void MipiSpi::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
245 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
246 if (!this->setup_complete_ || this->is_failed())
247 return;
248 if (w <= 0 || h <= 0)
249 return;
250 if (bitness != this->color_depth_ || big_endian != (this->bit_order_ == spi::BIT_ORDER_MSB_FIRST)) {
251 Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
252 return;
253 }
254 if (this->draw_from_origin_) {
255 auto stride = x_offset + w + x_pad;
256 for (int y = 0; y != h; y++) {
257 memcpy(this->buffer_ + ((y + y_start) * this->width_ + x_start) * 2,
258 ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2);
259 }
260 ptr = this->buffer_;
261 w = this->width_;
262 h += y_start;
263 x_start = 0;
264 y_start = 0;
265 x_offset = 0;
266 y_offset = 0;
267 }
268 this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
269}
270
271void MipiSpi::write_18_from_16_bit_(const uint16_t *ptr, size_t w, size_t h, size_t stride) {
272 stride -= w;
273 uint8_t transfer_buffer[6 * 256];
274 size_t idx = 0; // index into transfer_buffer
275 while (h-- != 0) {
276 for (auto x = w; x-- != 0;) {
277 auto color_val = *ptr++;
278 // deal with byte swapping
279 transfer_buffer[idx++] = (color_val & 0xF8); // Blue
280 transfer_buffer[idx++] = ((color_val & 0x7) << 5) | ((color_val & 0xE000) >> 11); // Green
281 transfer_buffer[idx++] = (color_val >> 5) & 0xF8; // Red
282 if (idx == sizeof(transfer_buffer)) {
283 this->write_array(transfer_buffer, idx);
284 idx = 0;
285 }
286 }
287 ptr += stride;
288 }
289 if (idx != 0)
290 this->write_array(transfer_buffer, idx);
291}
292
293void MipiSpi::write_18_from_8_bit_(const uint8_t *ptr, size_t w, size_t h, size_t stride) {
294 stride -= w;
295 uint8_t transfer_buffer[6 * 256];
296 size_t idx = 0; // index into transfer_buffer
297 while (h-- != 0) {
298 for (auto x = w; x-- != 0;) {
299 auto color_val = *ptr++;
300 transfer_buffer[idx++] = color_val & 0xE0; // Red
301 transfer_buffer[idx++] = (color_val << 3) & 0xE0; // Green
302 transfer_buffer[idx++] = color_val << 6; // Blue
303 if (idx == sizeof(transfer_buffer)) {
304 this->write_array(transfer_buffer, idx);
305 idx = 0;
306 }
307 }
308 ptr += stride;
309 }
310 if (idx != 0)
311 this->write_array(transfer_buffer, idx);
312}
313
314void MipiSpi::write_16_from_8_bit_(const uint8_t *ptr, size_t w, size_t h, size_t stride) {
315 stride -= w;
316 uint8_t transfer_buffer[6 * 256];
317 size_t idx = 0; // index into transfer_buffer
318 while (h-- != 0) {
319 for (auto x = w; x-- != 0;) {
320 auto color_val = *ptr++;
321 transfer_buffer[idx++] = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
322 transfer_buffer[idx++] = (color_val & 0x3) << 3;
323 if (idx == sizeof(transfer_buffer)) {
324 this->write_array(transfer_buffer, idx);
325 idx = 0;
326 }
327 }
328 ptr += stride;
329 }
330 if (idx != 0)
331 this->write_array(transfer_buffer, idx);
332}
333
334void MipiSpi::write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
335 int x_pad) {
336 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
337 auto stride = x_offset + w + x_pad;
338 const auto *offset_ptr = ptr;
340 offset_ptr += y_offset * stride + x_offset;
341 } else {
342 stride *= 2;
343 offset_ptr += y_offset * stride + x_offset * 2;
344 }
345
346 switch (this->bus_width_) {
347 case 4:
348 this->enable();
349 if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
350 // we could deal here with a non-zero y_offset, but if x_offset is zero, y_offset probably will be so don't
351 // bother
352 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w * h * 2, 4);
353 } else {
354 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, nullptr, 0, 4);
355 for (int y = 0; y != h; y++) {
356 this->write_cmd_addr_data(0, 0, 0, 0, offset_ptr, w * 2, 4);
357 offset_ptr += stride;
358 }
359 }
360 break;
361
362 case 8:
363 this->write_command_(WDATA);
364 this->enable();
365 if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
366 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w * h * 2, 8);
367 } else {
368 for (int y = 0; y != h; y++) {
369 this->write_cmd_addr_data(0, 0, 0, 0, offset_ptr, w * 2, 8);
370 offset_ptr += stride;
371 }
372 }
373 break;
374
375 default:
376 this->write_command_(WDATA);
377 this->enable();
378
380 // Source buffer is 16-bit RGB565
381 if (this->pixel_mode_ == PIXEL_MODE_18) {
382 // Convert RGB565 to RGB666
383 this->write_18_from_16_bit_(reinterpret_cast<const uint16_t *>(offset_ptr), w, h, stride / 2);
384 } else {
385 // Direct RGB565 output
386 if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
387 this->write_array(ptr, w * h * 2);
388 } else {
389 for (int y = 0; y != h; y++) {
390 this->write_array(offset_ptr, w * 2);
391 offset_ptr += stride;
392 }
393 }
394 }
395 } else {
396 // Source buffer is 8-bit RGB332
397 if (this->pixel_mode_ == PIXEL_MODE_18) {
398 // Convert RGB332 to RGB666
399 this->write_18_from_8_bit_(offset_ptr, w, h, stride);
400 } else {
401 this->write_16_from_8_bit_(offset_ptr, w, h, stride);
402 }
403 break;
404 }
405 }
406 this->disable();
407}
408
409void MipiSpi::write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) {
410 ESP_LOGV(TAG, "Command %02X, length %d, bytes %s", cmd, len, format_hex_pretty(bytes, len).c_str());
411 if (this->bus_width_ == 4) {
412 this->enable();
413 this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len);
414 this->disable();
415 } else if (this->bus_width_ == 8) {
416 this->dc_pin_->digital_write(false);
417 this->enable();
418 this->write_cmd_addr_data(0, 0, 0, 0, &cmd, 1, 8);
419 this->disable();
420 this->dc_pin_->digital_write(true);
421 if (len != 0) {
422 this->enable();
423 this->write_cmd_addr_data(0, 0, 0, 0, bytes, len, 8);
424 this->disable();
425 }
426 } else {
427 this->dc_pin_->digital_write(false);
428 this->enable();
429 this->write_byte(cmd);
430 this->disable();
431 this->dc_pin_->digital_write(true);
432 if (len != 0) {
433 if (this->spi_16_) {
434 for (size_t i = 0; i != len; i++) {
435 this->enable();
436 this->write_byte(0);
437 this->write_byte(bytes[i]);
438 this->disable();
439 }
440 } else {
441 this->enable();
442 this->write_array(bytes, len);
443 this->disable();
444 }
445 }
446 }
447}
448
450 ESP_LOGCONFIG(TAG, "MIPI_SPI Display");
451 ESP_LOGCONFIG(TAG, " Model: %s", this->model_);
452 ESP_LOGCONFIG(TAG, " Width: %u", this->width_);
453 ESP_LOGCONFIG(TAG, " Height: %u", this->height_);
454 if (this->offset_width_ != 0)
455 ESP_LOGCONFIG(TAG, " Offset width: %u", this->offset_width_);
456 if (this->offset_height_ != 0)
457 ESP_LOGCONFIG(TAG, " Offset height: %u", this->offset_height_);
458 ESP_LOGCONFIG(TAG, " Swap X/Y: %s", YESNO(this->madctl_ & MADCTL_MV));
459 ESP_LOGCONFIG(TAG, " Mirror X: %s", YESNO(this->madctl_ & (MADCTL_MX | MADCTL_XFLIP)));
460 ESP_LOGCONFIG(TAG, " Mirror Y: %s", YESNO(this->madctl_ & (MADCTL_MY | MADCTL_YFLIP)));
461 ESP_LOGCONFIG(TAG, " Color depth: %d bits", this->color_depth_ == display::COLOR_BITNESS_565 ? 16 : 8);
462 ESP_LOGCONFIG(TAG, " Invert colors: %s", YESNO(this->invert_colors_));
463 ESP_LOGCONFIG(TAG, " Color order: %s", this->madctl_ & MADCTL_BGR ? "BGR" : "RGB");
464 ESP_LOGCONFIG(TAG, " Pixel mode: %s", this->pixel_mode_ == PIXEL_MODE_18 ? "18bit" : "16bit");
465 if (this->brightness_.has_value())
466 ESP_LOGCONFIG(TAG, " Brightness: %u", this->brightness_.value());
467 if (this->spi_16_)
468 ESP_LOGCONFIG(TAG, " SPI 16bit: YES");
469 ESP_LOGCONFIG(TAG, " Draw rounding: %u", this->draw_rounding_);
470 if (this->draw_from_origin_)
471 ESP_LOGCONFIG(TAG, " Draw from origin: YES");
472 LOG_PIN(" CS Pin: ", this->cs_);
473 LOG_PIN(" Reset Pin: ", this->reset_pin_);
474 LOG_PIN(" DC Pin: ", this->dc_pin_);
475 ESP_LOGCONFIG(TAG, " SPI Mode: %d", this->mode_);
476 ESP_LOGCONFIG(TAG, " SPI Data rate: %dMHz", static_cast<unsigned>(this->data_rate_ / 1000000));
477 ESP_LOGCONFIG(TAG, " SPI Bus width: %d", this->bus_width_);
478}
479
480} // namespace mipi_spi
481} // namespace esphome
uint8_t h
Definition bl0906.h:2
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool is_ready() const
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)
static uint8_t color_to_332(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void dump_config() override
Definition mipi_spi.cpp:449
std::vector< uint8_t > init_sequence_
Definition mipi_spi.h:168
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition mipi_spi.cpp:229
int get_width_internal() override
Definition mipi_spi.h:87
void write_18_from_16_bit_(const uint16_t *ptr, size_t w, size_t h, size_t stride)
Definition mipi_spi.cpp:271
int get_height_internal() override
Definition mipi_spi.h:88
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 mipi_spi.cpp:334
optional< uint8_t > brightness_
Definition mipi_spi.h:166
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition mipi_spi.cpp:151
display::ColorBitness color_depth_
Definition mipi_spi.h: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 mipi_spi.cpp:409
std::vector< GPIOPin * > enable_pins_
Definition mipi_spi.h:145
void write_18_from_8_bit_(const uint8_t *ptr, size_t w, size_t h, size_t stride)
Definition mipi_spi.cpp:293
void write_16_from_8_bit_(const uint8_t *ptr, size_t w, size_t h, size_t stride)
Definition mipi_spi.cpp:314
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 mipi_spi.cpp:244
void update() override
Definition mipi_spi.cpp:92
void fill(Color color) override
Definition mipi_spi.cpp:122
void setup() override
Definition mipi_spi.cpp:7
bool has_value() const
Definition optional.h:87
value_type const & value() const
Definition optional.h:89
uint32_t data_rate_
Definition spi.h:406
SPIBitOrder bit_order_
Definition spi.h:404
SPIComponent * parent_
Definition spi.h:407
size_t get_bus_width() const
Definition spi.h:358
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
uint8_t duration
Definition msa3xx.h:0
@ 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
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:27
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
uint8_t g
Definition color.h:18
uint8_t b
Definition color.h:22
uint8_t r
Definition color.h:14
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6