1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-12-17     Wayne        The first version
9  */
10 #include <lvgl.h>
11 
12 #define LOG_TAG             "lvgl.disp"
13 #define DBG_ENABLE
14 #define DBG_SECTION_NAME   LOG_TAG
15 #define DBG_LEVEL DBG_ERROR
16 #define DBG_COLOR
17 #include <rtdbg.h>
18 
19 /*A static or global variable to store the buffers*/
20 static lv_disp_draw_buf_t disp_buf;
21 static lv_disp_drv_t disp_drv;  /*Descriptor of a display driver*/
22 
23 static rt_device_t lcd_device = 0;
24 static struct rt_device_graphic_info info;
25 
lcd_fb_flush(lv_disp_drv_t * disp_drv,const lv_area_t * area,lv_color_t * color_p)26 static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
27 {
28     /* Rendering */
29     struct rt_device_rect_info rect;
30 
31     rect.x = area->x1;
32     rect.y = area->y1;
33     rect.width = area->x2 - area->x1 + 1;
34     rect.height = area->y2 - area->y1 + 1;
35 
36     rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect);
37     lv_disp_flush_ready(disp_drv);
38 }
39 
lcd_perf_monitor(struct _lv_disp_drv_t * disp_drv,uint32_t time,uint32_t px)40 void lcd_perf_monitor(struct _lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px)
41 {
42     rt_kprintf("Elapsed: %dms, Pixel: %d, Bytes:%d\n", time, px, px * sizeof(lv_color_t));
43 }
44 
lv_port_disp_init(void)45 void lv_port_disp_init(void)
46 {
47     rt_err_t result;
48     void *buf1 = RT_NULL;
49 
50     lcd_device = rt_device_find("lcd");
51     if (lcd_device == 0)
52     {
53         LOG_E("error!");
54         return;
55     }
56 
57     /* get framebuffer address */
58     result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
59     if (result != RT_EOK && info.framebuffer == RT_NULL)
60     {
61         LOG_E("error!");
62         /* get device information failed */
63         return;
64     }
65 
66     RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
67               info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
68 
69     buf1 = (void *)info.framebuffer;
70     rt_kprintf("LVGL: Use one buffers - buf1@%08x, size: %d bytes\n", buf1, info.smem_len);
71 
72     /*Initialize `disp_buf` with the buffer(s).*/
73     lv_disp_draw_buf_init(&disp_buf, buf1, RT_NULL, info.smem_len / (info.bits_per_pixel / 8));
74 
75     result = rt_device_open(lcd_device, 0);
76     if (result != RT_EOK)
77     {
78         LOG_E("error!");
79         return;
80     }
81 
82     lv_disp_drv_init(&disp_drv); /*Basic initialization*/
83 
84     /*Set the resolution of the display*/
85     disp_drv.hor_res = info.width;
86     disp_drv.ver_res = info.height;
87 
88     /*Set a display buffer*/
89     disp_drv.draw_buf = &disp_buf;
90 
91     /*Write the internal buffer (draw_buf) to the display*/
92     disp_drv.flush_cb = lcd_fb_flush;
93 
94     /* Called after every refresh cycle to tell the rendering and flushing time + the number of flushed pixels */
95     //disp_drv.monitor_cb = lcd_perf_monitor;
96 
97     /*Finally register the driver*/
98     lv_disp_drv_register(&disp_drv);
99 }
100