ESPHome 2025.9.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"
4
5namespace esphome {
6namespace mipi_dsi {
7
8static bool notify_refresh_ready(esp_lcd_panel_handle_t panel, esp_lcd_dpi_panel_event_data_t *edata, void *user_ctx) {
9 auto *sem = static_cast<SemaphoreHandle_t *>(user_ctx);
10 BaseType_t need_yield = pdFALSE;
11 xSemaphoreGiveFromISR(sem, &need_yield);
12 return (need_yield == pdTRUE);
13}
15 ESP_LOGCONFIG(TAG, "Running Setup");
16
17 if (!this->enable_pins_.empty()) {
18 for (auto *pin : this->enable_pins_) {
19 pin->setup();
20 pin->digital_write(true);
21 }
22 delay(10);
23 }
24
25 esp_lcd_dsi_bus_config_t bus_config = {
26 .bus_id = 0, // index from 0, specify the DSI host to use
27 .num_data_lanes =
28 this->lanes_, // Number of data lanes to use, can't set a value that exceeds the chip's capability
29 .phy_clk_src = MIPI_DSI_PHY_CLK_SRC_DEFAULT, // Clock source for the DPHY
30 .lane_bit_rate_mbps = this->lane_bit_rate_, // Bit rate of the data lanes, in Mbps
31 };
32 auto err = esp_lcd_new_dsi_bus(&bus_config, &this->bus_handle_);
33 if (err != ESP_OK) {
34 this->smark_failed("lcd_new_dsi_bus failed", err);
35 return;
36 }
37 esp_lcd_dbi_io_config_t dbi_config = {
38 .virtual_channel = 0,
39 .lcd_cmd_bits = 8, // according to the LCD spec
40 .lcd_param_bits = 8, // according to the LCD spec
41 };
42 err = esp_lcd_new_panel_io_dbi(this->bus_handle_, &dbi_config, &this->io_handle_);
43 if (err != ESP_OK) {
44 this->smark_failed("new_panel_io_dbi failed", err);
45 return;
46 }
47 auto pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565;
49 pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB888;
50 }
51 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
52 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
53 .dpi_clock_freq_mhz = this->pclk_frequency_,
54 .pixel_format = pixel_format,
55 .num_fbs = 1, // number of frame buffers to allocate
56 .video_timing =
57 {
58 .h_size = this->width_,
59 .v_size = this->height_,
60 .hsync_pulse_width = this->hsync_pulse_width_,
61 .hsync_back_porch = this->hsync_back_porch_,
62 .hsync_front_porch = this->hsync_front_porch_,
63 .vsync_pulse_width = this->vsync_pulse_width_,
64 .vsync_back_porch = this->vsync_back_porch_,
65 .vsync_front_porch = this->vsync_front_porch_,
66 },
67 .flags = {
68 .use_dma2d = true,
69 }};
70 err = esp_lcd_new_panel_dpi(this->bus_handle_, &dpi_config, &this->handle_);
71 if (err != ESP_OK) {
72 this->smark_failed("esp_lcd_new_panel_dpi failed", err);
73 return;
74 }
75 if (this->reset_pin_ != nullptr) {
76 this->reset_pin_->setup();
77 this->reset_pin_->digital_write(true);
78 delay(5);
79 this->reset_pin_->digital_write(false);
80 delay(5);
81 this->reset_pin_->digital_write(true);
82 } else {
83 esp_lcd_panel_io_tx_param(this->io_handle_, SW_RESET_CMD, nullptr, 0);
84 }
85 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
86 auto when = millis() + 120;
87 err = esp_lcd_panel_init(this->handle_);
88 if (err != ESP_OK) {
89 this->smark_failed("esp_lcd_init failed", err);
90 return;
91 }
92 size_t index = 0;
93 auto &vec = this->init_sequence_;
94 while (index != vec.size()) {
95 if (vec.size() - index < 2) {
96 this->mark_failed("Malformed init sequence");
97 return;
98 }
99 uint8_t cmd = vec[index++];
100 uint8_t x = vec[index++];
101 if (x == DELAY_FLAG) {
102 ESP_LOGD(TAG, "Delay %dms", cmd);
103 delay(cmd);
104 } else {
105 uint8_t num_args = x & 0x7F;
106 if (vec.size() - index < num_args) {
107 this->mark_failed("Malformed init sequence");
108 return;
109 }
110 if (cmd == SLEEP_OUT) {
111 // are we ready, boots?
112 int duration = when - millis();
113 if (duration > 0) {
115 }
116 }
117 const auto *ptr = vec.data() + index;
118 ESP_LOGVV(TAG, "Command %02X, length %d, byte(s) %s", cmd, num_args,
119 format_hex_pretty(ptr, num_args, '.', false).c_str());
120 err = esp_lcd_panel_io_tx_param(this->io_handle_, cmd, ptr, num_args);
121 if (err != ESP_OK) {
122 this->smark_failed("lcd_panel_io_tx_param failed", err);
123 return;
124 }
125 index += num_args;
126 if (cmd == SLEEP_OUT)
127 delay(10);
128 }
129 }
130 this->io_lock_ = xSemaphoreCreateBinary();
131 esp_lcd_dpi_panel_event_callbacks_t cbs = {
132 .on_color_trans_done = notify_refresh_ready,
133 };
134
135 err = (esp_lcd_dpi_panel_register_event_callbacks(this->handle_, &cbs, this->io_lock_));
136 if (err != ESP_OK) {
137 this->smark_failed("Failed to register callbacks", err);
138 return;
139 }
140
141 ESP_LOGCONFIG(TAG, "MIPI DSI setup complete");
142}
143
145 if (this->auto_clear_enabled_) {
146 this->clear();
147 }
148 if (this->show_test_card_) {
149 this->test_card();
150 } else if (this->page_ != nullptr) {
151 this->page_->get_writer()(*this);
152 } else if (this->writer_.has_value()) {
153 (*this->writer_)(*this);
154 } else {
155 this->stop_poller();
156 }
157 if (this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
158 return;
159 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_);
160 int w = this->x_high_ - this->x_low_ + 1;
161 int h = this->y_high_ - this->y_low_ + 1;
162 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_, this->y_low_,
163 this->width_ - w - this->x_low_);
164 // invalidate watermarks
165 this->x_low_ = this->width_;
166 this->y_low_ = this->height_;
167 this->x_high_ = 0;
168 this->y_high_ = 0;
169}
170
171void MIPI_DSI::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
172 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
173 if (w <= 0 || h <= 0)
174 return;
175 // if color mapping is required, pass the buck.
176 // note that endianness is not considered here - it is assumed to match!
177 if (bitness != this->color_depth_) {
178 display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
179 x_pad);
180 }
181 this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad);
182}
183
184void 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,
185 int x_pad) {
186 esp_err_t err = ESP_OK;
187 auto bytes_per_pixel = 3 - this->color_depth_;
188 auto stride = (x_offset + w + x_pad) * bytes_per_pixel;
189 ptr += y_offset * stride + x_offset * bytes_per_pixel; // skip to the first pixel
190 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
191 if (x_offset == 0 && x_pad == 0) {
192 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
193 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
194
195 } else {
196 // draw line by line
197 for (int y = 0; y != h; y++) {
198 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1, ptr);
199 if (err != ESP_OK)
200 break;
201 ptr += stride; // next line
202 xSemaphoreTake(this->io_lock_, portMAX_DELAY);
203 }
204 }
205 if (err != ESP_OK)
206 ESP_LOGE(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
207}
208
210 if (this->is_failed())
211 return false;
212 if (this->buffer_ != nullptr)
213 return true;
214 // this is dependent on the enum values.
215 auto bytes_per_pixel = 3 - this->color_depth_;
216 RAMAllocator<uint8_t> allocator;
217 this->buffer_ = allocator.allocate(this->height_ * this->width_ * bytes_per_pixel);
218 if (this->buffer_ == nullptr) {
219 this->mark_failed("Could not allocate buffer for display!");
220 return false;
221 }
222 return true;
223}
224
225void MIPI_DSI::draw_pixel_at(int x, int y, Color color) {
226 if (!this->get_clipping().inside(x, y))
227 return;
228
229 switch (this->rotation_) {
231 break;
233 std::swap(x, y);
234 x = this->width_ - x - 1;
235 break;
237 x = this->width_ - x - 1;
238 y = this->height_ - y - 1;
239 break;
241 std::swap(x, y);
242 y = this->height_ - y - 1;
243 break;
244 }
245 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
246 return;
247 }
249 if (!this->check_buffer_())
250 return;
251 size_t pos = (y * this->width_) + x;
252 switch (this->color_depth_) {
254 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
255 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
256 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
257 uint16_t new_color = lo_byte | (hi_byte << 8); // little endian
258 if (ptr_16[pos] == new_color)
259 return;
260 ptr_16[pos] = new_color;
261 break;
262 }
265 this->buffer_[pos * 3] = color.b;
266 this->buffer_[pos * 3 + 1] = color.g;
267 this->buffer_[pos * 3 + 2] = color.r;
268 } else {
269 this->buffer_[pos * 3] = color.r;
270 this->buffer_[pos * 3 + 1] = color.g;
271 this->buffer_[pos * 3 + 2] = color.b;
272 }
273 break;
275 break;
276 }
277 // low and high watermark may speed up drawing from buffer
279 this->x_low_ = x;
281 this->y_low_ = y;
282 if (x > this->x_high_)
283 this->x_high_ = x;
284 if (y > this->y_high_)
285 this->y_high_ = y;
286}
288 if (!this->check_buffer_())
289 return;
290 switch (this->color_depth_) {
292 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
293 uint8_t hi_byte = static_cast<uint8_t>(color.r & 0xF8) | (color.g >> 5);
294 uint8_t lo_byte = static_cast<uint8_t>((color.g & 0x1C) << 3) | (color.b >> 3);
295 uint16_t new_color = lo_byte | (hi_byte << 8); // little endian
296 std::fill_n(ptr_16, this->width_ * this->height_, new_color);
297 break;
298 }
299
302 for (size_t i = 0; i != this->width_ * this->height_; i++) {
303 this->buffer_[i * 3 + 0] = color.b;
304 this->buffer_[i * 3 + 1] = color.g;
305 this->buffer_[i * 3 + 2] = color.r;
306 }
307 } else {
308 for (size_t i = 0; i != this->width_ * this->height_; i++) {
309 this->buffer_[i * 3 + 0] = color.r;
310 this->buffer_[i * 3 + 1] = color.g;
311 this->buffer_[i * 3 + 2] = color.b;
312 }
313 }
314
315 default:
316 break;
317 }
318}
319
331
343
344static const uint8_t PIXEL_MODES[] = {0, 16, 18, 24};
345
347 ESP_LOGCONFIG(TAG,
348 "MIPI_DSI RGB LCD"
349 "\n Model: %s"
350 "\n Width: %u"
351 "\n Height: %u"
352 "\n Mirror X: %s"
353 "\n Mirror Y: %s"
354 "\n Swap X/Y: %s"
355 "\n Rotation: %d degrees"
356 "\n DSI Lanes: %u"
357 "\n Lane Bit Rate: %uMbps"
358 "\n HSync Pulse Width: %u"
359 "\n HSync Back Porch: %u"
360 "\n HSync Front Porch: %u"
361 "\n VSync Pulse Width: %u"
362 "\n VSync Back Porch: %u"
363 "\n VSync Front Porch: %u"
364 "\n Buffer Color Depth: %d bit"
365 "\n Display Pixel Mode: %d bit"
366 "\n Color Order: %s"
367 "\n Invert Colors: %s"
368 "\n Pixel Clock: %dMHz",
369 this->model_, this->width_, this->height_, YESNO(this->madctl_ & (MADCTL_XFLIP | MADCTL_MX)),
370 YESNO(this->madctl_ & (MADCTL_YFLIP | MADCTL_MY)), YESNO(this->madctl_ & MADCTL_MV), this->rotation_,
373 (3 - this->color_depth_) * 8, this->pixel_mode_, this->madctl_ & MADCTL_BGR ? "BGR" : "RGB",
374 YESNO(this->invert_colors_), this->pclk_frequency_);
375 LOG_PIN(" Reset Pin ", this->reset_pin_);
376}
377} // namespace mipi_dsi
378} // namespace esphome
379#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:818
T * allocate(size_t n)
Definition helpers.h:838
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:17
DisplayPage * page_
Definition display.h:682
optional< display_writer_t > writer_
Definition display.h:681
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:715
DisplayRotation rotation_
Definition display.h:680
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:836
void fill(Color color) override
Definition mipi_dsi.cpp:287
display::ColorOrder color_mode_
Definition mipi_dsi.h:106
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:171
void dump_config() override
Definition mipi_dsi.cpp:346
std::vector< GPIOPin * > enable_pins_
Definition mipi_dsi.h:89
display::ColorBitness color_depth_
Definition mipi_dsi.h:107
SemaphoreHandle_t io_lock_
Definition mipi_dsi.h:113
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:184
void smark_failed(const char *message, esp_err_t err)
Definition mipi_dsi.h:65
esp_lcd_panel_io_handle_t io_handle_
Definition mipi_dsi.h:112
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_dsi.cpp:225
int get_width_internal() override
Definition mipi_dsi.h:51
esp_lcd_dsi_bus_handle_t bus_handle_
Definition mipi_dsi.h:111
std::vector< uint8_t > init_sequence_
Definition mipi_dsi.h:100
int get_height_internal() override
Definition mipi_dsi.h:52
esp_lcd_panel_handle_t handle_
Definition mipi_dsi.h:110
bool has_value() const
Definition optional.h:92
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:229
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Definition helpers.cpp:280
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
uint8_t g
Definition color.h:25
uint8_t b
Definition color.h:29
uint8_t r
Definition color.h:21
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6