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 <drv_clcd.h>
12 
13 /*A static or global variable to store the buffers*/
14 static lv_disp_draw_buf_t disp_buf;
15 
16 static rt_device_t lcd_device = 0;
17 static struct rt_device_graphic_info info;
18 
19 static lv_disp_drv_t disp_drv;  /*Descriptor of a display driver*/
20 
21 /*Flush the content of the internal buffer the specific area on the display
22  *You can use DMA or any hardware acceleration to do this operation in the background but
23  *'lv_disp_flush_ready()' has to be called when finished.*/
lcd_fb_flush(lv_disp_drv_t * disp_drv,const lv_area_t * area,lv_color_t * color_p)24 static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
25 {
26     uint32_t x;
27     uint32_t y;
28     uint32_t location = 0;
29 
30     /* 16 bit per pixel */
31     lv_color16_t *fbp16 = (lv_color16_t *)info.framebuffer;
32 
33     for (y = area->y1; y <area->y2 + 1; y++)
34     {
35         for (x = area->x1; x <area->x2 + 1; x++)
36         {
37             location = x + y * info.width;
38             fbp16[location].full = color_p->full;
39             color_p++;
40         }
41     }
42 
43     lv_disp_flush_ready(disp_drv);
44 }
45 
lv_port_disp_init(void)46 void lv_port_disp_init(void)
47 {
48     rt_err_t result;
49     lv_color_t *fbuf1, *fbuf2;
50 
51     lcd_device = rt_device_find("lcd");
52     if (lcd_device == 0)
53     {
54         rt_kprintf("error!\n");
55         return;
56     }
57     result = rt_device_open(lcd_device, 0);
58     if (result != RT_EOK)
59     {
60         rt_kprintf("error!\n");
61         return;
62     }
63     /* get framebuffer address */
64     result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
65     if (result != RT_EOK)
66     {
67         rt_kprintf("error!\n");
68         /* get device information failed */
69         return;
70     }
71 
72     RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
73               info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
74 
75     fbuf1 = rt_malloc(info.width * info.height * sizeof(lv_color_t));
76     if (fbuf1 == RT_NULL)
77     {
78         rt_kprintf("Error: alloc disp buf fail\n");
79         return;
80     }
81 
82     fbuf2 = rt_malloc(info.width * info.height * sizeof(lv_color_t));
83     if (fbuf2 == RT_NULL)
84     {
85         rt_kprintf("Error: alloc disp buf fail\n");
86         rt_free(fbuf1);
87         return;
88     }
89 
90     /*Initialize `disp_buf` with the buffer(s). With only one buffer use NULL instead buf_2 */
91     lv_disp_draw_buf_init(&disp_buf, fbuf1, fbuf2, info.width * info.height);
92 
93     lv_disp_drv_init(&disp_drv); /*Basic initialization*/
94 
95     /*Set the resolution of the display*/
96     disp_drv.hor_res = info.width;
97     disp_drv.ver_res = info.height;
98 
99     /*Set a display buffer*/
100     disp_drv.draw_buf = &disp_buf;
101 
102     /*Used to copy the buffer's content to the display*/
103     disp_drv.flush_cb = lcd_fb_flush;
104 
105     /*Finally register the driver*/
106     lv_disp_drv_register(&disp_drv);
107 }
108