8#if defined(ARDUINO) && defined(ARDUINO_ESP32P4_DEV)
10#include "soc/soc_caps.h"
12#ifndef SOC_MIPI_DSI_SUPPORTED
13#define SOC_MIPI_DSI_SUPPORTED
16#if SOC_MIPI_DSI_SUPPORTED
17#include "freertos/FreeRTOS.h"
18#include "freertos/task.h"
19#include "driver/gpio.h"
21#include "esp_lcd_panel_commands.h"
22#include "esp_lcd_panel_interface.h"
23#include "esp_lcd_panel_io.h"
24#include "esp_lcd_mipi_dsi.h"
25#include "esp_lcd_panel_vendor.h"
30#define HI8561_CMD_SHLR_BIT (1ULL << 0)
31#define HI8561_CMD_UPDN_BIT (1ULL << 1)
32#define HI8561_MDCTL_VALUE_DEFAULT (0x01)
34static const hi8561_lcd_init_cmd_t vendor_specific_init_default[] = {
37 {0xDF, (uint8_t[]){0x90, 0x69, 0xF9}, 3, 0},
38 {0xDE, (uint8_t[]){0x00}, 1, 0},
39 {0xBB, (uint8_t[]){0x0F, 0x10, 0x43, 0x50, 0x32, 0x44, 0x44}, 7, 0},
40 {0xBF, (uint8_t[]){0x46, 0x32}, 2, 0},
41 {0xC0, (uint8_t[]){0x01, 0xAD, 0x01, 0xAD}, 4, 0},
42 {0xBD, (uint8_t[]){0x00, 0xB4}, 2, 0},
43 {0xC6, (uint8_t[]){0x00, 0x7D, 0x00, 0xC8, 0x00, 0x17, 0x1A, 0x82, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01}, 23, 0},
44 {0xC8, (uint8_t[]){0x23, 0x48, 0x87}, 3, 0},
46 {0xCC, (uint8_t[]){0x31}, 1, 0},
48 {0xBC, (uint8_t[]){0x2E, 0x80, 0x84}, 3, 0},
49 {0xC3, (uint8_t[]){0x3B, 0x01, 0x02, 0x05, 0x0C, 0x0C, 0x75, 0x0A, 0x79, 0x0A, 0x79, 0x02, 0x6E, 0x02, 0x6E, 0x02, 0x6E, 0x0A, 0x0D, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F}, 25, 0},
50 {0xC4, (uint8_t[]){0x01, 0x02, 0x05, 0x0C, 0x0C, 0x75, 0x0A, 0x79, 0x0A, 0x79, 0x02, 0x6E, 0x02, 0x6E, 0x02, 0x6E, 0x0A, 0x0D, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F}, 24, 0},
51 {0xC5, (uint8_t[]){0x03, 0x05, 0x0C, 0x0C, 0x75, 0x0A, 0x79, 0x0A, 0x79, 0x02, 0x6E, 0x02, 0x6E, 0x02, 0x6E, 0x0A, 0x0D, 0x0A, 0x0F, 0x0A, 0x0F, 0x0A, 0x0F}, 23, 0},
52 {0xD7, (uint8_t[]){0x00, 0x0A, 0x63, 0x0A, 0x63, 0x0A, 0x63, 0x0A, 0x63, 0x0A, 0x63, 0x0A, 0x63, 0x0A, 0x63, 0x0A, 0x63}, 17, 0},
53 {0xCB, (uint8_t[]){0x7F, 0x78, 0x71, 0x64, 0x5A, 0x58, 0x4B, 0x51, 0x3A, 0x53, 0x51, 0x4F, 0x6A, 0x54, 0x57, 0x46, 0x3F, 0x2F, 0x1B, 0x0F, 0x08, 0x7F, 0x78, 0x71, 0x64, 0x5A, 0x58, 0x4B, 0x51, 0x3A, 0x53, 0x51, 0x4F, 0x6A, 0x54, 0x57, 0x46, 0x3F, 0x2F, 0x1B, 0x0F, 0x08, 0x00}, 43, 0},
54 {0xCE, (uint8_t[]){0x00, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C}, 23, 0},
55 {0xCF, (uint8_t[]){0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 45, 0},
56 {0xD0, (uint8_t[]){0x00, 0x1F, 0x1F, 0x11, 0x1E, 0x1F, 0x0F, 0x0F, 0x0D, 0x0D, 0x0B, 0x0B, 0x09, 0x09, 0x07, 0x07, 0x05, 0x05, 0x01, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 29, 0},
57 {0xD1, (uint8_t[]){0x00, 0x1F, 0x1F, 0x10, 0x1E, 0x1F, 0x0E, 0x0E, 0x0C, 0x0C, 0x0A, 0x0A, 0x08, 0x08, 0x06, 0x06, 0x04, 0x04, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 29, 0},
58 {0xD2, (uint8_t[]){0x00, 0x5F, 0x1F, 0x10, 0x1F, 0x1E, 0x08, 0x08, 0x4A, 0x0A, 0x0C, 0x0C, 0x0E, 0x0E, 0x04, 0x04, 0x06, 0x06, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 29, 0},
59 {0xD3, (uint8_t[]){0x00, 0x1F, 0x1F, 0x11, 0x1F, 0x1E, 0x09, 0x09, 0x0B, 0x0B, 0x0D, 0x0D, 0x0F, 0x0F, 0x05, 0x05, 0x07, 0x07, 0x01, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 29, 0},
60 {0xD4, (uint8_t[]){0x00, 0x20, 0x0B, 0x00, 0x0D, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x00, 0x81, 0x04, 0xAE, 0x04, 0xB0, 0x04, 0xB2, 0x04, 0xB4, 0x04, 0xB6, 0x04, 0xB8, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x06, 0x44, 0x06, 0x46, 0x03, 0x03, 0x00, 0x00, 0x07, 0x00, 0x06, 0x04, 0xA7, 0x04, 0xA8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00}, 87, 0},
61 {0xD5, (uint8_t[]){0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x07, 0x32, 0x5A, 0x00, 0x00, 0x3C, 0x00, 0x1E, 0x00, 0x1E, 0xB3, 0x00, 0x0F, 0x06, 0x0C, 0x00, 0x71, 0x20, 0x04, 0x10, 0x04, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 61, 0},
62 {0xCD, (uint8_t[]){0x00, 0x00}, 2, 0},
64 {0xDE, (uint8_t[]){0x01}, 1, 0},
65 {0xB9, (uint8_t[]){0x00, 0xFF, 0xFF, 0x04}, 4, 0},
66 {0xC7, (uint8_t[]){0x1F, 0x14, 0x0E}, 3, 0},
68 {0xDE, (uint8_t[]){0x02}, 1, 0},
69 {0xE5, (uint8_t[]){0x00, 0x60, 0x60, 0x02, 0x18, 0x60, 0x18, 0x60, 0x09, 0x04, 0x00, 0xC5, 0x01, 0x2C, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04}, 24, 0},
70 {0xE6, (uint8_t[]){0x10, 0x10, 0x82}, 3, 0},
71 {0xC4, (uint8_t[]){0x00, 0x11, 0x07, 0x00, 0x11, 0x01, 0x08}, 7, 0},
72 {0xC3, (uint8_t[]){0x20, 0xFF}, 2, 0},
73 {0xBD, (uint8_t[]){0x1B}, 1, 0},
74 {0xC6, (uint8_t[]){0x4A, 0x00}, 2, 0},
75 {0xCD, (uint8_t[]){0x14, 0x64, 0x11, 0x40}, 4, 0},
76 {0xC1, (uint8_t[]){0x00, 0x40, 0x00, 0x02, 0x02, 0x02, 0x02, 0x7F, 0x00, 0x00}, 10, 0},
77 {0xB3, (uint8_t[]){0x00, 0xA8}, 2, 0},
78 {0xBB, (uint8_t[]){0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x40, 0x43, 0x04}, 11, 0},
79 {0xC2, (uint8_t[]){0x02, 0x42, 0x50, 0x00, 0x02, 0xE4, 0x61, 0x73, 0xF9, 0x08}, 10, 0},
80 {0xEC, (uint8_t[]){0x07, 0x07, 0x40, 0x00, 0x22, 0x02, 0x00, 0xFF, 0x08, 0x7C, 0x00, 0x00, 0x00, 0x00}, 14, 0},
82 {0xDE, (uint8_t[]){0x03}, 1, 0},
83 {0xD1, (uint8_t[]){0x00, 0x00, 0x21, 0xFF, 0x00}, 5, 0},
85 {0xDE, (uint8_t[]){0x00}, 1, 0},
86 {0x35, (uint8_t[]){0x00}, 0, 30},
88 {0x11, (uint8_t[]){0x00}, 0, 120},
90 {0x29, (uint8_t[]){0x00}, 0, 50},
96 esp_lcd_panel_io_handle_t io;
99 const hi8561_lcd_init_cmd_t *init_cmds;
100 uint16_t init_cmds_size;
103 unsigned int reset_level : 1;
106 esp_err_t (*del)(esp_lcd_panel_t *panel);
107 esp_err_t (*init)(esp_lcd_panel_t *panel);
110static const char *TAG =
"hi8561";
112static esp_err_t panel_hi8561_send_init_cmds(hi8561_panel_t *hi8561);
114static esp_err_t panel_hi8561_del(esp_lcd_panel_t *panel);
115static esp_err_t panel_hi8561_init(esp_lcd_panel_t *panel);
116static esp_err_t panel_hi8561_reset(esp_lcd_panel_t *panel);
117static esp_err_t panel_hi8561_mirror(esp_lcd_panel_t *panel,
bool mirror_x,
bool mirror_y);
118static esp_err_t panel_hi8561_invert_color(esp_lcd_panel_t *panel,
bool invert_color_data);
119static esp_err_t panel_hi8561_sleep(esp_lcd_panel_t *panel,
bool sleep);
120static esp_err_t panel_hi8561_on_off(esp_lcd_panel_t *panel,
bool on_off);
122esp_err_t esp_lcd_new_panel_hi8561(
const esp_lcd_panel_io_handle_t io,
const esp_lcd_panel_dev_config_t *panel_dev_config,
123 esp_lcd_panel_handle_t *ret_panel)
125 ESP_RETURN_ON_FALSE(io && panel_dev_config && ret_panel, ESP_ERR_INVALID_ARG, TAG,
"Invalid arguments");
126 hi8561_vendor_config_t *vendor_config = (hi8561_vendor_config_t *)panel_dev_config->vendor_config;
127 ESP_RETURN_ON_FALSE(vendor_config && vendor_config->mipi_config.dpi_config && vendor_config->mipi_config.dsi_bus, ESP_ERR_INVALID_ARG, TAG,
128 "Invalid vendor config");
130 esp_err_t ret = ESP_OK;
131 hi8561_panel_t *hi8561 = (hi8561_panel_t *)calloc(1,
sizeof(hi8561_panel_t));
132 ESP_RETURN_ON_FALSE(hi8561, ESP_ERR_NO_MEM, TAG,
"No mem for hi8561 panel");
134 if (panel_dev_config->reset_gpio_num >= 0) {
135 gpio_config_t io_conf = {
136 .pin_bit_mask = 1ULL << panel_dev_config->reset_gpio_num,
137 .mode = GPIO_MODE_OUTPUT,
139 ESP_GOTO_ON_ERROR(gpio_config(&io_conf), err, TAG,
"Configure GPIO for RST line failed");
143 hi8561->init_cmds = vendor_config->init_cmds;
144 hi8561->init_cmds_size = vendor_config->init_cmds_size;
145 hi8561->lane_num = vendor_config->mipi_config.lane_num;
146 hi8561->reset_gpio_num = panel_dev_config->reset_gpio_num;
147 hi8561->flags.reset_level = panel_dev_config->flags.reset_active_high;
148 hi8561->madctl_val = HI8561_MDCTL_VALUE_DEFAULT;
151 ESP_GOTO_ON_ERROR(esp_lcd_new_panel_dpi(vendor_config->mipi_config.dsi_bus, vendor_config->mipi_config.dpi_config, ret_panel), err, TAG,
152 "Create MIPI DPI panel Failed");
154 hi8561->del = (*ret_panel)->del;
155 hi8561->init = (*ret_panel)->init;
157 (*ret_panel)->del = panel_hi8561_del;
158 (*ret_panel)->init = panel_hi8561_init;
159 (*ret_panel)->reset = panel_hi8561_reset;
160 (*ret_panel)->mirror = panel_hi8561_mirror;
161 (*ret_panel)->invert_color = panel_hi8561_invert_color;
162 (*ret_panel)->disp_sleep = panel_hi8561_sleep;
163 (*ret_panel)->disp_on_off = panel_hi8561_on_off;
164 (*ret_panel)->user_data = hi8561;
166 ESP_LOGD(TAG,
"New hi8561 panel @%p", hi8561);
172 if (panel_dev_config->reset_gpio_num >= 0) {
173 gpio_reset_pin((gpio_num_t)panel_dev_config->reset_gpio_num);
180static esp_err_t panel_hi8561_send_init_cmds(hi8561_panel_t *hi8561)
182 esp_lcd_panel_io_handle_t io = hi8561->io;
183 const hi8561_lcd_init_cmd_t *init_cmds = NULL;
184 uint16_t init_cmds_size = 0;
186 init_cmds = vendor_specific_init_default;
187 init_cmds_size =
sizeof(vendor_specific_init_default) /
sizeof(hi8561_lcd_init_cmd_t);
188 for (
int i = 0;
i < init_cmds_size;
i++) {
189 esp_lcd_panel_io_tx_param(io, init_cmds[
i].
cmd, init_cmds[
i].data, init_cmds[
i].data_bytes);
190 vTaskDelay(pdMS_TO_TICKS(init_cmds[
i].delay_ms));
195static esp_err_t panel_hi8561_del(esp_lcd_panel_t *panel)
197 hi8561_panel_t *hi8561 = (hi8561_panel_t *)panel->user_data;
199 if (hi8561->reset_gpio_num >= 0) {
200 gpio_reset_pin((gpio_num_t)hi8561->reset_gpio_num);
207static esp_err_t panel_hi8561_init(esp_lcd_panel_t *panel)
209 hi8561_panel_t *hi8561 = (hi8561_panel_t *)panel->user_data;
211 ESP_RETURN_ON_ERROR(panel_hi8561_send_init_cmds(hi8561), TAG,
"Send init commands failed");
212 ESP_RETURN_ON_ERROR(hi8561->init(panel), TAG,
"Init MIPI DPI panel failed");
217static esp_err_t panel_hi8561_reset(esp_lcd_panel_t *panel)
219 hi8561_panel_t *hi8561 = (hi8561_panel_t *)panel->user_data;
220 esp_lcd_panel_io_handle_t io = hi8561->io;
221 if (hi8561->reset_gpio_num >= 0) {
222 gpio_set_level((gpio_num_t)hi8561->reset_gpio_num, hi8561->flags.reset_level);
223 vTaskDelay(pdMS_TO_TICKS(10));
224 gpio_set_level((gpio_num_t)hi8561->reset_gpio_num, !hi8561->flags.reset_level);
225 vTaskDelay(pdMS_TO_TICKS(20));
227 ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_SWRESET, NULL, 0), TAG,
"Send command failed");
228 vTaskDelay(pdMS_TO_TICKS(20));
233static esp_err_t panel_hi8561_sleep(esp_lcd_panel_t *panel,
bool sleep)
235 hi8561_panel_t *hi8561 = (hi8561_panel_t *)panel->user_data;
236 esp_lcd_panel_io_handle_t io = hi8561->io;
238 ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, 0x10, 0x00, 0), TAG,
"esp_lcd_panel_io_tx_param failed");
239 ESP_LOGI(TAG,
"sleep on");
241 ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, 0x11, 0x00, 0), TAG,
"esp_lcd_panel_io_tx_param failed");
242 ESP_LOGI(TAG,
"sleep off");
245 vTaskDelay(pdMS_TO_TICKS(120));
249static esp_err_t panel_hi8561_on_off(esp_lcd_panel_t *panel,
bool on_off)
251 hi8561_panel_t *hi8561 = (hi8561_panel_t *)panel->user_data;
252 esp_lcd_panel_io_handle_t io = hi8561->io;
253 if (on_off ==
true) {
254 ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, 0x29, 0x00, 0), TAG,
"esp_lcd_panel_io_tx_param failed");
255 ESP_LOGI(TAG,
"display on");
257 ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, 0x28, 0x00, 0), TAG,
"esp_lcd_panel_io_tx_param failed");
258 ESP_LOGI(TAG,
"display off");
260 vTaskDelay(pdMS_TO_TICKS(120));
264static esp_err_t panel_hi8561_mirror(esp_lcd_panel_t *panel,
bool mirror_x,
bool mirror_y)
266 hi8561_panel_t *hi8561 = (hi8561_panel_t *)panel->user_data;
267 esp_lcd_panel_io_handle_t io = hi8561->io;
268 uint8_t madctl_val = hi8561->madctl_val;
269 ESP_RETURN_ON_FALSE(io, ESP_ERR_INVALID_STATE, TAG,
"Invalid panel gpio");
272 madctl_val |= HI8561_CMD_SHLR_BIT;
274 madctl_val &= ~HI8561_CMD_SHLR_BIT;
277 madctl_val |= HI8561_CMD_UPDN_BIT;
279 madctl_val &= ~HI8561_CMD_UPDN_BIT;
281 ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_MADCTL, (uint8_t[]) {
283 }, 1), TAG,
"Send command failed");
284 hi8561->madctl_val = madctl_val;
288static esp_err_t panel_hi8561_invert_color(esp_lcd_panel_t *panel,
bool invert_color_data)
290 hi8561_panel_t *hi8561 = (hi8561_panel_t *)panel->user_data;
291 esp_lcd_panel_io_handle_t io = hi8561->io;
293 ESP_RETURN_ON_FALSE(io, ESP_ERR_INVALID_STATE, TAG,
"Invalid panel IO");
294 if (invert_color_data) {
295 command = LCD_CMD_INVON;
297 command = LCD_CMD_INVOFF;
299 ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, command, NULL, 0), TAG,
"Send command failed");