# MIPI DSI 接口的 LCD

[English]

1. 创建 DSI 总线，并对 D-PHY 进行初始化设置。

   >     esp_lcd_dsi_bus_handle_t mipi_dsi_bus = NULL;
   >     esp_lcd_dsi_bus_config_t bus_config = {
   >         .bus_id = 0, // 从 0 开始编号，指定要使用的 DSI 主机
   >         .num_data_lanes = 2, // 要使用的数据通道数，不能超过芯片支持的数量
   >         .lane_bit_rate_mbps = EXAMPLE_MIPI_DSI_LANE_BITRATE_MBPS, // 数据通道的比特率 (Mbps)
   >     };
   >     ESP_ERROR_CHECK(esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus));

2. 从 DSI 总线派生 DBI 接口。DBI 接口主要用作 esp_lcd 组件中的控制 IO 层。使用该接口可读写 LCD 设备内部的配置寄存器。在此步骤中，需要提供以下信息：

   > - `esp_lcd_dbi_io_config_t::virtual_channel` 设置虚拟通道号。虚拟通道是一种逻辑通道，用于从不同来源多路复用数据。如果只连接了一个 LCD，则将此值设置为 `0`。

   > - `esp_lcd_dbi_io_config_t::lcd_cmd_bits` 和 `esp_lcd_dbi_io_config_t::lcd_param_bits` 分别设置 LCD 控制器芯片可识别的命令及参数的位宽。不同芯片对位宽要求不同，请提前参阅 LCD 规格书。

   >     esp_lcd_panel_io_handle_t mipi_dbi_io = NULL;
   >     esp_lcd_dbi_io_config_t dbi_config = {
   >         .virtual_channel = 0,
   >         .lcd_cmd_bits = 8,   // 依据 LCD 规格书说明来设置
   >         .lcd_param_bits = 8, // 依据 LCD 规格书说明来设置
   >     };
   >     ESP_ERROR_CHECK(esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_config, &mipi_dbi_io));

3. 安装 LCD 控制器驱动程序。LCD 控制器驱动程序负责向 LCD 控制器芯片发送命令和参数。在此步骤中，需要指定上一步骤中分配到的 MIPI DBI IO 设备句柄以及一些面板特定配置：

   > - `esp_lcd_panel_dev_config_t::bits_per_pixel` 设置每个像素的位宽。LCD 驱动程序使用此值计算要发送到 LCD 控制器芯片的字节数。

   > - `esp_lcd_panel_dev_config_t::reset_gpio_num` 设置复位管脚的 GPIO 编号。如果 LCD 控制器芯片没有硬件复位管脚，则将此设置为 `-1`。

   > - `esp_lcd_panel_dev_config_t::rgb_ele_order` 设置像素数据的 RGB 元素顺序，可以是 RGB 或 BGR。

   >     esp_lcd_panel_handle_t ili9881c_ctrl_panel = NULL;
   >     esp_lcd_panel_dev_config_t lcd_dev_config = {
   >         .bits_per_pixel = 24, // MIPI LCD 上通常每像素使用 24 位（即 RGB888）
   >         .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
   >         .reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST,
   >     };
   >     ESP_ERROR_CHECK(esp_lcd_new_panel_ili9881c(mipi_dbi_io, &lcd_dev_config, &ili9881c_ctrl_panel));

4. 使用上一步骤中返回的 LCD 控制面板，在基本初始化完成后可以重置 LCD 设备。之后，可以打开显示器。

   >     ESP_ERROR_CHECK(esp_lcd_panel_reset(ili9881c_ctrl_panel));
   >     ESP_ERROR_CHECK(esp_lcd_panel_init(ili9881c_ctrl_panel));
   >     ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(ili9881c_ctrl_panel, true));

