ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mipi_dsi.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32_VARIANT_ESP32P4
2#include <utility>
3#include "mipi_dsi.h"
5
6#include <algorithm>
7#include <cstring>
8
9namespace esphome::mipi_dsi {
10
11// Maximum bytes to log for init commands (truncated if larger)
12static constexpr size_t MIPI_DSI_MAX_CMD_LOG_BYTES = 64;
13
14static bool notify_refresh_ready(esp_lcd_panel_handle_t panel, esp_lcd_dpi_panel_event_data_t *edata, void *user_ctx) {
15 SemaphoreHandle_t sem = static_cast<SemaphoreHandle_t>(user_ctx);
16 BaseType_t need_yield = pdFALSE;
17 xSemaphoreGiveFromISR(sem, &need_yield);
18 return (need_yield == pdTRUE);
19}
20
21void MipiDsi::smark_failed(const LogString *message, esp_err_t err) {
22 ESP_LOGE(TAG, "%s: %s", LOG_STR_ARG(message), esp_err_to_name(err));
23 this->mark_failed(message);
24}
25
26void MipiDsi::setup() {
27 ESP_LOGCONFIG(TAG, "Running Setup");
28
29 if (!this->enable_pins_.empty()) {
30 for (auto *pin : this->enable_pins_) {
31 pin->setup();
32 pin->digital_write(true);
33 }
34 delay(10);
35 }
36
37 esp_lcd_dsi_bus_config_t bus_config = {
38 .bus_id = 0, // index from 0, specify the DSI host to use
39 .num_data_lanes =
40 this->lanes_, // Number of data lanes to use, can't set a value that exceeds the chip's capability
41 // phy_clk_src left at 0 to enable runtime auto-select.
42 .lane_bit_rate_mbps = this->lane_bit_rate_, // Bit rate of the data lanes, in Mbps
43 };
44 auto err = esp_lcd_new_dsi_bus(&bus_config, &this->bus_handle_);
45 if (err != ESP_OK) {
46 this->smark_failed(LOG_STR("lcd_new_dsi_bus failed"), err);
47 return;
48 }
49 esp_lcd_dbi_io_config_t dbi_config = {
50 .virtual_channel = 0,
51 .lcd_cmd_bits = 8, // according to the LCD spec
52 .lcd_param_bits = 8, // according to the LCD spec
53 };
54 err = esp_lcd_new_panel_io_dbi(this->bus_handle_, &dbi_config, &this->io_handle_);
55 if (err != ESP_OK) {
56 this->smark_failed(LOG_STR("new_panel_io_dbi failed"), err);
57 return;
58 }
59 // clang-format off
60#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
61 auto color_format = LCD_COLOR_FMT_RGB565;
63 color_format = LCD_COLOR_FMT_RGB888;
64 }
65 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
66 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
67 .dpi_clock_freq_mhz = this->pclk_frequency_,
68 .in_color_format = color_format,
69#else
70 auto pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565;
72 pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB888;
73 }
74 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
75 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
76 .dpi_clock_freq_mhz = this->pclk_frequency_,
77 .pixel_format = pixel_format,
78#endif
79 .num_fbs = 1, // number of frame buffers to allocate
80 .video_timing =
81 {
82 .h_size = this->width_,
83 .v_size = this->height_,
84 .hsync_pulse_width = this->hsync_pulse_width_,
85 .hsync_back_porch = this->hsync_back_porch_,
86 .hsync_front_porch = this->hsync_front_porch_,
87 .vsync_pulse_width = this->vsync_pulse_width_,
88 .vsync_back_porch = this->vsync_back_porch_,
89 .vsync_front_porch = this->vsync_front_porch_,
90 },
91 .flags = {
92#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
93 .use_dma2d = true,
94#endif
95 }};
96 // clang-format on
97 err = esp_lcd_new_panel_dpi(this->bus_handle_, &dpi_config, &this->handle_);
98 if (err != ESP_OK) {
99 this->smark_failed(LOG_STR("esp_lcd_new_panel_dpi failed"), err);
100 return;
101 }
102#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
103 err = esp_lcd_dpi_panel_enable_dma2d(this->handle_);
104 if (err != ESP_OK) {
105 this->smark_failed(LOG_STR("esp_lcd_dpi_panel_enable_dma2d failed"), err);
106 return;
107 }
108#endif
109 if (this->reset_pin_ != nullptr) {
110 this->reset_pin_->setup();
111 this->reset_pin_->digital_write(true);
112 delay(5);
113 this->reset_pin_->digital_write(false);
114 delay(5);
115 this->reset_pin_->digital_write(true);
116 } else {
117 esp_lcd_panel_io_tx_param(this->io_handle_, SW_RESET_CMD, nullptr, 0);
118 }
119 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
120 auto when = millis() + 120;
121 err = esp_lcd_panel_init(this->handle_);
122 if (err != ESP_OK) {
123 this->smark_failed(LOG_STR("esp_lcd_init failed"), err);
124 return;
125 }
126 size_t index = 0;
127 const uint8_t *seq = this->init_sequence_;
128 const size_t len = this->init_sequence_len_;
129 while (index != len) {
130 if (len - index < 2) {
131 this->mark_failed(LOG_STR("Malformed init sequence"));
132 return;
133 }
134 uint8_t cmd = seq[index++];
135 uint8_t x = seq[index++];
136 if (x == DELAY_FLAG) {
137 ESP_LOGD(TAG, "Delay %dms", cmd);
138 delay(cmd);
139 } else {
140 uint8_t num_args = x & 0x7F;
141 if (len - index < num_args) {
142 this->mark_failed(LOG_STR("Malformed init sequence"));
143 return;
144 }
145 if (cmd == SLEEP_OUT) {
146 // are we ready, boots?
147 int duration = when - millis();
148 if (duration > 0) {
150 }
151 }
152 const auto *ptr = seq + index;
153#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
154 char hex_buf[format_hex_pretty_size(MIPI_DSI_MAX_CMD_LOG_BYTES)];
155#endif
156 ESP_LOGVV(TAG, "Command %02X, length %d, byte(s) %s", cmd, num_args,
157 format_hex_pretty_to(hex_buf, ptr, num_args, '.'));
158 err = esp_lcd_panel_io_tx_param(this->io_handle_, cmd, ptr, num_args);
159 if (err != ESP_OK) {
160 this->smark_failed(LOG_STR("lcd_panel_io_tx_param failed"), err);
161 return;
162 }
163 index += num_args;
164 if (cmd == SLEEP_OUT)
165 delay(10);
166 }
167 }
168 this->io_lock_ = xSemaphoreCreateBinary();
169 esp_lcd_dpi_panel_event_callbacks_t cbs = {
170 .on_color_trans_done = notify_refresh_ready,
171 };
172
173 err = (esp_lcd_dpi_panel_register_event_callbacks(this->handle_, &cbs, this->io_lock_));
174 if (err != ESP_OK) {
175 this->smark_failed(LOG_STR("Failed to register callbacks"), err);
176 return;
177 }
178
179 ESP_LOGCONFIG(TAG, "MIPI DSI setup complete");
180}
181
182void MipiDsi::update() {
183 if (this->auto_clear_enabled_) {
184 this->clear();
185 }
186 this->updating_ = true;
187 if (this->show_test_card_) {
188 this->test_card();
189 } else if (this->page_ != nullptr) {
190 this->page_->get_writer()(*this);
191 } else if (this->writer_.has_value()) {
192 (*this->writer_)(*this);
193 } else {
194 this->stop_poller();
195 }
196 this->updating_ = false;
197 this->flush_dirty_();
198}
199
201 // Inside update() the one flush at its end sends everything
202 if (this->updating_ || this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
203 return;
204 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_);
205 int w = this->x_high_ - this->x_low_ + 1;
206 int h = this->y_high_ - this->y_low_ + 1;
207 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_, this->y_low_,
208 this->width_ - w - this->x_low_);
209 // invalidate watermarks
210 this->x_low_ = this->width_;
211 this->y_low_ = this->height_;
212 this->x_high_ = 0;
213 this->y_high_ = 0;
214}
215
216void MipiDsi::draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr,
218 int x_pad) {
219 if (w <= 0 || h <= 0)
220 return;
221 if (bitness != this->color_depth_ || (bitness == display::COLOR_BITNESS_565 && big_endian != this->big_endian_) ||
224 this->mark_dirty_(x_start, y_start, w, h);
225 this->flush_dirty_();
226 return;
227 }
228 if (this->buffer_ != nullptr) {
229 // With a buffer the block goes through it, or a flush of the old contents would paint over it
230 const int first_col = std::max(0, -x_start), end_col = std::min(w, static_cast<int>(this->width_) - x_start);
231 const int first_row = std::max(0, -y_start), end_row = std::min(h, static_cast<int>(this->height_) - y_start);
232 if (first_col >= end_col || first_row >= end_row)
233 return;
234 const size_t bytes_per_pixel = 3 - this->color_depth_;
235 const size_t stride = x_offset + w + x_pad;
236 for (int y = first_row; y != end_row; y++) {
237 memcpy(this->buffer_ + ((y_start + y) * this->width_ + x_start + first_col) * bytes_per_pixel,
238 ptr + ((y_offset + y) * stride + x_offset + first_col) * bytes_per_pixel,
239 (end_col - first_col) * bytes_per_pixel);
240 }
241 if (this->updating_) {
242 this->mark_dirty_(x_start + first_col, y_start + first_row, end_col - first_col, end_row - first_row);
243 } else {
244 this->write_to_display_(x_start + first_col, y_start + first_row, end_col - first_col, end_row - first_row, ptr,
245 x_offset + first_col, y_offset + first_row, x_pad + w - end_col);
246 }
247 return;
248 }
249 this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
250}
251
252void MipiDsi::write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
253 int x_pad) {
254 esp_err_t err = ESP_OK;
255 auto bytes_per_pixel = 3 - this->color_depth_;
256 auto stride = (x_offset + w + x_pad) * bytes_per_pixel;
257 ptr += y_offset * stride + x_offset * bytes_per_pixel; // skip to the first pixel
258 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
259 if (x_offset == 0 && x_pad == 0) {
260 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
261 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
262
263 } else {
264 // draw line by line
265 for (int y = 0; y != h; y++) {
266 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1, ptr);
267 if (err != ESP_OK)
268 break;
269 ptr += stride; // next line
270 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
271 }
272 }
273 if (err != ESP_OK) {
274 ESP_LOGE(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
275 }
276}
277
279 if (this->is_failed())
280 return false;
281 if (this->buffer_ != nullptr)
282 return true;
283 // this is dependent on the enum values.
284 auto bytes_per_pixel = 3 - this->color_depth_;
285 RAMAllocator<uint8_t> allocator;
286 this->buffer_ = allocator.allocate(this->height_ * this->width_ * bytes_per_pixel);
287 if (this->buffer_ == nullptr) {
288 this->mark_failed(LOG_STR("Could not allocate buffer for display!"));
289 return false;
290 }
291 return true;
292}
293
294// Widen the dirty box to the drawn rectangle, even where the buffer already held the same values.
295void MipiDsi::mark_dirty_(int x_start, int y_start, int w, int h) {
296 int x1 = x_start, y1 = y_start;
297 int x2 = x_start + w - 1, y2 = y_start + h - 1;
298 this->rotate_(x1, y1);
299 this->rotate_(x2, y2);
300 const int right = static_cast<int>(this->width_) - 1;
301 const int bottom = static_cast<int>(this->height_) - 1;
302 const int left = std::clamp(std::min(x1, x2), 0, right);
303 const int top = std::clamp(std::min(y1, y2), 0, bottom);
304 x2 = std::clamp(std::max(x1, x2), 0, right);
305 y2 = std::clamp(std::max(y1, y2), 0, bottom);
306 // An empty box is low above high, so it is replaced rather than widened
307 if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_) {
308 this->x_low_ = left;
309 this->x_high_ = x2;
310 this->y_low_ = top;
311 this->y_high_ = y2;
312 return;
313 }
314 this->x_low_ = std::min<int>(this->x_low_, left);
315 this->x_high_ = std::max<int>(this->x_high_, x2);
316 this->y_low_ = std::min<int>(this->y_low_, top);
317 this->y_high_ = std::max<int>(this->y_high_, y2);
318}
319
320void MipiDsi::draw_pixel_at(int x, int y, Color color) {
321 if (this->is_point_clipped(x, y))
322 return;
323
324 this->rotate_(x, y);
325 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
326 return;
327 }
328 if (!this->check_buffer_())
329 return;
330 size_t pos = (y * this->width_) + x;
331 switch (this->color_depth_) {
333 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
334 const uint16_t new_color = this->to_panel_565_(color);
335 if (ptr_16[pos] == new_color)
336 return;
337 ptr_16[pos] = new_color;
338 break;
339 }
341 // The panel applies the colour order, so the buffer keeps the standard b,g,r layout
342 this->buffer_[pos * 3] = color.b;
343 this->buffer_[pos * 3 + 1] = color.g;
344 this->buffer_[pos * 3 + 2] = color.r;
345 break;
347 break;
348 }
349 // low and high watermark may speed up drawing from buffer
351 this->x_low_ = x;
353 this->y_low_ = y;
354 if (x > this->x_high_)
355 this->x_high_ = x;
356 if (y > this->y_high_)
357 this->y_high_ = y;
358}
359void MipiDsi::fill(Color color) {
360 if (!this->check_buffer_())
361 return;
362
363 // If clipping is active, fall back to base implementation
364 if (this->get_clipping().is_set()) {
365 Display::fill(color);
366 return;
367 }
368
369 switch (this->color_depth_) {
371 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
372 const uint16_t new_color = this->to_panel_565_(color);
373 std::fill_n(ptr_16, this->width_ * this->height_, new_color);
374 break;
375 }
376
378 for (size_t i = 0; i != this->width_ * this->height_; i++) {
379 this->buffer_[i * 3 + 0] = color.b;
380 this->buffer_[i * 3 + 1] = color.g;
381 this->buffer_[i * 3 + 2] = color.r;
382 }
383 break;
384
385 default:
386 break;
387 }
388}
389
390int MipiDsi::get_width() {
391 switch (this->rotation_) {
394 return this->get_height_internal();
397 default:
398 return this->get_width_internal();
399 }
400}
401
403 switch (this->rotation_) {
406 return this->get_height_internal();
409 default:
410 return this->get_width_internal();
411 }
412}
413
414static const uint8_t PIXEL_MODES[] = {0, 16, 18, 24};
415
417 ESP_LOGCONFIG(TAG,
418 "MIPI_DSI RGB LCD"
419 "\n Model: %s"
420 "\n Width: %u"
421 "\n Height: %u"
422 "\n Rotation: %d degrees"
423 "\n DSI Lanes: %u"
424 "\n Lane Bit Rate: %.0fMbps"
425 "\n HSync Pulse Width: %u"
426 "\n HSync Back Porch: %u"
427 "\n HSync Front Porch: %u"
428 "\n VSync Pulse Width: %u"
429 "\n VSync Back Porch: %u"
430 "\n VSync Front Porch: %u"
431 "\n Buffer Color Depth: %d bit"
432 "\n Display Pixel Mode: %d bit"
433 "\n Invert Colors: %s"
434 "\n Pixel Clock: %.1fMHz",
435 this->model_, this->width_, this->height_, this->rotation_, this->lanes_, this->lane_bit_rate_,
437 this->vsync_back_porch_, this->vsync_front_porch_, (3 - this->color_depth_) * 8, this->pixel_mode_,
438 YESNO(this->invert_colors_), this->pclk_frequency_);
439 LOG_PIN(" Reset Pin ", this->reset_pin_);
440}
441} // namespace esphome::mipi_dsi
442#endif // USE_ESP32_VARIANT_ESP32P4
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
virtual void setup()=0
virtual void digital_write(bool value)=0
display_writer_t writer_
Definition display.h:864
int int int const uint8_t ColorOrder ColorBitness bitness
Definition display.h:392
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:17
bool is_clipping() const
Definition display.h:805
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int int int x_pad
Definition display.h:392
int int int const uint8_t * ptr
Definition display.h:391
bool ESPHOME_ALWAYS_INLINE is_point_clipped(int x, int y) const
Whether (x, y) falls outside the active clipping rectangle.
Definition display.h:810
DisplayPage * page_
Definition display.h:865
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Definition display.h:392
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:826
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
Definition display.h:392
DisplayRotation rotation_
Definition display.h:863
virtual void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Draws a block for draw_pixels_at(); the parameters are the same.
Definition display.cpp:119
const display_writer_t & get_writer() const
Definition display.cpp:956
void mark_dirty_(int x_start, int y_start, int w, int h)
display::ColorBitness color_depth_
Definition mipi_dsi.h:135
esp_lcd_panel_handle_t handle_
Definition mipi_dsi.h:140
int get_height_internal() override
Definition mipi_dsi.h:54
esp_lcd_panel_io_handle_t io_handle_
Definition mipi_dsi.h:142
uint16_t to_panel_565_(Color color) const
Pack a colour as the 565 value the panel's byte order expects in memory.
Definition mipi_dsi.h:112
int get_width_internal() override
Definition mipi_dsi.h:53
void draw_pixel_at(int x, int y, Color color) override
std::vector< GPIOPin * > enable_pins_
Definition mipi_dsi.h: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)
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
SemaphoreHandle_t io_lock_
Definition mipi_dsi.h:143
void smark_failed(const LogString *message, esp_err_t err)
esp_lcd_dsi_bus_handle_t bus_handle_
Definition mipi_dsi.h:141
void fill(Color color) override
const uint8_t * init_sequence_
Definition mipi_dsi.h:128
void flush_dirty_()
Send the region touched since the last flush to the panel.
void dump_config() override
void rotate_(int &x, int &y) const
Definition mipi_dsi.h:92
const LogString * message
Definition component.cpp:35
uint8_t duration
Definition msa3xx.h:0
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:136
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:139
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:138
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:137
static void int x_start
Definition display.cpp:62
const char * esp_err_to_name(esp_err_t err)
const uint8_t DELAY_FLAG
Definition mipi_dsi.h:34
const uint8_t SLEEP_OUT
Definition mipi_dsi.h:27
const uint8_t SW_RESET_CMD
Definition mipi_dsi.h:26
const void size_t len
Definition hal.h:68
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:498
size_t size_t pos
Definition helpers.h:1255
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:1609
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
uint16_t seq
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6