-
Notifications
You must be signed in to change notification settings - Fork 21
/
Goodix.cpp
350 lines (269 loc) · 6.79 KB
/
Goodix.cpp
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
#include "Goodix.h"
#include "Wire.h"
#ifndef ICACHE_RAM_ATTR
#define ICACHE_RAM_ATTR
#endif
// Interrupt handling
volatile uint8_t goodixIRQ = 0;
void ICACHE_RAM_ATTR _goodix_irq_handler() {
noInterrupts();
goodixIRQ = 1;
interrupts();
}
// Implementation
Goodix::Goodix() {
}
void Goodix::setHandler(void (*handler)(int8_t, GTPoint*)) {
touchHandler = handler;
}
bool Goodix::begin(uint8_t interruptPin, uint8_t resetPin, uint8_t addr) {
intPin = interruptPin;
rstPin = resetPin;
i2cAddr = addr;
// Take chip some time to start
msSleep(300);
bool result = reset();
msSleep(200);
return result;
}
bool Goodix::reset() {
msSleep(1);
pinOut(intPin);
pinOut(rstPin);
pinHold(intPin);
pinHold(rstPin);
/* begin select I2C slave addr */
/* T2: > 10ms */
msSleep(11);
/* HIGH: 0x28/0x29 (0x14 7bit), LOW: 0xBA/0xBB (0x5D 7bit) */
pinSet(intPin, i2cAddr == GOODIX_I2C_ADDR_28);
/* T3: > 100us */
usSleep(110);
pinIn(rstPin);
//if (!pinCheck(rstPin, HIGH))
// return false;
/* T4: > 5ms */
msSleep(6);
pinHold(intPin);
/* end select I2C slave addr */
/* T5: 50ms */
msSleep(51);
pinIn(intPin); // INT pin has no pullups so simple set to floating input
attachInterrupt(intPin, _goodix_irq_handler, RISING);
// detachInterrupt(intPin, _goodix_irq_handler);
return true;
}
/**
Read goodix touchscreen version
set 4 chars + zero productID to target
*/
uint8_t Goodix::productID(char *target) {
uint8_t error;
uint8_t buf[4];
error = read(GOODIX_REG_ID, buf, 4);
if (error) {
return error;
}
memcpy(target, buf, 4);
target[4] = 0;
return 0;
}
/**
goodix_i2c_test - I2C test function to check if the device answers.
@client: the i2c client
*/
uint8_t Goodix::test() {
uint8_t testByte;
return read(GOODIX_REG_CONFIG_DATA, &testByte, 1);
}
uint8_t Goodix::calcChecksum(uint8_t* buf, uint8_t len) {
uint8_t ccsum = 0;
for (uint8_t i = 0; i < len; i++) {
ccsum += buf[i];
}
//ccsum %= 256;
ccsum = (~ccsum) + 1;
return ccsum;
}
uint8_t Goodix::readChecksum() {
uint16_t aStart = GT_REG_CFG;
uint16_t aStop = 0x80FE;
uint8_t len = aStop - aStart + 1;
uint8_t buf[len];
read(aStart, buf, len);
return calcChecksum(buf, len);
}
uint8_t Goodix::fwResolution(uint16_t maxX, uint16_t maxY) {
uint8_t len = 0x8100 - GT_REG_CFG + 1;
uint8_t cfg[len];
read(GT_REG_CFG, cfg, len);
cfg[1] = (maxX & 0xff);
cfg[2] = (maxX >> 8);
cfg[3] = (maxY & 0xff);
cfg[4] = (maxY >> 8);
cfg[len - 2] = calcChecksum(cfg, len - 2);
cfg[len - 1] = 1;
write(GT_REG_CFG, cfg, len);
}
GTConfig* Goodix::readConfig() {
read(GT_REG_CFG, (uint8_t *) &config, sizeof(config));
return &config;
}
GTInfo* Goodix::readInfo() {
read(GT_REG_DATA, (uint8_t *) &info, sizeof(config));
return &info;
}
void Goodix::armIRQ() {
attachInterrupt(intPin, _goodix_irq_handler, RISING);
}
void Goodix::onIRQ() {
//uint8_t buf[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
int8_t contacts;
contacts = readInput(points);
if (contacts < 0)
return;
if (contacts > 0) {
touchHandler(contacts, (GTPoint *)points);
/*
Serial.print("Contacts: ");
Serial.println(contacts);
for (uint8_t i = 0; i < contacts; i++) {
Serial.print("C ");
Serial.print(i);
Serial.print(": #");
Serial.print(points[i].trackId);
Serial.print(" ");
Serial.print(points[i].x);
Serial.print(", ");
Serial.print(points[i].y);
Serial.print(" s.");
Serial.print(points[i].size);
Serial.println();
}
*/
}
//Serial.println(&points[1 + GOODIX_CONTACT_SIZE * i]);
// goodix_ts_report_touch(&points[1 + GOODIX_CONTACT_SIZE * i]);
write(GOODIX_READ_COORD_ADDR, 0);
/*struct goodix_ts_data *ts = dev_id;
goodix_process_events(ts);
write(GOODIX_READ_COORD_ADDR, 0);
//if (write(GOODIX_READ_COORD_ADDR, 0) < 0)
// dev_err(&ts->client->dev, "I2C write end_cmd error\n");
return IRQ_HANDLED;
*/
}
void Goodix::loop() {
noInterrupts();
uint8_t irq = goodixIRQ;
goodixIRQ = 0;
interrupts();
if (irq) {
onIRQ();
}
}
#define EAGAIN 100 // Try again error
int16_t Goodix::readInput(uint8_t *data) {
int touch_num;
int error;
uint8_t regState[1];
error = read(GOODIX_READ_COORD_ADDR, regState, 1);
//log_printf("regState: %#06x\n", regState);
if (error) {
//dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
return -error;
}
if (!(regState[0] & 0x80))
return -EAGAIN;
touch_num = regState[0] & 0x0f;
//if (touch_num > ts->max_touch_num)
// return -EPROTO;
//log_printf("touch num: %d\n", touch_num);
if (touch_num > 0) {
/* data += 1 + GOODIX_CONTACT_SIZE;
error = read(GOODIX_READ_COORD_ADDR + 1 + GOODIX_CONTACT_SIZE, data,
GOODIX_CONTACT_SIZE * (touch_num - 1));
*/
error = read(GOODIX_READ_COORD_ADDR + 1, data, GOODIX_CONTACT_SIZE * (touch_num));
if (error)
return -error;
}
return touch_num;
}
//----- Utils -----
void Goodix::i2cStart(uint16_t reg) {
Wire.beginTransmission(i2cAddr);
Wire.write(highByte(reg));
Wire.write(lowByte(reg));
}
void Goodix::i2cRestart() {
Wire.endTransmission(false);
Wire.beginTransmission(i2cAddr);
}
uint8_t Goodix::i2cStop() {
return Wire.endTransmission(true);
}
uint8_t Goodix::write(uint16_t reg, uint8_t *buf, size_t len) {
uint8_t error;
uint16_t startPos = 0;
while (startPos < len) {
i2cStart(reg + startPos);
startPos += Wire.write(buf + startPos, len - startPos);
error = Wire.endTransmission();
if (error)
return error;
}
return 0;
}
uint8_t Goodix::write(uint16_t reg, uint8_t buf) {
i2cStart(reg);
Wire.write(buf);
return Wire.endTransmission();
}
uint8_t Goodix::read(uint16_t reg, uint8_t *buf, size_t len) {
uint8_t res;
i2cStart(reg);
res = Wire.endTransmission(false);
if (res != GOODIX_OK) {
return res;
}
uint16_t pos = 0, prevPos = 0;
size_t readLen = 0;
uint8_t maxErrs = 3;
while (pos < len) {
readLen = Wire.requestFrom(i2cAddr, (len - pos));
prevPos = pos;
while (Wire.available()) {
buf[pos] = Wire.read();
pos++;
}
if (prevPos == pos)
maxErrs--;
if (maxErrs <= 0) {
break;
}
delay(0);
}
return 0;
}
void Goodix::pinOut(uint8_t pin) {
pinMode(pin, OUTPUT);
}
void Goodix::pinIn(uint8_t pin) {
pinMode(pin, INPUT);
}
void Goodix::pinSet(uint8_t pin, uint8_t level) {
digitalWrite(pin, level);
}
void Goodix::pinHold(uint8_t pin) {
pinSet(pin, LOW);
}
bool Goodix::pinCheck(uint8_t pin, uint8_t level) {
return digitalRead(pin) == level;
}
void Goodix::msSleep(uint16_t milliseconds) {
delay(milliseconds);
}
void Goodix::usSleep(uint16_t microseconds) {
delayMicroseconds(microseconds);
}