5. 此时仍然无法使用控制面板向 MIPI LCD 发送像素数据，因为 MIPI LCD 具有高分辨率，而 LCD 控制器中没有 GRAM。因此需要维护 LCD 帧 buffer，并通过 MIPI DSI DPI 接口将其刷新到 LCD 屏幕上。分配 DPI 数据面板需要许多基本参数，例如 DPI 时钟频率、像素格式、视频时序等。

   > - `esp_lcd_dpi_panel_config_t::virtual_channel` 设置虚拟通道号。与 DBI 接口类似，DPI 接口也需要设置虚拟通道。如果只连接了一个 LCD，则将此值设置为 `0`。

   > - `esp_lcd_dpi_panel_config_t::dpi_clk_src` 设置 DPI 接口的时钟源。可用的时钟源列表见 `mipi_dsi_dpi_clock_source_t`。

   > - `esp_lcd_dpi_panel_config_t::dpi_clock_freq_mhz` 设置 DPI 时钟频率 (MHz)。像素时钟频率越高，刷新率越高，但如果 DMA 带宽不足或 LCD 控制器芯片不支持高像素时钟频率，则可能会导致闪烁。

   > - `esp_lcd_dpi_panel_config_t::in_color_format` 设置输入的像素数据的格式。可用的像素格式见 `lcd_color_format_t`。MIPI LCD 通常使用 RGB888 来获得最佳色彩深度。

   > - `esp_lcd_dpi_panel_config_t::video_timing` 设置 LCD 面板的特定时序参数。包括 LCD 分辨率和消隐间隔在内的必要参数列表见 `esp_lcd_video_timing_t`，请依据 LCD 技术规格书填写参数。

   >     esp_lcd_panel_handle_t mipi_dpi_panel = NULL;
   >     esp_lcd_dpi_panel_config_t dpi_config = {
   >         .virtual_channel = 0,
   >         .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
   >         .dpi_clock_freq_mhz = 1 * 1000,
   >         .in_color_format = LCD_COLOR_FMT_RGB888,
   >         .video_timing = {
   >             .h_size = EXAMPLE_MIPI_DSI_LCD_H_RES,
   >             .v_size = EXAMPLE_MIPI_DSI_LCD_V_RES,
   >             .hsync_back_porch = EXAMPLE_MIPI_DSI_LCD_HBP,
   >             .hsync_pulse_width = EXAMPLE_MIPI_DSI_LCD_HSYNC,
   >             .hsync_front_porch = EXAMPLE_MIPI_DSI_LCD_HFP,
   >             .vsync_back_porch = EXAMPLE_MIPI_DSI_LCD_VBP,
   >             .vsync_pulse_width = EXAMPLE_MIPI_DSI_LCD_VSYNC,
   >             .vsync_front_porch = EXAMPLE_MIPI_DSI_LCD_VFP,
   >         },
   >     };
   >     ESP_ERROR_CHECK(esp_lcd_new_panel_dpi(mipi_dsi_bus, &dpi_config, &mipi_dpi_panel));
   >     ESP_ERROR_CHECK(esp_lcd_panel_init(mipi_dpi_panel));

6. 配置绘制位图钩子函数（可选）

   > 若想使用 DMA2D 实现绘制位图，驱动程序内部已实现 DMA2D 绘制位图的钩子函数，用户只需调用 `esp_lcd_dpi_panel_enable_dma2d()` 即可。

   >     ESP_ERROR_CHECK(esp_lcd_dpi_panel_enable_dma2d(mipi_dpi_panel));

   > 备注

   > 由于硬件限制，如果启用了外部存储加密，则 DMA2D 只能访问地址和长度都对齐到 16 字节的地址空间。你需要确保你的绘制 buffer 的地址和长度都对齐到 16 字节。 peripherals/lcd/mipi_dsi 演示了如何使用 LVGL 控制重新绘制区域来确保对齐。

   > 若需更高级的应用，用户可为绘制位图添加自定义钩子，例如通过 PPA 实现旋转、缩放等操作。

   >     esp_lcd_panel_hooks_t hooks = {
   >         .draw_bitmap_hook = custom_draw_bitmap_hook,
   >     };
   >     ESP_ERROR_CHECK(esp_lcd_dpi_panel_register_hooks(mipi_dpi_panel, &hooks, &user_ctx));

## 关于 MIPI DPHY 的供电

ESP32-P4 中的 MIPI DPHY 需要独立的 2.5V 电源供电，请查阅原理图，确保在使用 MIPI DSI 驱动之前，已将其供电引脚（名字可能是 `VDD_MIPI_DPHY`）连接至 2.5V 电源。

在 ESP32-P4 中， MIPI DPHY 可以使用内部的可调 LDO 供电。请将 LDO 通道的输出引脚连接至 MIPI DPHY 的供电引脚。然后在初始化 DSI 驱动之前，使用 低压差线性稳压器 (LDO) 中提供的 API 配置 LDO 输出 2.5V 电压。

## API 参考

### Header File

- components/esp_lcd/dsi/include/esp_lcd_mipi_dsi.h

- This header file can be included with:

  >     #include "esp_lcd_mipi_dsi.h"

- This header file is a part of the API provided by the `esp_lcd` component. To declare that your component depends on `esp_lcd`, add the following to your CMakeLists.txt:

  >     REQUIRES esp_lcd

  > or

  >     PRIV_REQUIRES esp_lcd

### Functions

