-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathili9488.c
416 lines (327 loc) · 10.6 KB
/
ili9488.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (c) 2024 embeddedboys developers
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "pico/time.h"
#define pr_fmt(fmt) "ili9488: " fmt
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
#include "pico/stdio.h"
#include "pico/stdio_uart.h"
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "ili9488.h"
/*
* ili9488 Command Table
*/
#define DRV_NAME "ili9488"
#define pr_debug printf
struct ili9488_priv;
typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;
struct ili9488_operations {
int (*init_display)(struct ili9488_priv *priv);
int (*reset)(struct ili9488_priv *priv);
int (*clear)(struct ili9488_priv *priv, u16 clear);
int (*blank)(struct ili9488_priv *priv, bool on);
int (*sleep)(struct ili9488_priv *priv, bool on);
int (*set_var)(struct ili9488_priv *priv);
int (*set_addr_win)(struct ili9488_priv *priv, int xs, int ys, int xe, int ye);
int (*set_cursor)(struct ili9488_priv *priv, int x, int y);
};
struct ili9488_display {
u32 xres;
u32 yres;
u32 bpp;
u32 rotate;
};
struct ili9488_priv {
u8 *buf;
struct {
int reset;
int cs; /* chip select */
int rs; /* register/data select */
int wr; /* write signal */
int rd; /* read signal */
int bl; /* backlight */
int db[LCD_PIN_DB_COUNT];
} gpio;
/* device specific */
const struct ili9488_operations *tftops;
struct ili9488_display *display;
} g_priv;
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#define dm_gpio_set_value(p,v) gpio_put(p, v)
#define mdelay(v) sleep_ms(v)
extern int i80_pio_init(uint8_t db_base, uint8_t db_count, uint8_t pin_wr);
extern int i80_write_buf_rs(void *buf, size_t len, bool rs);
static void fbtft_write_gpio8_wr(struct ili9488_priv *priv, void *buf, size_t len)
{
u8 data;
int i;
#ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
static u8 prev_data;
#endif
/* Start writing by pulling down /WR */
dm_gpio_set_value(priv->gpio.wr, 1);
while (len) {
data = *(u8 *)buf;
/* Start writing by pulling down /WR */
dm_gpio_set_value(priv->gpio.wr, 0);
// printf("data : 0x%x\n", data);
/* Set data */
#ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
if (data == prev_data) {
dm_gpio_set_value(priv->gpio.wr, 1); /* used as delay */
} else {
for (i = 0; i < 8; i++) {
if ((data & 1) != (prev_data & 1))
dm_gpio_set_value(priv->gpio.db[i],
data & 1);
data >>= 1;
prev_data >>= 1;
}
}
#else
for (i = 0; i < 8; i++) {
dm_gpio_set_value(&priv->gpio.db[i], data & 1);
data >>= 1;
}
#endif
/* Pullup /WR */
dm_gpio_set_value(priv->gpio.wr, 1);
#ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
prev_data = *(u8 *)buf;
#endif
buf ++;
len --;
}
}
static void fbtft_write_gpio8_wr_rs(struct ili9488_priv *priv, void *buf, size_t len, bool rs)
{
dm_gpio_set_value(priv->gpio.rs, rs);
fbtft_write_gpio8_wr(priv, buf, len);
}
/* rs=0 means writing register, rs=1 means writing data */
#if DISP_OVER_PIO
#define write_buf_rs(p, b, l, r) i80_write_buf_rs(b, l, r)
#else
#define write_buf_rs(p, b, l, r) fbtft_write_gpio8_wr_rs(p, b, l, r)
#endif
static int ili9488_write_reg(struct ili9488_priv *priv, int len, ...)
{
u8 *buf = (u8 *)priv->buf;
va_list args;
int i;
va_start(args, len);
*buf = (u8)va_arg(args, unsigned int);
write_buf_rs(priv, buf, sizeof(u8), 0);
len--;
/* if there no privams */
if (len == 0)
return 0;
for (i = 0; i < len; i++) {
*buf = (u8)va_arg(args, unsigned int);
buf++;
}
write_buf_rs(priv, priv->buf, len, 1);
va_end(args);
return 0;
}
#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
#define write_reg(priv, ...) \
ili9488_write_reg(priv, NUMARGS(__VA_ARGS__), __VA_ARGS__)
static int ili9488_reset(struct ili9488_priv *priv)
{
dm_gpio_set_value(priv->gpio.reset, 1);
mdelay(10);
dm_gpio_set_value(priv->gpio.reset, 0);
mdelay(10);
dm_gpio_set_value(priv->gpio.reset, 1);
mdelay(10);
return 0;
}
static int ili9488_set_var(struct ili9488_priv *priv)
{
pr_debug("%s\n", __func__);
return 0;
}
static int ili9488_init_display(struct ili9488_priv *priv)
{
pr_debug("%s, writing initial sequence...\n", __func__);
ili9488_reset(priv);
// dm_gpio_set_value(&priv->gpio.rd, 1);
// mdelay(150);
write_reg(priv, 0xf7, 0xa9, 0x51, 0x2c, 0x82);
write_reg(priv, 0xc0, 0x11, 0x09);
write_reg(priv, 0xc1, 0x41);
write_reg(priv, 0xc5, 0x00, 0x28, 0x80);
// write_reg(priv, 0xb1, 0xb0, 0x11); // 60Hz
write_reg(priv, 0xb1, 0xd0, 0x14); // 90Hz
write_reg(priv, 0xb4, 0x02);
write_reg(priv, 0xb6, 0x02, 0x22);
write_reg(priv, 0xb7, 0xc6);
write_reg(priv, 0xbe, 0x00, 0x04);
write_reg(priv, 0xe9, 0x00);
write_reg(priv, 0x36, 0x8 | (1 << 5) | (1 << 6));
write_reg(priv, 0x3a, 0x55);
write_reg(priv, 0xe0, 0x00, 0x07, 0x10, 0x09, 0x17, 0x0b, 0x41, 0x89, 0x4b, 0x0a, 0x0c, 0x0e, 0x18, 0x1b, 0x0f);
write_reg(priv, 0xe1, 0x00, 0x17, 0x1a, 0x04, 0x0e, 0x06, 0x2f, 0x45, 0x43, 0x02, 0x0a, 0x09, 0x32, 0x36, 0x0f);
write_reg(priv, 0x11);
mdelay(60);
write_reg(priv, 0x29);
return 0;
}
static int ili9488_set_addr_win(struct ili9488_priv *priv, int xs, int ys, int xe,
int ye)
{
/* set column adddress */
write_reg(priv, 0x2A, xs >> 8, xs, xe >> 8, xe);
/* set row address */
write_reg(priv, 0x2B, ys >> 8, ys, ye >> 8, ye);
/* write start */
write_reg(priv, 0x2C);
return 0;
}
static int ili9488_clear(struct ili9488_priv *priv, u16 clear)
{
u32 width = priv->display->xres;
u32 height = priv->display->yres;
int x, y;
pr_debug("clearing screen (%d x %d) with color 0x%x\n", width, height, clear);
priv->tftops->set_addr_win(priv, 0, 0,
priv->display->xres - 1,
priv->display->yres - 1);
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
write_buf_rs(priv, &clear, sizeof(u16), 1);
}
}
return 0;
}
static int ili9488_blank(struct ili9488_priv *priv, bool on)
{
pr_debug("%s\n", __func__);
return 0;
}
static int ili9488_sleep(struct ili9488_priv *priv, bool on)
{
pr_debug("%s\n", __func__);
return 0;
}
static const struct ili9488_operations default_ili9488_ops = {
.init_display = ili9488_init_display,
.reset = ili9488_reset,
.clear = ili9488_clear,
.blank = ili9488_blank,
.sleep = ili9488_sleep,
.set_var = ili9488_set_var,
.set_addr_win = ili9488_set_addr_win,
};
static int ili9488_gpio_init(struct ili9488_priv *priv)
{
printf("initializing gpios...\n");
#if DISP_OVER_PIO
gpio_init(priv->gpio.reset);
gpio_init(priv->gpio.rs);
gpio_set_dir(priv->gpio.reset, GPIO_OUT);
gpio_set_dir(priv->gpio.rs, GPIO_OUT);
#else
int *pp = (int *)&priv->gpio;
int len = sizeof(priv->gpio)/sizeof(priv->gpio.reset);
while(len--) {
gpio_init(*pp);
gpio_set_dir(*pp, GPIO_OUT);
pp++;
}
#endif
return 0;
}
static int ili9488_hw_init(struct ili9488_priv *priv)
{
printf("initializing hardware...\n");
#if DISP_OVER_PIO
i80_pio_init(priv->gpio.db[0], ARRAY_SIZE(priv->gpio.db), priv->gpio.wr);
#endif
ili9488_gpio_init(priv);
priv->tftops->init_display(priv);
/* clear screen to black */
// priv->tftops->clear(priv, 0x0);
return 0;
}
static struct ili9488_display default_ili9488_display = {
.xres = ILI9488_X_RES,
.yres = ILI9488_Y_RES,
.bpp = 16,
.rotate = 0,
};
/* ########### standalone ######## */
static inline void ili9488_write_cmd(uint8_t cmd)
{
write_buf_rs(&g_priv, &cmd, sizeof(cmd), 0);
}
#define write_cmd ili9488_write_cmd
static inline void ili9488_write_data(uint8_t data)
{
write_buf_rs(&g_priv, &data, sizeof(data), 1);
}
#define write_data ili9488_write_data
#include "lvgl/lvgl.h"
void ili9488_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{
write_cmd(0x2A);
write_data(area->x1 >> 8);
write_data(area->x1);
write_data(area->x2 >> 8);
write_data(area->x2);
/* set row address */
write_cmd(0x2B);
write_data(area->y1 >> 8);
write_data(area->y1);
write_data(area->y2 >> 8);
write_data(area->y2);
/* write start */
write_cmd(0x2C);
write_buf_rs(&g_priv, color_p, lv_area_get_size(area) * 2, 1);
lv_disp_flush_ready(disp_drv);
}
/* ########### standlone ######## */
#define BUF_SIZE 64
static int ili9488_probe(struct ili9488_priv *priv)
{
pr_debug("ili9488 probing ...\n");
priv->buf = (u8 *)malloc(BUF_SIZE);
priv->display = &default_ili9488_display;
priv->tftops = &default_ili9488_ops;
priv->gpio.bl = LCD_PIN_BL;
priv->gpio.reset = LCD_PIN_RST;
priv->gpio.rs = LCD_PIN_RS;
priv->gpio.wr = LCD_PIN_WR;
priv->gpio.cs = LCD_PIN_CS;
/* pin0 - pin15 for I8080 data bus */
for (int i = LCD_PIN_DB_BASE; i < ARRAY_SIZE(priv->gpio.db); i++)
priv->gpio.db[i] = i;
ili9488_hw_init(priv);
return 0;
}
int ili9488_driver_init(void)
{
ili9488_probe(&g_priv);
return 0;
}