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 #include <xc.h>
30 #include <p32xxxx.h>
31 #include <proc/p32mz2048efh100.h>
32 #include <sys/attribs.h>
33 #include <sys/kmem.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdbool.h>
39 #include "MainBrain.h"
40
41
42 #define FT_REG_MODE 0x00
43 #define FT_REG_NUMTOUCHES 0x02
44 #define FT_REG_CALIBRATE 0x02
45 #define FT_TP1_REG (0X03)
46 #define FT_TP2_REG (0X09)
47 #define FT_REG_FACTORYMODE 0x40
48 #define FT_REG_THRESHHOLD 0x80
49 #define FT_REG_POINTRATE 0x88
50 #define FT_ID_G_LIB_VERSION (0xA1)
51 #define FT_REG_FIRMVERS 0xA6
52 #define FT_REG_CHIPID 0xA3
53 #define FT_ID_G_MODE (0xA4)
54 #define FT_REG_VENDID 0xA8
55 #define NVM_ADDR_DATA_WR (0xD0)
56
57
58 #define FT6234_VENVID 0x79
59 #define FT6236_VENDID 0x11
60 #define FT5436_VENDID 0x79
61 #define FT6206_CHIPID 0x06
62 #define FT6234_CHIPID 0x54
63 #define FT6236_CHIPID 0x36
64 #define FT5436_CHIPID 0x54
65 #define FT6236U_CHIPID 0x64
66
67 uint8_t address;
68 uint32_t reg_data_buf;
69 uint8_t read_buf[16];
70 int wait_time = 10000;
71 uint16_t scn_pos_x;
72
73 uint16_t scn_Y;
74
75 uint8_t MenuLevel = 0;
76 uint8_t ButtonNum = 0;
77 bool ExitButton = false;
78 bool isActive = false;
79
80 void FT5436_Write_Reg(uint8_t reg, int8_t reg_data);
81 void FT5436_Read_Reg(uint8_t reg);
82 void I2C_wait_for_idle(void);
83 void I2C_start(void);
84 void I2C_stop(void);
85 void I2C_restart(void);
86 void I2C_ack(void);
87 void I2C_nack(void);
88 void FT5436_Read(uint8_t num_bytes);
89
90 void I2C_init()
91 {
92
93 address = 0x38;
94
95
96 TRISBbits.TRISB13 = 0;
97 PORTBbits.RB13 = 1;
98
99
100 TRISDbits.TRISD0 = 1;
101
102
103
104 TRISAbits.TRISA2 = 1;
105
106
107 TRISAbits.TRISA3 = 1;
108
109
110
111
112
113
114 I2C2BRG = 0x0069;
115
116
117 IEC0bits.IC2IE = 1;
118 IFS0bits.IC2IF = 0;
119
120
121 IPC37bits.I2C2MIP = 7;
122 IEC4bits.I2C2MIE = 0;
123 IFS4bits.I2C2MIF = 0;
124
125
126 IPC37bits.I2C2SIP = 7;
127 IEC4bits.I2C2SIE = 0;
128 IFS4bits.I2C2SIF = 0;
129
130
131 IPC0bits.INT0IP = 7;
132 IEC0bits.INT0IE = 1;
133 IFS0bits.INT0IF = 0;
134
135
136 I2C2CONbits.SDAHT = 0;
137
138 I2C2CONbits.PCIE = 1;
139
140
141 I2C2CONbits.ON = 1;
142
143
144
145 int i;
146 for(i=0;i<10000;i++)
147 {
148 PORTBbits.RB13 = 0;
149 }
150
151 PORTBbits.RB13 = 1;
152 for(i=0;i<100000;i++);
153
154
155 if(!Device_Present())
156 {
157 return;
158 }
159
160
161 FT5436_Write_Reg(FT_REG_MODE, 0);
162
163
164
165
166 FT5436_Write_Reg(FT_ID_G_MODE, 0x00);
167
168
169 FT5436_Write_Reg(FT_REG_THRESHHOLD, 20);
170
171
172 FT5436_Write_Reg(FT_REG_POINTRATE, 12);
173
174 FT5436_Write_Reg(0x86, 0x0);
175 }
176
177
178 void __attribute__((vector(_EXTERNAL_0_VECTOR), interrupt(ipl7srs), nomips16)) INT0_Handler()
179 {
180 FT5436_Read(16);
181
182
183 if(read_buf[2] > 0)
184 {
185 scn_pos_x = read_buf[5];
186 scn_pos_x = scn_pos_x << 8;
187 scn_pos_x = scn_pos_x + read_buf[6];
188
189 scn_Y = read_buf[3] - 128;
190 scn_Y = scn_Y << 8;
191 scn_Y = scn_Y + read_buf[4];
192
193 if(scn_Y > 320)
194 {
195 scn_Y = 320;
196 }
197
198 scn_Y = 320 - scn_Y;
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 if((scn_pos_x > 30 && scn_pos_x < 200) && (scn_Y > 50 && scn_Y < 100))
217 {
218 if(ButtonNum == 0)
219 {
220 ButtonNum = 1;
221 }
222 }
223
224
225 if((scn_pos_x > 30 && scn_pos_x < 200) && (scn_Y > 125 && scn_Y < 175))
226 {
227 if(ButtonNum == 0)
228 {
229 ButtonNum = 2;
230 }
231 }
232
233
234 if(DrawScreenOpen == true)
235 {
236 if((scn_pos_x > 10 && scn_pos_x < 100) && (scn_Y > 40 && scn_Y < 90))
237 {
238 ButtonNum = 10;
239 }
240
241 }
242
243
244 if((scn_pos_x > 170 && scn_pos_x < 320) && (scn_Y > 250 && scn_Y < 300))
245 {
246 if(ButtonNum > 0)
247 {
248 ExitButton = true;
249 }
250 }
251 }
252
253 IFS0bits.INT0IF = 0;
254 }
255
256 bool Device_Present(void)
257 {
258 int i;
259
260
261
262
263 I2C_start();
264
265
266 I2C2TRN = 0x70;
267 while (I2C2STATbits.TBF == 1);
268 I2C_wait_for_idle();
269
270
271 for(i=0;i<wait_time;i++)
272 {
273 if(I2C2STATbits.ACKSTAT == 0)
274 {
275 return true;
276 i = wait_time;
277 }
278 }
279 if(I2C2STATbits.ACKSTAT == 1)
280 {
281 return false;
282 }
283
284 }
285
286
287 void FT5436_Write_Reg(uint8_t reg, int8_t reg_data)
288 {
289 I2C_start();
290
291
292 I2C2TRN = 0x70;
293 while (I2C2STATbits.TBF == 1);
294 I2C_wait_for_idle();
295 while (I2C2STATbits.ACKSTAT == 1);
296
297 I2C2TRN = reg;
298 while (I2C2STATbits.TBF == 1);
299 I2C_wait_for_idle();
300 while (I2C2STATbits.ACKSTAT == 1);
301
302 I2C2TRN = reg_data;
303 while (I2C2STATbits.TBF == 1);
304 I2C_wait_for_idle();
305 while (I2C2STATbits.ACKSTAT == 1);
306
307 I2C_stop();
308
309 }
310
311
312 void FT5436_Read_Reg(uint8_t reg)
313 {
314 I2C_start();
315
316
317 I2C2TRN = 0x70;
318 while (I2C2STATbits.TBF == 1);
319 I2C_wait_for_idle();
320 while (I2C2STATbits.ACKSTAT == 1);
321
322 I2C2TRN = reg;
323 while (I2C2STATbits.TBF == 1);
324 I2C_wait_for_idle();
325 while (I2C2STATbits.ACKSTAT == 1);
326
327 I2C_restart();
328
329
330 I2C2TRN = 0x71;
331 while (I2C2STATbits.TBF == 1);
332 I2C_wait_for_idle();
333 while (I2C2STATbits.ACKSTAT == 1);
334
335 I2C2CONbits.RCEN = 1;
336 while (I2C2CONbits.RCEN);
337 while (!I2C2STATbits.RBF);
338 reg_data_buf = I2C2RCV;
339
340 I2C_nack();
341 I2C_stop();
342 }
343
344
345 void FT5436_Read(uint8_t num_bytes)
346 {
347 int i;
348
349
350 if(!num_bytes)
351 {
352 num_bytes;
353 }
354
355
356 I2C_start();
357
358
359 I2C2TRN = 0x70;
360 while (I2C2STATbits.TBF == 1);
361 I2C_wait_for_idle();
362 while (I2C2STATbits.ACKSTAT == 1);
363
364 I2C2TRN = 0;
365 while (I2C2STATbits.TBF == 1);
366 I2C_wait_for_idle();
367 while (I2C2STATbits.ACKSTAT == 1);
368
369 I2C_restart();
370
371
372 I2C2TRN = 0x71;
373 while (I2C2STATbits.TBF == 1);
374 I2C_wait_for_idle();
375 while (I2C2STATbits.ACKSTAT == 1);
376
377 for(i=0;i<num_bytes;i++)
378 {
379 I2C2CONbits.RCEN = 1;
380 while (I2C2CONbits.RCEN);
381 while (!I2C2STATbits.RBF);
382 read_buf[i] = I2C2RCV;
383 I2C_ack();
384 }
385
386 I2C2CONbits.RCEN = 1;
387 while (I2C2CONbits.RCEN);
388 while (!I2C2STATbits.RBF);
389 read_buf[15] = I2C2RCV;
390
391 I2C_nack();
392 I2C_stop();
393 }
394
395
396
397 void I2C_wait_for_idle(void)
398 {
399 while(I2C2CON & 0x1F);
400
401
402
403
404 while(I2C2STATbits.TRSTAT);
405 }
406
407
408 void I2C_start(void)
409 {
410 I2C_wait_for_idle();
411 I2C2CONbits.SEN = 1;
412 while (I2C2CONbits.SEN == 1);
413 }
414
415
416 void I2C_stop(void)
417 {
418 I2C_wait_for_idle();
419 I2C2CONbits.PEN = 1;
420 }
421
422
423 void I2C_restart(void)
424 {
425 I2C_wait_for_idle();
426 I2C2CONbits.RSEN = 1;
427 while (I2C2CONbits.RSEN == 1);
428 }
429
430
431 void I2C_ack(void)
432 {
433 I2C_wait_for_idle();
434 I2C2CONbits.ACKDT = 0;
435 I2C2CONbits.ACKEN = 1;
436 while(I2C2CONbits.ACKEN);
437 }
438
439
440 void I2C_nack(void)
441 {
442 I2C_wait_for_idle();
443 I2C2CONbits.ACKDT = 1;
444 I2C2CONbits.ACKEN = 1;
445 while(I2C2CONbits.ACKEN);
446 }
447
448
449 void __attribute__((vector(_I2C1_MASTER_VECTOR), interrupt(ipl7srs), nomips16)) I2C_Master_Handler()
450 {
451
452 IFS4bits.I2C2MIF = 0;
453 }
454
455
456 void __attribute__((vector(_I2C2_SLAVE_VECTOR), interrupt(ipl7srs), nomips16)) I2C_Slave_Handler()
457 {
458
459 IFS0bits.IC2IF = 0;
460 }