lv_port_disp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-18 Meco Man The first version
  9. * 2021-12-24 Rb Refresh using dma2d
  10. */
  11. #include <lvgl.h>
  12. #include <lcd_port.h>
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "LVGL.port.disp"
  15. #include <drv_log.h>
  16. /*A static or global variable to store the buffers*/
  17. static lv_disp_draw_buf_t disp_buf;
  18. static rt_device_t lcd_device = RT_NULL;
  19. static struct rt_device_graphic_info info;
  20. static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  21. #define DISP_BUF_SIZE (LV_HOR_RES_MAX * LV_VER_RES_MAX / 4)
  22. static lv_disp_drv_t g_disp_drv;
  23. extern LTDC_HandleTypeDef hltdc;
  24. volatile rt_bool_t g_gpu_state = RT_FALSE;
  25. static void lvgl_dma_config(void)
  26. {
  27. HAL_NVIC_SetPriority(DMA2D_IRQn, 0, 0);
  28. HAL_NVIC_EnableIRQ(DMA2D_IRQn);
  29. __HAL_RCC_DMA2D_CLK_ENABLE();
  30. }
  31. static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  32. {
  33. uint32_t OffLineSrc = LV_HOR_RES_MAX - (area->x2 - area->x1 + 1);
  34. uint32_t addr = (uint32_t) hltdc.LayerCfg[0].FBStartAdress + 2 * (LV_HOR_RES_MAX * area->y1 + area->x1);
  35. DMA2D->CR = 0x00000000UL | (1 << 9);
  36. DMA2D->FGMAR = (uint32_t) (uint16_t*) (color_p);
  37. DMA2D->OMAR = (uint32_t) addr;
  38. DMA2D->FGOR = 0;
  39. DMA2D->OOR = OffLineSrc;
  40. DMA2D->FGPFCCR = DMA2D_OUTPUT_RGB565;
  41. DMA2D->OPFCCR = DMA2D_OUTPUT_RGB565;
  42. DMA2D->NLR = (area->y2 - area->y1 + 1) | ((area->x2 - area->x1 + 1) << 16);
  43. DMA2D->CR |= DMA2D_IT_TC | DMA2D_IT_TE | DMA2D_IT_CE;
  44. DMA2D->CR |= DMA2D_CR_START;
  45. g_gpu_state = RT_TRUE;
  46. }
  47. void DMA2D_IRQHandler(void)
  48. {
  49. rt_interrupt_enter();
  50. if ((DMA2D->ISR & DMA2D_FLAG_TC) != 0U)
  51. {
  52. if ((DMA2D->CR & DMA2D_IT_TC) != 0U)
  53. {
  54. DMA2D->CR &= ~DMA2D_IT_TC;
  55. DMA2D->IFCR = DMA2D_FLAG_TC;
  56. if (g_gpu_state == RT_TRUE)
  57. {
  58. g_gpu_state = RT_FALSE;
  59. lv_disp_flush_ready(&g_disp_drv);
  60. }
  61. }
  62. }
  63. rt_interrupt_leave();
  64. }
  65. void lv_port_disp_init(void)
  66. {
  67. rt_err_t result;
  68. static lv_color_t lv_disp_buf1[DISP_BUF_SIZE] = {0};
  69. lcd_device = rt_device_find("lcd");
  70. if (lcd_device == 0)
  71. {
  72. LOG_E("lcd_device error!");
  73. return;
  74. }
  75. result = rt_device_open(lcd_device, 0);
  76. if (result != RT_EOK)
  77. {
  78. LOG_E("error!");
  79. return;
  80. }
  81. /* get framebuffer address */
  82. result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
  83. if (result != RT_EOK)
  84. {
  85. LOG_E("error!");
  86. /* get device information failed */
  87. return;
  88. }
  89. RT_ASSERT (info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
  90. info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
  91. lvgl_dma_config();
  92. /*Initialize `disp_buf` with the buffer(s).*/
  93. lv_disp_draw_buf_init(&disp_buf, lv_disp_buf1, RT_NULL, DISP_BUF_SIZE);
  94. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  95. /*Set the resolution of the display*/
  96. disp_drv.hor_res = info.width;
  97. disp_drv.ver_res = info.height;
  98. /*Set a display buffer*/
  99. disp_drv.draw_buf = &disp_buf;
  100. /*Used to copy the buffer's content to the display*/
  101. disp_drv.flush_cb = lcd_fb_flush;
  102. /*Finally register the driver*/
  103. lv_disp_drv_register(&disp_drv);
  104. g_disp_drv = disp_drv;
  105. }