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 * 2020-08-07 NU-LL first version
9 */
10
11 #include <board.h>
12
13 #ifdef BSP_USING_LCD_SPI
14 #include <lcd.h>
15 #include <lcd_port.h>
16 #include <string.h>
17
18 #define DRV_DEBUG
19 #define LOG_TAG "drv.lcd"
20 #include <drv_log.h>
21
22 #define LCD_DEVICE(dev) (struct drv_lcd_device*)(dev)
23
24 struct drv_lcd_device
25 {
26 struct rt_device parent;
27
28 struct rt_device_graphic_info lcd_info;
29
30 struct rt_semaphore lcd_lock;
31
32 /* 0:front_buf is being used 1: back_buf is being used*/
33 rt_uint8_t cur_buf;
34 rt_uint8_t *front_buf;
35 rt_uint8_t *back_buf;
36 };
37
38 struct drv_lcd_device _lcd;
39
drv_lcd_init(struct rt_device * device)40 static rt_err_t drv_lcd_init(struct rt_device *device)
41 {
42 struct drv_lcd_device *lcd = LCD_DEVICE(device);
43 /* nothing, right now */
44 lcd = lcd;
45 return RT_EOK;
46 }
47
drv_lcd_control(struct rt_device * device,int cmd,void * args)48 static rt_err_t drv_lcd_control(struct rt_device *device, int cmd, void *args)
49 {
50 struct drv_lcd_device *lcd = LCD_DEVICE(device);
51
52 switch (cmd)
53 {
54 case RTGRAPHIC_CTRL_RECT_UPDATE:
55 {
56 LCD_FillRGBRect(0, 0, _lcd.lcd_info.framebuffer, _lcd.lcd_info.width, _lcd.lcd_info.height);
57 }
58 break;
59
60 case RTGRAPHIC_CTRL_GET_INFO:
61 {
62 struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args;
63
64 RT_ASSERT(info != RT_NULL);
65 info->pixel_format = lcd->lcd_info.pixel_format;
66 info->bits_per_pixel = 16;
67 info->width = lcd->lcd_info.width;
68 info->height = lcd->lcd_info.height;
69 info->framebuffer = lcd->lcd_info.framebuffer;
70 }
71 break;
72 }
73
74 return RT_EOK;
75 }
76
stm32_lcd_init(struct drv_lcd_device * lcd)77 rt_err_t stm32_lcd_init(struct drv_lcd_device *lcd)
78 {
79 rt_err_t result;
80 struct rt_spi_device *spi_device;
81 struct rt_spi_configuration cfg;
82
83
84 /* attach the device to spi bus*/
85 spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
86 RT_ASSERT(spi_device != RT_NULL);
87 result = rt_spi_bus_attach_device(spi_device, LCD_SPI_DEV_NAME, LCD_SPI_BUS_NAME, (void *)RT_NULL);
88 if (result != RT_EOK)
89 {
90 LOG_E("%s attach to %s faild, %d\n", LCD_SPI_DEV_NAME, LCD_SPI_BUS_NAME, result);
91 }
92 RT_ASSERT(result == RT_EOK);
93
94 cfg.data_width = 8;
95 cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS | RT_SPI_3WIRE;
96 cfg.max_hz = 20 * 1000 *1000; /* 20M */
97 rt_spi_configure(spi_device, &cfg);
98
99 return result;
100 }
101
102 #ifdef RT_USING_DEVICE_OPS
103 const static struct rt_device_ops lcd_ops =
104 {
105 drv_lcd_init,
106 RT_NULL,
107 RT_NULL,
108 RT_NULL,
109 RT_NULL,
110 drv_lcd_control
111 };
112 #endif
113
drv_lcd_hw_init(void)114 int drv_lcd_hw_init(void)
115 {
116 rt_err_t result = RT_EOK;
117 struct rt_device *device = &_lcd.parent;
118
119 /* memset _lcd to zero */
120 memset(&_lcd, 0x00, sizeof(_lcd));
121
122 /* init lcd_lock semaphore */
123 result = rt_sem_init(&_lcd.lcd_lock, "lcd_lock", 0, RT_IPC_FLAG_FIFO);
124 if (result != RT_EOK)
125 {
126 LOG_E("init semaphore failed!\n");
127 result = -RT_ENOMEM;
128 goto __exit;
129 }
130
131 /* config LCD dev info */
132 _lcd.lcd_info.height = LCD_HEIGHT;
133 _lcd.lcd_info.width = LCD_WIDTH;
134 _lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
135 _lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT;
136
137 /* malloc memory for Triple Buffering */
138 _lcd.lcd_info.framebuffer = rt_malloc(LCD_BUF_SIZE);
139 _lcd.back_buf = rt_malloc(LCD_BUF_SIZE);
140 _lcd.front_buf = rt_malloc(LCD_BUF_SIZE);
141 if (_lcd.lcd_info.framebuffer == RT_NULL || _lcd.back_buf == RT_NULL || _lcd.front_buf == RT_NULL)
142 {
143 LOG_E("init frame buffer failed!\n");
144 result = -RT_ENOMEM;
145 goto __exit;
146 }
147
148 /* memset buff to 0xFF */
149 memset(_lcd.lcd_info.framebuffer, 0xFF, LCD_BUF_SIZE);
150 memset(_lcd.back_buf, 0xFF, LCD_BUF_SIZE);
151 memset(_lcd.front_buf, 0xFF, LCD_BUF_SIZE);
152
153 device->type = RT_Device_Class_Graphic;
154 #ifdef RT_USING_DEVICE_OPS
155 device->ops = &lcd_ops;
156 #else
157 device->init = drv_lcd_init;
158 device->control = drv_lcd_control;
159 #endif
160
161 /* register lcd device */
162 rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
163
164 /* init stm32 LTDC */
165 if (stm32_lcd_init(&_lcd) != RT_EOK)
166 {
167 result = -RT_ERROR;
168 goto __exit;
169 }
170 else
171 {
172 LCD_SetBrightness(MAX_BRIGHTNESS);
173 }
174
175 __exit:
176 if (result != RT_EOK)
177 {
178 rt_sem_delete(&_lcd.lcd_lock);
179
180 if (_lcd.lcd_info.framebuffer)
181 {
182 rt_free(_lcd.lcd_info.framebuffer);
183 }
184
185 if (_lcd.back_buf)
186 {
187 rt_free(_lcd.back_buf);
188 }
189
190 if (_lcd.front_buf)
191 {
192 rt_free(_lcd.front_buf);
193 }
194 }
195 return result;
196 }
197 INIT_DEVICE_EXPORT(drv_lcd_hw_init);
198
199 #ifdef DRV_DEBUG
200 #ifdef FINSH_USING_MSH
lcd_test()201 int lcd_test()
202 {
203 struct drv_lcd_device *lcd;
204 lcd = (struct drv_lcd_device *)rt_device_find("lcd");
205
206 while (1)
207 {
208 /* red */
209 for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
210 {
211 lcd->lcd_info.framebuffer[2 * i] = 0x00;
212 lcd->lcd_info.framebuffer[2 * i + 1] = 0xF8;
213 }
214 lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
215 rt_thread_mdelay(1000);
216 /* green */
217 for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
218 {
219 lcd->lcd_info.framebuffer[2 * i] = 0xE0;
220 lcd->lcd_info.framebuffer[2 * i + 1] = 0x07;
221 }
222 lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
223 rt_thread_mdelay(1000);
224 /* blue */
225 for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
226 {
227 lcd->lcd_info.framebuffer[2 * i] = 0x1F;
228 lcd->lcd_info.framebuffer[2 * i + 1] = 0x00;
229 }
230 lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
231 rt_thread_mdelay(1000);
232 }
233 }
234 MSH_CMD_EXPORT(lcd_test, lcd_test);
235 #endif /* FINSH_USING_MSH */
236 #endif /* DRV_DEBUG */
237 #endif /* BSP_USING_LCD */
238