i80_controller_example_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_timer.h"
  10. #include "esp_lcd_panel_io.h"
  11. #include "esp_lcd_panel_vendor.h"
  12. #include "esp_lcd_panel_ops.h"
  13. #include "driver/gpio.h"
  14. #include "esp_err.h"
  15. #include "esp_log.h"
  16. #include "lvgl.h"
  17. static const char *TAG = "example";
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. //////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  22. // PCLK frequency can't go too high as the limitation of PSRAM bandwidth
  23. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (2 * 1000 * 1000)
  24. #else
  25. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (10 * 1000 * 1000)
  26. #endif // CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  27. #define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1
  28. #define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
  29. #define EXAMPLE_PIN_NUM_DATA0 6
  30. #define EXAMPLE_PIN_NUM_DATA1 7
  31. #define EXAMPLE_PIN_NUM_DATA2 8
  32. #define EXAMPLE_PIN_NUM_DATA3 9
  33. #define EXAMPLE_PIN_NUM_DATA4 10
  34. #define EXAMPLE_PIN_NUM_DATA5 11
  35. #define EXAMPLE_PIN_NUM_DATA6 12
  36. #define EXAMPLE_PIN_NUM_DATA7 13
  37. #if CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH > 8
  38. #define EXAMPLE_PIN_NUM_DATA8 14
  39. #define EXAMPLE_PIN_NUM_DATA9 15
  40. #define EXAMPLE_PIN_NUM_DATA10 16
  41. #define EXAMPLE_PIN_NUM_DATA11 17
  42. #define EXAMPLE_PIN_NUM_DATA12 18
  43. #define EXAMPLE_PIN_NUM_DATA13 19
  44. #define EXAMPLE_PIN_NUM_DATA14 20
  45. #define EXAMPLE_PIN_NUM_DATA15 21
  46. #endif
  47. #define EXAMPLE_PIN_NUM_PCLK 5
  48. #define EXAMPLE_PIN_NUM_CS 3
  49. #define EXAMPLE_PIN_NUM_DC 4
  50. #define EXAMPLE_PIN_NUM_RST 2
  51. #define EXAMPLE_PIN_NUM_BK_LIGHT 1
  52. // The pixel number in horizontal and vertical
  53. #define EXAMPLE_LCD_H_RES 240
  54. #define EXAMPLE_LCD_V_RES 280
  55. // Bit number used to represent command and parameter
  56. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  57. #define EXAMPLE_LCD_CMD_BITS 8
  58. #define EXAMPLE_LCD_PARAM_BITS 8
  59. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  60. #define EXAMPLE_LCD_CMD_BITS 16
  61. #define EXAMPLE_LCD_PARAM_BITS 16
  62. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
  63. #define EXAMPLE_LCD_CMD_BITS 8
  64. #define EXAMPLE_LCD_PARAM_BITS 8
  65. #endif
  66. #define EXAMPLE_LVGL_TICK_PERIOD_MS 2
  67. // Supported alignment: 16, 32, 64. A higher alignment can enables higher burst transfer size, thus a higher i80 bus throughput.
  68. #define EXAMPLE_PSRAM_DATA_ALIGNMENT 64
  69. extern void example_lvgl_demo_ui(lv_obj_t *scr);
  70. static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
  71. {
  72. lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
  73. lv_disp_flush_ready(disp_driver);
  74. return false;
  75. }
  76. static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
  77. {
  78. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  79. int offsetx1 = area->x1;
  80. int offsetx2 = area->x2;
  81. int offsety1 = area->y1;
  82. int offsety2 = area->y2;
  83. // copy a buffer's content to a specific area of the display
  84. esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
  85. }
  86. static void example_increase_lvgl_tick(void *arg)
  87. {
  88. /* Tell LVGL how many milliseconds has elapsed */
  89. lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
  90. }
  91. void app_main(void)
  92. {
  93. static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
  94. static lv_disp_drv_t disp_drv; // contains callback functions
  95. ESP_LOGI(TAG, "Turn off LCD backlight");
  96. gpio_config_t bk_gpio_config = {
  97. .mode = GPIO_MODE_OUTPUT,
  98. .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT
  99. };
  100. ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
  101. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL);
  102. ESP_LOGI(TAG, "Initialize Intel 8080 bus");
  103. esp_lcd_i80_bus_handle_t i80_bus = NULL;
  104. esp_lcd_i80_bus_config_t bus_config = {
  105. .clk_src = LCD_CLK_SRC_DEFAULT,
  106. .dc_gpio_num = EXAMPLE_PIN_NUM_DC,
  107. .wr_gpio_num = EXAMPLE_PIN_NUM_PCLK,
  108. .data_gpio_nums = {
  109. EXAMPLE_PIN_NUM_DATA0,
  110. EXAMPLE_PIN_NUM_DATA1,
  111. EXAMPLE_PIN_NUM_DATA2,
  112. EXAMPLE_PIN_NUM_DATA3,
  113. EXAMPLE_PIN_NUM_DATA4,
  114. EXAMPLE_PIN_NUM_DATA5,
  115. EXAMPLE_PIN_NUM_DATA6,
  116. EXAMPLE_PIN_NUM_DATA7,
  117. #if CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH > 8
  118. EXAMPLE_PIN_NUM_DATA8,
  119. EXAMPLE_PIN_NUM_DATA9,
  120. EXAMPLE_PIN_NUM_DATA10,
  121. EXAMPLE_PIN_NUM_DATA11,
  122. EXAMPLE_PIN_NUM_DATA12,
  123. EXAMPLE_PIN_NUM_DATA13,
  124. EXAMPLE_PIN_NUM_DATA14,
  125. EXAMPLE_PIN_NUM_DATA15,
  126. #endif
  127. },
  128. .bus_width = CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH,
  129. .max_transfer_bytes = EXAMPLE_LCD_H_RES * 100 * sizeof(uint16_t),
  130. .psram_trans_align = EXAMPLE_PSRAM_DATA_ALIGNMENT,
  131. .sram_trans_align = 4,
  132. };
  133. ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
  134. esp_lcd_panel_io_handle_t io_handle = NULL;
  135. esp_lcd_panel_io_i80_config_t io_config = {
  136. .cs_gpio_num = EXAMPLE_PIN_NUM_CS,
  137. .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  138. .trans_queue_depth = 10,
  139. .dc_levels = {
  140. .dc_idle_level = 0,
  141. .dc_cmd_level = 0,
  142. .dc_dummy_level = 0,
  143. .dc_data_level = 1,
  144. },
  145. .flags = {
  146. .swap_color_bytes = !LV_COLOR_16_SWAP, // Swap can be done in LvGL (default) or DMA
  147. },
  148. .on_color_trans_done = example_notify_lvgl_flush_ready,
  149. .user_ctx = &disp_drv,
  150. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
  151. .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
  152. };
  153. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &io_handle));
  154. esp_lcd_panel_handle_t panel_handle = NULL;
  155. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  156. ESP_LOGI(TAG, "Install LCD driver of st7789");
  157. esp_lcd_panel_dev_config_t panel_config = {
  158. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  159. .color_space = ESP_LCD_COLOR_SPACE_RGB,
  160. .bits_per_pixel = 16,
  161. };
  162. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  163. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  164. ESP_LOGI(TAG, "Install LCD driver of nt35510");
  165. esp_lcd_panel_dev_config_t panel_config = {
  166. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  167. .color_space = ESP_LCD_COLOR_SPACE_BGR,
  168. .bits_per_pixel = 16,
  169. };
  170. ESP_ERROR_CHECK(esp_lcd_new_panel_nt35510(io_handle, &panel_config, &panel_handle));
  171. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
  172. // ILI9341 is NOT a distinct driver, but a special case of ST7789
  173. // (essential registers are identical). A few lines further down in this code,
  174. // it's shown how to issue additional device-specific commands.
  175. ESP_LOGI(TAG, "Install LCD driver of ili9341 (st7789 compatible)");
  176. esp_lcd_panel_dev_config_t panel_config = {
  177. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  178. .color_space = ESP_LCD_COLOR_SPACE_BGR,
  179. .bits_per_pixel = 16,
  180. };
  181. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  182. #endif
  183. esp_lcd_panel_reset(panel_handle);
  184. esp_lcd_panel_init(panel_handle);
  185. // Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
  186. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  187. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  188. esp_lcd_panel_invert_color(panel_handle, true);
  189. esp_lcd_panel_set_gap(panel_handle, 0, 20);
  190. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  191. esp_lcd_panel_swap_xy(panel_handle, true);
  192. esp_lcd_panel_mirror(panel_handle, true, false);
  193. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
  194. esp_lcd_panel_swap_xy(panel_handle, true);
  195. esp_lcd_panel_invert_color(panel_handle, false);
  196. // ILI9341 is very similar to ST7789 and shares the same driver.
  197. // Anything unconventional (such as this custom gamma table) can
  198. // be issued here in user code and need not modify the driver.
  199. esp_lcd_panel_io_tx_param(io_handle, 0xF2, (uint8_t[]) { 0 }, 1); // 3Gamma function disable
  200. esp_lcd_panel_io_tx_param(io_handle, 0x26, (uint8_t[]) { 1 }, 1); // Gamma curve 1 selected
  201. esp_lcd_panel_io_tx_param(io_handle, 0xE0, (uint8_t[]) { // Set positive gamma
  202. 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00 }, 15);
  203. esp_lcd_panel_io_tx_param(io_handle, 0xE1, (uint8_t[]) { // Set negative gamma
  204. 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F }, 15);
  205. #endif
  206. // user can flush pre-defined pattern to the screen before we turn on the screen or backlight
  207. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  208. ESP_LOGI(TAG, "Turn on LCD backlight");
  209. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
  210. ESP_LOGI(TAG, "Initialize LVGL library");
  211. lv_init();
  212. // alloc draw buffers used by LVGL
  213. // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
  214. lv_color_t *buf1 = NULL;
  215. lv_color_t *buf2 = NULL;
  216. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  217. buf1 = heap_caps_aligned_alloc(EXAMPLE_PSRAM_DATA_ALIGNMENT, EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  218. #else
  219. buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  220. #endif
  221. assert(buf1);
  222. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  223. buf2 = heap_caps_aligned_alloc(EXAMPLE_PSRAM_DATA_ALIGNMENT, EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  224. #else
  225. buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  226. #endif
  227. assert(buf2);
  228. ESP_LOGI(TAG, "buf1@%p, buf2@%p", buf1, buf2);
  229. // initialize LVGL draw buffers
  230. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 100);
  231. ESP_LOGI(TAG, "Register display driver to LVGL");
  232. lv_disp_drv_init(&disp_drv);
  233. disp_drv.hor_res = EXAMPLE_LCD_H_RES;
  234. disp_drv.ver_res = EXAMPLE_LCD_V_RES;
  235. disp_drv.flush_cb = example_lvgl_flush_cb;
  236. disp_drv.draw_buf = &disp_buf;
  237. disp_drv.user_data = panel_handle;
  238. lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
  239. ESP_LOGI(TAG, "Install LVGL tick timer");
  240. // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  241. const esp_timer_create_args_t lvgl_tick_timer_args = {
  242. .callback = &example_increase_lvgl_tick,
  243. .name = "lvgl_tick"
  244. };
  245. esp_timer_handle_t lvgl_tick_timer = NULL;
  246. ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  247. ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
  248. ESP_LOGI(TAG, "Display LVGL animation");
  249. lv_obj_t *scr = lv_disp_get_scr_act(disp);
  250. example_lvgl_demo_ui(scr);
  251. while (1) {
  252. // raise the task priority of LVGL and/or reduce the handler period can improve the performance
  253. vTaskDelay(pdMS_TO_TICKS(10));
  254. // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
  255. lv_timer_handler();
  256. }
  257. }