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-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 
14 //#define DRV_DEBUG
15 #define LOG_TAG             "LVGL.port.disp"
16 #include <drv_log.h>
17 
18 /*A static or global variable to store the buffers*/
19 static lv_disp_draw_buf_t disp_buf;
20 
21 static rt_device_t lcd_device = RT_NULL;
22 static struct rt_device_graphic_info info;
23 
24 static lv_disp_drv_t disp_drv;  /*Descriptor of a display driver*/
25 
26 #define DISP_BUF_SIZE        (LV_HOR_RES_MAX * LV_VER_RES_MAX / 2)
27 
28 static lv_disp_drv_t g_disp_drv;
29 extern LTDC_HandleTypeDef  hltdc;
30 volatile rt_bool_t g_gpu_state = RT_FALSE;
31 
lvgl_dma_config(void)32 static void lvgl_dma_config(void)
33 {
34     HAL_NVIC_SetPriority(DMA2D_IRQn, 2, 0);
35     HAL_NVIC_EnableIRQ(DMA2D_IRQn);
36     __HAL_RCC_DMA2D_CLK_ENABLE();
37 }
38 
lcd_fb_flush(lv_disp_drv_t * disp_drv,const lv_area_t * area,lv_color_t * color_p)39 static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
40 {
41     uint32_t OffLineSrc = LV_HOR_RES_MAX - (area->x2 - area->x1 + 1);
42     uint32_t addr = (uint32_t) hltdc.LayerCfg[0].FBStartAdress + 2 * (LV_HOR_RES_MAX * area->y1 + area->x1);
43 
44     DMA2D->CR = 0x00000000UL | (1 << 9);
45     DMA2D->FGMAR = (uint32_t) (uint16_t*) (color_p);
46     DMA2D->OMAR = (uint32_t) addr;
47 
48     DMA2D->FGOR = 0;
49     DMA2D->OOR = OffLineSrc;
50 
51     DMA2D->FGPFCCR = DMA2D_OUTPUT_RGB565;
52     DMA2D->OPFCCR = DMA2D_OUTPUT_RGB565;
53 
54     DMA2D->NLR = (area->y2 - area->y1 + 1) | ((area->x2 - area->x1 + 1) << 16);
55     DMA2D->CR |= DMA2D_IT_TC | DMA2D_IT_TE | DMA2D_IT_CE;
56     DMA2D->CR |= DMA2D_CR_START;
57 
58     g_gpu_state = RT_TRUE;
59 }
60 
DMA2D_IRQHandler(void)61 void DMA2D_IRQHandler(void)
62 {
63     rt_interrupt_enter();
64 
65     if ((DMA2D->ISR & DMA2D_FLAG_TC) != 0U)
66     {
67         if ((DMA2D->CR & DMA2D_IT_TC) != 0U)
68         {
69 
70             DMA2D->CR &= ~DMA2D_IT_TC;
71             DMA2D->IFCR = DMA2D_FLAG_TC;
72 
73             if (g_gpu_state == RT_TRUE)
74             {
75                 g_gpu_state = RT_FALSE;
76                 lv_disp_flush_ready(&g_disp_drv);
77             }
78         }
79     }
80 
81     rt_interrupt_leave();
82 }
83 
lv_port_disp_init(void)84 void lv_port_disp_init(void)
85 {
86     rt_err_t result;
87 
88     void *lv_disp_buf1 = RT_NULL;
89     void *lv_disp_buf2 = RT_NULL;
90 
91     lv_disp_buf1 = rt_malloc(DISP_BUF_SIZE * sizeof(lv_color_t));
92     rt_memset(lv_disp_buf1, 0, DISP_BUF_SIZE * sizeof(lv_color_t));
93     RT_ASSERT(lv_disp_buf1 != RT_NULL);
94 
95     lv_disp_buf2 = rt_malloc(DISP_BUF_SIZE * sizeof(lv_color_t));
96     rt_memset(lv_disp_buf2, 0, DISP_BUF_SIZE * sizeof(lv_color_t));
97     RT_ASSERT(lv_disp_buf2 != RT_NULL);
98 
99     lcd_device = rt_device_find("lcd");
100 
101     if (lcd_device == 0)
102     {
103         LOG_E("lcd_device error!");
104         return;
105     }
106 
107     result = rt_device_open(lcd_device, 0);
108 
109     if (result != RT_EOK)
110     {
111         LOG_E("error!");
112         return;
113     }
114 
115     /* get framebuffer address */
116     result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
117 
118     if (result != RT_EOK)
119     {
120         LOG_E("error!");
121         /* get device information failed */
122         return;
123     }
124 
125     RT_ASSERT (info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
126                info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
127 
128     lvgl_dma_config();
129 
130     /*Initialize `disp_buf` with the buffer(s).*/
131     lv_disp_draw_buf_init(&disp_buf, lv_disp_buf1, lv_disp_buf2, DISP_BUF_SIZE);
132 
133     lv_disp_drv_init(&disp_drv); /*Basic initialization*/
134 
135     /*Set the resolution of the display*/
136     disp_drv.hor_res = info.width;
137     disp_drv.ver_res = info.height;
138 
139     /*Set a display buffer*/
140     disp_drv.draw_buf = &disp_buf;
141 
142     /*Used to copy the buffer's content to the display*/
143     disp_drv.flush_cb = lcd_fb_flush;
144 
145     /*Finally register the driver*/
146     lv_disp_drv_register(&disp_drv);
147 
148     g_disp_drv = disp_drv;
149 }
150