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_pos_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 IFS0bits.IC2IF = 0;
118 IEC0bits.IC2IE = 1;
119
120
121 IPC37bits.I2C2MIP = 4;
122 IPC37bits.I2C2MIS = 3;
123
124 IFS4bits.I2C2MIF = 0;
125 IEC4bits.I2C2MIE = 0;
126
127
128 IPC37bits.I2C2SIP = 4;
129 IPC37bits.I2C2SIS = 2;
130
131 IEC4bits.I2C2SIE = 0;
132 IFS4bits.I2C2SIF = 0;
133
134
135 IPC0bits.INT0IP = 4;
136 IPC0bits.INT0IS = 1;
137
138 IFS0bits.INT0IF = 0;
139 IEC0bits.INT0IE = 1;
140
141
142
143 I2C2CONbits.SDAHT = 0;
144
145 I2C2CONbits.PCIE = 1;
146
147
148 I2C2CONbits.ON = 1;
149
150
151
152 int i;
153 for(i=0;i<10000;i++)
154 {
155 PORTBbits.RB13 = 0;
156 }
157
158 PORTBbits.RB13 = 1;
159 for(i=0;i<100000;i++);
160
161
162
163
164
165
166
167
168
169
170 if(!Device_Present())
171 {
172 return;
173 }
174
175
176 FT5436_Write_Reg(FT_REG_MODE, 0);
177
178
179
180
181 FT5436_Write_Reg(FT_ID_G_MODE, 0x00);
182
183
184 FT5436_Write_Reg(FT_REG_THRESHHOLD, 20);
185
186
187 FT5436_Write_Reg(FT_REG_POINTRATE, 12);
188
189 FT5436_Write_Reg(0x86, 0x0);
190 }
191
192
193 void __attribute__((vector(_EXTERNAL_0_VECTOR), interrupt(ipl4srs), nomips16)) INT0_Handler()
194 {
195 FT5436_Read(16);
196
197
198 if(read_buf[2] == 1)
199 {
200 scn_pos_x = read_buf[5];
201 scn_pos_x = scn_pos_x << 8;
202 scn_pos_x = scn_pos_x + read_buf[6];
203
204 scn_pos_y = read_buf[3] - 128;
205 scn_pos_y = scn_pos_y << 8;
206 scn_pos_y = scn_pos_y + read_buf[4];
207
208 if(scn_pos_y > 320)
209 {
210 scn_pos_y = 320;
211 }
212
213 scn_pos_y = 320 - scn_pos_y;
214 }
215
216 IFS0bits.INT0IF = 0;
217 }
218
219 bool Device_Present(void)
220 {
221 int i;
222
223
224
225
226 I2C_start();
227
228
229 I2C2TRN = 0x70;
230 while (I2C2STATbits.TBF == 1);
231 I2C_wait_for_idle();
232
233
234 for(i=0;i<wait_time;i++)
235 {
236 if(I2C2STATbits.ACKSTAT == 0)
237 {
238 return true;
239 i = wait_time;
240 }
241 }
242 if(I2C2STATbits.ACKSTAT == 1)
243 {
244 return false;
245 }
246
247 }
248
249
250 void FT5436_Write_Reg(uint8_t reg, int8_t reg_data)
251 {
252 I2C_start();
253
254
255 I2C2TRN = 0x70;
256 while (I2C2STATbits.TBF == 1);
257 I2C_wait_for_idle();
258 while (I2C2STATbits.ACKSTAT == 1);
259
260 I2C2TRN = reg;
261 while (I2C2STATbits.TBF == 1);
262 I2C_wait_for_idle();
263 while (I2C2STATbits.ACKSTAT == 1);
264
265 I2C2TRN = reg_data;
266 while (I2C2STATbits.TBF == 1);
267 I2C_wait_for_idle();
268 while (I2C2STATbits.ACKSTAT == 1);
269
270 I2C_stop();
271
272 }
273
274
275 void FT5436_Read_Reg(uint8_t reg)
276 {
277 I2C_start();
278
279
280 I2C2TRN = 0x70;
281 while (I2C2STATbits.TBF == 1);
282 I2C_wait_for_idle();
283 while (I2C2STATbits.ACKSTAT == 1);
284
285 I2C2TRN = reg;
286 while (I2C2STATbits.TBF == 1);
287 I2C_wait_for_idle();
288 while (I2C2STATbits.ACKSTAT == 1);
289
290 I2C_restart();
291
292
293 I2C2TRN = 0x71;
294 while (I2C2STATbits.TBF == 1);
295 I2C_wait_for_idle();
296 while (I2C2STATbits.ACKSTAT == 1);
297
298 I2C2CONbits.RCEN = 1;
299 while (I2C2CONbits.RCEN);
300 while (!I2C2STATbits.RBF);
301 reg_data_buf = I2C2RCV;
302
303 I2C_nack();
304 I2C_stop();
305 }
306
307
308 void FT5436_Read(uint8_t num_bytes)
309 {
310 int i;
311
312
313 if(!num_bytes)
314 {
315 num_bytes;
316 }
317
318
319 I2C_start();
320
321
322 I2C2TRN = 0x70;
323 while (I2C2STATbits.TBF == 1);
324 I2C_wait_for_idle();
325 while (I2C2STATbits.ACKSTAT == 1);
326
327 I2C2TRN = 0;
328 while (I2C2STATbits.TBF == 1);
329 I2C_wait_for_idle();
330 while (I2C2STATbits.ACKSTAT == 1);
331
332 I2C_restart();
333
334
335 I2C2TRN = 0x71;
336 while (I2C2STATbits.TBF == 1);
337 I2C_wait_for_idle();
338 while (I2C2STATbits.ACKSTAT == 1);
339
340 for(i=0;i<num_bytes;i++)
341 {
342 I2C2CONbits.RCEN = 1;
343 while (I2C2CONbits.RCEN);
344 while (!I2C2STATbits.RBF);
345 read_buf[i] = I2C2RCV;
346 I2C_ack();
347 }
348
349 I2C2CONbits.RCEN = 1;
350 while (I2C2CONbits.RCEN);
351 while (!I2C2STATbits.RBF);
352 read_buf[15] = I2C2RCV;
353
354 I2C_nack();
355 I2C_stop();
356 }
357
358
359
360 void I2C_wait_for_idle(void)
361 {
362 while(I2C2CON & 0x1F);
363
364
365
366
367 while(I2C2STATbits.TRSTAT);
368 }
369
370
371 void I2C_start(void)
372 {
373 I2C_wait_for_idle();
374 I2C2CONbits.SEN = 1;
375 while (I2C2CONbits.SEN == 1);
376 }
377
378
379 void I2C_stop(void)
380 {
381 I2C_wait_for_idle();
382 I2C2CONbits.PEN = 1;
383 }
384
385
386 void I2C_restart(void)
387 {
388 I2C_wait_for_idle();
389 I2C2CONbits.RSEN = 1;
390 while (I2C2CONbits.RSEN == 1);
391 }
392
393
394 void I2C_ack(void)
395 {
396 I2C_wait_for_idle();
397 I2C2CONbits.ACKDT = 0;
398 I2C2CONbits.ACKEN = 1;
399 while(I2C2CONbits.ACKEN);
400 }
401
402
403 void I2C_nack(void)
404 {
405 I2C_wait_for_idle();
406 I2C2CONbits.ACKDT = 1;
407 I2C2CONbits.ACKEN = 1;
408 while(I2C2CONbits.ACKEN);
409 }
410
411
412 void __attribute__((vector(_I2C1_MASTER_VECTOR), interrupt(ipl4srs), nomips16)) I2C_Master_Handler()
413 {
414
415 IFS4bits.I2C2MIF = 0;
416 }
417
418
419 void __attribute__((vector(_I2C2_SLAVE_VECTOR), interrupt(ipl4srs), nomips16)) I2C_Slave_Handler()
420 {
421
422 IFS0bits.IC2IF = 0;
423 }