i80_controller_example_main.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #endif
  63. #define EXAMPLE_LVGL_TICK_PERIOD_MS 2
  64. // Supported alignment: 16, 32, 64. A higher alignment can enables higher burst transfer size, thus a higher i80 bus throughput.
  65. #define EXAMPLE_PSRAM_DATA_ALIGNMENT 64
  66. extern void example_lvgl_demo_ui(lv_obj_t *scr);
  67. 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)
  68. {
  69. lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
  70. lv_disp_flush_ready(disp_driver);
  71. return false;
  72. }
  73. static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
  74. {
  75. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  76. int offsetx1 = area->x1;
  77. int offsetx2 = area->x2;
  78. int offsety1 = area->y1;
  79. int offsety2 = area->y2;
  80. // copy a buffer's content to a specific area of the display
  81. esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
  82. }
  83. static void example_increase_lvgl_tick(void *arg)
  84. {
  85. /* Tell LVGL how many milliseconds has elapsed */
  86. lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
  87. }
  88. void app_main(void)
  89. {
  90. static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
  91. static lv_disp_drv_t disp_drv; // contains callback functions
  92. ESP_LOGI(TAG, "Turn off LCD backlight");
  93. gpio_config_t bk_gpio_config = {
  94. .mode = GPIO_MODE_OUTPUT,
  95. .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT
  96. };
  97. ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
  98. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL);
  99. ESP_LOGI(TAG, "Initialize Intel 8080 bus");
  100. esp_lcd_i80_bus_handle_t i80_bus = NULL;
  101. esp_lcd_i80_bus_config_t bus_config = {
  102. .clk_src = LCD_CLK_SRC_DEFAULT,
  103. .dc_gpio_num = EXAMPLE_PIN_NUM_DC,
  104. .wr_gpio_num = EXAMPLE_PIN_NUM_PCLK,
  105. .data_gpio_nums = {
  106. EXAMPLE_PIN_NUM_DATA0,
  107. EXAMPLE_PIN_NUM_DATA1,
  108. EXAMPLE_PIN_NUM_DATA2,
  109. EXAMPLE_PIN_NUM_DATA3,
  110. EXAMPLE_PIN_NUM_DATA4,
  111. EXAMPLE_PIN_NUM_DATA5,
  112. EXAMPLE_PIN_NUM_DATA6,
  113. EXAMPLE_PIN_NUM_DATA7,
  114. #if CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH > 8
  115. EXAMPLE_PIN_NUM_DATA8,
  116. EXAMPLE_PIN_NUM_DATA9,
  117. EXAMPLE_PIN_NUM_DATA10,
  118. EXAMPLE_PIN_NUM_DATA11,
  119. EXAMPLE_PIN_NUM_DATA12,
  120. EXAMPLE_PIN_NUM_DATA13,
  121. EXAMPLE_PIN_NUM_DATA14,
  122. EXAMPLE_PIN_NUM_DATA15,
  123. #endif
  124. },
  125. .bus_width = CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH,
  126. .max_transfer_bytes = EXAMPLE_LCD_H_RES * 100 * sizeof(uint16_t),
  127. .psram_trans_align = EXAMPLE_PSRAM_DATA_ALIGNMENT,
  128. .sram_trans_align = 4,
  129. };
  130. ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
  131. esp_lcd_panel_io_handle_t io_handle = NULL;
  132. esp_lcd_panel_io_i80_config_t io_config = {
  133. .cs_gpio_num = EXAMPLE_PIN_NUM_CS,
  134. .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  135. .trans_queue_depth = 10,
  136. .dc_levels = {
  137. .dc_idle_level = 0,
  138. .dc_cmd_level = 0,
  139. .dc_dummy_level = 0,
  140. .dc_data_level = 1,
  141. },
  142. .on_color_trans_done = example_notify_lvgl_flush_ready,
  143. .user_ctx = &disp_drv,
  144. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
  145. .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
  146. };
  147. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &io_handle));
  148. esp_lcd_panel_handle_t panel_handle = NULL;
  149. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  150. ESP_LOGI(TAG, "Install LCD driver of st7789");
  151. esp_lcd_panel_dev_config_t panel_config = {
  152. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  153. .color_space = ESP_LCD_COLOR_SPACE_RGB,
  154. .bits_per_pixel = 16,
  155. };
  156. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  157. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  158. ESP_LOGI(TAG, "Install LCD driver of nt35510");
  159. esp_lcd_panel_dev_config_t panel_config = {
  160. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  161. .color_space = ESP_LCD_COLOR_SPACE_BGR,
  162. .bits_per_pixel = 16,
  163. };
  164. ESP_ERROR_CHECK(esp_lcd_new_panel_nt35510(io_handle, &panel_config, &panel_handle));
  165. #endif
  166. esp_lcd_panel_reset(panel_handle);
  167. esp_lcd_panel_init(panel_handle);
  168. // Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
  169. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  170. esp_lcd_panel_invert_color(panel_handle, true);
  171. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  172. esp_lcd_panel_swap_xy(panel_handle, true);
  173. esp_lcd_panel_mirror(panel_handle, true, false);
  174. #endif
  175. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  176. esp_lcd_panel_set_gap(panel_handle, 0, 20);
  177. // user can flush pre-defined pattern to the screen before we turn on the screen or backlight
  178. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  179. ESP_LOGI(TAG, "Turn on LCD backlight");
  180. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
  181. ESP_LOGI(TAG, "Initialize LVGL library");
  182. lv_init();
  183. // alloc draw buffers used by LVGL
  184. // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
  185. lv_color_t *buf1 = NULL;
  186. lv_color_t *buf2 = NULL;
  187. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  188. buf1 = heap_caps_aligned_alloc(EXAMPLE_PSRAM_DATA_ALIGNMENT, EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  189. #else
  190. buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  191. #endif
  192. assert(buf1);
  193. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  194. buf2 = heap_caps_aligned_alloc(EXAMPLE_PSRAM_DATA_ALIGNMENT, EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  195. #else
  196. buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  197. #endif
  198. assert(buf2);
  199. ESP_LOGI(TAG, "buf1@%p, buf2@%p", buf1, buf2);
  200. // initialize LVGL draw buffers
  201. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 100);
  202. ESP_LOGI(TAG, "Register display driver to LVGL");
  203. lv_disp_drv_init(&disp_drv);
  204. disp_drv.hor_res = EXAMPLE_LCD_H_RES;
  205. disp_drv.ver_res = EXAMPLE_LCD_V_RES;
  206. disp_drv.flush_cb = example_lvgl_flush_cb;
  207. disp_drv.draw_buf = &disp_buf;
  208. disp_drv.user_data = panel_handle;
  209. lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
  210. ESP_LOGI(TAG, "Install LVGL tick timer");
  211. // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  212. const esp_timer_create_args_t lvgl_tick_timer_args = {
  213. .callback = &example_increase_lvgl_tick,
  214. .name = "lvgl_tick"
  215. };
  216. esp_timer_handle_t lvgl_tick_timer = NULL;
  217. ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  218. ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
  219. ESP_LOGI(TAG, "Display LVGL animation");
  220. lv_obj_t *scr = lv_disp_get_scr_act(disp);
  221. example_lvgl_demo_ui(scr);
  222. while (1) {
  223. // raise the task priority of LVGL and/or reduce the handler period can improve the performance
  224. vTaskDelay(pdMS_TO_TICKS(10));
  225. // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
  226. lv_timer_handler();
  227. }
  228. }