esp_err_t esp_lcd_new_dsi_bus(const esp_lcd_dsi_bus_config_t *bus_config, esp_lcd_dsi_bus_handle_t *ret_bus)

  Create MIPI DSI bus handle.

  参数:

    - bus_config -- [in] Bus configuration

    - ret_bus -- [out] Returned bus handle

  返回:

    - ESP_OK: Create MIPI DSI bus successfully

    - ESP_ERR_INVALID_ARG: Create MIPI DSI bus failed because of invalid argument

    - ESP_ERR_NO_MEM: Create MIPI DSI bus failed because of out of memory

    - ESP_ERR_NOT_FOUND: Create MIPI DSI bus failed because no more free DSI hardware instance

    - ESP_FAIL: Create MIPI DSI bus failed because of other error

esp_err_t esp_lcd_del_dsi_bus(esp_lcd_dsi_bus_handle_t bus)

  Destroy MIPI DSI bus handle.

  参数:

    bus -- [in] MIPI DSI bus handle, returned from `esp_lcd_new_dsi_bus`

  返回:

    - ESP_OK: Destroy MIPI DSI bus successfully

    - ESP_ERR_INVALID_ARG: Destroy MIPI DSI bus failed because of invalid argument

    - ESP_FAIL: Destroy MIPI DSI bus failed because of other error

esp_err_t esp_lcd_new_panel_io_dbi(esp_lcd_dsi_bus_handle_t bus, const esp_lcd_dbi_io_config_t *io_config, esp_lcd_panel_io_handle_t *ret_io)

  Create LCD panel IO, for MIPI DSI DBI interface.

  备注

  Although we call it "DBI", internally the driver is using a co-called "generic" interface for transmitting/receiving LCD commands and parameters.

  参数:

    - bus -- [in] MIPI DSI bus handle, returned from `esp_lcd_new_dsi_bus`

    - io_config -- [in] IO configuration

    - ret_io -- [out] Returned panel IO handle

  返回:

    - ESP_OK: Create MIPI DSI command IO successfully

    - ESP_ERR_INVALID_ARG: Create MIPI DSI command IO failed because of invalid argument

    - ESP_ERR_NO_MEM: Create MIPI DSI command IO failed because of out of memory

    - ESP_FAIL: Create MIPI DSI command IO failed because of other error

esp_err_t esp_lcd_new_panel_dpi(esp_lcd_dsi_bus_handle_t bus, const esp_lcd_dpi_panel_config_t *panel_config, esp_lcd_panel_handle_t *ret_panel)

  Create LCD panel for MIPI DSI DPI interface.

  参数:

    - bus -- [in] MIPI DSI bus handle, returned from `esp_lcd_new_dsi_bus`

    - panel_config -- [in] DSI data panel configuration

    - ret_panel -- [out] Returned LCD panel handle

  返回:

    - ESP_OK: Create MIPI DSI data panel successfully

    - ESP_ERR_INVALID_ARG: Create MIPI DSI data panel failed because of invalid argument

    - ESP_ERR_NO_MEM: Create MIPI DSI data panel failed because of out of memory

    - ESP_ERR_NOT_SUPPORTED: Create MIPI DSI data panel failed because of unsupported feature

    - ESP_FAIL: Create MIPI DSI data panel failed because of other error

esp_err_t esp_lcd_dpi_panel_get_frame_buffer(esp_lcd_panel_handle_t dpi_panel, uint32_t fb_num, void **fb0, ...)

  Get the address of the frame buffer(s) that allocated by the driver.

  参数:

    - dpi_panel -- [in] MIPI DPI panel handle, returned from esp_lcd_new_panel_dpi()

    - fb_num -- [in] Number of frame buffer(s) to get. This value must be the same as the number of the followed parameters.

    - fb0 -- [out] Address of the frame buffer 0 (first frame buffer)

    - ... -- [out] List of other frame buffers if any

  返回:

    - ESP_ERR_INVALID_ARG: Get frame buffer address failed because of invalid argument

    - ESP_OK: Get frame buffer address successfully

esp_err_t esp_lcd_dpi_panel_set_pattern(esp_lcd_panel_handle_t dpi_panel, mipi_dsi_pattern_type_t pattern)

  Set pre-defined pattern to the screen for testing or debugging purpose.

  参数:

    - dpi_panel -- [in] MIPI DPI panel handle, returned from esp_lcd_new_panel_dpi()

    - pattern -- [in] Pattern type

  返回:

    - ESP_OK: Set pattern successfully

    - ESP_ERR_INVALID_ARG: Set pattern failed because of invalid argument

    - ESP_FAIL: Set pattern failed because of other error

