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