1 /*********************************************************************
  2     FileName:           Touch.c
  3     Dependencies:       See #includes
  4     Processor:          PIC32MZ
  5     Hardware:           MainBrain MZ
  6     Complier:           XC32 4.40, 4.45
  7     Author:             Larry Knight 2024
  8 
  9     Software License Agreement:
 10         This software is licensed under the Apache License Agreement
 11 
 12     Description:
 13         I2C code for capacitive touch display with FT5436 controller
 14         SCL frequency = 400KHz
 15         Address 0x38 (7-bit) or 0x70 (write) and 0x71 (read) (8-bit)
 16         5 touch points
 17     References:
 18         Understanding the I2C Bus - https://www.ti.com/lit/pdf/slva704 
 19         FocalTech Application Note for FT5426 - 5526 CTPM
 20         https://github.com/crystalfontz/CFAF320480C7-035Tx/blob/main/CFAF320480C7-035Tx/CFAF320480C7-035Tx.ino
 21         https://github.com/Bodmer/Touch_FT5436/blob/main/Touch_FT5436.cpp
 22 
 23     File Description:
 24 
 25     Change History:
 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 //Register addresses
 42 #define FT_REG_MODE 0x00                //Device mode, either WORKING or FACTORY
 43 #define FT_REG_NUMTOUCHES 0x02          //Number of touch points
 44 #define FT_REG_CALIBRATE 0x02           //Calibrate mode
 45 #define FT_TP1_REG (0X03)
 46 #define FT_TP2_REG (0X09)
 47 #define FT_REG_FACTORYMODE 0x40         //Factory mode
 48 #define FT_REG_THRESHHOLD 0x80          //Threshold for touch detection
 49 #define FT_REG_POINTRATE 0x88           //Point rate
 50 #define FT_ID_G_LIB_VERSION (0xA1)
 51 #define FT_REG_FIRMVERS 0xA6            //Firmware version
 52 #define FT_REG_CHIPID 0xA3              //Chip selecting
 53 #define FT_ID_G_MODE (0xA4)
 54 #define FT_REG_VENDID 0xA8              //FocalTech's panel ID
 55 #define NVM_ADDR_DATA_WR (0xD0)
 56 
 57 //FocalTech ID's
 58 #define FT6234_VENVID 0x79  // FocalTech's panel ID
 59 #define FT6236_VENDID 0x11  // FocalTech's panel ID
 60 #define FT5436_VENDID 0x79  // FocalTech's panel ID
 61 #define FT6206_CHIPID 0x06  // FT6206 ID
 62 #define FT6234_CHIPID 0x54  // FT6234 ID
 63 #define FT6236_CHIPID 0x36  // FT6236 ID
 64 #define FT5436_CHIPID 0x54  // FT5436 ID
 65 #define FT6236U_CHIPID 0x64 // FT6236U ID
 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     //FT5436 Device Address
 93     address = 0x38;
 94     
 95     //Touch Controller Reset
 96     TRISBbits.TRISB13 = 0;
 97     PORTBbits.RB13 = 1;
 98     
 99     //INT pin 
100     TRISDbits.TRISD0 = 1;
101     //PORTDbits.RD0 = 1;
102     
103     //RA2 - Pin 59 - SCL2
104     TRISAbits.TRISA2 = 1;
105     
106     //RA3 - Pin 60 - SDA2
107     TRISAbits.TRISA3 = 1;
108      
109     //Disable Slew Rate Control bit
110     //I2C2CONbits.DISSLW = 1;
111     
112     //BAUD rate - 400KHz
113     //set with the scope where one clock period is 2.5uS (400KHz)
114     I2C2BRG = 0x0069; 
115     
116     //I2C-2 Interrupt
117     IFS0bits.IC2IF = 0; 
118     IEC0bits.IC2IE = 1;
119     
120     //Master
121     IPC37bits.I2C2MIP = 4;
122     IPC37bits.I2C2MIS = 3;
123     
124     IFS4bits.I2C2MIF = 0;
125     IEC4bits.I2C2MIE = 0;
126     
127     //Slave
128     IPC37bits.I2C2SIP = 4;
129     IPC37bits.I2C2SIS = 2;
130     
131     IEC4bits.I2C2SIE = 0;
132     IFS4bits.I2C2SIF = 0;
133             
134     //INT0 
135     IPC0bits.INT0IP = 4;
136     IPC0bits.INT0IS = 1;
137     
138     IFS0bits.INT0IF = 0;
139     IEC0bits.INT0IE = 1;
140     
141     
142     //SDA Hold Time 0 = 100nS, 1 = 300nS
143     I2C2CONbits.SDAHT = 0;
144     
145     I2C2CONbits.PCIE = 1;
146 
147     //Enable module
148     I2C2CONbits.ON = 1;
149     
150     //RESET Touch Controller
151     //Should be low for 10mS
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 //    //If there is no touch panel is detected by the time Timer 5 rolls over
162 //    //the wait for the device completes and returns to the calling function
163 //    //Start Timer 5
164 //    T5CONbits.TCKPS = 0;
165 //    IFS0bits.T5IF = 0;
166 //    T5CONbits.ON = 0;
167 //    IFS0bits.T5IF
168 //    
169     //Check for device present    
170     if(!Device_Present())
171     {
172         return;
173     }
174     
175     //Set mode
176     FT5436_Write_Reg(FT_REG_MODE, 0);
177     
178     //ID_G_MODE set interrupt
179         //0x00 - Polling Mode
180         //0x01 - Trigger Mode
181     FT5436_Write_Reg(FT_ID_G_MODE, 0x00);
182     
183     //Touch Threshold
184     FT5436_Write_Reg(FT_REG_THRESHHOLD, 20);
185 
186         //Reporting rate
187         FT5436_Write_Reg(FT_REG_POINTRATE, 12);   
188     
189     FT5436_Write_Reg(0x86, 0x0);
190 }
191 
192 //INT0 Interrupt handler
193 void __attribute__((vector(_EXTERNAL_0_VECTOR), interrupt(ipl4srs), nomips16)) INT0_Handler()
194 {
195     FT5436_Read(16);
196     
197     //if there is a touch but only 1 touch point
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     //Now we have to determine if there is a device present
224     //The display is optional and should not be required
225     //******************************************************
226     I2C_start();
227     
228     //write to slave
229     I2C2TRN = 0x70;                         // Send slave address with Read/Write bit cleared
230     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
231     I2C_wait_for_idle();                    // Wait until I2C bus is idle
232 
233     //wait a finite amount of time then RETURN if not acknowledged by device
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 //Writes a byte to the specified register
250 void FT5436_Write_Reg(uint8_t reg, int8_t reg_data)
251 {
252     I2C_start();
253     
254     //write to slave
255     I2C2TRN = 0x70;                         // Send slave address with Read/Write bit cleared
256     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
257     I2C_wait_for_idle();                    // Wait until I2C bus is idle
258     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received  
259 
260     I2C2TRN = reg;                          // Register
261     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
262     I2C_wait_for_idle();                    // Wait until I2C bus is idle
263     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received
264     
265     I2C2TRN = reg_data;                     // Register data
266     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
267     I2C_wait_for_idle();                    // Wait until I2C bus is idle
268     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received
269     
270     I2C_stop();
271     
272 }
273 
274 //Reads a byte from the specified register and stores it in reg_data_buf
275 void FT5436_Read_Reg(uint8_t reg)
276 {
277     I2C_start();
278     
279     //write to slave
280     I2C2TRN = 0x70;                         // Send slave address with Read/Write bit cleared
281     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
282     I2C_wait_for_idle();                    // Wait until I2C bus is idle
283     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received  
284 
285     I2C2TRN = reg;                          // Register
286     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
287     I2C_wait_for_idle();                    // Wait until I2C bus is idle
288     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received
289     
290     I2C_restart();
291     
292     //write to slave
293     I2C2TRN = 0x71;                         // Send slave address with Read/Write bit cleared
294     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
295     I2C_wait_for_idle();                    // Wait until I2C bus is idle
296     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received  
297 
298     I2C2CONbits.RCEN = 1;                   // Receive enable
299     while (I2C2CONbits.RCEN);               // Wait until RCEN is cleared (automatic)  
300     while (!I2C2STATbits.RBF);              // Wait until Receive Buffer is Full (RBF flag)  
301     reg_data_buf = I2C2RCV;                 // Retrieve value from I2C2RCV
302 
303     I2C_nack();
304     I2C_stop(); 
305 }
306 
307 //Reads 16 bytes from the device
308 void FT5436_Read(uint8_t num_bytes)
309 {
310     int i;
311     
312     //read at least 1 byte
313     if(!num_bytes)
314     {
315         num_bytes;
316     }
317     
318     //Send start condition
319     I2C_start();
320     
321     //write to slave
322     I2C2TRN = 0x70;                         // Send slave address with Read/Write bit cleared
323     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
324     I2C_wait_for_idle();                    // Wait until I2C bus is idle
325     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received  
326 
327     I2C2TRN = 0;                            // Device mode
328     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
329     I2C_wait_for_idle();                    // Wait until I2C bus is idle
330     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received
331     
332     I2C_restart();
333     
334     //Read from slave
335     I2C2TRN = 0x71;                         // Send slave address with Read/Write bit set
336     while (I2C2STATbits.TBF == 1);          // Wait until transmit buffer is empty
337     I2C_wait_for_idle();                    // Wait until I2C bus is idle
338     while (I2C2STATbits.ACKSTAT == 1);      // Wait until ACK is received  
339 
340     for(i=0;i<num_bytes;i++)
341     {
342         I2C2CONbits.RCEN = 1;                   // Receive enable
343         while (I2C2CONbits.RCEN);               // Wait until RCEN is cleared (automatic)  
344         while (!I2C2STATbits.RBF);              // Wait until Receive Buffer is Full (RBF flag)  
345         read_buf[i] = I2C2RCV;                 // Retrieve value from I2C2RCV
346         I2C_ack();
347     }
348     
349     I2C2CONbits.RCEN = 1;                   // Receive enable
350     while (I2C2CONbits.RCEN);               // Wait until RCEN is cleared (automatic)  
351     while (!I2C2STATbits.RBF);              // Wait until Receive Buffer is Full (RBF flag)  
352     read_buf[15] = I2C2RCV;                 // Retrieve value from I2C2RCV
353     
354     I2C_nack();
355     I2C_stop(); 
356 }
357 
358 //DRIVER FOUNDATION 
359 //Waits until the I2C peripheral is no longer doing anything  
360 void I2C_wait_for_idle(void)
361 {
362     while(I2C2CON & 0x1F); // Acknowledge sequence not in progress
363                                 // Receive sequence not in progress
364                                 // Stop condition not in progress
365                                 // Repeated Start condition not in progress
366                                 // Start condition not in progress
367     while(I2C2STATbits.TRSTAT); // Bit = 0 Master transmit is not in progress
368 }
369 
370 //Sends a start condition  
371 void I2C_start(void)
372 {
373     I2C_wait_for_idle();
374     I2C2CONbits.SEN = 1;
375     while (I2C2CONbits.SEN == 1);
376 }
377 
378 //Sends a stop condition  
379 void I2C_stop(void)
380 {
381     I2C_wait_for_idle();
382     I2C2CONbits.PEN = 1;
383 }
384 
385 //Sends a repeated start/restart condition
386 void I2C_restart(void)
387 {
388     I2C_wait_for_idle();
389     I2C2CONbits.RSEN = 1;
390     while (I2C2CONbits.RSEN == 1);
391 }
392 
393 //Sends an ACK condition
394 void I2C_ack(void)
395 {
396     I2C_wait_for_idle();
397     I2C2CONbits.ACKDT = 0; // Set hardware to send ACK bit
398     I2C2CONbits.ACKEN = 1; // Send ACK bit, will be automatically cleared by hardware when sent  
399     while(I2C2CONbits.ACKEN); // Wait until ACKEN bit is cleared, meaning ACK bit has been sent
400 }
401 
402 //Sends a NACK condition
403 void I2C_nack(void) // Acknowledge Data bit
404 {
405     I2C_wait_for_idle();
406     I2C2CONbits.ACKDT = 1; // Set hardware to send NACK bit
407     I2C2CONbits.ACKEN = 1; // Send NACK bit, will be automatically cleared by hardware when sent  
408     while(I2C2CONbits.ACKEN); // Wait until ACKEN bit is cleared, meaning NACK bit has been sent
409 }
410 
411 //Master Interrupt Handler
412 void __attribute__((vector(_I2C1_MASTER_VECTOR), interrupt(ipl4srs), nomips16)) I2C_Master_Handler()
413 {
414 
415     IFS4bits.I2C2MIF = 0;
416 }
417 
418 //Slave Interrupt Handler
419 void __attribute__((vector(_I2C2_SLAVE_VECTOR), interrupt(ipl4srs), nomips16)) I2C_Slave_Handler()
420 {
421     
422     IFS0bits.IC2IF = 0;
423 }