ESPHome 2026.1.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
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 }
194 this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
195}
196
197void 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,
198 int x_pad) {
199 esp_err_t err = ESP_OK;
200 auto bytes_per_pixel = 3 - this->color_depth_;
201 auto stride = (x_offset + w + x_pad) * bytes_per_pixel;
202 ptr += y_offset * stride + x_offset * bytes_per_pixel; // skip to the first pixel
203 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
204 if (x_offset == 0 && x_pad == 0) {
205 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
206 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
207
208 } else {
209 // draw line by line
210 for (int y = 0; y != h; y++) {
211 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1, ptr);
212 if (err != ESP_OK)
213 break;
214 ptr += stride; // next line
215 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
216 }
217 }
218 if (err != ESP_OK)
219 ESP_LOGE(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
220}
221
223 if (this->is_failed())
224 return false;
225 if (this->buffer_ != nullptr)
226 return true;
227 // this is dependent on the enum values.
228 auto bytes_per_pixel = 3 - this->color_depth_;
229 RAMAllocator<uint8_t> allocator;
230 this->buffer_ = allocator.allocate(this->height_ * this->width_ * bytes_per_pixel);
231 if (this->buffer_ == nullptr) {
232 this->mark_failed(LOG_STR("Could not allocate buffer for display!"));
233 return false;
234 }
235 return true;
236}
237
238void MIPI_DSI::draw_pixel_at(int x, int y, Color color) {
239 if (!this->get_clipping().inside(x, y))
240 return;
241
242 switch (this->rotation_) {
244 break;
246 std::swap(x, y);
247 x = this->width_ - x - 1;
248 break;
250 x = this->width_ - x - 1;
251 y = this->height_ - y - 1;
252 break;
254 std::swap(x, y);
255 y = this->height_ - y - 1;
256 break;
257 }
258 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
259 return;
260 }
262 if (!this->check_buffer_())
263 return;
264 size_t pos = (y * this->width_) + x;
265 switch (this->color_depth_) {
267 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
268 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
269 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
270 uint16_t new_color = lo_byte | (hi_byte << 8); // little endian
271 if (ptr_16[pos] == new_color)
272 return;
273 ptr_16[pos] = new_color;
274 break;
275 }
278 this->buffer_[pos * 3] = color.b;
279 this->buffer_[pos * 3 + 1] = color.g;
280 this->buffer_[pos * 3 + 2] = color.r;
281 } else {
282 this->buffer_[pos * 3] = color.r;
283 this->buffer_[pos * 3 + 1] = color.g;
284 this->buffer_[pos * 3 + 2] = color.b;
285 }
286 break;
288 break;
289 }
290 // low and high watermark may speed up drawing from buffer
292 this->x_low_ = x;
294 this->y_low_ = y;
295 if (x > this->x_high_)
296 this->x_high_ = x;
297 if (y > this->y_high_)
298 this->y_high_ = y;
299}
301 if (!this->check_buffer_())
302 return;
303
304 // If clipping is active, fall back to base implementation
305 if (this->get_clipping().is_set()) {
306 Display::fill(color);
307 return;
308 }
309
310 switch (this->color_depth_) {
312 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
313 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
314 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
315 uint16_t new_color = lo_byte | (hi_byte << 8); // little endian
316 std::fill_n(ptr_16, this->width_ * this->height_, new_color);
317 break;
318 }
319
322 for (size_t i = 0; i != this->width_ * this->height_; i++) {
323 this->buffer_[i * 3 + 0] = color.b;
324 this->buffer_[i * 3 + 1] = color.g;
325 this->buffer_[i * 3 + 2] = color.r;
326 }
327 } else {
328 for (size_t i = 0; i != this->width_ * this->height_; i++) {
329 this->buffer_[i * 3 + 0] = color.r;
330 this->buffer_[i * 3 + 1] = color.g;
331 this->buffer_[i * 3 + 2] = color.b;
332 }
333 }
334
335 default:
336 break;
337 }
338}
339
351
363
364static const uint8_t PIXEL_MODES[] = {0, 16, 18, 24};
365
367 ESP_LOGCONFIG(TAG,
368 "MIPI_DSI RGB LCD"
369 "\n Model: %s"
370 "\n Width: %u"
371 "\n Height: %u"
372 "\n Mirror X: %s"
373 "\n Mirror Y: %s"
374 "\n Swap X/Y: %s"
375 "\n Rotation: %d degrees"
376 "\n DSI Lanes: %u"
377 "\n Lane Bit Rate: %uMbps"
378 "\n HSync Pulse Width: %u"
379 "\n HSync Back Porch: %u"
380 "\n HSync Front Porch: %u"
381 "\n VSync Pulse Width: %u"
382 "\n VSync Back Porch: %u"
383 "\n VSync Front Porch: %u"
384 "\n Buffer Color Depth: %d bit"
385 "\n Display Pixel Mode: %d bit"
386 "\n Color Order: %s"
387 "\n Invert Colors: %s"
388 "\n Pixel Clock: %dMHz",
389 this->model_, this->width_, this->height_, YESNO(this->madctl_ & (MADCTL_XFLIP | MADCTL_MX)),
390 YESNO(this->madctl_ & (MADCTL_YFLIP | MADCTL_MY)), YESNO(this->madctl_ & MADCTL_MV), this->rotation_,
393 (3 - this->color_depth_) * 8, this->pixel_mode_, this->madctl_ & MADCTL_BGR ? "BGR" : "RGB",
394 YESNO(this->invert_colors_), this->pclk_frequency_);
395 LOG_PIN(" Reset Pin ", this->reset_pin_);
396}
397} // namespace mipi_dsi
398} // namespace esphome
399#endif // USE_ESP32_VARIANT_ESP32P4
uint8_t h
Definition bl0906.h:2
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
virtual void setup()=0
virtual void digital_write(bool value)=0
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:1274
T * allocate(size_t n)
Definition helpers.h:1294
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:16
DisplayPage * page_
Definition display.h:792
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:764
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:55
const display_writer_t & get_writer() const
Definition display.cpp:896
void fill(Color color) override
Definition mipi_dsi.cpp:300
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:366
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:197
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:238
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:476
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:331
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:735
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
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