esp_lcd_gc9503.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <driver/gpio.h>
  7. #include <freertos/FreeRTOS.h>
  8. #include <freertos/task.h>
  9. #include <esp_check.h>
  10. #include <esp_lcd_panel_commands.h>
  11. #include <esp_lcd_panel_interface.h>
  12. #include <esp_lcd_panel_io.h>
  13. #include <esp_lcd_panel_vendor.h>
  14. #include <esp_log.h>
  15. #include "esp_lcd_gc9503.h"
  16. #define GC9503_CMD_MADCTL (0xB1) // Memory data access control
  17. #define GC9503_CMD_MADCTL_DEFAULT (0x10) // Default value of Memory data access control
  18. #define GC9503_CMD_SS_BIT (1 << 0) // Source driver scan direction, 0: top to bottom, 1: bottom to top
  19. #define GC9503_CMD_GS_BIT (1 << 1) // Gate driver scan direction, 0: left to right, 1: right to left
  20. #define GC9503_CMD_BGR_BIT (1 << 5) // RGB/BGR order, 0: RGB, 1: BGR
  21. typedef struct
  22. {
  23. esp_lcd_panel_io_handle_t io;
  24. int reset_gpio_num;
  25. uint8_t madctl_val; // Save current value of GC9503_CMD_MADCTL register
  26. uint8_t colmod_val; // Save current value of LCD_CMD_COLMOD register
  27. const gc9503_lcd_init_cmd_t *init_cmds;
  28. uint16_t init_cmds_size;
  29. struct
  30. {
  31. unsigned int mirror_by_cmd : 1;
  32. unsigned int auto_del_panel_io : 1;
  33. unsigned int display_on_off_use_cmd : 1;
  34. unsigned int reset_level : 1;
  35. } flags;
  36. // To save the original functions of RGB panel
  37. esp_err_t (*init)(esp_lcd_panel_t *panel);
  38. esp_err_t (*del)(esp_lcd_panel_t *panel);
  39. esp_err_t (*reset)(esp_lcd_panel_t *panel);
  40. esp_err_t (*mirror)(esp_lcd_panel_t *panel, bool x_axis, bool y_axis);
  41. esp_err_t (*disp_on_off)(esp_lcd_panel_t *panel, bool on_off);
  42. } gc9503_panel_t;
  43. static const char *TAG = "gc9503";
  44. static esp_err_t panel_gc9503_send_init_cmds(gc9503_panel_t *gc9503);
  45. static esp_err_t panel_gc9503_init(esp_lcd_panel_t *panel);
  46. static esp_err_t panel_gc9503_del(esp_lcd_panel_t *panel);
  47. static esp_err_t panel_gc9503_reset(esp_lcd_panel_t *panel);
  48. static esp_err_t panel_gc9503_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y);
  49. static esp_err_t panel_gc9503_disp_on_off(esp_lcd_panel_t *panel, bool off);
  50. esp_err_t esp_lcd_new_panel_gc9503(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config,
  51. esp_lcd_panel_handle_t *ret_panel)
  52. {
  53. ESP_RETURN_ON_FALSE(io && panel_dev_config && ret_panel, ESP_ERR_INVALID_ARG, TAG, "invalid arguments");
  54. gc9503_vendor_config_t *vendor_config = (gc9503_vendor_config_t *)panel_dev_config->vendor_config;
  55. ESP_RETURN_ON_FALSE(vendor_config && vendor_config->rgb_config, ESP_ERR_INVALID_ARG, TAG, "`verndor_config` and `rgb_config` are necessary");
  56. ESP_RETURN_ON_FALSE(!vendor_config->flags.auto_del_panel_io || !vendor_config->flags.mirror_by_cmd,
  57. ESP_ERR_INVALID_ARG, TAG, "`mirror_by_cmd` and `auto_del_panel_io` cannot work together");
  58. esp_err_t ret = ESP_OK;
  59. gpio_config_t io_conf = {0};
  60. gc9503_panel_t *gc9503 = (gc9503_panel_t *)calloc(1, sizeof(gc9503_panel_t));
  61. ESP_RETURN_ON_FALSE(gc9503, ESP_ERR_NO_MEM, TAG, "no mem for gc9503 panel");
  62. if (panel_dev_config->reset_gpio_num >= 0)
  63. {
  64. io_conf.mode = GPIO_MODE_OUTPUT;
  65. io_conf.pin_bit_mask = 1ULL << panel_dev_config->reset_gpio_num;
  66. ESP_GOTO_ON_ERROR(gpio_config(&io_conf), err, TAG, "configure GPIO for RST line failed");
  67. }
  68. gc9503->madctl_val = GC9503_CMD_MADCTL_DEFAULT;
  69. switch (panel_dev_config->rgb_ele_order)
  70. {
  71. case LCD_RGB_ELEMENT_ORDER_RGB:
  72. gc9503->madctl_val &= ~GC9503_CMD_BGR_BIT;
  73. break;
  74. case LCD_RGB_ELEMENT_ORDER_BGR:
  75. gc9503->madctl_val |= GC9503_CMD_BGR_BIT;
  76. break;
  77. default:
  78. ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported color element order");
  79. break;
  80. }
  81. gc9503->colmod_val = 0;
  82. switch (panel_dev_config->bits_per_pixel)
  83. {
  84. case 16: // RGB565
  85. gc9503->colmod_val = 0x50;
  86. break;
  87. case 18: // RGB666
  88. gc9503->colmod_val = 0x60;
  89. break;
  90. case 24: // RGB888
  91. gc9503->colmod_val = 0x70;
  92. break;
  93. default:
  94. ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported pixel width");
  95. break;
  96. }
  97. gc9503->io = io;
  98. gc9503->init_cmds = vendor_config->init_cmds;
  99. gc9503->init_cmds_size = vendor_config->init_cmds_size;
  100. gc9503->reset_gpio_num = panel_dev_config->reset_gpio_num;
  101. gc9503->flags.reset_level = panel_dev_config->flags.reset_active_high;
  102. gc9503->flags.auto_del_panel_io = vendor_config->flags.auto_del_panel_io;
  103. gc9503->flags.mirror_by_cmd = vendor_config->flags.mirror_by_cmd;
  104. gc9503->flags.display_on_off_use_cmd = (vendor_config->rgb_config->disp_gpio_num >= 0) ? 0 : 1;
  105. if (gc9503->flags.auto_del_panel_io)
  106. {
  107. if (gc9503->reset_gpio_num >= 0)
  108. { // Perform hardware reset
  109. gpio_set_level(gc9503->reset_gpio_num, gc9503->flags.reset_level);
  110. vTaskDelay(pdMS_TO_TICKS(10));
  111. gpio_set_level(gc9503->reset_gpio_num, !gc9503->flags.reset_level);
  112. }
  113. else
  114. { // Perform software reset
  115. ESP_GOTO_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_SWRESET, NULL, 0), err, TAG, "send command failed");
  116. }
  117. vTaskDelay(pdMS_TO_TICKS(120));
  118. /**
  119. * In order to enable the 3-wire SPI interface pins (such as SDA and SCK) to share other pins of the RGB interface
  120. * (such as HSYNC) and save GPIOs, we need to send LCD initialization commands via the 3-wire SPI interface before
  121. * `esp_lcd_new_rgb_panel()` is called.
  122. */
  123. ESP_GOTO_ON_ERROR(panel_gc9503_send_init_cmds(gc9503), err, TAG, "send init commands failed");
  124. // After sending the initialization commands, the 3-wire SPI interface can be deleted
  125. ESP_GOTO_ON_ERROR(esp_lcd_panel_io_del(io), err, TAG, "delete panel IO failed");
  126. gc9503->io = NULL;
  127. ESP_LOGD(TAG, "delete panel IO");
  128. }
  129. // Create RGB panel
  130. ESP_GOTO_ON_ERROR(esp_lcd_new_rgb_panel(vendor_config->rgb_config, ret_panel), err, TAG, "create RGB panel failed");
  131. ESP_LOGD(TAG, "new RGB panel @%p", ret_panel);
  132. // Save the original functions of RGB panel
  133. gc9503->init = (*ret_panel)->init;
  134. gc9503->del = (*ret_panel)->del;
  135. gc9503->reset = (*ret_panel)->reset;
  136. gc9503->mirror = (*ret_panel)->mirror;
  137. gc9503->disp_on_off = (*ret_panel)->disp_on_off;
  138. // Overwrite the functions of RGB panel
  139. (*ret_panel)->init = panel_gc9503_init;
  140. (*ret_panel)->del = panel_gc9503_del;
  141. (*ret_panel)->reset = panel_gc9503_reset;
  142. (*ret_panel)->mirror = panel_gc9503_mirror;
  143. (*ret_panel)->disp_on_off = panel_gc9503_disp_on_off;
  144. (*ret_panel)->user_data = gc9503;
  145. ESP_LOGD(TAG, "new gc9503 panel @%p", gc9503);
  146. // ESP_LOGI(TAG, "LCD panel create success, version: %d.%d.%d", ESP_LCD_GC9503_VER_MAJOR, ESP_LCD_GC9503_VER_MINOR,
  147. // ESP_LCD_GC9503_VER_PATCH);
  148. return ESP_OK;
  149. err:
  150. if (gc9503)
  151. {
  152. if (panel_dev_config->reset_gpio_num >= 0)
  153. {
  154. gpio_reset_pin(panel_dev_config->reset_gpio_num);
  155. }
  156. free(gc9503);
  157. }
  158. return ret;
  159. }
  160. // *INDENT-OFF*
  161. // static const gc9503_lcd_init_cmd_t vendor_specific_init_default[] = {
  162. // // {cmd, { data }, data_size, delay_ms}
  163. // {0x11, (uint8_t []){0x00}, 0, 120},
  164. // {0xf0, (uint8_t []){0x55, 0xaa, 0x52, 0x08, 0x00}, 5, 0},
  165. // {0xf6, (uint8_t []){0x5a, 0x87}, 2, 0},
  166. // {0xc1, (uint8_t []){0x3f}, 1, 0},
  167. // {0xc2, (uint8_t []){0x0e}, 1, 0},
  168. // {0xc6, (uint8_t []){0xf8}, 1, 0},
  169. // {0xc9, (uint8_t []){0x10}, 1, 0},
  170. // {0xcd, (uint8_t []){0x25}, 1, 0},
  171. // {0xf8, (uint8_t []){0x8a}, 1, 0},
  172. // {0xac, (uint8_t []){0x45}, 1, 0},
  173. // {0xa0, (uint8_t []){0xdd}, 1, 0},
  174. // {0xa7, (uint8_t []){0x47}, 1, 0},
  175. // {0xfa, (uint8_t []){0x00, 0x00, 0x00, 0x04}, 4, 0},
  176. // {0x86, (uint8_t []){0x99, 0xa3, 0xa3, 0x51}, 4, 0},
  177. // {0xa3, (uint8_t []){0xee}, 1, 0},
  178. // {0xfd, (uint8_t []){0x3c, 0x3c, 0x00}, 3, 0},
  179. // {0x71, (uint8_t []){0x48}, 1, 0},
  180. // {0x72, (uint8_t []){0x48}, 1, 0},
  181. // {0x73, (uint8_t []){0x00, 0x44}, 2, 0},
  182. // {0x97, (uint8_t []){0xee}, 1, 0},
  183. // {0x83, (uint8_t []){0x93}, 1, 0},
  184. // {0x9a, (uint8_t []){0x72}, 1, 0},
  185. // {0x9b, (uint8_t []){0x5a}, 1, 0},
  186. // {0x82, (uint8_t []){0x2c, 0x2c}, 2, 0},
  187. // {0x6d, (uint8_t []){0x00, 0x1f, 0x19, 0x1a, 0x10, 0x0e, 0x0c, 0x0a, 0x02, 0x07, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  188. // 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x08, 0x01, 0x09, 0x0b, 0x0d, 0x0f, 0x1a, 0x19, 0x1f, 0x00}, 32, 0},
  189. // {0x64, (uint8_t []){0x38, 0x05, 0x01, 0xdb, 0x03, 0x03, 0x38, 0x04, 0x01, 0xdc, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0},
  190. // {0x65, (uint8_t []){0x38, 0x03, 0x01, 0xdd, 0x03, 0x03, 0x38, 0x02, 0x01, 0xde, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0},
  191. // {0x66, (uint8_t []){0x38, 0x01, 0x01, 0xdf, 0x03, 0x03, 0x38, 0x00, 0x01, 0xe0, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0},
  192. // {0x67, (uint8_t []){0x30, 0x01, 0x01, 0xe1, 0x03, 0x03, 0x30, 0x02, 0x01, 0xe2, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a}, 16, 0},
  193. // {0x68, (uint8_t []){0x00, 0x08, 0x15, 0x08, 0x15, 0x7a, 0x7a, 0x08, 0x15, 0x08, 0x15, 0x7a, 0x7a}, 13, 0},
  194. // {0x60, (uint8_t []){0x38, 0x08, 0x7a, 0x7a, 0x38, 0x09, 0x7a, 0x7a}, 8, 0},
  195. // {0x63, (uint8_t []){0x31, 0xe4, 0x7a, 0x7a, 0x31, 0xe5, 0x7a, 0x7a}, 8, 0},
  196. // {0x69, (uint8_t []){0x04, 0x22, 0x14, 0x22, 0x14, 0x22, 0x08}, 7, 0},
  197. // {0x6b, (uint8_t []){0x07}, 1, 0},
  198. // {0x7a, (uint8_t []){0x08, 0x13}, 2, 0},
  199. // {0x7b, (uint8_t []){0x08, 0x13}, 2, 0},
  200. // {0xd1, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
  201. // 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
  202. // 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
  203. // 0xff}, 52, 0},
  204. // {0xd2, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
  205. // 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
  206. // 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
  207. // 0xff}, 52, 0},
  208. // {0xd3, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
  209. // 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
  210. // 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
  211. // 0xff}, 52, 0},
  212. // {0xd4, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
  213. // 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
  214. // 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
  215. // 0xff}, 52, 0},
  216. // {0xd5, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
  217. // 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
  218. // 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
  219. // 0xff}, 52, 0},
  220. // {0xd6, (uint8_t []){0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
  221. // 0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
  222. // 0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
  223. // 0xff}, 52, 0},
  224. // {0x11, (uint8_t []){0x00}, 0, 120},
  225. // {0x29, (uint8_t []){0x00}, 0, 20},
  226. // };
  227. static const gc9503_lcd_init_cmd_t vendor_specific_init_default[] = {
  228. // {0x11, (uint8_t[]){}, 0, 20},
  229. {0xF0, (uint8_t[]){0x55, 0xAA, 0x52, 0x08, 0x00}, 5, 0},
  230. {0xF6, (uint8_t[]){0x5A, 0x87}, 2, 0},
  231. {0xC1, (uint8_t[]){0x3F}, 1, 0},
  232. {0xCD, (uint8_t[]){0x25}, 1, 0},
  233. {0xC9, (uint8_t[]){0x10}, 1, 0},
  234. {0xF8, (uint8_t[]){0x8A}, 1, 0},
  235. {0xAC, (uint8_t[]){0x45}, 1, 0},
  236. {0xA7, (uint8_t[]){0x47}, 1, 0},
  237. {0xA0, (uint8_t[]){0x88}, 1, 0},
  238. {0x86, (uint8_t[]){0x99, 0xA3, 0xA3, 0x51}, 4, 0},
  239. {0xFA, (uint8_t[]){0x08, 0x08, 0x00, 0x04}, 4, 0},
  240. {0xA3, (uint8_t[]){0x6E}, 1, 0},
  241. {0xFD, (uint8_t[]){0x28, 0x3C, 0x00}, 3, 0},
  242. {0x9A, (uint8_t[]){0x4B}, 1, 0},
  243. {0x9B, (uint8_t[]){0x4B}, 1, 0},
  244. {0x82, (uint8_t[]){0x20, 0x20}, 2, 0},
  245. {0xB1, (uint8_t[]){0x10}, 1, 0},
  246. {0x7A, (uint8_t[]){0x0F, 0x13}, 2, 0},
  247. {0x7B, (uint8_t[]){0x0F, 0x13}, 2, 0},
  248. {0x6D, (uint8_t[]){0x1e, 0x1e, 0x04, 0x02, 0x0d, 0x1e, 0x12, 0x11, 0x14, 0x13, 0x05, 0x06, 0x1d, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1d, 0x06, 0x05, 0x0b, 0x0c, 0x09, 0x0a, 0x1e, 0x0d, 0x01, 0x03, 0x1e, 0x1e}, 32, 0},
  249. {0x64, (uint8_t[]){0x38, 0x08, 0x03, 0xc0, 0x03, 0x03, 0x38, 0x06, 0x03, 0xc2, 0x03, 0x03, 0x20, 0x6d, 0x20, 0x6d}, 16, 0},
  250. {0x65, (uint8_t[]){0x38, 0x04, 0x03, 0xc4, 0x03, 0x03, 0x38, 0x02, 0x03, 0xc6, 0x03, 0x03, 0x20, 0x6d, 0x20, 0x6d}, 16, 0},
  251. {0x66, (uint8_t[]){0x83, 0xcf, 0x03, 0xc8, 0x03, 0x03, 0x83, 0xd3, 0x03, 0xd2, 0x03, 0x03, 0x20, 0x6d, 0x20, 0x6d}, 16, 0},
  252. {0x60, (uint8_t[]){0x38, 0x0C, 0x20, 0x6D, 0x38, 0x0B, 0x20, 0x6D}, 8, 0},
  253. {0x61, (uint8_t[]){0x38, 0x0A, 0x20, 0x6D, 0x38, 0x09, 0x20, 0x6D}, 8, 0},
  254. {0x62, (uint8_t[]){0x38, 0x25, 0x20, 0x6D, 0x63, 0xC9, 0x20, 0x6D}, 8, 0},
  255. {0x69, (uint8_t[]){0x14, 0x22, 0x14, 0x22, 0x14, 0x22, 0x08}, 7, 0},
  256. {0x6B, (uint8_t[]){0x07}, 1, 0},
  257. {0xD1, (uint8_t[]){0x00, 0x00, 0x00, 0x70, 0x00, 0x8f, 0x00, 0xab, 0x00, 0xbf, 0x00, 0xdf, 0x00, 0xfa, 0x01, 0x2a, 0x01, 0x52, 0x01, 0x90, 0x01, 0xc1, 0x02, 0x0e, 0x02, 0x4f, 0x02, 0x51, 0x02, 0x8d, 0x02, 0xd3, 0x02, 0xff, 0x03, 0x3c, 0x03, 0x64, 0x03, 0xa1, 0x03, 0xf1, 0x03, 0xff, 0x03, 0xfF, 0x03, 0xff, 0x03, 0xFf, 0x03, 0xFF}, 52, 0},
  258. {0xD2, (uint8_t[]){0x00, 0x00, 0x00, 0x70, 0x00, 0x8f, 0x00, 0xab, 0x00, 0xbf, 0x00, 0xdf, 0x00, 0xfa, 0x01, 0x2a, 0x01, 0x52, 0x01, 0x90, 0x01, 0xc1, 0x02, 0x0e, 0x02, 0x4f, 0x02, 0x51, 0x02, 0x8d, 0x02, 0xd3, 0x02, 0xff, 0x03, 0x3c, 0x03, 0x64, 0x03, 0xa1, 0x03, 0xf1, 0x03, 0xff, 0x03, 0xfF, 0x03, 0xff, 0x03, 0xFf, 0x03, 0xFF}, 52, 0},
  259. {0xD3, (uint8_t[]){0x00, 0x00, 0x00, 0x70, 0x00, 0x8f, 0x00, 0xab, 0x00, 0xbf, 0x00, 0xdf, 0x00, 0xfa, 0x01, 0x2a, 0x01, 0x52, 0x01, 0x90, 0x01, 0xc1, 0x02, 0x0e, 0x02, 0x4f, 0x02, 0x51, 0x02, 0x8d, 0x02, 0xd3, 0x02, 0xff, 0x03, 0x3c, 0x03, 0x64, 0x03, 0xa1, 0x03, 0xf1, 0x03, 0xff, 0x03, 0xfF, 0x03, 0xff, 0x03, 0xFf, 0x03, 0xFF}, 52, 0},
  260. {0xD4, (uint8_t[]){0x00, 0x00, 0x00, 0x70, 0x00, 0x8f, 0x00, 0xab, 0x00, 0xbf, 0x00, 0xdf, 0x00, 0xfa, 0x01, 0x2a, 0x01, 0x52, 0x01, 0x90, 0x01, 0xc1, 0x02, 0x0e, 0x02, 0x4f, 0x02, 0x51, 0x02, 0x8d, 0x02, 0xd3, 0x02, 0xff, 0x03, 0x3c, 0x03, 0x64, 0x03, 0xa1, 0x03, 0xf1, 0x03, 0xff, 0x03, 0xfF, 0x03, 0xff, 0x03, 0xFf, 0x03, 0xFF}, 52, 0},
  261. {0xD5, (uint8_t[]){0x00, 0x00, 0x00, 0x70, 0x00, 0x8f, 0x00, 0xab, 0x00, 0xbf, 0x00, 0xdf, 0x00, 0xfa, 0x01, 0x2a, 0x01, 0x52, 0x01, 0x90, 0x01, 0xc1, 0x02, 0x0e, 0x02, 0x4f, 0x02, 0x51, 0x02, 0x8d, 0x02, 0xd3, 0x02, 0xff, 0x03, 0x3c, 0x03, 0x64, 0x03, 0xa1, 0x03, 0xf1, 0x03, 0xff, 0x03, 0xfF, 0x03, 0xff, 0x03, 0xFf, 0x03, 0xFF}, 52, 0},
  262. {0xD6, (uint8_t[]){0x00, 0x00, 0x00, 0x70, 0x00, 0x8f, 0x00, 0xab, 0x00, 0xbf, 0x00, 0xdf, 0x00, 0xfa, 0x01, 0x2a, 0x01, 0x52, 0x01, 0x90, 0x01, 0xc1, 0x02, 0x0e, 0x02, 0x4f, 0x02, 0x51, 0x02, 0x8d, 0x02, 0xd3, 0x02, 0xff, 0x03, 0x3c, 0x03, 0x64, 0x03, 0xa1, 0x03, 0xf1, 0x03, 0xff, 0x03, 0xfF, 0x03, 0xff, 0x03, 0xFf, 0x03, 0xFF}, 52, 0},
  263. // {0x3A, (uint8_t[]){0x55}, 1, 0},
  264. {0x11, NULL, 0, 120}, // Delay 120ms
  265. {0x29, NULL, 0, 120}};
  266. // *INDENT-OFF*
  267. static esp_err_t panel_gc9503_send_init_cmds(gc9503_panel_t *gc9503)
  268. {
  269. esp_lcd_panel_io_handle_t io = gc9503->io;
  270. ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, GC9503_CMD_MADCTL, (uint8_t[]){
  271. gc9503->madctl_val,
  272. },
  273. 1),
  274. TAG, "send command failed");
  275. ;
  276. ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_COLMOD, (uint8_t[]){
  277. gc9503->colmod_val,
  278. },
  279. 1),
  280. TAG, "send command failed");
  281. ;
  282. // Vendor specific initialization, it can be different between manufacturers
  283. // should consult the LCD supplier for initialization sequence code
  284. const gc9503_lcd_init_cmd_t *init_cmds = NULL;
  285. uint16_t init_cmds_size = 0;
  286. if (gc9503->init_cmds)
  287. {
  288. init_cmds = gc9503->init_cmds;
  289. init_cmds_size = gc9503->init_cmds_size;
  290. }
  291. else
  292. {
  293. init_cmds = vendor_specific_init_default;
  294. init_cmds_size = sizeof(vendor_specific_init_default) / sizeof(gc9503_lcd_init_cmd_t);
  295. }
  296. bool is_cmd_overwritten = false;
  297. for (int i = 0; i < init_cmds_size; i++)
  298. {
  299. // Check if the command has been used or conflicts with the internal
  300. switch (init_cmds[i].cmd)
  301. {
  302. case LCD_CMD_MADCTL:
  303. is_cmd_overwritten = true;
  304. gc9503->madctl_val = ((uint8_t *)init_cmds[i].data)[0];
  305. break;
  306. case LCD_CMD_COLMOD:
  307. is_cmd_overwritten = true;
  308. gc9503->colmod_val = ((uint8_t *)init_cmds[i].data)[0];
  309. break;
  310. default:
  311. is_cmd_overwritten = false;
  312. break;
  313. }
  314. if (is_cmd_overwritten)
  315. {
  316. ESP_LOGW(TAG, "The %02Xh command has been used and will be overwritten by external initialization sequence",
  317. init_cmds[i].cmd);
  318. }
  319. ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, init_cmds[i].cmd, init_cmds[i].data, init_cmds[i].data_bytes),
  320. TAG, "send command failed");
  321. vTaskDelay(pdMS_TO_TICKS(init_cmds[i].delay_ms));
  322. }
  323. ESP_LOGD(TAG, "send init commands success");
  324. return ESP_OK;
  325. }
  326. static esp_err_t panel_gc9503_init(esp_lcd_panel_t *panel)
  327. {
  328. gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data;
  329. if (!gc9503->flags.auto_del_panel_io)
  330. {
  331. ESP_RETURN_ON_ERROR(panel_gc9503_send_init_cmds(gc9503), TAG, "send init commands failed");
  332. }
  333. // Init RGB panel
  334. ESP_RETURN_ON_ERROR(gc9503->init(panel), TAG, "init RGB panel failed");
  335. return ESP_OK;
  336. }
  337. static esp_err_t panel_gc9503_del(esp_lcd_panel_t *panel)
  338. {
  339. gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data;
  340. if (gc9503->reset_gpio_num >= 0)
  341. {
  342. gpio_reset_pin(gc9503->reset_gpio_num);
  343. }
  344. // Delete RGB panel
  345. gc9503->del(panel);
  346. free(gc9503);
  347. ESP_LOGD(TAG, "del gc9503 panel @%p", gc9503);
  348. return ESP_OK;
  349. }
  350. static esp_err_t panel_gc9503_reset(esp_lcd_panel_t *panel)
  351. {
  352. gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data;
  353. esp_lcd_panel_io_handle_t io = gc9503->io;
  354. // Perform hardware reset
  355. if (gc9503->reset_gpio_num >= 0)
  356. {
  357. gpio_set_level(gc9503->reset_gpio_num, gc9503->flags.reset_level);
  358. vTaskDelay(pdMS_TO_TICKS(10));
  359. gpio_set_level(gc9503->reset_gpio_num, !gc9503->flags.reset_level);
  360. vTaskDelay(pdMS_TO_TICKS(120));
  361. }
  362. else if (io)
  363. { // Perform software reset
  364. ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, LCD_CMD_SWRESET, NULL, 0), TAG, "send command failed");
  365. vTaskDelay(pdMS_TO_TICKS(120));
  366. }
  367. // Reset RGB panel
  368. ESP_RETURN_ON_ERROR(gc9503->reset(panel), TAG, "reset RGB panel failed");
  369. return ESP_OK;
  370. }
  371. static esp_err_t panel_gc9503_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y)
  372. {
  373. gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data;
  374. esp_lcd_panel_io_handle_t io = gc9503->io;
  375. if (gc9503->flags.mirror_by_cmd)
  376. {
  377. ESP_RETURN_ON_FALSE(io, ESP_FAIL, TAG, "Panel IO is deleted, cannot send command");
  378. // Control mirror through LCD command
  379. if (mirror_x)
  380. {
  381. gc9503->madctl_val |= GC9503_CMD_GS_BIT;
  382. }
  383. else
  384. {
  385. gc9503->madctl_val &= ~GC9503_CMD_GS_BIT;
  386. }
  387. if (mirror_y)
  388. {
  389. gc9503->madctl_val |= GC9503_CMD_SS_BIT;
  390. }
  391. else
  392. {
  393. gc9503->madctl_val &= ~GC9503_CMD_SS_BIT;
  394. }
  395. ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, GC9503_CMD_MADCTL, (uint8_t[]){
  396. gc9503->madctl_val,
  397. },
  398. 1),
  399. TAG, "send command failed");
  400. ;
  401. }
  402. else
  403. {
  404. // Control mirror through RGB panel
  405. ESP_RETURN_ON_ERROR(gc9503->mirror(panel, mirror_x, mirror_y), TAG, "RGB panel mirror failed");
  406. }
  407. return ESP_OK;
  408. }
  409. static esp_err_t panel_gc9503_disp_on_off(esp_lcd_panel_t *panel, bool on_off)
  410. {
  411. gc9503_panel_t *gc9503 = (gc9503_panel_t *)panel->user_data;
  412. esp_lcd_panel_io_handle_t io = gc9503->io;
  413. int command = 0;
  414. if (gc9503->flags.display_on_off_use_cmd)
  415. {
  416. ESP_RETURN_ON_FALSE(io, ESP_FAIL, TAG, "Panel IO is deleted, cannot send command");
  417. // Control display on/off through LCD command
  418. if (on_off)
  419. {
  420. command = LCD_CMD_DISPON;
  421. }
  422. else
  423. {
  424. command = LCD_CMD_DISPOFF;
  425. }
  426. ESP_RETURN_ON_ERROR(esp_lcd_panel_io_tx_param(io, command, NULL, 0), TAG, "send command failed");
  427. }
  428. else
  429. {
  430. // Control display on/off through display control signal
  431. ESP_RETURN_ON_ERROR(gc9503->disp_on_off(panel, on_off), TAG, "RGB panel disp_on_off failed");
  432. }
  433. return ESP_OK;
  434. }