STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
ft6x06.c
Go to the documentation of this file.
1 
39 /* Includes ------------------------------------------------------------------*/
40 #include "ft6x06.h"
41 
54 /* Private typedef -----------------------------------------------------------*/
55 
59 #define FT6x06_MAX_INSTANCE 2
60 
64 /* Private macro -------------------------------------------------------------*/
65 
70 /* Touch screen driver structure initialization */
72 {
76 
80 
85 
86 };
87 
88 /* ft6x06 instances by address */
89 uint8_t ft6x06[FT6x06_MAX_INSTANCE] = {0};
90 
91 /* Global ft6x06 handle */
92 static ft6x06_handle_TypeDef ft6x06_handle = { FT6206_I2C_NOT_INITIALIZED, 0, 0};
93 
101 static uint8_t ft6x06_GetInstance(uint16_t DeviceAddr);
102 /* Private functions prototypes-----------------------------------------------*/
103 #if (TS_AUTO_CALIBRATION_SUPPORTED == 1)
104 
109 static uint32_t ft6x06_TS_Calibration(uint16_t DeviceAddr);
110 #endif /* TS_AUTO_CALIBRATION_SUPPORTED == 1 */
111 
117 static uint32_t ft6x06_TS_Configure(uint16_t DeviceAddr);
118 
133 void ft6x06_Init(uint16_t DeviceAddr)
134 {
135  uint8_t instance;
136  uint8_t empty;
137 
138  /* Check if device instance already exists */
139  instance = ft6x06_GetInstance(DeviceAddr);
140 
141  /* To prevent double initialization */
142  if(instance == 0xFF)
143  {
144  /* Look for empty instance */
145  empty = ft6x06_GetInstance(0);
146 
147  if(empty < FT6x06_MAX_INSTANCE)
148  {
149  /* Register the current device instance */
150  ft6x06[empty] = DeviceAddr;
151 
152  /* Initialize IO BUS layer */
153  TS_IO_Init();
154  }
155  }
156 }
157 
164 void ft6x06_Reset(uint16_t DeviceAddr)
165 {
166  /* Do nothing */
167  /* No software reset sequence available in FT6206 IC */
168 }
169 
176 uint16_t ft6x06_ReadID(uint16_t DeviceAddr)
177 {
178  /* Initialize I2C link if needed */
179  TS_IO_Init();
180 
181  /* Return the device ID value */
182  return (TS_IO_Read(DeviceAddr, FT6206_CHIP_ID_REG));
183 }
184 
192 void ft6x06_TS_Start(uint16_t DeviceAddr)
193 {
194 #if (TS_AUTO_CALIBRATION_SUPPORTED == 1)
195  /* Hw Calibration sequence start : should be done once after each power up */
196  /* This is called internal calibration of the touch screen */
197  ft6x06_TS_Calibration(DeviceAddr);
198 #endif
199  /* Minimum static configuration of FT6206 */
200  ft6x06_TS_Configure(DeviceAddr);
201 
202  /* By default set FT6206 IC in Polling mode : no INT generation on FT6206 for new touch available */
203  /* Note TS_INT is active low */
204  ft6x06_TS_DisableIT(DeviceAddr);
205 }
206 
214 uint8_t ft6x06_TS_DetectTouch(uint16_t DeviceAddr)
215 {
216  volatile uint8_t nbTouch = 0;
217 
218  /* Read register FT6206_TD_STAT_REG to check number of touches detection */
219  nbTouch = TS_IO_Read(DeviceAddr, FT6206_TD_STAT_REG);
220  nbTouch &= FT6206_TD_STAT_MASK;
221 
222  if(nbTouch > FT6206_MAX_DETECTABLE_TOUCH)
223  {
224  /* If invalid number of touch detected, set it to zero */
225  nbTouch = 0;
226  }
227 
228  /* Update ft6x06 driver internal global : current number of active touches */
229  ft6x06_handle.currActiveTouchNb = nbTouch;
230 
231  /* Reset current active touch index on which to work on */
232  ft6x06_handle.currActiveTouchIdx = 0;
233 
234  return(nbTouch);
235 }
236 
246 void ft6x06_TS_GetXY(uint16_t DeviceAddr, uint16_t *X, uint16_t *Y)
247 {
248  uint8_t regAddress = 0;
249  uint8_t dataxy[4];
250 
251  if(ft6x06_handle.currActiveTouchIdx < ft6x06_handle.currActiveTouchNb)
252  {
253  switch(ft6x06_handle.currActiveTouchIdx)
254  {
255  case 0 :
256  regAddress = FT6206_P1_XH_REG;
257  break;
258  case 1 :
259  regAddress = FT6206_P2_XH_REG;
260  break;
261 
262  default :
263  break;
264  }
265 
266  /* Read X and Y positions */
267  TS_IO_ReadMultiple(DeviceAddr, regAddress, dataxy, sizeof(dataxy));
268 
269  /* Send back ready X position to caller */
270  *X = ((dataxy[0] & FT6206_MSB_MASK) << 8) | (dataxy[1] & FT6206_LSB_MASK);
271 
272  /* Send back ready Y position to caller */
273  *Y = ((dataxy[2] & FT6206_MSB_MASK) << 8) | (dataxy[3] & FT6206_LSB_MASK);
274 
275  ft6x06_handle.currActiveTouchIdx++;
276  }
277 }
278 
285 void ft6x06_TS_EnableIT(uint16_t DeviceAddr)
286 {
287  uint8_t regValue = 0;
289 
290  /* Set interrupt trigger mode in FT6206_GMODE_REG */
291  TS_IO_Write(DeviceAddr, FT6206_GMODE_REG, regValue);
292 }
293 
300 void ft6x06_TS_DisableIT(uint16_t DeviceAddr)
301 {
302  uint8_t regValue = 0;
304 
305  /* Set interrupt polling mode in FT6206_GMODE_REG */
306  TS_IO_Write(DeviceAddr, FT6206_GMODE_REG, regValue);
307 }
308 
317 uint8_t ft6x06_TS_ITStatus(uint16_t DeviceAddr)
318 {
319  /* Always return 0 as feature not applicable to FT6206 */
320  return 0;
321 }
322 
330 void ft6x06_TS_ClearIT(uint16_t DeviceAddr)
331 {
332  /* Nothing to be done here for FT6206 */
333 }
334 
335 /**** NEW FEATURES enabled when Multi-touch support is enabled ****/
336 
337 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
338 
345 void ft6x06_TS_GetGestureID(uint16_t DeviceAddr, uint32_t * pGestureId)
346 {
347  volatile uint8_t ucReadData = 0;
348 
349  ucReadData = TS_IO_Read(DeviceAddr, FT6206_GEST_ID_REG);
350 
351  * pGestureId = ucReadData;
352 }
353 
369 void ft6x06_TS_GetTouchInfo(uint16_t DeviceAddr,
370  uint32_t touchIdx,
371  uint32_t * pWeight,
372  uint32_t * pArea,
373  uint32_t * pEvent)
374 {
375  uint8_t regAddress = 0;
376  uint8_t dataxy[3];
377 
378  if(touchIdx < ft6x06_handle.currActiveTouchNb)
379  {
380  switch(touchIdx)
381  {
382  case 0 :
383  regAddress = FT6206_P1_WEIGHT_REG;
384  break;
385 
386  case 1 :
387  regAddress = FT6206_P2_WEIGHT_REG;
388  break;
389 
390  default :
391  break;
392 
393  } /* end switch(touchIdx) */
394 
395  /* Read weight, area and Event Id of touch index */
396  TS_IO_ReadMultiple(DeviceAddr, regAddress, dataxy, sizeof(dataxy));
397 
398  /* Return weight of touch index */
399  * pWeight = (dataxy[0] & FT6206_TOUCH_WEIGHT_MASK) >> FT6206_TOUCH_WEIGHT_SHIFT;
400  /* Return area of touch index */
401  * pArea = (dataxy[1] & FT6206_TOUCH_AREA_MASK) >> FT6206_TOUCH_AREA_SHIFT;
402  /* Return Event Id of touch index */
403  * pEvent = (dataxy[2] & FT6206_TOUCH_EVT_FLAG_MASK) >> FT6206_TOUCH_EVT_FLAG_SHIFT;
404 
405  } /* of if(touchIdx < ft6x06_handle.currActiveTouchNb) */
406 }
407 
408 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
409 
410 #if (TS_AUTO_CALIBRATION_SUPPORTED == 1)
411 
416 static uint32_t ft6x06_TS_Calibration(uint16_t DeviceAddr)
417 {
418  uint32_t nbAttempt = 0;
419  volatile uint8_t ucReadData;
420  volatile uint8_t regValue;
421  uint32_t status = FT6206_STATUS_OK;
422  uint8_t bEndCalibration = 0;
423 
424  /* >> Calibration sequence start */
425 
426  /* Switch FT6206 back to factory mode to calibrate */
428  TS_IO_Write(DeviceAddr, FT6206_DEV_MODE_REG, regValue); /* 0x40 */
429 
430  /* Read back the same register FT6206_DEV_MODE_REG */
431  ucReadData = TS_IO_Read(DeviceAddr, FT6206_DEV_MODE_REG);
432  TS_IO_Delay(300); /* Wait 300 ms */
433 
435  {
436  /* Return error to caller */
437  return(FT6206_STATUS_NOT_OK);
438  }
439 
440  /* Start calibration command */
441  TS_IO_Write(DeviceAddr, FT6206_TD_STAT_REG, 0x04);
442  TS_IO_Delay(300); /* Wait 300 ms */
443 
444  /* 100 attempts to wait switch from factory mode (calibration) to working mode */
445  for (nbAttempt=0; ((nbAttempt < 100) && (!bEndCalibration)) ; nbAttempt++)
446  {
447  ucReadData = TS_IO_Read(DeviceAddr, FT6206_DEV_MODE_REG);
448  ucReadData = (ucReadData & (FT6206_DEV_MODE_MASK << FT6206_DEV_MODE_SHIFT)) >> FT6206_DEV_MODE_SHIFT;
449  if(ucReadData == FT6206_DEV_MODE_WORKING)
450  {
451  /* Auto Switch to FT6206_DEV_MODE_WORKING : means calibration have ended */
452  bEndCalibration = 1; /* exit for loop */
453  }
454 
455  TS_IO_Delay(200); /* Wait 200 ms */
456  }
457 
458  /* Calibration sequence end << */
459 
460  return(status);
461 }
462 #endif /* TS_AUTO_CALIBRATION_SUPPORTED == 1 */
463 
469 static uint32_t ft6x06_TS_Configure(uint16_t DeviceAddr)
470 {
471  uint32_t status = FT6206_STATUS_OK;
472 
473  /* Nothing special to be done for FT6206 */
474 
475  return(status);
476 }
477 
484 static uint8_t ft6x06_GetInstance(uint16_t DeviceAddr)
485 {
486  uint8_t idx = 0;
487 
488  /* Check all the registered instances */
489  for(idx = 0; idx < FT6x06_MAX_INSTANCE ; idx ++)
490  {
491  if(ft6x06[idx] == DeviceAddr)
492  {
493  return idx;
494  }
495  }
496 
497  return 0xFF;
498 }
499 
516 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define FT6206_DEV_MODE_WORKING
Definition: ft6x06.h:124
#define FT6206_P1_XH_REG
Definition: ft6x06.h:165
#define FT6206_TOUCH_EVT_FLAG_SHIFT
Definition: ft6x06.h:155
#define FT6206_P2_XH_REG
Definition: ft6x06.h:184
#define FT6206_TD_STAT_REG
Definition: ft6x06.h:143
uint32_t idx
Definition: lcd_log.c:247
#define FT6206_MSB_MASK
Definition: ft6x06.h:158
void ft6x06_TS_ClearIT(uint16_t DeviceAddr)
Clear IT status in FT6206 interrupt status clear registers Should be called Following an EXTI coming ...
Definition: ft6x06.c:330
#define FT6206_P2_WEIGHT_REG
Definition: ft6x06.h:188
#define FT6206_TOUCH_AREA_SHIFT
Definition: ft6x06.h:182
#define FT6206_GMODE_REG
Definition: ft6x06.h:249
#define FT6206_G_MODE_INTERRUPT_SHIFT
Definition: ft6x06.h:252
uint8_t ft6x06_TS_DetectTouch(uint16_t DeviceAddr)
Return if there is touches detected or not. Try to detect new touches and forget the old ones (reset ...
Definition: ft6x06.c:214
#define FT6206_TD_STAT_MASK
Definition: ft6x06.h:146
uint8_t ft6x06_TS_ITStatus(uint16_t DeviceAddr)
Get IT status from FT6206 interrupt status registers Should be called Following an EXTI coming to the...
Definition: ft6x06.c:317
#define FT6206_G_MODE_INTERRUPT_MASK
Definition: ft6x06.h:251
#define FT6206_DEV_MODE_FACTORY
Definition: ft6x06.h:125
#define FT6206_G_MODE_INTERRUPT_TRIGGER
Definition: ft6x06.h:256
void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
Writes a single data.
uint8_t currActiveTouchIdx
Definition: ft6x06.h:73
#define FT6206_P1_WEIGHT_REG
Definition: ft6x06.h:171
#define FT6206_DEV_MODE_MASK
Definition: ft6x06.h:127
#define FT6206_GEST_ID_REG
Definition: ft6x06.h:131
uint8_t currActiveTouchNb
Definition: ft6x06.h:70
#define FT6206_STATUS_NOT_OK
Definition: ft6x06.h:107
This file contains all the functions prototypes for the ft6x06.c IO expander driver.
#define FT6206_STATUS_OK
Definition: ft6x06.h:106
void ft6x06_TS_DisableIT(uint16_t DeviceAddr)
Configure the FT6206 device to stop generating IT on the given INT pin connected to MCU as EXTI...
Definition: ft6x06.c:300
#define FT6206_TOUCH_EVT_FLAG_MASK
Definition: ft6x06.h:156
#define FT6206_I2C_NOT_INITIALIZED
Definition: ft6x06.h:110
void ft6x06_TS_EnableIT(uint16_t DeviceAddr)
Configure the FT6206 device to generate IT on given INT pin connected to MCU as EXTI.
Definition: ft6x06.c:285
uint8_t ft6x06[FT6x06_MAX_INSTANCE]
Definition: ft6x06.c:89
void ft6x06_Init(uint16_t DeviceAddr)
Initialize the ft6x06 communication bus from MCU to FT6206 : ie I2C channel initialization (if requir...
Definition: ft6x06.c:133
#define FT6x06_MAX_INSTANCE
Definition: ft6x06.c:59
uint16_t ft6x06_ReadID(uint16_t DeviceAddr)
Read the ft6x06 device ID, pre initialize I2C in case of need to be able to read the FT6206 device ID...
Definition: ft6x06.c:176
uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg)
Reads a single data.
#define FT6206_G_MODE_INTERRUPT_POLLING
Definition: ft6x06.h:255
#define FT6206_CHIP_ID_REG
Definition: ft6x06.h:265
void ft6x06_TS_GetXY(uint16_t DeviceAddr, uint16_t *X, uint16_t *Y)
Get the touch screen X and Y positions values Manage multi touch thanks to touch Index global variabl...
Definition: ft6x06.c:246
#define FT6206_TOUCH_WEIGHT_SHIFT
Definition: ft6x06.h:175
uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
Reads multiple data with I2C communication channel from TouchScreen.
void TS_IO_Delay(uint32_t Delay)
Delay function used in TouchScreen low level driver.
#define FT6206_DEV_MODE_SHIFT
Definition: ft6x06.h:128
void ft6x06_TS_Start(uint16_t DeviceAddr)
Configures the touch Screen IC device to start detecting touches It goes through an internal calibrat...
Definition: ft6x06.c:192
#define FT6206_TOUCH_WEIGHT_MASK
Definition: ft6x06.h:174
TS_DrvTypeDef ft6x06_ts_drv
Definition: ft6x06.c:71
#define FT6206_TOUCH_AREA_MASK
Definition: ft6x06.h:181
void ft6x06_Reset(uint16_t DeviceAddr)
Software Reset the ft6x06.
Definition: ft6x06.c:164
#define FT6206_MAX_DETECTABLE_TOUCH
Definition: ft6x06.h:114
void TS_IO_Init(void)
Initializes Touchscreen low level.
#define FT6206_LSB_MASK
Definition: ft6x06.h:162
#define FT6206_DEV_MODE_REG
: Definitions for FT6206 I2C register addresses on 8 bit
Definition: ft6x06.h:121