lv_port_disp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. */
  10. #include <lvgl.h>
  11. #include <lcd_port.h>
  12. //#define DRV_DEBUG
  13. #define LOG_TAG "LVGL.port.disp"
  14. #include <drv_log.h>
  15. /*A static or global variable to store the buffers*/
  16. static lv_disp_draw_buf_t disp_buf;
  17. static rt_device_t lcd_device = RT_NULL;
  18. static struct rt_device_graphic_info info;
  19. static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  20. static DMA_HandleTypeDef DmaHandle;
  21. #define DMA_STREAM DMA2_Stream0
  22. #define DMA_CHANNEL DMA_CHANNEL_0
  23. #define DMA_STREAM_IRQ DMA2_Stream0_IRQn
  24. #define DMA_STREAM_IRQHANDLER DMA2_Stream0_IRQHandler
  25. static int32_t x1_flush;
  26. static int32_t y1_flush;
  27. static int32_t x2_flush;
  28. static int32_t y2_fill;
  29. static int32_t y_fill_act;
  30. static const lv_color_t * buf_to_flush;
  31. static void DMA_TransferComplete(DMA_HandleTypeDef *han)
  32. {
  33. y_fill_act ++;
  34. if(y_fill_act > y2_fill)
  35. {
  36. lv_disp_flush_ready(&disp_drv);
  37. }
  38. else
  39. {
  40. buf_to_flush += (x2_flush - x1_flush + 1);
  41. if (HAL_DMA_Start_IT(han,(uint32_t)buf_to_flush,
  42. (uint32_t)&((uint32_t *)info.framebuffer)[y_fill_act * info.width + x1_flush],
  43. (x2_flush - x1_flush + 1)) != HAL_OK)
  44. {
  45. LOG_E("HAL_DMA_Start_IT error");
  46. RT_ASSERT(0);
  47. }
  48. }
  49. }
  50. static void DMA_TransferError(DMA_HandleTypeDef *han)
  51. {
  52. LOG_E("DMA_TransferError");
  53. RT_ASSERT(0);
  54. }
  55. void DMA_STREAM_IRQHANDLER(void)
  56. {
  57. rt_interrupt_enter();
  58. HAL_DMA_IRQHandler(&DmaHandle);
  59. rt_interrupt_leave();
  60. }
  61. static void lvgl_dma_config(void)
  62. {
  63. /*## -1- Enable DMA2 clock #################################################*/
  64. __HAL_RCC_DMA2_CLK_ENABLE();
  65. /*##-2- Select the DMA functional Parameters ###############################*/
  66. DmaHandle.Init.Channel = DMA_CHANNEL; /* DMA_CHANNEL_0 */
  67. DmaHandle.Init.Direction = DMA_MEMORY_TO_MEMORY; /* M2M transfer mode */
  68. DmaHandle.Init.PeriphInc = DMA_PINC_ENABLE; /* Peripheral increment mode Enable */
  69. DmaHandle.Init.MemInc = DMA_MINC_ENABLE; /* Memory increment mode Enable */
  70. DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; /* Peripheral data alignment : 32bit */
  71. DmaHandle.Init.MemDataAlignment = DMA_PDATAALIGN_WORD; /* memory data alignment : 32bit */
  72. DmaHandle.Init.Mode = DMA_NORMAL; /* Normal DMA mode */
  73. DmaHandle.Init.Priority = DMA_PRIORITY_HIGH; /* priority level : high */
  74. DmaHandle.Init.FIFOMode = DMA_FIFOMODE_ENABLE; /* FIFO mode enabled */
  75. DmaHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_1QUARTERFULL; /* FIFO threshold: 1/4 full */
  76. DmaHandle.Init.MemBurst = DMA_MBURST_SINGLE; /* Memory burst */
  77. DmaHandle.Init.PeriphBurst = DMA_PBURST_SINGLE; /* Peripheral burst */
  78. DmaHandle.Instance = DMA_STREAM;
  79. if (HAL_DMA_Init(&DmaHandle) != HAL_OK)
  80. {
  81. LOG_E("HAL_DMA_Init error");
  82. RT_ASSERT(0);
  83. }
  84. HAL_DMA_RegisterCallback(&DmaHandle, HAL_DMA_XFER_CPLT_CB_ID, DMA_TransferComplete);
  85. HAL_DMA_RegisterCallback(&DmaHandle, HAL_DMA_XFER_ERROR_CB_ID, DMA_TransferError);
  86. HAL_NVIC_SetPriority(DMA_STREAM_IRQ, 0, 0);
  87. HAL_NVIC_EnableIRQ(DMA_STREAM_IRQ);
  88. }
  89. static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  90. {
  91. /*Return if the area is out the screen*/
  92. if (area->x2 < 0) return;
  93. if (area->y2 < 0) return;
  94. if (area->x1 > info.width - 1) return;
  95. if (area->y1 > info.height - 1) return;
  96. /*Truncate the area to the screen*/
  97. int32_t act_x1 = area->x1 < 0 ? 0 : area->x1;
  98. int32_t act_y1 = area->y1 < 0 ? 0 : area->y1;
  99. int32_t act_x2 = area->x2 > info.width - 1 ? info.width - 1 : area->x2;
  100. int32_t act_y2 = area->y2 > info.height - 1 ? info.height - 1 : area->y2;
  101. x1_flush = act_x1;
  102. y1_flush = act_y1;
  103. x2_flush = act_x2;
  104. y2_fill = act_y2;
  105. y_fill_act = act_y1;
  106. buf_to_flush = color_p;
  107. if (HAL_DMA_Start_IT(&DmaHandle,(uint32_t)buf_to_flush,
  108. (uint32_t)&((uint32_t *)info.framebuffer)[y_fill_act * info.width + x1_flush],
  109. (x2_flush - x1_flush + 1)) != HAL_OK)
  110. {
  111. LOG_E("HAL_DMA_Start_IT error");
  112. RT_ASSERT(0);
  113. }
  114. }
  115. void lv_port_disp_init(void)
  116. {
  117. rt_err_t result;
  118. void* buf_1 = RT_NULL;
  119. void* buf_2 = RT_NULL;
  120. lcd_device = rt_device_find("lcd");
  121. if (lcd_device == 0)
  122. {
  123. LOG_E("error!");
  124. return;
  125. }
  126. result = rt_device_open(lcd_device, 0);
  127. if (result != RT_EOK)
  128. {
  129. LOG_E("error!");
  130. return;
  131. }
  132. /* get framebuffer address */
  133. result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
  134. if (result != RT_EOK)
  135. {
  136. LOG_E("error!");
  137. /* get device information failed */
  138. return;
  139. }
  140. RT_ASSERT (info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
  141. info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
  142. lvgl_dma_config();
  143. buf_1 = rt_malloc(info.width * info.height * sizeof(lv_color_t));
  144. if (buf_1 == RT_NULL)
  145. {
  146. LOG_E("malloc memory failed");
  147. return;
  148. }
  149. buf_2 = rt_malloc(info.width * info.height * sizeof(lv_color_t));
  150. if (buf_2 == RT_NULL)
  151. {
  152. LOG_E("malloc memory failed");
  153. return;
  154. }
  155. /*Initialize `disp_buf` with the buffer(s).*/
  156. lv_disp_draw_buf_init(&disp_buf, buf_1, buf_2, info.width * info.height);
  157. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  158. /*Set the resolution of the display*/
  159. disp_drv.hor_res = info.width;
  160. disp_drv.ver_res = info.height;
  161. /*Set a display buffer*/
  162. disp_drv.draw_buf = &disp_buf;
  163. /*Used to copy the buffer's content to the display*/
  164. disp_drv.flush_cb = lcd_fb_flush;
  165. /*Finally register the driver*/
  166. lv_disp_drv_register(&disp_drv);
  167. }