-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGSL1680.ino
318 lines (266 loc) · 7.04 KB
/
GSL1680.ino
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
// driver for the GSL1680 touch panel
// Information gleaned from https://github.com/rastersoft/gsl1680.git and various other sources
// firmware for the specific panel was found here:- http://www.buydisplay.com/default/5-inch-tft-lcd-module-800x480-display-w-controller-i2c-serial-spi
// As was some test code.
// This is for that 800X480 display and the 480x272 from buydisplay.com
/*
Pin outs
the FPC on the touch panel is six pins, pin 1 is to the left pin 6 to the right with the display facing up
pin | function | Arduino Uno
-----------------------------
1 | SCL | A5
2 | SDA | A4
3 | VDD (3v3) | 3v3
4 | Wake | 4
5 | Int | 2
6 | Gnd | gnd
*/
#include <Wire.h>
#include "Arduino.h"
// set this for teensy3
//#define BIGFLASH
// TODO define for other resolution
#ifndef BIGFLASH
#include "gslfw.h" // this is compacted format made by compress_data.c
#else
#include "gslX680firmware.h"
#endif
// Pins
#define WAKE 4
#define INTRPT 2
#define LED 13
#define SCREEN_MAX_X 800
#define SCREEN_MAX_Y 480
#define GSLX680_I2C_ADDR 0x40
#define GSL_DATA_REG 0x80
#define GSL_STATUS_REG 0xe0
#define GSL_PAGE_REG 0xf0
#define delayus delayMicroseconds
struct _coord { uint32_t x, y; uint8_t finger; };
struct _ts_event
{
uint8_t n_fingers;
struct _coord coords[5];
};
struct _ts_event ts_event;
bool led= false;
static inline void wiresend(uint8_t x) {
#if ARDUINO >= 100
Wire.write((uint8_t)x);
#else
Wire.send(x);
#endif
}
static inline uint8_t wirerecv(void) {
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
bool i2c_write(uint8_t reg, uint8_t *buf, int cnt)
{
#if 0
Serial.print("i2c write: "); Serial.println(reg, HEX);
for(int i=0; i<cnt; i++){
Serial.print(buf[i], HEX); Serial.print(",");
}
Serial.println();
#endif
Wire.beginTransmission(GSLX680_I2C_ADDR);
wiresend(reg);
for(int i=0; i<cnt; i++){
wiresend(buf[i]);
}
int r= Wire.endTransmission();
if(r != 0){ Serial.print("i2c write error: "); Serial.print(r); Serial.print(" "); Serial.println(reg, HEX); }
return r == 0;
}
int i2c_read(uint8_t reg, uint8_t *buf, int cnt)
{
Wire.beginTransmission(GSLX680_I2C_ADDR);
wiresend(reg);
int r= Wire.endTransmission();
if(r != 0){ Serial.print("i2c read error: "); Serial.print(r); Serial.print(" "); Serial.println(reg, HEX); }
int n= Wire.requestFrom(GSLX680_I2C_ADDR, cnt);
if(n != cnt){ Serial.print("i2c read error: did not get expected count "); Serial.print(n); Serial.print(" - "); Serial.println(cnt); }
for(int i=0; i<n; i++){
buf[i]= wirerecv();
}
return n;
}
void clr_reg(void)
{
uint8_t buf[4];
buf[0] = 0x88;
i2c_write(0xe0, buf, 1);
delay(20);
buf[0] = 0x01;
i2c_write(0x80, buf, 1);
delay(5);
buf[0] = 0x04;
i2c_write(0xe4, buf, 1);
delay(5);
buf[0] = 0x00;
i2c_write(0xe0, buf, 1);
delay(20);
}
void reset_chip()
{
uint8_t buf[4];
buf[0] = 0x88;
i2c_write(GSL_STATUS_REG, buf, 1);
delay(20);
buf[0] = 0x04;
i2c_write(0xe4,buf, 1);
delay(10);
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x00;
buf[3] = 0x00;
i2c_write(0xbc,buf, 4);
delay(10);
}
#ifndef BIGFLASH
// the data is in blocks of 128 bytes, each one preceded by the page number
// we first send the page number then we send the data in blocks of 32 until the entire page is sent
// NOTE that the firmware data is stored in flash as it is huge! around 28kBytes
void load_fw(void)
{
uint8_t buf[32];
size_t source_len = sizeof(gslx680_fw);
Serial.print("Firmware length: "); Serial.println(source_len);
int blockstart= 1;
int reg= 0;
int off= 0;
size_t source_line;
for (source_line=0; source_line < source_len; source_line++) {
if(off == 32){
i2c_write(reg, buf, 32); // write accumulated block
reg += 32;
off= 0;
if(reg >= 128) blockstart= 1;
}
if(blockstart) {
blockstart= 0;
buf[0] = pgm_read_byte_far(gslx680_fw + source_line); // gslx680_fw[source_line];
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
i2c_write(GSL_PAGE_REG, buf, 4);
reg= 0;
}else{
buf[off++] = pgm_read_byte_far(gslx680_fw + source_line); // gslx680_fw[source_line];
}
}
if(off == 32){ // write last accumulated block
i2c_write(reg, buf, 32);
}
}
#else
void load_fw(void)
{
uint8_t addr;
uint8_t Wrbuf[4];
uint16_t source_line = 0;
uint16_t source_len = sizeof(GSLX680_FW) / sizeof(struct fw_data);
for (source_line = 0; source_line < source_len; source_line++) {
addr = GSLX680_FW[source_line].offset;
Wrbuf[0] = (char)(GSLX680_FW[source_line].val & 0x000000ff);
Wrbuf[1] = (char)((GSLX680_FW[source_line].val & 0x0000ff00) >> 8);
Wrbuf[2] = (char)((GSLX680_FW[source_line].val & 0x00ff0000) >> 16);
Wrbuf[3] = (char)((GSLX680_FW[source_line].val & 0xff000000) >> 24);
i2c_write(addr, Wrbuf, 4);
}
}
#endif
void startup_chip(void)
{
uint8_t buf[4];
buf[0] = 0x00;
i2c_write(0xe0, buf, 1);
}
void init_chip()
{
#if 1
Serial.println("Toggle Wake");
digitalWrite(WAKE, HIGH);
delay(50);
digitalWrite(WAKE, LOW);
delay(50);
digitalWrite(WAKE, HIGH);
delay(30);
delay(500);
// CTP startup sequence
Serial.println("clr reg");
clr_reg();
Serial.println("reset_chip");
reset_chip();
Serial.println("load_fw");
load_fw();
//startup_chip();
Serial.println("reset_chip2");
reset_chip();
Serial.println("startup_chip");
startup_chip();
Serial.println("init done");
#else
// rastersoft int sequence
reset_chip();
load_fw();
startup_chip();
reset_chip();
digitalWrite(WAKE, LOW);
delay(50);
digitalWrite(WAKE, HIGH);
delay(30);
digitalWrite(WAKE, LOW);
delay(5);
digitalWrite(WAKE, HIGH);
delay(20);
reset_chip();
startup_chip();
#endif
}
int read_data(void)
{
Serial.println("reading data...");
uint8_t touch_data[24] = {0};
int n= i2c_read(GSL_DATA_REG, touch_data, 24);
Serial.print("read: "); Serial.println(n);
ts_event.n_fingers= touch_data[0];
for(int i=0; i<ts_event.n_fingers; i++){
ts_event.coords[i].x = ( (((uint32_t)touch_data[(i*4)+5])<<8) | (uint32_t)touch_data[(i*4)+4] ) & 0x00000FFF; // 12 bits of X coord
ts_event.coords[i].y = ( (((uint32_t)touch_data[(i*4)+7])<<8) | (uint32_t)touch_data[(i*4)+6] ) & 0x00000FFF;
ts_event.coords[i].finger = (uint32_t)touch_data[(i*4)+7] >> 4; // finger that did the touch
}
return ts_event.n_fingers;
}
void setup() {
Serial.begin(9600);
Serial.println("Starting");
pinMode(LED, OUTPUT);
pinMode(WAKE, OUTPUT);
digitalWrite(WAKE, LOW);
pinMode(INTRPT, INPUT_PULLUP);
delay(100);
Wire.begin();
init_chip();
#if 0
uint8_t buf[4];
int n= i2c_read(0xB0, buf, 4);
Serial.print(buf[0], HEX); Serial.print(","); Serial.print(buf[1], HEX); Serial.print(","); Serial.print(buf[2], HEX); Serial.print(","); Serial.println(buf[3], HEX);
#endif
}
void loop() {
if(digitalRead(INTRPT) == HIGH) {
digitalWrite(LED, led);
led= !led;
int n= read_data();
for(int i=0; i<n; i++){
Serial.print(ts_event.coords[i].finger); Serial.print(" "), Serial.print(ts_event.coords[i].x); Serial.print(" "), Serial.print(ts_event.coords[i].y);
Serial.println("");
}
Serial.println("---");
}
}