esp_err_t esp_lcd_dpi_panel_set_yuv_conversion(esp_lcd_panel_handle_t dpi_panel, const esp_lcd_color_conv_yuv_config_t *config)

  Set color conversion (YUV<->RGB) configuration for DPI panel.

  参数:

    - dpi_panel -- [in] MIPI DPI panel handle, returned from esp_lcd_new_panel_dpi()

    - config -- [in] Color conversion configuration

  返回:

    - ESP_OK: Set color conversion configuration successfully

    - ESP_ERR_INVALID_ARG: Set color conversion configuration failed because of invalid argument

    - ESP_FAIL: Set color conversion configuration failed because of other error

esp_err_t esp_lcd_dpi_panel_register_event_callbacks(esp_lcd_panel_handle_t dpi_panel, const esp_lcd_dpi_panel_event_callbacks_t *cbs, void *user_ctx)

  Register LCD DPI panel callbacks.

  参数:

    - dpi_panel -- [in] LCD DPI panel handle, which is returned from esp_lcd_new_panel_dpi()

    - cbs -- [in] structure with all LCD panel callbacks

    - user_ctx -- [in] User private data, passed directly to callback's user_ctx

  返回:

    - ESP_ERR_INVALID_ARG: Register callbacks failed because of invalid argument

    - ESP_OK: Register callbacks successfully

esp_err_t esp_lcd_dpi_panel_register_hooks(esp_lcd_panel_handle_t dpi_panel, const esp_lcd_panel_hooks_t *hooks, void *hook_ctx)

  Register panel hooks to the DPI panel.

  备注

  You can register panel hooks to implement custom operations like scaling, rotation, color space conversion, etc. with hardware accelerators like PPA or DMA2D. The hook will be overridden when this function is called multiple times.

  参数:

    - dpi_panel -- [in] LCD DPI panel handle, which is returned from esp_lcd_new_panel_dpi()

    - hooks -- [in] Panel hooks

    - hook_ctx -- [in] Hook context

  返回:

    - ESP_OK: Register hooks successfully

    - Other error codes on failure

esp_err_t esp_lcd_dpi_panel_enable_dma2d(esp_lcd_panel_handle_t dpi_panel)

  Enable DMA2D for DPI panel.

  备注

  The function will register a built-in DMA2D draw bitmap hook to perform draw bitmap operations using DMA2D.

  参数:

    dpi_panel -- [in] LCD DPI panel handle, which is returned from esp_lcd_new_panel_dpi()

  返回:

    - ESP_OK: Enable DMA2D successfully

    - Other error codes on failure

esp_err_t esp_lcd_dpi_panel_disable_dma2d(esp_lcd_panel_handle_t dpi_panel)

  Disable DMA2D for DPI panel.

  备注

  The function will unregister the built-in DMA2D draw bitmap hook.

  参数:

    dpi_panel -- [in] LCD DPI panel handle, which is returned from esp_lcd_new_panel_dpi()

  返回:

    - ESP_OK: Disable DMA2D successfully

    - Other error codes on failure

### Structures

struct esp_lcd_dsi_bus_config_t

  MIPI DSI bus configuration structure.

  Public Members

  int bus_id

    Select which DSI controller, index from 0

  uint8_t num_data_lanes

    Number of data lanes, if set to 0, the driver will fallback to use maximum number of lanes

  mipi_dsi_phy_pllref_clock_source_t phy_clk_src

    The clock source for the PHY PLL

  float lane_bit_rate_mbps

    Lane bit rate in Mbps

  struct esp_lcd_dsi_bus_config_t::extra_dsi_bus_flags flags

    Extra configuration flags

  struct extra_dsi_bus_flags

    Extra configuration flags for MIPI DSI bus.

    Public Members

    uint32_t clock_lane_force_hs

      Force the clock lane to high speed mode

struct esp_lcd_dbi_io_config_t

  Panel IO configuration structure, for MIPI DSI command interface.

  Public Members

  uint8_t virtual_channel

    Virtual channel ID, index from 0

  int lcd_cmd_bits

    Bit-width of LCD command

  int lcd_param_bits

    Bit-width of LCD parameter

