i80_controller_example_main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 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 "freertos/semphr.h"
  10. #include "esp_timer.h"
  11. #include "esp_lcd_panel_io.h"
  12. #include "esp_lcd_panel_vendor.h"
  13. #include "esp_lcd_panel_ops.h"
  14. #include "esp_lcd_touch.h"
  15. #include "esp_spiffs.h"
  16. #include "driver/gpio.h"
  17. #include "driver/i2c.h"
  18. #include "esp_err.h"
  19. #include "esp_log.h"
  20. #include "esp_dma_utils.h"
  21. #include "lvgl.h"
  22. #if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
  23. #include "esp_lcd_touch_gt911.h"
  24. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
  25. #include "esp_lcd_touch_tt21100.h"
  26. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
  27. #include "esp_lcd_touch_ft5x06.h"
  28. #endif
  29. static const char *TAG = "example";
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  34. // PCLK frequency can't go too high as the limitation of PSRAM bandwidth
  35. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (2 * 1000 * 1000)
  36. #else
  37. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (10 * 1000 * 1000)
  38. #endif // CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  39. #define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1
  40. #define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
  41. #define EXAMPLE_PIN_NUM_DATA0 6
  42. #define EXAMPLE_PIN_NUM_DATA1 7
  43. #define EXAMPLE_PIN_NUM_DATA2 8
  44. #define EXAMPLE_PIN_NUM_DATA3 9
  45. #define EXAMPLE_PIN_NUM_DATA4 10
  46. #define EXAMPLE_PIN_NUM_DATA5 11
  47. #define EXAMPLE_PIN_NUM_DATA6 12
  48. #define EXAMPLE_PIN_NUM_DATA7 13
  49. #if CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH > 8
  50. #define EXAMPLE_PIN_NUM_DATA8 14
  51. #define EXAMPLE_PIN_NUM_DATA9 15
  52. #define EXAMPLE_PIN_NUM_DATA10 16
  53. #define EXAMPLE_PIN_NUM_DATA11 17
  54. #define EXAMPLE_PIN_NUM_DATA12 18
  55. #define EXAMPLE_PIN_NUM_DATA13 19
  56. #define EXAMPLE_PIN_NUM_DATA14 20
  57. #define EXAMPLE_PIN_NUM_DATA15 21
  58. #endif
  59. #define EXAMPLE_PIN_NUM_PCLK 5
  60. #define EXAMPLE_PIN_NUM_CS 3
  61. #define EXAMPLE_PIN_NUM_DC 4
  62. #define EXAMPLE_PIN_NUM_RST 2
  63. #define EXAMPLE_PIN_NUM_BK_LIGHT 1
  64. // The pixel number in horizontal and vertical
  65. #define EXAMPLE_LCD_H_RES 240
  66. #define EXAMPLE_LCD_V_RES 280
  67. // Bit number used to represent command and parameter
  68. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  69. #define EXAMPLE_LCD_CMD_BITS 8
  70. #define EXAMPLE_LCD_PARAM_BITS 8
  71. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  72. #define EXAMPLE_LCD_CMD_BITS 16
  73. #define EXAMPLE_LCD_PARAM_BITS 16
  74. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
  75. #define EXAMPLE_LCD_CMD_BITS 8
  76. #define EXAMPLE_LCD_PARAM_BITS 8
  77. #endif
  78. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  79. #define EXAMPLE_I2C_NUM 0 // I2C number
  80. #define EXAMPLE_I2C_SCL 39
  81. #define EXAMPLE_I2C_SDA 40
  82. #endif
  83. #define EXAMPLE_LVGL_TICK_PERIOD_MS 2
  84. #define EXAMPLE_LVGL_TASK_MAX_DELAY_MS 500
  85. #define EXAMPLE_LVGL_TASK_MIN_DELAY_MS 1
  86. #define EXAMPLE_LVGL_TASK_STACK_SIZE (4 * 1024)
  87. #define EXAMPLE_LVGL_TASK_PRIORITY 2
  88. // Supported alignment: 16, 32, 64. A higher alignment can enables higher burst transfer size, thus a higher i80 bus throughput.
  89. #define EXAMPLE_PSRAM_DATA_ALIGNMENT 64
  90. static SemaphoreHandle_t lvgl_mux = NULL;
  91. extern void example_lvgl_demo_ui(lv_disp_t *disp);
  92. 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)
  93. {
  94. lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
  95. lv_disp_flush_ready(disp_driver);
  96. return false;
  97. }
  98. static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
  99. {
  100. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  101. int offsetx1 = area->x1;
  102. int offsetx2 = area->x2;
  103. int offsety1 = area->y1;
  104. int offsety2 = area->y2;
  105. // copy a buffer's content to a specific area of the display
  106. esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
  107. }
  108. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  109. static void example_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
  110. {
  111. uint16_t touchpad_x[1] = {0};
  112. uint16_t touchpad_y[1] = {0};
  113. uint8_t touchpad_cnt = 0;
  114. /* Read touch controller data */
  115. esp_lcd_touch_read_data(drv->user_data);
  116. /* Get coordinates */
  117. bool touchpad_pressed = esp_lcd_touch_get_coordinates(drv->user_data, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1);
  118. if (touchpad_pressed && touchpad_cnt > 0) {
  119. data->point.x = touchpad_x[0];
  120. data->point.y = touchpad_y[0];
  121. data->state = LV_INDEV_STATE_PRESSED;
  122. } else {
  123. data->state = LV_INDEV_STATE_RELEASED;
  124. }
  125. }
  126. #endif
  127. static void example_increase_lvgl_tick(void *arg)
  128. {
  129. /* Tell LVGL how many milliseconds has elapsed */
  130. lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
  131. }
  132. bool example_lvgl_lock(int timeout_ms)
  133. {
  134. // Convert timeout in milliseconds to FreeRTOS ticks
  135. // If `timeout_ms` is set to -1, the program will block until the condition is met
  136. const TickType_t timeout_ticks = (timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
  137. return xSemaphoreTakeRecursive(lvgl_mux, timeout_ticks) == pdTRUE;
  138. }
  139. void example_lvgl_unlock(void)
  140. {
  141. xSemaphoreGiveRecursive(lvgl_mux);
  142. }
  143. static void example_lvgl_port_task(void *arg)
  144. {
  145. ESP_LOGI(TAG, "Starting LVGL task");
  146. uint32_t task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS;
  147. while (1) {
  148. // Lock the mutex due to the LVGL APIs are not thread-safe
  149. if (example_lvgl_lock(-1)) {
  150. task_delay_ms = lv_timer_handler();
  151. // Release the mutex
  152. example_lvgl_unlock();
  153. }
  154. if (task_delay_ms > EXAMPLE_LVGL_TASK_MAX_DELAY_MS) {
  155. task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS;
  156. } else if (task_delay_ms < EXAMPLE_LVGL_TASK_MIN_DELAY_MS) {
  157. task_delay_ms = EXAMPLE_LVGL_TASK_MIN_DELAY_MS;
  158. }
  159. vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
  160. }
  161. }
  162. #if CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  163. void example_init_filesystem(void)
  164. {
  165. ESP_LOGI(TAG, "Initializing filesystem");
  166. esp_vfs_spiffs_conf_t conf = {
  167. .base_path = "/spiffs",
  168. .partition_label = "storage",
  169. .max_files = 5,
  170. .format_if_mount_failed = true
  171. };
  172. // Use settings defined above to initialize and mount SPIFFS filesystem.
  173. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  174. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  175. if (ret != ESP_OK) {
  176. if (ret == ESP_FAIL) {
  177. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  178. } else if (ret == ESP_ERR_NOT_FOUND) {
  179. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  180. } else {
  181. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  182. }
  183. return;
  184. }
  185. size_t total = 0, used = 0;
  186. ret = esp_spiffs_info(conf.partition_label, &total, &used);
  187. if (ret != ESP_OK) {
  188. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
  189. esp_spiffs_format(conf.partition_label);
  190. return;
  191. } else {
  192. ESP_LOGI(TAG, "Partition size: total: %zu, used: %zu", total, used);
  193. }
  194. }
  195. #endif // CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  196. void example_init_i80_bus(esp_lcd_panel_io_handle_t *io_handle, void *user_ctx)
  197. {
  198. ESP_LOGI(TAG, "Initialize Intel 8080 bus");
  199. esp_lcd_i80_bus_handle_t i80_bus = NULL;
  200. esp_lcd_i80_bus_config_t bus_config = {
  201. .clk_src = LCD_CLK_SRC_DEFAULT,
  202. .dc_gpio_num = EXAMPLE_PIN_NUM_DC,
  203. .wr_gpio_num = EXAMPLE_PIN_NUM_PCLK,
  204. .data_gpio_nums = {
  205. EXAMPLE_PIN_NUM_DATA0,
  206. EXAMPLE_PIN_NUM_DATA1,
  207. EXAMPLE_PIN_NUM_DATA2,
  208. EXAMPLE_PIN_NUM_DATA3,
  209. EXAMPLE_PIN_NUM_DATA4,
  210. EXAMPLE_PIN_NUM_DATA5,
  211. EXAMPLE_PIN_NUM_DATA6,
  212. EXAMPLE_PIN_NUM_DATA7,
  213. #if CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH > 8
  214. EXAMPLE_PIN_NUM_DATA8,
  215. EXAMPLE_PIN_NUM_DATA9,
  216. EXAMPLE_PIN_NUM_DATA10,
  217. EXAMPLE_PIN_NUM_DATA11,
  218. EXAMPLE_PIN_NUM_DATA12,
  219. EXAMPLE_PIN_NUM_DATA13,
  220. EXAMPLE_PIN_NUM_DATA14,
  221. EXAMPLE_PIN_NUM_DATA15,
  222. #endif
  223. },
  224. .bus_width = CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH,
  225. .max_transfer_bytes = EXAMPLE_LCD_H_RES * 100 * sizeof(uint16_t),
  226. .psram_trans_align = EXAMPLE_PSRAM_DATA_ALIGNMENT,
  227. .sram_trans_align = 4,
  228. };
  229. ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
  230. esp_lcd_panel_io_i80_config_t io_config = {
  231. .cs_gpio_num = EXAMPLE_PIN_NUM_CS,
  232. .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  233. .trans_queue_depth = 10,
  234. .dc_levels = {
  235. .dc_idle_level = 0,
  236. .dc_cmd_level = 0,
  237. .dc_dummy_level = 0,
  238. .dc_data_level = 1,
  239. },
  240. .flags = {
  241. .swap_color_bytes = !LV_COLOR_16_SWAP, // Swap can be done in LvGL (default) or DMA
  242. },
  243. .on_color_trans_done = example_notify_lvgl_flush_ready,
  244. .user_ctx = user_ctx,
  245. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
  246. .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
  247. };
  248. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, io_handle));
  249. }
  250. void example_init_lcd_panel(esp_lcd_panel_io_handle_t io_handle, esp_lcd_panel_handle_t *panel)
  251. {
  252. esp_lcd_panel_handle_t panel_handle = NULL;
  253. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  254. ESP_LOGI(TAG, "Install LCD driver of st7789");
  255. esp_lcd_panel_dev_config_t panel_config = {
  256. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  257. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
  258. .bits_per_pixel = 16,
  259. };
  260. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  261. esp_lcd_panel_reset(panel_handle);
  262. esp_lcd_panel_init(panel_handle);
  263. // Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
  264. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  265. esp_lcd_panel_invert_color(panel_handle, true);
  266. esp_lcd_panel_set_gap(panel_handle, 0, 20);
  267. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  268. ESP_LOGI(TAG, "Install LCD driver of nt35510");
  269. esp_lcd_panel_dev_config_t panel_config = {
  270. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  271. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
  272. .bits_per_pixel = 16,
  273. };
  274. ESP_ERROR_CHECK(esp_lcd_new_panel_nt35510(io_handle, &panel_config, &panel_handle));
  275. esp_lcd_panel_reset(panel_handle);
  276. esp_lcd_panel_init(panel_handle);
  277. // Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
  278. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  279. esp_lcd_panel_swap_xy(panel_handle, true);
  280. esp_lcd_panel_mirror(panel_handle, true, false);
  281. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
  282. // ILI9341 is NOT a distinct driver, but a special case of ST7789
  283. // (essential registers are identical). A few lines further down in this code,
  284. // it's shown how to issue additional device-specific commands.
  285. ESP_LOGI(TAG, "Install LCD driver of ili9341 (st7789 compatible)");
  286. esp_lcd_panel_dev_config_t panel_config = {
  287. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  288. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
  289. .bits_per_pixel = 16,
  290. };
  291. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  292. esp_lcd_panel_reset(panel_handle);
  293. esp_lcd_panel_init(panel_handle);
  294. // Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
  295. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  296. esp_lcd_panel_swap_xy(panel_handle, true);
  297. esp_lcd_panel_invert_color(panel_handle, false);
  298. // ILI9341 is very similar to ST7789 and shares the same driver.
  299. // Anything unconventional (such as this custom gamma table) can
  300. // be issued here in user code and need not modify the driver.
  301. esp_lcd_panel_io_tx_param(io_handle, 0xF2, (uint8_t[]) {
  302. 0
  303. }, 1); // 3Gamma function disable
  304. esp_lcd_panel_io_tx_param(io_handle, 0x26, (uint8_t[]) {
  305. 1
  306. }, 1); // Gamma curve 1 selected
  307. esp_lcd_panel_io_tx_param(io_handle, 0xE0, (uint8_t[]) { // Set positive gamma
  308. 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00
  309. }, 15);
  310. esp_lcd_panel_io_tx_param(io_handle, 0xE1, (uint8_t[]) { // Set negative gamma
  311. 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F
  312. }, 15);
  313. #endif
  314. *panel = panel_handle;
  315. }
  316. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  317. void example_init_lcd_touch(esp_lcd_touch_handle_t *tp_handle)
  318. {
  319. esp_lcd_touch_handle_t tp = NULL;
  320. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  321. const i2c_config_t i2c_conf = {
  322. .mode = I2C_MODE_MASTER,
  323. .sda_io_num = EXAMPLE_I2C_SDA,
  324. .scl_io_num = EXAMPLE_I2C_SCL,
  325. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  326. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  327. .master.clk_speed = 400000,
  328. };
  329. /* Initialize I2C */
  330. ESP_ERROR_CHECK(i2c_param_config(EXAMPLE_I2C_NUM, &i2c_conf));
  331. ESP_ERROR_CHECK(i2c_driver_install(EXAMPLE_I2C_NUM, i2c_conf.mode, 0, 0, 0));
  332. #if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
  333. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
  334. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
  335. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_TT21100_CONFIG();
  336. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
  337. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
  338. #endif
  339. ESP_LOGI(TAG, "Initialize touch IO (I2C)");
  340. /* Touch IO handle */
  341. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)EXAMPLE_I2C_NUM, &tp_io_config, &tp_io_handle));
  342. esp_lcd_touch_config_t tp_cfg = {
  343. .x_max = EXAMPLE_LCD_V_RES,
  344. .y_max = EXAMPLE_LCD_H_RES,
  345. .rst_gpio_num = -1,
  346. .int_gpio_num = -1,
  347. .flags = {
  348. .swap_xy = 1,
  349. .mirror_x = 1,
  350. .mirror_y = 0,
  351. },
  352. };
  353. /* Initialize touch */
  354. #if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
  355. ESP_LOGI(TAG, "Initialize touch controller GT911");
  356. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp));
  357. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
  358. ESP_LOGI(TAG, "Initialize touch controller TT21100");
  359. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_tt21100(tp_io_handle, &tp_cfg, &tp));
  360. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
  361. ESP_LOGI(TAG, "Initialize touch controller FT5X06");
  362. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp));
  363. #endif
  364. *tp_handle = tp;
  365. }
  366. #endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  367. void app_main(void)
  368. {
  369. static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
  370. static lv_disp_drv_t disp_drv; // contains callback functions
  371. #if EXAMPLE_PIN_NUM_BK_LIGHT >= 0
  372. ESP_LOGI(TAG, "Turn off LCD backlight");
  373. gpio_config_t bk_gpio_config = {
  374. .mode = GPIO_MODE_OUTPUT,
  375. .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT
  376. };
  377. ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
  378. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL);
  379. #endif // EXAMPLE_PIN_NUM_BK_LIGHT >= 0
  380. #if CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  381. example_init_filesystem();
  382. #endif // CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  383. esp_lcd_panel_io_handle_t io_handle = NULL;
  384. example_init_i80_bus(&io_handle, &disp_drv);
  385. esp_lcd_panel_handle_t panel_handle = NULL;
  386. example_init_lcd_panel(io_handle, &panel_handle);
  387. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  388. esp_lcd_touch_handle_t tp_handle = NULL;
  389. example_init_lcd_touch(&tp_handle);
  390. #endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  391. // Stub: user can flush pre-defined pattern to the screen before we turn on the screen or backlight
  392. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  393. #if EXAMPLE_PIN_NUM_BK_LIGHT >= 0
  394. ESP_LOGI(TAG, "Turn on LCD backlight");
  395. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
  396. #endif // EXAMPLE_PIN_NUM_BK_LIGHT >= 0
  397. ESP_LOGI(TAG, "Initialize LVGL library");
  398. lv_init();
  399. // alloc draw buffers used by LVGL
  400. // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
  401. lv_color_t *buf1 = NULL;
  402. lv_color_t *buf2 = NULL;
  403. uint32_t malloc_flags = 0;
  404. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  405. malloc_flags |= ESP_DMA_MALLOC_FLAG_PSRAM;
  406. #endif // CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  407. ESP_ERROR_CHECK(esp_dma_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), malloc_flags, (void *)&buf1, NULL));
  408. ESP_ERROR_CHECK(esp_dma_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), malloc_flags, (void *)&buf2, NULL));
  409. assert(buf1);
  410. assert(buf2);
  411. ESP_LOGI(TAG, "buf1@%p, buf2@%p", buf1, buf2);
  412. // initialize LVGL draw buffers
  413. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 100);
  414. ESP_LOGI(TAG, "Register display driver to LVGL");
  415. lv_disp_drv_init(&disp_drv);
  416. disp_drv.hor_res = EXAMPLE_LCD_H_RES;
  417. disp_drv.ver_res = EXAMPLE_LCD_V_RES;
  418. disp_drv.flush_cb = example_lvgl_flush_cb;
  419. disp_drv.draw_buf = &disp_buf;
  420. disp_drv.user_data = panel_handle;
  421. lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
  422. ESP_LOGI(TAG, "Install LVGL tick timer");
  423. // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  424. const esp_timer_create_args_t lvgl_tick_timer_args = {
  425. .callback = &example_increase_lvgl_tick,
  426. .name = "lvgl_tick"
  427. };
  428. esp_timer_handle_t lvgl_tick_timer = NULL;
  429. ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  430. ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
  431. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  432. static lv_indev_drv_t indev_drv; // Input device driver (Touch)
  433. lv_indev_drv_init(&indev_drv);
  434. indev_drv.type = LV_INDEV_TYPE_POINTER;
  435. indev_drv.disp = disp;
  436. indev_drv.read_cb = example_lvgl_touch_cb;
  437. indev_drv.user_data = tp_handle;
  438. lv_indev_drv_register(&indev_drv);
  439. #endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  440. lvgl_mux = xSemaphoreCreateRecursiveMutex();
  441. assert(lvgl_mux);
  442. ESP_LOGI(TAG, "Create LVGL task");
  443. xTaskCreate(example_lvgl_port_task, "LVGL", EXAMPLE_LVGL_TASK_STACK_SIZE, NULL, EXAMPLE_LVGL_TASK_PRIORITY, NULL);
  444. ESP_LOGI(TAG, "Display LVGL animation");
  445. // Lock the mutex due to the LVGL APIs are not thread-safe
  446. if (example_lvgl_lock(-1)) {
  447. example_lvgl_demo_ui(disp);
  448. // Release the mutex
  449. example_lvgl_unlock();
  450. }
  451. }