ESPHome 2026.6.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
6namespace esphome::mipi_dsi {
7
8// Maximum bytes to log for init commands (truncated if larger)
9static constexpr size_t MIPI_DSI_MAX_CMD_LOG_BYTES = 64;
10
11static bool notify_refresh_ready(esp_lcd_panel_handle_t panel, esp_lcd_dpi_panel_event_data_t *edata, void *user_ctx) {
12 SemaphoreHandle_t sem = static_cast<SemaphoreHandle_t>(user_ctx);
13 BaseType_t need_yield = pdFALSE;
14 xSemaphoreGiveFromISR(sem, &need_yield);
15 return (need_yield == pdTRUE);
16}
17
18void MipiDsi::smark_failed(const LogString *message, esp_err_t err) {
19 ESP_LOGE(TAG, "%s: %s", LOG_STR_ARG(message), esp_err_to_name(err));
20 this->mark_failed(message);
21}
22
23void MipiDsi::setup() {
24 ESP_LOGCONFIG(TAG, "Running Setup");
25
26 if (!this->enable_pins_.empty()) {
27 for (auto *pin : this->enable_pins_) {
28 pin->setup();
29 pin->digital_write(true);
30 }
31 delay(10);
32 }
33
34 esp_lcd_dsi_bus_config_t bus_config = {
35 .bus_id = 0, // index from 0, specify the DSI host to use
36 .num_data_lanes =
37 this->lanes_, // Number of data lanes to use, can't set a value that exceeds the chip's capability
38 .phy_clk_src = MIPI_DSI_PHY_CLK_SRC_DEFAULT, // Clock source for the DPHY
39 .lane_bit_rate_mbps = this->lane_bit_rate_, // Bit rate of the data lanes, in Mbps
40 };
41 auto err = esp_lcd_new_dsi_bus(&bus_config, &this->bus_handle_);
42 if (err != ESP_OK) {
43 this->smark_failed(LOG_STR("lcd_new_dsi_bus failed"), err);
44 return;
45 }
46 esp_lcd_dbi_io_config_t dbi_config = {
47 .virtual_channel = 0,
48 .lcd_cmd_bits = 8, // according to the LCD spec
49 .lcd_param_bits = 8, // according to the LCD spec
50 };
51 err = esp_lcd_new_panel_io_dbi(this->bus_handle_, &dbi_config, &this->io_handle_);
52 if (err != ESP_OK) {
53 this->smark_failed(LOG_STR("new_panel_io_dbi failed"), err);
54 return;
55 }
56 // clang-format off
57#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
58 auto color_format = LCD_COLOR_FMT_RGB565;
60 color_format = LCD_COLOR_FMT_RGB888;
61 }
62 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
63 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
64 .dpi_clock_freq_mhz = this->pclk_frequency_,
65 .in_color_format = color_format,
66#else
67 auto pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565;
69 pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB888;
70 }
71 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
72 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
73 .dpi_clock_freq_mhz = this->pclk_frequency_,
74 .pixel_format = pixel_format,
75#endif
76 .num_fbs = 1, // number of frame buffers to allocate
77 .video_timing =
78 {
79 .h_size = this->width_,
80 .v_size = this->height_,
81 .hsync_pulse_width = this->hsync_pulse_width_,
82 .hsync_back_porch = this->hsync_back_porch_,
83 .hsync_front_porch = this->hsync_front_porch_,
84 .vsync_pulse_width = this->vsync_pulse_width_,
85 .vsync_back_porch = this->vsync_back_porch_,
86 .vsync_front_porch = this->vsync_front_porch_,
87 },
88 .flags = {
89#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
90 .use_dma2d = true,
91#endif
92 }};
93 // clang-format on
94 err = esp_lcd_new_panel_dpi(this->bus_handle_, &dpi_config, &this->handle_);
95 if (err != ESP_OK) {
96 this->smark_failed(LOG_STR("esp_lcd_new_panel_dpi failed"), err);
97 return;
98 }
99#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
100 err = esp_lcd_dpi_panel_enable_dma2d(this->handle_);
101 if (err != ESP_OK) {
102 this->smark_failed(LOG_STR("esp_lcd_dpi_panel_enable_dma2d failed"), err);
103 return;
104 }
105#endif
106 if (this->reset_pin_ != nullptr) {
107 this->reset_pin_->setup();
108 this->reset_pin_->digital_write(true);
109 delay(5);
110 this->reset_pin_->digital_write(false);
111 delay(5);
112 this->reset_pin_->digital_write(true);
113 } else {
114 esp_lcd_panel_io_tx_param(this->io_handle_, SW_RESET_CMD, nullptr, 0);
115 }
116 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
117 auto when = millis() + 120;
118 err = esp_lcd_panel_init(this->handle_);
119 if (err != ESP_OK) {
120 this->smark_failed(LOG_STR("esp_lcd_init failed"), err);
121 return;
122 }
123 size_t index = 0;
124 auto &vec = this->init_sequence_;
125 while (index != vec.size()) {
126 if (vec.size() - index < 2) {
127 this->mark_failed(LOG_STR("Malformed init sequence"));
128 return;
129 }
130 uint8_t cmd = vec[index++];
131 uint8_t x = vec[index++];
132 if (x == DELAY_FLAG) {
133 ESP_LOGD(TAG, "Delay %dms", cmd);
134 delay(cmd);
135 } else {
136 uint8_t num_args = x & 0x7F;
137 if (vec.size() - index < num_args) {
138 this->mark_failed(LOG_STR("Malformed init sequence"));
139 return;
140 }
141 if (cmd == SLEEP_OUT) {
142 // are we ready, boots?
143 int duration = when - millis();
144 if (duration > 0) {
146 }
147 }
148 const auto *ptr = vec.data() + index;
149#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
150 char hex_buf[format_hex_pretty_size(MIPI_DSI_MAX_CMD_LOG_BYTES)];
151#endif
152 ESP_LOGVV(TAG, "Command %02X, length %d, byte(s) %s", cmd, num_args,
153 format_hex_pretty_to(hex_buf, ptr, num_args, '.'));
154 err = esp_lcd_panel_io_tx_param(this->io_handle_, cmd, ptr, num_args);
155 if (err != ESP_OK) {
156 this->smark_failed(LOG_STR("lcd_panel_io_tx_param failed"), err);
157 return;
158 }
159 index += num_args;
160 if (cmd == SLEEP_OUT)
161 delay(10);
162 }
163 }
164 this->io_lock_ = xSemaphoreCreateBinary();
165 esp_lcd_dpi_panel_event_callbacks_t cbs = {
166 .on_color_trans_done = notify_refresh_ready,
167 };
168
169 err = (esp_lcd_dpi_panel_register_event_callbacks(this->handle_, &cbs, this->io_lock_));
170 if (err != ESP_OK) {
171 this->smark_failed(LOG_STR("Failed to register callbacks"), err);
172 return;
173 }
174
175 ESP_LOGCONFIG(TAG, "MIPI DSI setup complete");
176}
177
178void MipiDsi::update() {
179 if (this->auto_clear_enabled_) {
180 this->clear();
181 }
182 if (this->show_test_card_) {
183 this->test_card();
184 } else if (this->page_ != nullptr) {
185 this->page_->get_writer()(*this);
186 } else if (this->writer_.has_value()) {
187 (*this->writer_)(*this);
188 } else {
189 this->stop_poller();
190 }
191 if (this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
192 return;
193 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_);
194 int w = this->x_high_ - this->x_low_ + 1;
195 int h = this->y_high_ - this->y_low_ + 1;
196 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_, this->y_low_,
197 this->width_ - w - this->x_low_);
198 // invalidate watermarks
199 this->x_low_ = this->width_;
200 this->y_low_ = this->height_;
201 this->x_high_ = 0;
202 this->y_high_ = 0;
203}
204
205void MipiDsi::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
206 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
207 if (w <= 0 || h <= 0)
208 return;
209 // if color mapping is required, pass the buck.
210 // note that endianness is not considered here - it is assumed to match!
211 if (bitness != this->color_depth_) {
212 display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
213 x_pad);
214 return;
215 }
216 this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
217}
218
219void MipiDsi::write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
220 int x_pad) {
221 esp_err_t err = ESP_OK;
222 auto bytes_per_pixel = 3 - this->color_depth_;
223 auto stride = (x_offset + w + x_pad) * bytes_per_pixel;
224 ptr += y_offset * stride + x_offset * bytes_per_pixel; // skip to the first pixel
225 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
226 if (x_offset == 0 && x_pad == 0) {
227 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
228 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
229
230 } else {
231 // draw line by line
232 for (int y = 0; y != h; y++) {
233 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1, ptr);
234 if (err != ESP_OK)
235 break;
236 ptr += stride; // next line
237 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
238 }
239 }
240 if (err != ESP_OK)
241 ESP_LOGE(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
242}
243
245 if (this->is_failed())
246 return false;
247 if (this->buffer_ != nullptr)
248 return true;
249 // this is dependent on the enum values.
250 auto bytes_per_pixel = 3 - this->color_depth_;
251 RAMAllocator<uint8_t> allocator;
252 this->buffer_ = allocator.allocate(this->height_ * this->width_ * bytes_per_pixel);
253 if (this->buffer_ == nullptr) {
254 this->mark_failed(LOG_STR("Could not allocate buffer for display!"));
255 return false;
256 }
257 return true;
258}
259
260void MipiDsi::draw_pixel_at(int x, int y, Color color) {
261 if (!this->get_clipping().inside(x, y))
262 return;
263
264 switch (this->rotation_) {
266 break;
268 std::swap(x, y);
269 x = this->width_ - x - 1;
270 break;
272 x = this->width_ - x - 1;
273 y = this->height_ - y - 1;
274 break;
276 std::swap(x, y);
277 y = this->height_ - y - 1;
278 break;
279 }
280 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
281 return;
282 }
283 if (!this->check_buffer_())
284 return;
285 size_t pos = (y * this->width_) + x;
286 switch (this->color_depth_) {
288 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
289 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
290 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
291 uint16_t new_color = lo_byte | (hi_byte << 8); // little endian
292 if (ptr_16[pos] == new_color)
293 return;
294 ptr_16[pos] = new_color;
295 break;
296 }
299 this->buffer_[pos * 3] = color.b;
300 this->buffer_[pos * 3 + 1] = color.g;
301 this->buffer_[pos * 3 + 2] = color.r;
302 } else {
303 this->buffer_[pos * 3] = color.r;
304 this->buffer_[pos * 3 + 1] = color.g;
305 this->buffer_[pos * 3 + 2] = color.b;
306 }
307 break;
309 break;
310 }
311 // low and high watermark may speed up drawing from buffer
313 this->x_low_ = x;
315 this->y_low_ = y;
316 if (x > this->x_high_)
317 this->x_high_ = x;
318 if (y > this->y_high_)
319 this->y_high_ = y;
320}
321void MipiDsi::fill(Color color) {
322 if (!this->check_buffer_())
323 return;
324
325 // If clipping is active, fall back to base implementation
326 if (this->get_clipping().is_set()) {
327 Display::fill(color);
328 return;
329 }
330
331 switch (this->color_depth_) {
333 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
334 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
335 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
336 uint16_t new_color = lo_byte | (hi_byte << 8); // little endian
337 std::fill_n(ptr_16, this->width_ * this->height_, new_color);
338 break;
339 }
340
343 for (size_t i = 0; i != this->width_ * this->height_; i++) {
344 this->buffer_[i * 3 + 0] = color.b;
345 this->buffer_[i * 3 + 1] = color.g;
346 this->buffer_[i * 3 + 2] = color.r;
347 }
348 } else {
349 for (size_t i = 0; i != this->width_ * this->height_; i++) {
350 this->buffer_[i * 3 + 0] = color.r;
351 this->buffer_[i * 3 + 1] = color.g;
352 this->buffer_[i * 3 + 2] = color.b;
353 }
354 }
355
356 default:
357 break;
358 }
359}
360
361int MipiDsi::get_width() {
362 switch (this->rotation_) {
365 return this->get_height_internal();
368 default:
369 return this->get_width_internal();
370 }
371}
372
374 switch (this->rotation_) {
377 return this->get_height_internal();
380 default:
381 return this->get_width_internal();
382 }
383}
384
385static const uint8_t PIXEL_MODES[] = {0, 16, 18, 24};
386
388 ESP_LOGCONFIG(TAG,
389 "MIPI_DSI RGB LCD"
390 "\n Model: %s"
391 "\n Width: %u"
392 "\n Height: %u"
393 "\n Rotation: %d degrees"
394 "\n DSI Lanes: %u"
395 "\n Lane Bit Rate: %.0fMbps"
396 "\n HSync Pulse Width: %u"
397 "\n HSync Back Porch: %u"
398 "\n HSync Front Porch: %u"
399 "\n VSync Pulse Width: %u"
400 "\n VSync Back Porch: %u"
401 "\n VSync Front Porch: %u"
402 "\n Buffer Color Depth: %d bit"
403 "\n Display Pixel Mode: %d bit"
404 "\n Invert Colors: %s"
405 "\n Pixel Clock: %.1fMHz",
406 this->model_, this->width_, this->height_, this->rotation_, this->lanes_, this->lane_bit_rate_,
408 this->vsync_back_porch_, this->vsync_front_porch_, (3 - this->color_depth_) * 8, this->pixel_mode_,
409 YESNO(this->invert_colors_), this->pclk_frequency_);
410 LOG_PIN(" Reset Pin ", this->reset_pin_);
411}
412} // namespace esphome::mipi_dsi
413#endif // USE_ESP32_VARIANT_ESP32P4
uint8_t h
Definition bl0906.h:2
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
virtual void setup()=0
virtual void digital_write(bool value)=0
display_writer_t writer_
Definition display.h:790
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:15
DisplayPage * page_
Definition display.h:791
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
DisplayRotation rotation_
Definition display.h:789
virtual void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorOrder order, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Given an array of pixels encoded in the nominated format, draw these into the display's buffer.
Definition display.cpp:54
const display_writer_t & get_writer() const
Definition display.cpp:898
display::ColorOrder color_mode_
Definition mipi_dsi.h:100
display::ColorBitness color_depth_
Definition mipi_dsi.h:101
esp_lcd_panel_handle_t handle_
Definition mipi_dsi.h:104
std::vector< uint8_t > init_sequence_
Definition mipi_dsi.h:94
int get_height_internal() override
Definition mipi_dsi.h:51
esp_lcd_panel_io_handle_t io_handle_
Definition mipi_dsi.h:106
int get_width_internal() override
Definition mipi_dsi.h:50
void draw_pixel_at(int x, int y, Color color) override
std::vector< GPIOPin * > enable_pins_
Definition mipi_dsi.h:84
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)
SemaphoreHandle_t io_lock_
Definition mipi_dsi.h:107
void smark_failed(const LogString *message, esp_err_t err)
esp_lcd_dsi_bus_handle_t bus_handle_
Definition mipi_dsi.h:105
void fill(Color color) override
void dump_config() override
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
const LogString * message
Definition component.cpp:35
uint8_t duration
Definition msa3xx.h:0
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:134
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:137
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:136
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:135
const uint8_t DELAY_FLAG
Definition mipi_dsi.h:30
const uint8_t SLEEP_OUT
Definition mipi_dsi.h:23
const uint8_t SW_RESET_CMD
Definition mipi_dsi.h:22
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:340
size_t size_t pos
Definition helpers.h:1038
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:1386
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6