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 #include <xc.h>
31 #include <p32xxxx.h>
32 #include <proc/p32mz2048efh100.h>
33 #include <sys/attribs.h>
34 #include <sys/kmem.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdbool.h>
40 #include "MainBrain.h"
41
42
43
44
45
46
47 #define FT_REG_MODE 0x00
48 #define FT_REG_GEST_ID 0x01
49 #define FT_REG_TD_STATUS 0x02
50
51
52 #define FT_TOUCH1_XH 0x03
53 #define FT_TOUCH1_XL 0x04
54 #define FT_TOUCH1_YH 0x05
55 #define FT_TOUCH1_YL 0x06
56 #define FT_TOUCH1_WEIGHT 0x07
57 #define FT_TOUCH1_MISC 0x08
58
59
60 #define FT_TOUCH2_XH 0x09
61 #define FT_TOUCH2_XL 0x0A
62 #define FT_TOUCH2_YH 0x0B
63 #define FT_TOUCH2_YL 0x0C
64 #define FT_TOUCH2_WEIGHT 0x0D
65 #define FT_TOUCH2_MISC 0x0E
66
67
68 #define FT_TOUCH3_XH 0x0F
69 #define FT_TOUCH3_XL 0x10
70 #define FT_TOUCH3_YH 0x11
71 #define FT_TOUCH3_YL 0x12
72 #define FT_TOUCH3_WEIGHT 0x13
73 #define FT_TOUCH3_MISC 0x14
74
75
76 #define FT_TOUCH4_XH 0x15
77 #define FT_TOUCH4_XL 0x16
78 #define FT_TOUCH4_YH 0x17
79 #define FT_TOUCH4_YL 0x18
80 #define FT_TOUCH4_WEIGHT 0x19
81 #define FT_TOUCH4_MISC 0x1A
82
83
84 #define FT_TOUCH5_XH 0x1B
85 #define FT_TOUCH5_XL 0x1C
86 #define FT_TOUCH5_YH 0x1D
87 #define FT_TOUCH5_YL 0x1E
88 #define FT_TOUCH5_WEIGHT 0x1F
89 #define FT_TOUCH5_MISC 0x20
90
91
92 #define FT_TOUCH6_XH 0x21
93 #define FT_TOUCH6_XL 0x22
94 #define FT_TOUCH6_YH 0x23
95 #define FT_TOUCH6_YL 0x24
96 #define FT_TOUCH6_WEIGHT 0x25
97 #define FT_TOUCH6_MISC 0x26
98
99
100 #define FT_TOUCH7_XH 0x27
101 #define FT_TOUCH7_XL 0x28
102 #define FT_TOUCH7_YH 0x29
103 #define FT_TOUCH7_YL 0x2A
104 #define FT_TOUCH7_WEIGHT 0x2B
105 #define FT_TOUCH7_MISC 0x2C
106
107
108 #define FT_TOUCH8_XH 0x2D
109 #define FT_TOUCH8_XL 0x2E
110 #define FT_TOUCH8_YH 0x2F
111 #define FT_TOUCH8_YL 0x30
112 #define FT_TOUCH8_WEIGHT 0x31
113 #define FT_TOUCH8_MISC 0x32
114
115
116 #define FT_TOUCH9_XH 0x33
117 #define FT_TOUCH9_XL 0x34
118 #define FT_TOUCH9_YH 0x35
119 #define FT_TOUCH9_YL 0x36
120 #define FT_TOUCH9_WEIGHT 0x37
121 #define FT_TOUCH9_MISC 0x38
122
123
124 #define FT_TOUCH10_XH 0x39
125 #define FT_TOUCH10_XL 0x3A
126 #define FT_TOUCH10_YH 0x3B
127 #define FT_TOUCH10_YL 0x3C
128 #define FT_TOUCH10_WEIGHT 0x3D
129 #define FT_TOUCH10_MISC 0x3E
130
131
132
133
134
135
136 #define FT_MODE_WORKING 0x00
137 #define FT_MODE_TEST 0x40
138
139
140 #define FT_EVENT_DOWN 0x00
141 #define FT_EVENT_UP 0x40
142 #define FT_EVENT_CONTACT 0x80
143 #define FT_EVENT_NONE 0xC0
144
145
146 #define FT_REG_MODE 0x00
147 #define FT_REG_NUMTOUCHES 0x02
148 #define FT_REG_CALIBRATE 0x02
149 #define FT_TP1_REG (0X03)
150 #define FT_TP2_REG (0X09)
151 #define FT_REG_FACTORYMODE 0x40
152 #define FT_REG_THRESHOLD 0x80
153 #define FT_ID_G_CTRL 0x86
154 #define FT_REG_POINTRATE 0x88
155 #define FT_ID_G_LIB_VERSION (0xA1)
156 #define FT_REG_FIRMVERS 0xA6
157 #define FT_REG_CHIPID 0xA3
158 #define FT_ID_G_MODE (0xA4)
159 #define FT_REG_VENDID 0xA8
160 #define NVM_ADDR_DATA_WR (0xD0)
161
162
163 #define FT6234_VENVID 0x79
164 #define FT6236_VENDID 0x11
165 #define FT5436_VENDID 0x79
166 #define FT6206_CHIPID 0x06
167 #define FT6234_CHIPID 0x54
168 #define FT6236_CHIPID 0x36
169 #define FT5436_CHIPID 0x54
170 #define FT6236U_CHIPID 0x64
171
172 uint8_t Event_Flag_1;
173 uint8_t Touch_ID_1;
174 uint8_t Touch_Area_1;
175 uint8_t Touch_Weight_1;
176 uint8_t address;
177 uint8_t reg_data_buf;
178 uint8_t read_buf[180];
179 int wait_time = 10000;
180 uint16_t scn_pos_x;
181
182 uint16_t scn_pos_y;
183
184 uint8_t MenuLevel = 0;
185 uint8_t ButtonNum = 0;
186 bool ExitButton = false;
187 bool isActive = false;
188
189 uint8_t FT5436_Write_Reg(uint8_t reg, uint8_t reg_data);
190 void FT5436_Read_Reg(uint8_t reg);
191 void FT5436_Read(uint8_t num_bytes);
192 void I2C_wait_for_idle(void);
193 void I2C_start(void);
194 void I2C_stop(void);
195 void I2C_restart(void);
196 void I2C_ack(void);
197 void I2C_nack(void);
198
199
200 bool I2C_init()
201 {
202
203 address = 0x38;
204
205
206 TRISBbits.TRISB13 = 0;
207 PORTBbits.RB13 = 1;
208
209
210 TRISDbits.TRISD0 = 1;
211
212
213
214 TRISAbits.TRISA2 = 1;
215
216
217 TRISAbits.TRISA3 = 1;
218
219
220
221
222
223
224 I2C2BRG = 0x0069;
225
226
227 IFS0bits.IC2IF = 0;
228 IEC0bits.IC2IE = 1;
229
230
231 IPC37bits.I2C2MIP = 4;
232 IPC37bits.I2C2MIS = 3;
233
234 IFS4bits.I2C2MIF = 0;
235 IEC4bits.I2C2MIE = 0;
236
237
238 IPC37bits.I2C2SIP = 4;
239 IPC37bits.I2C2SIS = 2;
240
241 IEC4bits.I2C2SIE = 0;
242 IFS4bits.I2C2SIF = 0;
243
244
245 IPC0bits.INT0IP = 4;
246 IPC0bits.INT0IS = 1;
247
248 IFS0bits.INT0IF = 0;
249 IEC0bits.INT0IE = 1;
250
251
252
253 I2C2CONbits.SDAHT = 0;
254
255 I2C2CONbits.PCIE = 1;
256
257
258 I2C2CONbits.ON = 1;
259
260
261
262 for(int i=0;i<1000000;i++)
263 {
264 PORTBbits.RB13 = 0;
265 }
266
267 PORTBbits.RB13 = 1;
268
269 for(int i=0;i<1000000;i++);
270
271
272 FT5436_Write_Reg(FT_REG_THRESHOLD, 20);
273 FT5436_Write_Reg(FT_REG_POINTRATE, 12);
274 FT5436_Write_Reg(FT_ID_G_CTRL, 0x01);
275 FT5436_Write_Reg(FT_ID_G_MODE, 0x01);
276
277
278 FT5436_Read_Reg(FT_REG_VENDID);
279 if(reg_data_buf != 0x79)
280 {
281 return false;
282 }
283
284 return true;
285 }
286
287
288 void __attribute__((vector(_EXTERNAL_0_VECTOR), interrupt(ipl4srs), nomips16)) INT0_Handler()
289 {
290
291 FT5436_Read(180);
292
293
294
295
296
297 Event_Flag_1 = (read_buf[3] & 0xC0) >> 6;
298
299
300 Touch_ID_1 = (read_buf[5] & 0xf0) >> 4;
301
302
303 Touch_Area_1 = read_buf[8] >> 4;
304
305
306 Touch_Weight_1 = read_buf[8];
307
308 scn_pos_x = read_buf[5] & 0x0f;
309 scn_pos_x = scn_pos_x << 8;
310 scn_pos_x = scn_pos_x + read_buf[6];
311
312 if (scn_pos_x > 480)
313 {
314 scn_pos_x = 480;
315 }
316
317 scn_pos_y = read_buf[3] & 0x0f;
318 scn_pos_y = scn_pos_y << 8;
319 scn_pos_y = scn_pos_y + read_buf[4];
320
321 if(scn_pos_y > 320)
322 {
323 scn_pos_y = 320;
324 }
325
326 scn_pos_y = 320 - scn_pos_y;
327
328 IFS0bits.INT0IF = 0;
329 }
330
331 bool Device_Present(void)
332 {
333 int i;
334
335
336
337
338 I2C_start();
339
340
341 I2C2TRN = 0x70;
342 while (I2C2STATbits.TBF == 1);
343 I2C_wait_for_idle();
344
345
346 for(i=0;i<wait_time;i++)
347 {
348 if(I2C2STATbits.ACKSTAT == 0)
349 {
350 return true;
351 i = wait_time;
352 }
353 }
354 if(I2C2STATbits.ACKSTAT == 1)
355 {
356 return false;
357 }
358
359 }
360
361
362 uint8_t FT5436_Write_Reg(uint8_t reg, uint8_t reg_data)
363 {
364 I2C_start();
365 while (I2C2CONbits.SEN);
366
367 I2C2TRN = 0x70;
368 while (I2C2STATbits.TRSTAT);
369 if (I2C2STATbits.ACKSTAT)
370 {
371 I2C_stop();
372 return 1;
373 }
374
375 I2C2TRN = reg;
376 while (I2C2STATbits.TRSTAT);
377 if (I2C2STATbits.ACKSTAT)
378 {
379 I2C_stop();
380 return 2;
381 }
382
383 I2C2TRN = reg_data;
384 while (I2C2STATbits.TRSTAT);
385 if (I2C2STATbits.ACKSTAT)
386 {
387 I2C_stop();
388 return 3;
389 }
390
391 I2C_stop();
392
393 return 0;
394 }
395
396 void FT5436_Read_Reg(uint8_t reg)
397 {
398 I2C_start();
399
400 I2C2TRN = 0x70;
401 while (I2C2STATbits.TBF == 1);
402 I2C_wait_for_idle();
403 while (I2C2STATbits.ACKSTAT == 1);
404
405 I2C2TRN = reg;
406 while (I2C2STATbits.TBF == 1);
407 I2C_wait_for_idle();
408 while (I2C2STATbits.ACKSTAT == 1);
409
410 I2C_restart();
411
412 I2C2TRN = 0x71;
413 while (I2C2STATbits.TBF == 1);
414 I2C_wait_for_idle();
415 while (I2C2STATbits.ACKSTAT == 1);
416
417
418 I2C2CONbits.RCEN = 1;
419 while (I2C2CONbits.RCEN);
420 while (!I2C2STATbits.RBF);
421 reg_data_buf = I2C2RCV;
422 I2C_ack(); <-- THIS is what you were missing
423
424
425 I2C2CONbits.RCEN = 1;
426 while (I2C2CONbits.RCEN);
427 while (!I2C2STATbits.RBF);
428 (void)I2C2RCV;
429
430 I2C_nack();
431 I2C_stop();
432 }
433
434
435 void FT5436_Read(uint8_t num_bytes)
436 {
437 int i;
438
439
440 if(!num_bytes)
441 {
442 num_bytes = 1;
443 }
444
445
446 I2C_start();
447
448
449 I2C2TRN = 0x70;
450 while (I2C2STATbits.TBF == 1);
451 I2C_wait_for_idle();
452 while (I2C2STATbits.ACKSTAT == 1);
453
454 I2C2TRN = 0x0;
455 while (I2C2STATbits.TBF == 1);
456 I2C_wait_for_idle();
457 while (I2C2STATbits.ACKSTAT == 1);
458
459 I2C_restart();
460
461
462 I2C2TRN = 0x71;
463 while (I2C2STATbits.TBF == 1);
464 I2C_wait_for_idle();
465 while (I2C2STATbits.ACKSTAT == 1);
466
467 for(i=0;i<num_bytes;i++)
468 {
469 I2C2CONbits.RCEN = 1;
470 while (I2C2CONbits.RCEN);
471 while (!I2C2STATbits.RBF);
472 read_buf[i] = I2C2RCV;
473 I2C_ack();
474 }
475
476 I2C2CONbits.RCEN = 1;
477 while (I2C2CONbits.RCEN);
478 while (!I2C2STATbits.RBF);
479 read_buf[num_bytes] = I2C2RCV;
480
481 I2C_nack();
482 I2C_stop();
483 }
484
485
486
487 void I2C_wait_for_idle(void)
488 {
489 while(I2C2CON & 0x1F);
490
491
492
493
494 while(I2C2STATbits.TRSTAT);
495 }
496
497
498 void I2C_start(void)
499 {
500 I2C_wait_for_idle();
501 I2C2CONbits.SEN = 1;
502 while (I2C2CONbits.SEN == 1);
503 }
504
505
506 void I2C_stop(void)
507 {
508 I2C_wait_for_idle();
509 I2C2CONbits.PEN = 1;
510 while (I2C2CONbits.PEN == 1); <-- wait for stop to complete
511 }
512
513
514 void I2C_restart(void)
515 {
516 I2C_wait_for_idle();
517 I2C2CONbits.RSEN = 1;
518 while (I2C2CONbits.RSEN == 1);
519 }
520
521
522 void I2C_ack(void)
523 {
524 I2C_wait_for_idle();
525 I2C2CONbits.ACKDT = 0;
526 I2C2CONbits.ACKEN = 1;
527 while(I2C2CONbits.ACKEN);
528 }
529
530
531 void I2C_nack(void)
532 {
533 I2C_wait_for_idle();
534 I2C2CONbits.ACKDT = 1;
535 I2C2CONbits.ACKEN = 1;
536 while(I2C2CONbits.ACKEN);
537 }
538
539
540 void __attribute__((vector(_I2C1_MASTER_VECTOR), interrupt(ipl4srs), nomips16)) I2C_Master_Handler()
541 {
542
543 IFS4bits.I2C2MIF = 0;
544 }
545
546
547 void __attribute__((vector(_I2C2_SLAVE_VECTOR), interrupt(ipl4srs), nomips16)) I2C_Slave_Handler()
548 {
549
550 IFS0bits.IC2IF = 0;
551 }