ST / BSP_DISCO_L496AG

Dependents:   DISCO_L496AG-LCD-prova_1 DISCO_L496AG-LCD-prova_2 DISCO_L496AG-LCD-demo DISCO_L496AG-SRAM-demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ft6x06.c Source File

ft6x06.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ft6x06.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of functions needed to manage the FT6X06
00006   *          IO Expander devices.
00007   ******************************************************************************
00008   * @attention
00009   *
00010   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00011   * All rights reserved.</center></h2>
00012   *
00013   * This software component is licensed by ST under BSD 3-Clause license,
00014   * the "License"; You may not use this file except in compliance with the
00015   * License. You may obtain a copy of the License at:
00016   *                        opensource.org/licenses/BSD-3-Clause
00017   *
00018   ******************************************************************************
00019   */
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "ft6x06.h"
00023 
00024 /** @addtogroup BSP
00025   * @{
00026   */
00027 
00028 /** @addtogroup Component
00029   * @{
00030   */
00031 
00032 /** @defgroup FT6X06
00033   * @{
00034   */
00035 
00036 /* Private typedef -----------------------------------------------------------*/
00037 
00038 /** @defgroup FT6X06_Private_Defines FT6X06 Private Defines
00039   * @{
00040   */
00041 #define FT6x06_MAX_INSTANCE  2
00042 /**
00043   * @}
00044   */
00045 
00046 /* Private macro -------------------------------------------------------------*/
00047 
00048 /** @defgroup FT6X06_Private_Variables FT6X06 Private Variables
00049   * @{
00050   */
00051 
00052 /* Touch screen driver structure initialization */
00053 TS_DrvTypeDef ft6x06_ts_drv =
00054 {
00055   ft6x06_Init,
00056   ft6x06_ReadID,
00057   ft6x06_Reset,
00058 
00059   ft6x06_TS_Start,
00060   ft6x06_TS_DetectTouch,
00061   ft6x06_TS_GetXY,
00062 
00063   ft6x06_TS_EnableIT,
00064   ft6x06_TS_ClearIT,
00065   ft6x06_TS_ITStatus,
00066   ft6x06_TS_DisableIT
00067 };
00068 
00069 /* ft6x06 instances by address */
00070 uint8_t ft6x06[FT6x06_MAX_INSTANCE] = {0};
00071 
00072 /* Global ft6x06 handle */
00073 static ft6x06_handle_TypeDef ft6x06_handle = { FT6206_I2C_NOT_INITIALIZED, 0, 0};
00074 
00075 /**
00076   * @}
00077   */
00078 
00079 /** @defgroup ft6x06_Private_Function_Prototypes ft6x06 Private Function Prototypes
00080   * @{
00081   */
00082 static uint8_t ft6x06_GetInstance(uint16_t DeviceAddr);
00083 /* Private functions prototypes-----------------------------------------------*/
00084 #if (TS_AUTO_CALIBRATION_SUPPORTED == 1)
00085 /**
00086   * @brief  Start TouchScreen calibration phase
00087   * @param  DeviceAddr: FT6206 Device address for communication on I2C Bus.
00088   * @retval Status FT6206_STATUS_OK or FT6206_STATUS_NOT_OK.
00089   */
00090 static uint32_t ft6x06_TS_Calibration(uint16_t DeviceAddr);
00091 #endif /* TS_AUTO_CALIBRATION_SUPPORTED == 1 */
00092 
00093 /**
00094   * @brief  Basic static configuration of TouchScreen
00095   * @param  DeviceAddr: FT6206 Device address for communication on I2C Bus.
00096   * @retval Status FT6206_STATUS_OK or FT6206_STATUS_NOT_OK.
00097   */
00098 static uint32_t ft6x06_TS_Configure(uint16_t DeviceAddr);
00099 
00100 /**
00101   * @}
00102   */
00103 
00104 /** @defgroup ft6x06_Private_Functions ft6x06 Private Functions
00105   * @{
00106   */
00107 
00108 /**
00109   * @brief  Initialize the ft6x06 communication bus
00110   *         from MCU to FT6206 : ie I2C channel initialization (if required).
00111   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT6206).
00112   * @retval None
00113   */
00114 void ft6x06_Init(uint16_t DeviceAddr)
00115 {  
00116   uint8_t instance;
00117   uint8_t empty;
00118   
00119   /* Check if device instance already exists */
00120   instance = ft6x06_GetInstance(DeviceAddr);
00121   
00122   /* To prevent double initialization */
00123   if(instance == 0xFF)
00124   {
00125     /* Look for empty instance */
00126     empty = ft6x06_GetInstance(0);
00127     
00128     if(empty < FT6x06_MAX_INSTANCE)
00129     {
00130       /* Register the current device instance */
00131       ft6x06[empty] = DeviceAddr;
00132       
00133       /* Initialize IO BUS layer */
00134       TS_IO_Init(); 
00135     }
00136   }
00137 }
00138 
00139 /**
00140   * @brief  Software Reset the ft6x06.
00141   *         @note : Not applicable to FT6206.
00142   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT6206).
00143   * @retval None
00144   */
00145 void ft6x06_Reset(uint16_t DeviceAddr)
00146 {
00147   /* Do nothing */
00148   /* No software reset sequence available in FT6206 IC */
00149 }
00150 
00151 /**
00152   * @brief  Read the ft6x06 device ID, pre initialize I2C in case of need to be
00153   *         able to read the FT6206 device ID, and verify this is a FT6206.
00154   * @param  DeviceAddr: I2C FT6x06 Slave address.
00155   * @retval The Device ID (two bytes).
00156   */
00157 uint16_t ft6x06_ReadID(uint16_t DeviceAddr)
00158 {
00159   /* Initialize I2C link if needed */
00160   TS_IO_Init();
00161   
00162   /* Return the device ID value */
00163   return (TS_IO_Read(DeviceAddr, FT6206_CHIP_ID_REG));
00164 }
00165 
00166 /**
00167   * @brief  Configures the touch Screen IC device to start detecting touches
00168   *         It goes through an internal calibration process (Hw calibration sequence of
00169   *         the touch screen).
00170   * @param  DeviceAddr: Device address on communication Bus (I2C slave address).
00171   * @retval None.
00172   */
00173 void ft6x06_TS_Start(uint16_t DeviceAddr)
00174 {
00175 #if (TS_AUTO_CALIBRATION_SUPPORTED == 1)
00176   /* Hw Calibration sequence start : should be done once after each power up */
00177   /* This is called internal calibration of the touch screen                 */
00178   ft6x06_TS_Calibration(DeviceAddr);
00179 #endif
00180   /* Minimum static configuration of FT6206 */
00181   ft6x06_TS_Configure(DeviceAddr);
00182 
00183   /* By default set FT6206 IC in Polling mode : no INT generation on FT6206 for new touch available */
00184   /* Note TS_INT is active low                                                                      */
00185   ft6x06_TS_DisableIT(DeviceAddr);
00186 }
00187 
00188 /**
00189   * @brief  Return if there is touches detected or not.
00190   *         Try to detect new touches and forget the old ones (reset internal global
00191   *         variables).
00192   * @param  DeviceAddr: Device address on communication Bus.
00193   * @retval : Number of active touches detected (can be 0, 1 or 2).
00194   */
00195 uint8_t ft6x06_TS_DetectTouch(uint16_t DeviceAddr)
00196 {
00197   volatile uint8_t nbTouch = 0;
00198 
00199   /* Read register FT6206_TD_STAT_REG to check number of touches detection */
00200   nbTouch = TS_IO_Read(DeviceAddr, FT6206_TD_STAT_REG);
00201   nbTouch &= FT6206_TD_STAT_MASK;
00202 
00203   if(nbTouch > FT6206_MAX_DETECTABLE_TOUCH)
00204   {
00205     /* If invalid number of touch detected, set it to zero */
00206     nbTouch = 0;
00207   }
00208 
00209   /* Update ft6x06 driver internal global : current number of active touches */
00210   ft6x06_handle.currActiveTouchNb = nbTouch;
00211 
00212   /* Reset current active touch index on which to work on */
00213   ft6x06_handle.currActiveTouchIdx = 0;
00214 
00215   return(nbTouch);
00216 }
00217 
00218 /**
00219   * @brief  Get the touch screen X and Y positions values
00220   *         Manage multi touch thanks to touch Index global
00221   *         variable 'ft6x06_handle.currActiveTouchIdx'.
00222   * @param  DeviceAddr: Device address on communication Bus.
00223   * @param  X: Pointer to X position value
00224   * @param  Y: Pointer to Y position value
00225   * @retval None.
00226   */
00227 void ft6x06_TS_GetXY(uint16_t DeviceAddr, uint16_t *X, uint16_t *Y)
00228 {
00229   uint8_t regAddress = 0;
00230   uint8_t  dataxy[4];
00231   
00232   if(ft6x06_handle.currActiveTouchIdx < ft6x06_handle.currActiveTouchNb)
00233   {
00234     switch(ft6x06_handle.currActiveTouchIdx)
00235     {
00236     case 0 :    
00237       regAddress = FT6206_P1_XH_REG; 
00238       break;
00239     case 1 :
00240       regAddress = FT6206_P2_XH_REG; 
00241       break;
00242 
00243     default :
00244       break;
00245     }
00246     
00247     /* Read X and Y positions */
00248     TS_IO_ReadMultiple(DeviceAddr, regAddress, dataxy, sizeof(dataxy)); 
00249 
00250     /* Send back ready X position to caller */
00251     *X = ((dataxy[0] & FT6206_MSB_MASK) << 8) | (dataxy[1] & FT6206_LSB_MASK);
00252     
00253     /* Send back ready Y position to caller */
00254     *Y = ((dataxy[2] & FT6206_MSB_MASK) << 8) | (dataxy[3] & FT6206_LSB_MASK);
00255     
00256     ft6x06_handle.currActiveTouchIdx++;
00257   }
00258 }
00259 
00260 /**
00261   * @brief  Configure the FT6206 device to generate IT on given INT pin
00262   *         connected to MCU as EXTI.
00263   * @param  DeviceAddr: Device address on communication Bus (Slave I2C address of FT6206).
00264   * @retval None
00265   */
00266 void ft6x06_TS_EnableIT(uint16_t DeviceAddr)
00267 {
00268   uint8_t regValue = 0;
00269   regValue = (FT6206_G_MODE_INTERRUPT_TRIGGER & (FT6206_G_MODE_INTERRUPT_MASK >> FT6206_G_MODE_INTERRUPT_SHIFT)) << FT6206_G_MODE_INTERRUPT_SHIFT;
00270   
00271   /* Set interrupt trigger mode in FT6206_GMODE_REG */
00272   TS_IO_Write(DeviceAddr, FT6206_GMODE_REG, regValue);
00273 }
00274 
00275 /**
00276   * @brief  Configure the FT6206 device to stop generating IT on the given INT pin
00277   *         connected to MCU as EXTI.
00278   * @param  DeviceAddr: Device address on communication Bus (Slave I2C address of FT6206).
00279   * @retval None
00280   */
00281 void ft6x06_TS_DisableIT(uint16_t DeviceAddr)
00282 {
00283   uint8_t regValue = 0;
00284   regValue = (FT6206_G_MODE_INTERRUPT_POLLING & (FT6206_G_MODE_INTERRUPT_MASK >> FT6206_G_MODE_INTERRUPT_SHIFT)) << FT6206_G_MODE_INTERRUPT_SHIFT;
00285 
00286   /* Set interrupt polling mode in FT6206_GMODE_REG */
00287   TS_IO_Write(DeviceAddr, FT6206_GMODE_REG, regValue);
00288 }
00289 
00290 /**
00291   * @brief  Get IT status from FT6206 interrupt status registers
00292   *         Should be called Following an EXTI coming to the MCU to know the detailed
00293   *         reason of the interrupt.
00294   *         @note : This feature is not applicable to FT6206.
00295   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT6206).
00296   * @retval TS interrupts status : always return 0 here
00297   */
00298 uint8_t ft6x06_TS_ITStatus(uint16_t DeviceAddr)
00299 {
00300   /* Always return 0 as feature not applicable to FT6206 */
00301   return 0;
00302 }
00303 
00304 /**
00305   * @brief  Clear IT status in FT6206 interrupt status clear registers
00306   *         Should be called Following an EXTI coming to the MCU.
00307   *         @note : This feature is not applicable to FT6206.
00308   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT6206).
00309   * @retval None
00310   */
00311 void ft6x06_TS_ClearIT(uint16_t DeviceAddr)
00312 {
00313   /* Nothing to be done here for FT6206 */
00314 }
00315 
00316 /**** NEW FEATURES enabled when Multi-touch support is enabled ****/
00317 
00318 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00319 /**
00320   * @brief  Get the last touch gesture identification (zoom, move up/down...).
00321   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT6x06).
00322   * @param  pGestureId : Pointer to get last touch gesture Identification.
00323   * @retval None.
00324   */
00325 void ft6x06_TS_GetGestureID(uint16_t DeviceAddr, uint32_t * pGestureId)
00326 {
00327   volatile uint8_t ucReadData = 0;
00328 
00329   ucReadData = TS_IO_Read(DeviceAddr, FT6206_GEST_ID_REG);
00330 
00331   * pGestureId = ucReadData;
00332 }
00333 
00334 /**
00335   * @brief  Get the touch detailed informations on touch number 'touchIdx' (0..1)
00336   *         This touch detailed information contains :
00337   *         - weight that was applied to this touch
00338   *         - sub-area of the touch in the touch panel
00339   *         - event of linked to the touch (press down, lift up, ...)
00340   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT6x06).
00341   * @param  touchIdx : Passed index of the touch (0..1) on which we want to get the
00342   *                    detailed information.
00343   * @param  pWeight : Pointer to to get the weight information of 'touchIdx'.
00344   * @param  pArea   : Pointer to to get the sub-area information of 'touchIdx'.
00345   * @param  pEvent  : Pointer to to get the event information of 'touchIdx'.
00346 
00347   * @retval None.
00348   */
00349 void ft6x06_TS_GetTouchInfo(uint16_t   DeviceAddr,
00350                             uint32_t   touchIdx,
00351                             uint32_t * pWeight,
00352                             uint32_t * pArea,
00353                             uint32_t * pEvent)
00354 {
00355   uint8_t regAddress = 0;
00356   uint8_t dataxy[3];
00357   
00358   if(touchIdx < ft6x06_handle.currActiveTouchNb)
00359   {
00360     switch(touchIdx)
00361     {
00362     case 0 : 
00363       regAddress = FT6206_P1_WEIGHT_REG;
00364       break;
00365       
00366     case 1 :
00367       regAddress = FT6206_P2_WEIGHT_REG;
00368       break;
00369       
00370     default :
00371       break;
00372       
00373     } /* end switch(touchIdx) */
00374     
00375     /* Read weight, area and Event Id of touch index */
00376     TS_IO_ReadMultiple(DeviceAddr, regAddress, dataxy, sizeof(dataxy)); 
00377     
00378     /* Return weight of touch index */
00379     * pWeight = (dataxy[0] & FT6206_TOUCH_WEIGHT_MASK) >> FT6206_TOUCH_WEIGHT_SHIFT;
00380     /* Return area of touch index */
00381     * pArea = (dataxy[1] & FT6206_TOUCH_AREA_MASK) >> FT6206_TOUCH_AREA_SHIFT;
00382     /* Return Event Id  of touch index */
00383     * pEvent = (dataxy[2] & FT6206_TOUCH_EVT_FLAG_MASK) >> FT6206_TOUCH_EVT_FLAG_SHIFT;
00384     
00385   } /* of if(touchIdx < ft6x06_handle.currActiveTouchNb) */
00386 }
00387 
00388 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00389 
00390 #if (TS_AUTO_CALIBRATION_SUPPORTED == 1)
00391 /**
00392   * @brief  Start TouchScreen calibration phase
00393   * @param  DeviceAddr: FT6206 Device address for communication on I2C Bus.
00394   * @retval Status FT6206_STATUS_OK or FT6206_STATUS_NOT_OK.
00395   */
00396 static uint32_t ft6x06_TS_Calibration(uint16_t DeviceAddr)
00397 {
00398   uint32_t nbAttempt = 0;
00399   volatile uint8_t ucReadData;
00400   volatile uint8_t regValue;
00401   uint32_t status = FT6206_STATUS_OK;
00402   uint8_t bEndCalibration = 0;
00403 
00404   /* >> Calibration sequence start */
00405 
00406   /* Switch FT6206 back to factory mode to calibrate */
00407   regValue = (FT6206_DEV_MODE_FACTORY & FT6206_DEV_MODE_MASK) << FT6206_DEV_MODE_SHIFT;
00408   TS_IO_Write(DeviceAddr, FT6206_DEV_MODE_REG, regValue); /* 0x40 */
00409 
00410   /* Read back the same register FT6206_DEV_MODE_REG */
00411   ucReadData = TS_IO_Read(DeviceAddr, FT6206_DEV_MODE_REG);
00412   TS_IO_Delay(300); /* Wait 300 ms */
00413 
00414   if(((ucReadData & (FT6206_DEV_MODE_MASK << FT6206_DEV_MODE_SHIFT)) >> FT6206_DEV_MODE_SHIFT) != FT6206_DEV_MODE_FACTORY )
00415   {
00416     /* Return error to caller */
00417     return(FT6206_STATUS_NOT_OK);
00418   }
00419 
00420   /* Start calibration command */
00421   TS_IO_Write(DeviceAddr, FT6206_TD_STAT_REG, 0x04);
00422   TS_IO_Delay(300); /* Wait 300 ms */
00423 
00424   /* 100 attempts to wait switch from factory mode (calibration) to working mode */
00425   for (nbAttempt=0; ((nbAttempt < 100) && (!bEndCalibration)) ; nbAttempt++)
00426   {
00427     ucReadData = TS_IO_Read(DeviceAddr, FT6206_DEV_MODE_REG);
00428     ucReadData = (ucReadData & (FT6206_DEV_MODE_MASK << FT6206_DEV_MODE_SHIFT)) >> FT6206_DEV_MODE_SHIFT;
00429     if(ucReadData == FT6206_DEV_MODE_WORKING)
00430     {
00431       /* Auto Switch to FT6206_DEV_MODE_WORKING : means calibration have ended */
00432       bEndCalibration = 1; /* exit for loop */
00433     }
00434     
00435     TS_IO_Delay(200); /* Wait 200 ms */
00436   }
00437 
00438   /* Calibration sequence end << */
00439 
00440   return(status);
00441 }
00442 #endif /* TS_AUTO_CALIBRATION_SUPPORTED == 1 */
00443 
00444 /**
00445   * @brief  Basic static configuration of TouchScreen
00446   * @param  DeviceAddr: FT6206 Device address for communication on I2C Bus.
00447   * @retval Status FT6206_STATUS_OK or FT6206_STATUS_NOT_OK.
00448   */
00449 static uint32_t ft6x06_TS_Configure(uint16_t DeviceAddr)
00450 {
00451   uint32_t status = FT6206_STATUS_OK;
00452 
00453   /* Nothing special to be done for FT6206 */
00454 
00455   return(status);
00456 }
00457 
00458 /**
00459   * @brief  Check if the device instance of the selected address is already registered
00460   *         and return its index  
00461   * @param  DeviceAddr: Device address on communication Bus.
00462   * @retval Index of the device instance if registered, 0xFF if not.
00463   */
00464 static uint8_t ft6x06_GetInstance(uint16_t DeviceAddr)
00465 {
00466   uint8_t idx = 0;
00467   
00468   /* Check all the registered instances */
00469   for(idx = 0; idx < FT6x06_MAX_INSTANCE ; idx ++)
00470   {
00471     if(ft6x06[idx] == DeviceAddr)
00472     {
00473       return idx; 
00474     }
00475   }
00476   
00477   return 0xFF;
00478 }
00479 
00480 /**
00481   * @}
00482   */
00483 
00484 /**
00485   * @}
00486   */
00487 
00488 /**
00489   * @}
00490   */
00491 
00492 /**
00493   * @}
00494   */
00495 
00496 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/