struct esp_lcd_dpi_panel_config_t

  MIPI DSI DPI panel configuration structure.

  Public Members

  uint8_t virtual_channel

    Virtual channel ID, index from 0

  mipi_dsi_dpi_clock_source_t dpi_clk_src

    MIPI DSI DPI clock source

  float dpi_clock_freq_mhz

    Pixel clock frequency in MHz

  lcd_color_format_t in_color_format

    Format of the input data (color space and pixel format), which is the format stored in the frame buffer

  lcd_color_format_t out_color_format

    Format of the output data (color space and pixel format), which is the format that the panel can accept

  uint8_t num_fbs

    Number of screen-sized frame buffers that allocated by the driver By default (set to either 0 or 1) only one frame buffer will be created

  esp_lcd_video_timing_t video_timing

    Video timing

  struct esp_lcd_dpi_panel_config_t::extra_dpi_panel_flags flags

    Extra configuration flags

  struct extra_dpi_panel_flags

    Extra configuration flags for MIPI DSI DPI panel.

    Public Members

    uint32_t disable_lp

      Disable low-power for DPI

struct esp_lcd_dpi_panel_event_data_t

  Type of LCD DPI panel event data.

struct esp_lcd_dpi_panel_event_callbacks_t

  Type of LCD DPI panel callbacks.

  Public Members

  esp_lcd_dpi_panel_color_trans_done_cb_t on_color_trans_done

    Invoked when user's color buffer copied to the internal frame buffer. This is an indicator that the draw buffer can be recycled safely. But doesn't mean the draw buffer finishes the refreshing to the screen.

  esp_lcd_dpi_panel_refresh_done_cb_t on_refresh_done

    Invoked when the internal frame buffer finishes refreshing to the screen

struct esp_lcd_draw_bitmap_hook_data_t

  Type of draw bitmap hook data.

  Public Members

  void *dst_data

    Destination buffer (usually frame buffer)

  int dst_x_size

    Destination bitmap width

  int dst_y_size

    Destination bitmap height

  int dst_x_start

    Destination start x coordinate

  int dst_y_start

    Destination start y coordinate

  int dst_x_end

    Destination end x coordinate (exclusive)

  int dst_y_end

    Destination end y coordinate (exclusive)

  const void *src_data

    Source bitmap data

  int src_x_size

    Source bitmap width

  int src_y_size

    Source bitmap height

  int src_x_start

    Source start x coordinate

  int src_y_start

    Source start y coordinate

  int src_x_end

    Source end x coordinate (exclusive)

  int src_y_end

    Source end y coordinate (exclusive)

  int bits_per_pixel

    Bits per pixel

  bool (*on_hook_end)(esp_lcd_panel_handle_t panel)

    Callback to be invoked when the hook completes its operation

struct esp_lcd_panel_hooks_t

  Type of LCD panel hooks.

  Public Members

  esp_lcd_panel_draw_bitmap_hook_t draw_bitmap_hook

    Draw bitmap hook function

### Type Definitions

typedef struct esp_lcd_dsi_bus_t *esp_lcd_dsi_bus_handle_t

  Type of MIPI DSI bus handle

typedef bool (*esp_lcd_dpi_panel_general_cb_t)(esp_lcd_panel_handle_t panel, esp_lcd_dpi_panel_event_data_t *edata, void *user_ctx)

  A general function callback prototype for DPI panel driver.

  Param panel:

    [in] LCD panel handle, which is created by factory API like esp_lcd_new_panel_dpi()

  Param edata:

    [in] DPI panel event data, fed by driver

  Param user_ctx:

    [in] User data

  Return:

    Whether a high priority task has been waken up by this function

typedef esp_lcd_dpi_panel_general_cb_t esp_lcd_dpi_panel_color_trans_done_cb_t

  Declare the prototype of the function that will be invoked when driver finishes coping user's color buffer to frame buffer.

typedef esp_lcd_dpi_panel_general_cb_t esp_lcd_dpi_panel_refresh_done_cb_t

  Declare the prototype of the function that will be invoked when driver finishes refreshing the frame buffer to the screen.

typedef esp_err_t (*esp_lcd_panel_draw_bitmap_hook_t)(esp_lcd_panel_handle_t panel, const esp_lcd_draw_bitmap_hook_data_t *hook_data, void *hook_ctx)

  draw bitmap hook function type for custom pixel processing operations

  This hook allows users to implement custom operations like scaling, rotation, color space conversion, etc. using hardware accelerators like PPA or DMA2D.

  备注

  The hook should ensure the synchronization of draw operations on its own.

  Param panel:

    [in] LCD panel handle

  Param hook_data:

    [in] Hook data

  Param hook_ctx:

    [in] Hook context

  Return:

    - ESP_OK on success

    - Other error codes on failure

此文档对您有帮助吗？

- 反馈已收到，谢谢！
- 如果您有其他意见，欢迎填写乐鑫文档反馈表。

- 我们重视您的反馈。
- 您可以填写乐鑫文档反馈表告诉我们如何改进该文档。