lv_port_disp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-24 Rbb666 The first version
  9. */
  10. #include <lvgl.h>
  11. #include "hal_data.h"
  12. #if DLG_LVGL_USE_GPU_RA6M3
  13. #include "lv_port_gpu.h"
  14. #endif
  15. #ifdef PKG_USING_ILI9341
  16. #include "lcd_ili9341.h"
  17. #else
  18. #include "lcd_port.h"
  19. #endif
  20. #define COLOR_BUFFER (LV_HOR_RES_MAX * LV_VER_RES_MAX / 4)
  21. /*A static or global variable to store the buffers*/
  22. static lv_disp_draw_buf_t disp_buf;
  23. /*Descriptor of a display driver*/
  24. static lv_disp_drv_t disp_drv;
  25. static struct rt_device_graphic_info info;
  26. /*Static or global buffer(s). The second buffer is optional*/
  27. // 0x1FFE0000 0x20040000
  28. __attribute__((section(".ARM.__at_0x1FFE0000"))) lv_color_t buf_1[COLOR_BUFFER];
  29. #if !DLG_LVGL_USE_GPU_RA6M3
  30. static void color_to16_maybe(lv_color16_t *dst, lv_color_t *src)
  31. {
  32. #if (LV_COLOR_DEPTH == 16)
  33. dst->full = src->full;
  34. #else
  35. dst->ch.blue = src->ch.blue;
  36. dst->ch.green = src->ch.green;
  37. dst->ch.red = src->ch.red;
  38. #endif
  39. }
  40. #endif
  41. static void disp_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  42. {
  43. #ifdef PKG_USING_ILI9341
  44. lcd_fill_array_spi(area->x1, area->y1, area->x2, area->y2, color_p);
  45. #elif DLG_LVGL_USE_GPU_RA6M3
  46. lv_port_gpu_flush();
  47. #else
  48. int x1, x2, y1, y2;
  49. x1 = area->x1;
  50. x2 = area->x2;
  51. y1 = area->y1;
  52. y2 = area->y2;
  53. /*Return if the area is out the screen*/
  54. if (x2 < 0)
  55. return;
  56. if (y2 < 0)
  57. return;
  58. if (x1 > info.width - 1)
  59. return;
  60. if (y1 > info.height - 1)
  61. return;
  62. /*Truncate the area to the screen*/
  63. int32_t act_x1 = x1 < 0 ? 0 : x1;
  64. int32_t act_y1 = y1 < 0 ? 0 : y1;
  65. int32_t act_x2 = x2 > info.width - 1 ? info.width - 1 : x2;
  66. int32_t act_y2 = y2 > info.height - 1 ? info.height - 1 : y2;
  67. uint32_t x;
  68. uint32_t y;
  69. long int location = 0;
  70. /* color_p is a buffer pointer; the buffer is provided by LVGL */
  71. lv_color16_t *fbp16 = (lv_color16_t *)info.framebuffer;
  72. for (y = act_y1; y <= act_y2; y++)
  73. {
  74. for (x = act_x1; x <= act_x2; x++)
  75. {
  76. location = (x) + (y) * info.width;
  77. color_to16_maybe(&fbp16[location], color_p);
  78. color_p++;
  79. }
  80. color_p += x2 - act_x2;
  81. }
  82. #endif
  83. lv_disp_flush_ready(disp_drv);
  84. }
  85. void lv_port_disp_init(void)
  86. {
  87. #ifdef PKG_USING_ILI9341
  88. spi_lcd_init(20);
  89. #else
  90. static rt_device_t device;
  91. /* LCD Device Init */
  92. device = rt_device_find("lcd");
  93. RT_ASSERT(device != RT_NULL);
  94. if (rt_device_open(device, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
  95. {
  96. rt_device_control(device, RTGRAPHIC_CTRL_GET_INFO, &info);
  97. }
  98. RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
  99. info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
  100. #endif
  101. /*Initialize `disp_buf` with the buffer(s). With only one buffer use NULL instead buf_2 */
  102. lv_disp_draw_buf_init(&disp_buf, buf_1, NULL, COLOR_BUFFER);
  103. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  104. /*Set the resolution of the display*/
  105. disp_drv.hor_res = LV_HOR_RES_MAX;
  106. disp_drv.ver_res = LV_VER_RES_MAX;
  107. /*Set a display buffer*/
  108. disp_drv.draw_buf = &disp_buf;
  109. /*Used to copy the buffer's content to the display*/
  110. disp_drv.flush_cb = disp_flush;
  111. #if DLG_LVGL_USE_GPU_RA6M3
  112. /* Initialize GPU module */
  113. lv_port_gpu_init();
  114. #endif /* LV_PORT_DISP_GPU_EN */
  115. /*Finally register the driver*/
  116. lv_disp_drv_register(&disp_drv);
  117. }