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