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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-02-25 Wayne the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <nu_bitutil.h>
#define DBG_TAG "ili_tpc"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include "ili.h"
#define ILI_MAX_TOUCH 5
#define ILI_ADDRESS 0x41
#define BL_V1_8 0x108
#define BL_V1_7 0x107
#define BL_V1_6 0x106
#define ILITEK_TP_CMD_GET_TP_RES 0x20
#define ILITEK_TP_CMD_GET_SCRN_RES 0x21
#define ILITEK_TP_CMD_SET_IC_SLEEP 0x30
#define ILITEK_TP_CMD_SET_IC_WAKE 0x31
#define ILITEK_TP_CMD_GET_FW_VER 0x40
#define ILITEK_TP_CMD_GET_PRL_VER 0x42
#define ILITEK_TP_CMD_GET_MCU_VER 0x61
#define ILITEK_TP_CMD_GET_IC_MODE 0xC0
#define ILITEK_TP_CMD_RESET 0x60
#define REPORT_COUNT_ADDRESS 61
struct ili_protocol_info
{
rt_uint16_t ver;
rt_uint8_t ver_major;
};
struct ili_ts_data
{
struct rt_touch_device touch_device;
struct rt_i2c_client *client;
rt_base_t reset_pin;
rt_base_t irq_pin;
struct ili_protocol_info ptl;
rt_uint8_t product_id[30];
rt_uint16_t mcu_ver;
rt_uint8_t ic_mode;
rt_uint8_t firmware_ver[8];
rt_int32_t screen_max_x;
rt_int32_t screen_max_y;
rt_int32_t screen_min_x;
rt_int32_t screen_min_y;
rt_int32_t max_tp;
};
typedef struct ili_ts_data *ili_ts_data_t;
// Private data
static struct ili_ts_data g_iliTsData;
static rt_err_t ili_i2c_write_and_read(struct rt_i2c_client *psI2cClient,
rt_uint8_t *cmd, int write_len, int delay,
rt_uint8_t *data, int read_len)
{
struct rt_i2c_msg msgs[] =
{
{
.addr = psI2cClient->client_addr,
.flags = RT_I2C_WR,
.len = write_len,
.buf = cmd,
},
{
.addr = psI2cClient->client_addr,
.flags = RT_I2C_RD,
.len = read_len,
.buf = data,
},
};
if (delay == 0 && write_len > 0 && read_len > 0)
{
if (rt_i2c_transfer(psI2cClient->bus, msgs, 2) != 2)
goto exit_ili_i2c_write_and_read;
}
else
{
if (write_len > 0)
{
if (rt_i2c_transfer(psI2cClient->bus, msgs, 1) != 1)
goto exit_ili_i2c_write_and_read;
}
if (delay > 0)
rt_thread_mdelay(delay);
if (read_len > 0)
{
if (rt_i2c_transfer(psI2cClient->bus, msgs + 1, 1) != 1)
goto exit_ili_i2c_write_and_read;
}
}
return RT_EOK;
exit_ili_i2c_write_and_read:
return -RT_ERROR;
}
static int ili_get_ptl_ver(ili_ts_data_t psIliTs,
rt_uint16_t cmd, rt_uint8_t *inbuf, rt_uint8_t *outbuf)
{
rt_int32_t error;
rt_uint8_t buf[64];
buf[0] = cmd;
error = ili_i2c_write_and_read(psIliTs->client, buf, 1, 5, outbuf, 3);
if (error)
return error;
psIliTs->ptl.ver = nu_get16_be(outbuf);
psIliTs->ptl.ver_major = outbuf[0];
return 0;
}
static int ili_get_mcu_ver(ili_ts_data_t psIliTs,
rt_uint16_t cmd, rt_uint8_t *inbuf, rt_uint8_t *outbuf)
{
rt_int32_t error;
rt_uint8_t buf[64];
buf[0] = cmd;
error = ili_i2c_write_and_read(psIliTs->client, buf, 1, 5, outbuf, 32);
if (error)
return error;
psIliTs->mcu_ver = nu_get16_le(outbuf);
rt_memset(psIliTs->product_id, 0, sizeof(psIliTs->product_id));
rt_memcpy(psIliTs->product_id, outbuf + 6, 26);
return 0;
}
static int ili_get_fw_ver(ili_ts_data_t psIliTs,
rt_uint16_t cmd, rt_uint8_t *inbuf, rt_uint8_t *outbuf)
{
rt_int32_t error;
rt_uint8_t buf[64];
buf[0] = cmd;
error = ili_i2c_write_and_read(psIliTs->client, buf, 1, 5, outbuf, 8);
if (error)
return error;
rt_memcpy(psIliTs->firmware_ver, outbuf, 8);
return 0;
}
static int ili_get_scrn_res(ili_ts_data_t psIliTs,
rt_uint16_t cmd, rt_uint8_t *inbuf, rt_uint8_t *outbuf)
{
rt_int32_t error;
rt_uint8_t buf[64];
buf[0] = cmd;
error = ili_i2c_write_and_read(psIliTs->client, buf, 1, 5, outbuf, 8);
if (error)
return error;
psIliTs->screen_min_x = nu_get16_le(outbuf);
psIliTs->screen_min_y = nu_get16_le(outbuf + 2);
psIliTs->screen_max_x = nu_get16_le(outbuf + 4);
psIliTs->screen_max_y = nu_get16_le(outbuf + 6);
return 0;
}
static int ili_get_tp_res(ili_ts_data_t psIliTs,
rt_uint16_t cmd, rt_uint8_t *inbuf, rt_uint8_t *outbuf)
{
rt_int32_t error;
rt_uint8_t buf[64];
buf[0] = cmd;
error = ili_i2c_write_and_read(psIliTs->client, buf, 1, 5, outbuf, 15);
if (error)
return error;
psIliTs->max_tp = outbuf[8];
if (psIliTs->max_tp > ILI_MAX_TOUCH)
{
return -RT_EINVAL;
}
return 0;
}
static int ili_get_ic_mode(ili_ts_data_t psIliTs,
rt_uint16_t cmd, rt_uint8_t *inbuf, rt_uint8_t *outbuf)
{
rt_int32_t error;
rt_uint8_t buf[64];
buf[0] = cmd;
error = ili_i2c_write_and_read(psIliTs->client, buf, 1, 5, outbuf, 2);
if (error)
return error;
psIliTs->ic_mode = outbuf[0];
return 0;
}
static int ili_send_soft_reset(ili_ts_data_t psIliTs,
rt_uint16_t cmd, rt_uint8_t *inbuf, rt_uint8_t *outbuf)
{
rt_int32_t error;
rt_uint8_t buf[64];
buf[0] = cmd;
error = ili_i2c_write_and_read(psIliTs->client, buf, 1, 0, RT_NULL, 0);
if (error)
return error;
return 0;
}
static void ili_tpc_reset(ili_ts_data_t psIliTs, int delay_ms)
{
if (0)
{
//FIXME
/* hw pin init*/
rt_pin_mode(psIliTs->reset_pin, PIN_MODE_OUTPUT);
/* Reset */
rt_pin_write(psIliTs->reset_pin, PIN_HIGH);
rt_thread_mdelay(10);
rt_pin_write(psIliTs->reset_pin, PIN_LOW);
}
else
{
rt_err_t error;
error = ili_send_soft_reset(psIliTs, ILITEK_TP_CMD_RESET, RT_NULL, RT_NULL);
if (error)
return;
}
rt_thread_mdelay(delay_ms);
}
static rt_err_t ili_get_info(ili_ts_data_t psIliTs, struct rt_touch_info *info)
{
rt_uint8_t outbuf[256];
rt_err_t error;
error = ili_get_ptl_ver(psIliTs, ILITEK_TP_CMD_GET_PRL_VER, RT_NULL, outbuf);
if (error)
goto exit_ili_get_info;
error = ili_get_mcu_ver(psIliTs, ILITEK_TP_CMD_GET_MCU_VER, RT_NULL, outbuf);
if (error)
goto exit_ili_get_info;
error = ili_get_fw_ver(psIliTs, ILITEK_TP_CMD_GET_FW_VER, NULL, outbuf);
if (error)
goto exit_ili_get_info;
error = ili_get_scrn_res(psIliTs, ILITEK_TP_CMD_GET_SCRN_RES, NULL, outbuf);
if (error)
goto exit_ili_get_info;
error = ili_get_tp_res(psIliTs, ILITEK_TP_CMD_GET_TP_RES, NULL, outbuf);
if (error)
goto exit_ili_get_info;
error = ili_get_ic_mode(psIliTs, ILITEK_TP_CMD_GET_IC_MODE, NULL, outbuf);
if (error)
goto exit_ili_get_info;
LOG_I("touch device probed");
return RT_EOK;
exit_ili_get_info:
return -RT_ERROR;
}
static void ili_info_dump(ili_ts_data_t psIliTs)
{
rt_kprintf("reset_pin: %d\n", psIliTs->reset_pin);
rt_kprintf("irq_pin: %d\n", psIliTs->irq_pin);
rt_kprintf("ptl.ver: %x \n", psIliTs->ptl.ver_major);
rt_kprintf("mcu_ver: %x\n", psIliTs->mcu_ver);
rt_kprintf("firmware_ver:[%02X%02X.%02X%02X.%02X%02X.%02X%02X]\n",
psIliTs->firmware_ver[0],
psIliTs->firmware_ver[1],
psIliTs->firmware_ver[2],
psIliTs->firmware_ver[3],
psIliTs->firmware_ver[4],
psIliTs->firmware_ver[5],
psIliTs->firmware_ver[6],
psIliTs->firmware_ver[7]);
rt_kprintf("product_id: %s\n", psIliTs->product_id);
rt_kprintf("screen_max_x: %d\n", psIliTs->screen_max_x);
rt_kprintf("screen_max_y: %d\n", psIliTs->screen_max_y);
rt_kprintf("screen_min_x: %d\n", psIliTs->screen_min_x);
rt_kprintf("screen_min_y: %d\n", psIliTs->screen_min_y);
rt_kprintf("max_tp: %d\n", psIliTs->max_tp);
rt_kprintf("ic_mode: %d\n", psIliTs->ic_mode);
}
static rt_int16_t pre_x[ILI_MAX_TOUCH];
static rt_int16_t pre_y[ILI_MAX_TOUCH];
static rt_int16_t pre_w[ILI_MAX_TOUCH];
static rt_uint8_t s_tp_dowm[ILI_MAX_TOUCH];
static void ili_touch_up(void *buf, int8_t id)
{
struct rt_touch_data *read_data = (struct rt_touch_data *)buf;
if (s_tp_dowm[id] == 1)
{
s_tp_dowm[id] = 0;
read_data[id].event = RT_TOUCH_EVENT_UP;
}
else
{
read_data[id].event = RT_TOUCH_EVENT_NONE;
}
read_data[id].timestamp = rt_touch_get_ts();
read_data[id].width = pre_w[id];
read_data[id].x_coordinate = pre_x[id];
read_data[id].y_coordinate = pre_y[id];
read_data[id].track_id = id;
pre_x[id] = -1; /* last point is none */
pre_y[id] = -1;
pre_w[id] = -1;
}
static void ili_touch_down(void *buf, int8_t id, int16_t x, int16_t y, int16_t w)
{
struct rt_touch_data *read_data = (struct rt_touch_data *)buf;
if (s_tp_dowm[id] == 1)
{
read_data[id].event = RT_TOUCH_EVENT_MOVE;
}
else
{
read_data[id].event = RT_TOUCH_EVENT_DOWN;
s_tp_dowm[id] = 1;
}
read_data[id].timestamp = rt_touch_get_ts();
read_data[id].width = w;
read_data[id].x_coordinate = x;
read_data[id].y_coordinate = y;
read_data[id].track_id = id;
pre_x[id] = x; /* save last point */
pre_y[id] = y;
pre_w[id] = w;
}
static int8_t pre_id[ILI_MAX_TOUCH];
static rt_uint8_t pre_touch = 0;
static rt_ssize_t ili_read_point(struct rt_touch_device *touch, void *buf, rt_size_t read_num)
{
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
struct ili_ts_data *ts = (ili_ts_data_t)touch;
rt_uint8_t tmpbuf[256] = {0};
rt_err_t error = 0;
rt_int32_t packet_len = 5;
rt_int32_t packet_max_point = 10;
rt_int8_t touch_num;
rt_int32_t i, count;
rt_uint16_t x, y;
rt_int32_t tip, point_id;
RT_ASSERT(touch);
RT_ASSERT(buf);
RT_ASSERT(read_num != 0);
RT_ASSERT(read_num <= ILI_MAX_TOUCH);
error = ili_i2c_write_and_read(ts->client, NULL, 0, 0, tmpbuf, 64);
if (error)
{
LOG_E("get touch info failed, err:%d\n", error);
goto exit_ili_read_point;
}
touch_num = tmpbuf[REPORT_COUNT_ADDRESS];
if (touch_num > ts->max_tp)
{
LOG_E("FW report max point:%d > panel info. max:%d\n", touch_num, ts->max_tp);
goto exit_ili_read_point;
}
count = DIV_ROUND_UP(touch_num, packet_max_point);
for (i = 1; i < count; i++)
{
error = ili_i2c_write_and_read(ts->client, NULL, 0, 0, tmpbuf + i * 64, 64);
if (error)
{
LOG_E("get touch info. failed, cnt:%d, err:%d\n", count, error);
goto exit_ili_read_point;
}
}
if (pre_touch > touch_num) /* point up */
{
for (i = 0; i < ILI_MAX_TOUCH; i++)
{
rt_uint8_t j;
for (j = 0; j < touch_num; j++) /* this time touch num */
{
point_id = tmpbuf[j * packet_len + 1] & 0x3F;
if (pre_id[i] == point_id) /* this id is not free */
break;
}
if ((j == touch_num) && (pre_id[i] != -1))
{
ili_touch_up(buf, pre_id[i]);
pre_id[i] = -1;
}
}
}
if (touch_num > 0)
{
uint32_t range_x = touch->info.range_x;
uint32_t range_y = touch->info.range_y;
for (i = 0; i < touch_num; i++)
{
tip = tmpbuf[i * packet_len + 1] & 0x40;
point_id = tmpbuf[i * packet_len + 1] & 0x3F;
pre_id[i] = point_id;
if (!tip)
{
// Up
ili_touch_up(buf, point_id);
continue;
}
x = nu_get16_le(tmpbuf + i * packet_len + 2);
y = nu_get16_le(tmpbuf + i * packet_len + 4);
if (x > ts->screen_max_x || x < ts->screen_min_x ||
y > ts->screen_max_y || y < ts->screen_min_y)
{
LOG_E("invalid position, X[%d,%u,%d], Y[%d,%u,%d]\n",
ts->screen_min_x, x, ts->screen_max_x,
ts->screen_min_y, y, ts->screen_max_y);
continue;
}
x = (uint16_t)(range_x * x / ts->screen_max_x);
y = (uint16_t)(range_y * y / ts->screen_max_y);
ili_touch_down(buf, point_id, x, y, 255);
}
}
pre_touch = touch_num;
return read_num;
exit_ili_read_point:
pre_touch = 0;
return 0;
}
static rt_err_t ili_control(struct rt_touch_device *touch, int cmd, void *arg)
{
switch (cmd)
{
case RT_TOUCH_CTRL_GET_ID:
{
RT_ASSERT(arg);
rt_uint8_t *pu8ID = arg;
rt_memcpy((void *)pu8ID, (void *)&g_iliTsData.product_id[0], 8);
}
break;
case RT_TOUCH_CTRL_GET_INFO:
{
RT_ASSERT(arg);
touch->info.type = RT_TOUCH_TYPE_CAPACITANCE;
touch->info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
touch->info.point_num = g_iliTsData.max_tp;
rt_memcpy(arg, &touch->info, sizeof(struct rt_touch_info));
}
break;
case RT_TOUCH_CTRL_SET_X_RANGE:
{
RT_ASSERT(arg);
uint16_t range_x = *(rt_uint16_t *)arg;
if (range_x > g_iliTsData.screen_max_x || range_x < g_iliTsData.screen_min_x)
{
LOG_E("Set x range failed. %d", range_x);
}
else
{
touch->info.range_x = range_x;
}
}
break;
case RT_TOUCH_CTRL_SET_Y_RANGE:
{
RT_ASSERT(arg);
uint16_t range_y = *(rt_uint16_t *)arg;
if (range_y > g_iliTsData.screen_max_y || range_y < g_iliTsData.screen_min_y)
{
LOG_E("Set y range failed. %d", range_y);
}
else
{
touch->info.range_y = range_y;
}
}
break;
case RT_TOUCH_CTRL_SET_MODE:
{
rt_uint16_t trig_type;
RT_ASSERT(arg);
trig_type = *(rt_uint16_t *)arg;
switch (trig_type)
{
case RT_DEVICE_FLAG_INT_RX:
break;
case RT_DEVICE_FLAG_RDONLY:
break;
default:
break;
}
}
break;
default:
{
}
break;
}
return RT_EOK;
}
static struct rt_touch_ops ili_touch_ops =
{
.touch_readpoint = ili_read_point,
.touch_control = ili_control,
};
int rt_hw_ili_tpc_init(const char *name, struct rt_touch_config *cfg)
{
struct rt_touch_device *touch_device = RT_NULL;
rt_uint32_t bus_speed = 400000;
touch_device = (struct rt_touch_device *)&g_iliTsData.touch_device;
rt_memset((void *)touch_device, 0, sizeof(struct rt_touch_device));
g_iliTsData.client = (struct rt_i2c_client *)rt_malloc(sizeof(struct rt_i2c_client));
if (g_iliTsData.client == RT_NULL)
{
LOG_E("touch device malloc fail");
goto exit_rt_hw_ili_tpc_init;
}
rt_memset((void *)g_iliTsData.client, 0, sizeof(struct rt_i2c_client));
g_iliTsData.reset_pin = *((rt_base_t *)cfg->user_data);
g_iliTsData.irq_pin = cfg->irq_pin.pin;
g_iliTsData.client->client_addr = ILI_ADDRESS;
g_iliTsData.client->bus = (struct rt_i2c_bus_device *)rt_device_find(cfg->dev_name);
if (g_iliTsData.client->bus == RT_NULL)
{
LOG_E("Can't find %s device", cfg->dev_name);
goto exit_rt_hw_ili_tpc_init;
}
if (rt_device_open((rt_device_t)g_iliTsData.client->bus, RT_DEVICE_FLAG_RDWR) != RT_EOK)
{
LOG_E("open %s device failed", cfg->dev_name);
goto exit_rt_hw_ili_tpc_init;
}
rt_memset(&pre_x[0], 0xff, ILI_MAX_TOUCH * sizeof(rt_int16_t));
rt_memset(&pre_y[0], 0xff, ILI_MAX_TOUCH * sizeof(rt_int16_t));
rt_memset(&pre_w[0], 0xff, ILI_MAX_TOUCH * sizeof(rt_int16_t));
rt_memset(&s_tp_dowm[0], 0, ILI_MAX_TOUCH * sizeof(rt_uint8_t));
rt_memset(&pre_id[0], 0xff, ILI_MAX_TOUCH * sizeof(rt_uint8_t));
/* register touch device */
rt_memcpy(&touch_device->config, cfg, sizeof(struct rt_touch_config));
touch_device->ops = &ili_touch_ops;
if (rt_hw_touch_register(touch_device, name, RT_DEVICE_FLAG_INT_RX, RT_NULL) != RT_EOK)
{
LOG_E("register %s device failed", name);
goto exit_rt_hw_ili_tpc_init;
}
ili_tpc_reset(&g_iliTsData, 300);
if (rt_device_control((rt_device_t)g_iliTsData.client->bus, RT_I2C_DEV_CTRL_CLK, &bus_speed) != RT_EOK)
{
LOG_E("control %s device failed", cfg->dev_name);
goto exit_rt_hw_ili_tpc_init;
}
/* Probe */
if (ili_get_info(&g_iliTsData, &touch_device->info) != RT_EOK)
{
LOG_E("Get info failed");
return -RT_ERROR;
}
ili_info_dump(&g_iliTsData);
return 0;
exit_rt_hw_ili_tpc_init:
if (g_iliTsData.client)
rt_free(g_iliTsData.client);
return -RT_ERROR;
}
|