ESPHome 2026.3.0
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
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 auto pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565;
59 pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB888;
60 }
61 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
62 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
63 .dpi_clock_freq_mhz = this->pclk_frequency_,
64 .pixel_format = pixel_format,
65 .num_fbs = 1, // number of frame buffers to allocate
66 .video_timing =
67 {
68 .h_size = this->width_,
69 .v_size = this->height_,
70 .hsync_pulse_width = this->hsync_pulse_width_,
71 .hsync_back_porch = this->hsync_back_porch_,
72 .hsync_front_porch = this->hsync_front_porch_,
73 .vsync_pulse_width = this->vsync_pulse_width_,
74 .vsync_back_porch = this->vsync_back_porch_,
75 .vsync_front_porch = this->vsync_front_porch_,
76 },
77 .flags = {
78 .use_dma2d = true,
79 }};
80 err = esp_lcd_new_panel_dpi(this->bus_handle_, &dpi_config, &this->handle_);
81 if (err != ESP_OK) {
82 this->smark_failed(LOG_STR("esp_lcd_new_panel_dpi failed"), err);
83 return;
84 }
85 if (this->reset_pin_ != nullptr) {
86 this->reset_pin_->setup();
87 this->reset_pin_->digital_write(true);
88 delay(5);
89 this->reset_pin_->digital_write(false);
90 delay(5);
91 this->reset_pin_->digital_write(true);
92 } else {
93 esp_lcd_panel_io_tx_param(this->io_handle_, SW_RESET_CMD, nullptr, 0);
94 }
95 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
96 auto when = millis() + 120;
97 err = esp_lcd_panel_init(this->handle_);
98 if (err != ESP_OK) {
99 this->smark_failed(LOG_STR("esp_lcd_init failed"), err);
100 return;
101 }
102 size_t index = 0;
103 auto &vec = this->init_sequence_;
104 while (index != vec.size()) {
105 if (vec.size() - index < 2) {
106 this->mark_failed(LOG_STR("Malformed init sequence"));
107 return;
108 }
109 uint8_t cmd = vec[index++];
110 uint8_t x = vec[index++];
111 if (x == DELAY_FLAG) {
112 ESP_LOGD(TAG, "Delay %dms", cmd);
113 delay(cmd);
114 } else {
115 uint8_t num_args = x & 0x7F;
116 if (vec.size() - index < num_args) {
117 this->mark_failed(LOG_STR("Malformed init sequence"));
118 return;
119 }
120 if (cmd == SLEEP_OUT) {
121 // are we ready, boots?
122 int duration = when - millis();
123 if (duration > 0) {
125 }
126 }
127 const auto *ptr = vec.data() + index;
128#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
129 char hex_buf[format_hex_pretty_size(MIPI_DSI_MAX_CMD_LOG_BYTES)];
130#endif
131 ESP_LOGVV(TAG, "Command %02X, length %d, byte(s) %s", cmd, num_args,
132 format_hex_pretty_to(hex_buf, ptr, num_args, '.'));
133 err = esp_lcd_panel_io_tx_param(this->io_handle_, cmd, ptr, num_args);
134 if (err != ESP_OK) {
135 this->smark_failed(LOG_STR("lcd_panel_io_tx_param failed"), err);
136 return;
137 }
138 index += num_args;
139 if (cmd == SLEEP_OUT)
140 delay(10);
141 }
142 }
143 this->io_lock_ = xSemaphoreCreateBinary();
144 esp_lcd_dpi_panel_event_callbacks_t cbs = {
145 .on_color_trans_done = notify_refresh_ready,
146 };
147
148 err = (esp_lcd_dpi_panel_register_event_callbacks(this->handle_, &cbs, this->io_lock_));
149 if (err != ESP_OK) {
150 this->smark_failed(LOG_STR("Failed to register callbacks"), err);
151 return;
152 }
153
154 ESP_LOGCONFIG(TAG, "MIPI DSI setup complete");
155}
156
158 if (this->auto_clear_enabled_) {
159 this->clear();
160 }
161 if (this->show_test_card_) {
162 this->test_card();
163 } else if (this->page_ != nullptr) {
164 this->page_->get_writer()(*this);
165 } else if (this->writer_.has_value()) {
166 (*this->writer_)(*this);
167 } else {
168 this->stop_poller();
169 }
170 if (this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
171 return;
172 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_);
173 int w = this->x_high_ - this->x_low_ + 1;
174 int h = this->y_high_ - this->y_low_ + 1;
175 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_, this->y_low_,
176 this->width_ - w - this->x_low_);
177 // invalidate watermarks
178 this->x_low_ = this->width_;
179 this->y_low_ = this->height_;
180 this->x_high_ = 0;
181 this->y_high_ = 0;
182}
183
184void MIPI_DSI::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
185 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
186 if (w <= 0 || h <= 0)
187 return;
188 // if color mapping is required, pass the buck.
189 // note that endianness is not considered here - it is assumed to match!
190 if (bitness != this->color_depth_) {
191 display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
192 x_pad);
193 return;
194 }
195 this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
196}
197
198void 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,
199 int x_pad) {
200 esp_err_t err = ESP_OK;
201 auto bytes_per_pixel = 3 - this->color_depth_;
202 auto stride = (x_offset + w + x_pad) * bytes_per_pixel;
203 ptr += y_offset * stride + x_offset * bytes_per_pixel; // skip to the first pixel
204 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
205 if (x_offset == 0 && x_pad == 0) {
206 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
207 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
208
209 } else {
210 // draw line by line
211 for (int y = 0; y != h; y++) {
212 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1, ptr);
213 if (err != ESP_OK)
214 break;
215 ptr += stride; // next line
216 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
217 }
218 }
219 if (err != ESP_OK)
220 ESP_LOGE(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
221}
222
224 if (this->is_failed())
225 return false;
226 if (this->buffer_ != nullptr)
227 return true;
228 // this is dependent on the enum values.
229 auto bytes_per_pixel = 3 - this->color_depth_;
230 RAMAllocator<uint8_t> allocator;
231 this->buffer_ = allocator.allocate(this->height_ * this->width_ * bytes_per_pixel);
232 if (this->buffer_ == nullptr) {
233 this->mark_failed(LOG_STR("Could not allocate buffer for display!"));
234 return false;
235 }
236 return true;
237}
238
239void MIPI_DSI::draw_pixel_at(int x, int y, Color color) {
240 if (!this->get_clipping().inside(x, y))
241 return;
242
243 switch (this->rotation_) {
245 break;
247 std::swap(x, y);
248 x = this->width_ - x - 1;
249 break;
251 x = this->width_ - x - 1;
252 y = this->height_ - y - 1;
253 break;
255 std::swap(x, y);
256 y = this->height_ - y - 1;
257 break;
258 }
259 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
260 return;
261 }
263 if (!this->check_buffer_())
264 return;
265 size_t pos = (y * this->width_) + x;
266 switch (this->color_depth_) {
268 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
269 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
270 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
271 uint16_t new_color = lo_byte | (hi_byte << 8); // little endian
272 if (ptr_16[pos] == new_color)
273 return;
274 ptr_16[pos] = new_color;
275 break;
276 }
279 this->buffer_[pos * 3] = color.b;
280 this->buffer_[pos * 3 + 1] = color.g;
281 this->buffer_[pos * 3 + 2] = color.r;
282 } else {
283 this->buffer_[pos * 3] = color.r;
284 this->buffer_[pos * 3 + 1] = color.g;
285 this->buffer_[pos * 3 + 2] = color.b;
286 }
287 break;
289 break;
290 }
291 // low and high watermark may speed up drawing from buffer
293 this->x_low_ = x;
295 this->y_low_ = y;
296 if (x > this->x_high_)
297 this->x_high_ = x;
298 if (y > this->y_high_)
299 this->y_high_ = y;
300}
302 if (!this->check_buffer_())
303 return;
304
305 // If clipping is active, fall back to base implementation
306 if (this->get_clipping().is_set()) {
307 Display::fill(color);
308 return;
309 }
310
311 switch (this->color_depth_) {
313 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
314 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
315 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
316 uint16_t new_color = lo_byte | (hi_byte << 8); // little endian
317 std::fill_n(ptr_16, this->width_ * this->height_, new_color);
318 break;
319 }
320
323 for (size_t i = 0; i != this->width_ * this->height_; i++) {
324 this->buffer_[i * 3 + 0] = color.b;
325 this->buffer_[i * 3 + 1] = color.g;
326 this->buffer_[i * 3 + 2] = color.r;
327 }
328 } else {
329 for (size_t i = 0; i != this->width_ * this->height_; i++) {
330 this->buffer_[i * 3 + 0] = color.r;
331 this->buffer_[i * 3 + 1] = color.g;
332 this->buffer_[i * 3 + 2] = color.b;
333 }
334 }
335
336 default:
337 break;
338 }
339}
340
352
364
365static const uint8_t PIXEL_MODES[] = {0, 16, 18, 24};
366
368 ESP_LOGCONFIG(TAG,
369 "MIPI_DSI RGB LCD"
370 "\n Model: %s"
371 "\n Width: %u"
372 "\n Height: %u"
373 "\n Mirror X: %s"
374 "\n Mirror Y: %s"
375 "\n Swap X/Y: %s"
376 "\n Rotation: %d degrees"
377 "\n DSI Lanes: %u"
378 "\n Lane Bit Rate: %.0fMbps"
379 "\n HSync Pulse Width: %u"
380 "\n HSync Back Porch: %u"
381 "\n HSync Front Porch: %u"
382 "\n VSync Pulse Width: %u"
383 "\n VSync Back Porch: %u"
384 "\n VSync Front Porch: %u"
385 "\n Buffer Color Depth: %d bit"
386 "\n Display Pixel Mode: %d bit"
387 "\n Color Order: %s"
388 "\n Invert Colors: %s"
389 "\n Pixel Clock: %.1fMHz",
390 this->model_, this->width_, this->height_, YESNO(this->madctl_ & (MADCTL_XFLIP | MADCTL_MX)),
391 YESNO(this->madctl_ & (MADCTL_YFLIP | MADCTL_MY)), YESNO(this->madctl_ & MADCTL_MV), this->rotation_,
394 (3 - this->color_depth_) * 8, this->pixel_mode_, this->madctl_ & MADCTL_BGR ? "BGR" : "RGB",
395 YESNO(this->invert_colors_), this->pclk_frequency_);
396 LOG_PIN(" Reset Pin ", this->reset_pin_);
397}
398} // namespace mipi_dsi
399} // namespace esphome
400#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:233
virtual void setup()=0
virtual void digital_write(bool value)=0
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:1899
T * allocate(size_t n)
Definition helpers.h:1916
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
Definition mipi_dsi.cpp:301
display::ColorOrder color_mode_
Definition mipi_dsi.h:103
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_dsi.cpp:184
void dump_config() override
Definition mipi_dsi.cpp:367
std::vector< GPIOPin * > enable_pins_
Definition mipi_dsi.h:86
void smark_failed(const LogString *message, esp_err_t err)
Definition mipi_dsi.cpp:19
display::ColorBitness color_depth_
Definition mipi_dsi.h:104
SemaphoreHandle_t io_lock_
Definition mipi_dsi.h:110
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_dsi.cpp:198
esp_lcd_panel_io_handle_t io_handle_
Definition mipi_dsi.h:109
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_dsi.cpp:239
int get_width_internal() override
Definition mipi_dsi.h:51
esp_lcd_dsi_bus_handle_t bus_handle_
Definition mipi_dsi.h:108
std::vector< uint8_t > init_sequence_
Definition mipi_dsi.h:97
int get_height_internal() override
Definition mipi_dsi.h:52
esp_lcd_panel_handle_t handle_
Definition mipi_dsi.h:107
const char * message
Definition component.cpp:38
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 MADCTL_YFLIP
Definition mipi_dsi.h:37
const uint8_t MADCTL_MV
Definition mipi_dsi.h:35
const uint8_t MADCTL_MX
Definition mipi_dsi.h:33
const uint8_t MADCTL_MY
Definition mipi_dsi.h:34
const uint8_t MADCTL_XFLIP
Definition mipi_dsi.h:36
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
const uint8_t MADCTL_BGR
Definition mipi_dsi.h:32
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:784
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:353
size_t size_t pos
Definition helpers.h:929
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:1200
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
uint8_t g
Definition color.h:34
uint8_t b
Definition color.h:38
uint8_t r
Definition color.h:30
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6