i80_controller_example_main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #define EXAMPLE_PIN_NUM_PCLK 5
  38. #define EXAMPLE_PIN_NUM_CS 3
  39. #define EXAMPLE_PIN_NUM_DC 4
  40. #define EXAMPLE_PIN_NUM_RST 2
  41. #define EXAMPLE_PIN_NUM_BK_LIGHT 1
  42. // The pixel number in horizontal and vertical
  43. #define EXAMPLE_LCD_H_RES 240
  44. #define EXAMPLE_LCD_V_RES 280
  45. // Bit number used to represent command and parameter
  46. #define EXAMPLE_LCD_CMD_BITS 8
  47. #define EXAMPLE_LCD_PARAM_BITS 8
  48. #define EXAMPLE_LVGL_TICK_PERIOD_MS 2
  49. // Supported alignment: 16, 32, 64. A higher alignment can enables higher burst transfer size, thus a higher i80 bus throughput.
  50. #define EXAMPLE_PSRAM_DATA_ALIGNMENT 64
  51. extern void example_lvgl_demo_ui(lv_obj_t *scr);
  52. 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)
  53. {
  54. lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
  55. lv_disp_flush_ready(disp_driver);
  56. return false;
  57. }
  58. static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
  59. {
  60. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  61. int offsetx1 = area->x1;
  62. int offsetx2 = area->x2;
  63. int offsety1 = area->y1;
  64. int offsety2 = area->y2;
  65. // copy a buffer's content to a specific area of the display
  66. esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
  67. }
  68. static void example_increase_lvgl_tick(void *arg)
  69. {
  70. /* Tell LVGL how many milliseconds has elapsed */
  71. lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
  72. }
  73. void app_main(void)
  74. {
  75. static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
  76. static lv_disp_drv_t disp_drv; // contains callback functions
  77. ESP_LOGI(TAG, "Turn off LCD backlight");
  78. gpio_config_t bk_gpio_config = {
  79. .mode = GPIO_MODE_OUTPUT,
  80. .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT
  81. };
  82. ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
  83. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL);
  84. ESP_LOGI(TAG, "Initialize Intel 8080 bus");
  85. esp_lcd_i80_bus_handle_t i80_bus = NULL;
  86. esp_lcd_i80_bus_config_t bus_config = {
  87. .clk_src = LCD_CLK_SRC_PLL160M,
  88. .dc_gpio_num = EXAMPLE_PIN_NUM_DC,
  89. .wr_gpio_num = EXAMPLE_PIN_NUM_PCLK,
  90. .data_gpio_nums = {
  91. EXAMPLE_PIN_NUM_DATA0,
  92. EXAMPLE_PIN_NUM_DATA1,
  93. EXAMPLE_PIN_NUM_DATA2,
  94. EXAMPLE_PIN_NUM_DATA3,
  95. EXAMPLE_PIN_NUM_DATA4,
  96. EXAMPLE_PIN_NUM_DATA5,
  97. EXAMPLE_PIN_NUM_DATA6,
  98. EXAMPLE_PIN_NUM_DATA7,
  99. },
  100. .bus_width = 8,
  101. .max_transfer_bytes = EXAMPLE_LCD_H_RES * 100 * sizeof(uint16_t),
  102. .psram_trans_align = EXAMPLE_PSRAM_DATA_ALIGNMENT,
  103. .sram_trans_align = 4,
  104. };
  105. ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
  106. esp_lcd_panel_io_handle_t io_handle = NULL;
  107. esp_lcd_panel_io_i80_config_t io_config = {
  108. .cs_gpio_num = EXAMPLE_PIN_NUM_CS,
  109. .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  110. .trans_queue_depth = 10,
  111. .dc_levels = {
  112. .dc_idle_level = 0,
  113. .dc_cmd_level = 0,
  114. .dc_dummy_level = 0,
  115. .dc_data_level = 1,
  116. },
  117. .on_color_trans_done = example_notify_lvgl_flush_ready,
  118. .user_ctx = &disp_drv,
  119. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
  120. .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
  121. };
  122. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &io_handle));
  123. ESP_LOGI(TAG, "Install LCD driver of st7789");
  124. esp_lcd_panel_handle_t panel_handle = NULL;
  125. esp_lcd_panel_dev_config_t panel_config = {
  126. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  127. .color_space = ESP_LCD_COLOR_SPACE_RGB,
  128. .bits_per_pixel = 16,
  129. };
  130. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  131. esp_lcd_panel_reset(panel_handle);
  132. esp_lcd_panel_init(panel_handle);
  133. esp_lcd_panel_invert_color(panel_handle, true);
  134. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  135. esp_lcd_panel_set_gap(panel_handle, 0, 20);
  136. ESP_LOGI(TAG, "Turn on LCD backlight");
  137. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
  138. ESP_LOGI(TAG, "Initialize LVGL library");
  139. lv_init();
  140. // alloc draw buffers used by LVGL
  141. // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
  142. lv_color_t *buf1 = NULL;
  143. lv_color_t *buf2 = NULL;
  144. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  145. buf1 = heap_caps_aligned_alloc(EXAMPLE_PSRAM_DATA_ALIGNMENT, EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  146. #else
  147. buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  148. #endif
  149. assert(buf1);
  150. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  151. buf2 = heap_caps_aligned_alloc(EXAMPLE_PSRAM_DATA_ALIGNMENT, EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  152. #else
  153. buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  154. #endif
  155. assert(buf2);
  156. ESP_LOGI(TAG, "buf1@%p, buf2@%p", buf1, buf2);
  157. // initialize LVGL draw buffers
  158. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 100);
  159. ESP_LOGI(TAG, "Register display driver to LVGL");
  160. lv_disp_drv_init(&disp_drv);
  161. disp_drv.hor_res = EXAMPLE_LCD_H_RES;
  162. disp_drv.ver_res = EXAMPLE_LCD_V_RES;
  163. disp_drv.flush_cb = example_lvgl_flush_cb;
  164. disp_drv.draw_buf = &disp_buf;
  165. disp_drv.user_data = panel_handle;
  166. lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
  167. ESP_LOGI(TAG, "Install LVGL tick timer");
  168. // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  169. const esp_timer_create_args_t lvgl_tick_timer_args = {
  170. .callback = &example_increase_lvgl_tick,
  171. .name = "lvgl_tick"
  172. };
  173. esp_timer_handle_t lvgl_tick_timer = NULL;
  174. ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  175. ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
  176. ESP_LOGI(TAG, "Display LVGL animation");
  177. lv_obj_t *scr = lv_disp_get_scr_act(disp);
  178. example_lvgl_demo_ui(scr);
  179. while (1) {
  180. // raise the task priority of LVGL and/or reduce the handler period can improve the performance
  181. vTaskDelay(pdMS_TO_TICKS(10));
  182. // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
  183. lv_timer_handler();
  184. }
  185. }