ST / BSP_DISCO_L4R9I

Dependents:   DISCO_L4R9I-LCD-demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ft3x67.c Source File

ft3x67.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ft3x67.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of functions needed to manage the FT3X67
00006   *          touch screen devices.
00007   ******************************************************************************
00008   * @attention
00009   *
00010   * <h2><center>&copy; Copyright (c) 2017 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 "ft3x67.h"
00023 
00024 /** @addtogroup BSP
00025   * @{
00026   */
00027 
00028 /** @addtogroup Component
00029   * @{
00030   */
00031 
00032 /** @addtogroup FT3X67
00033   * @{
00034   */
00035 
00036 /* Private typedef -----------------------------------------------------------*/
00037 
00038 /** @defgroup FT3X67_Private_Types_Definitions
00039   * @{
00040   */
00041 
00042 /* ft3x67 Handle definition. */
00043 typedef struct
00044 {
00045   uint8_t i2cInitialized;
00046 
00047   /* field holding the current number of simultaneous active touches */
00048   uint8_t currActiveTouchNb;
00049 
00050   /* field holding the touch index currently managed */
00051   uint8_t currActiveTouchIdx;
00052 
00053 } ft3x67_handle_TypeDef;
00054 
00055 /**
00056   * @}
00057   */
00058 
00059 /* Private define ------------------------------------------------------------*/
00060 /* Private macro -------------------------------------------------------------*/
00061 /* Private variables ---------------------------------------------------------*/
00062 
00063 /** @defgroup FT3X67_Private_Variables
00064   * @{
00065   */
00066 
00067 /* Touch screen driver structure initialization */
00068 TS_DrvTypeDef ft3x67_ts_drv =
00069 {
00070   ft3x67_Init,
00071   ft3x67_ReadID,
00072   ft3x67_Reset,
00073   ft3x67_TS_Start,
00074   ft3x67_TS_DetectTouch,
00075   ft3x67_TS_GetXY,
00076   ft3x67_TS_EnableIT,
00077   ft3x67_TS_ClearIT,
00078   ft3x67_TS_ITStatus,
00079   ft3x67_TS_DisableIT
00080 };
00081 
00082 /* Global ft3x67 handle */
00083 static ft3x67_handle_TypeDef ft3x67_handle = { FT3X67_I2C_NOT_INITIALIZED, 0U, 0U};
00084 
00085 /**
00086   * @}
00087   */
00088 
00089 /* Private functions prototypes-----------------------------------------------*/
00090 
00091 /** @defgroup FT3X67_Private_Functions
00092   * @{
00093   */
00094 
00095 static uint8_t  ft3x67_Get_I2C_InitializedStatus(void);
00096 static void     ft3x67_I2C_InitializeIfRequired(void);
00097 static uint32_t ft3x67_TS_Configure(uint16_t DeviceAddr);
00098 
00099 /**
00100   * @}
00101   */
00102 
00103 /* Exported functions --------------------------------------------------------*/
00104 
00105 /** @addtogroup FT3X67_Exported_Functions
00106   * @{
00107   */
00108 
00109 /**
00110   * @brief  Initialize the ft3x67 communication bus
00111   *         from MCU to FT3X67 : ie I2C channel initialization (if required).
00112   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT3X67).
00113   * @retval None
00114   */
00115 void ft3x67_Init(uint16_t DeviceAddr)
00116 {
00117   /* Initialize I2C link if needed */
00118   ft3x67_I2C_InitializeIfRequired();
00119 }
00120 
00121 /**
00122   * @brief  Software Reset the ft3x67.
00123   *         @note : Not applicable to FT3X67.
00124   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT3X67).
00125   * @retval None
00126   */
00127 void ft3x67_Reset(uint16_t DeviceAddr)
00128 {
00129   /* Do nothing */
00130   /* No software reset sequence available in FT3X67 IC */
00131 }
00132 
00133 /**
00134   * @brief  Read the ft3x67 device ID, pre initialize I2C in case of need to be
00135   *         able to read the FT3X67 device ID, and verify this is a FT3X67.
00136   * @param  DeviceAddr: I2C FT3X67 Slave address.
00137   * @retval The Device ID (two bytes).
00138   */
00139 uint16_t ft3x67_ReadID(uint16_t DeviceAddr)
00140 {
00141   /* Initialize I2C link if needed */
00142   ft3x67_I2C_InitializeIfRequired();
00143   
00144   /* Return the device ID value */
00145   return(TS_IO_Read(DeviceAddr, FT3X67_CHIP_ID_REG));
00146 }
00147 
00148 /**
00149   * @brief  Configures the touch Screen IC device to start detecting touches
00150   * @param  DeviceAddr: Device address on communication Bus (I2C slave address).
00151   * @retval None.
00152   */
00153 void ft3x67_TS_Start(uint16_t DeviceAddr)
00154 {
00155   /* Minimum static configuration of FT3X67 */
00156   ft3x67_TS_Configure(DeviceAddr);
00157   
00158   /* By default set FT3X67 IC in Polling mode : no INT generation on FT3X67 for new touch available */
00159   /* Note TS_INT is active low                                                                      */
00160   ft3x67_TS_DisableIT(DeviceAddr);
00161 }
00162 
00163 /**
00164   * @brief  Return if there is touches detected or not.
00165   *         Try to detect new touches and forget the old ones (reset internal global
00166   *         variables).
00167   * @param  DeviceAddr: Device address on communication Bus.
00168   * @retval : Number of active touches detected (can be 0, 1 or 2).
00169   */
00170 uint8_t ft3x67_TS_DetectTouch(uint16_t DeviceAddr)
00171 {
00172   volatile uint8_t nbTouch = 0U;
00173   
00174   /* Read register FT3X67_TD_STAT_REG to check number of touches detection */
00175   nbTouch = TS_IO_Read(DeviceAddr, FT3X67_TD_STAT_REG);
00176   nbTouch &= FT3X67_TD_STAT_MASK;
00177   
00178   if(nbTouch > FT3X67_MAX_DETECTABLE_TOUCH)
00179   {
00180     /* If invalid number of touch detected, set it to zero */
00181     nbTouch = 0U;
00182   }
00183   
00184   /* Update ft3x67 driver internal global : current number of active touches */
00185   ft3x67_handle.currActiveTouchNb = nbTouch;
00186   
00187   /* Reset current active touch index on which to work on */
00188   ft3x67_handle.currActiveTouchIdx = 0U;
00189   
00190   return(nbTouch);
00191 }
00192 
00193 /**
00194   * @brief  Get the touch screen X and Y positions values
00195   *         Manage multi touch thanks to touch Index global
00196   *         variable 'ft3x67_handle.currActiveTouchIdx'.
00197   * @param  DeviceAddr: Device address on communication Bus.
00198   * @param  X: Pointer to X position value
00199   * @param  Y: Pointer to Y position value
00200   * @retval None.
00201   */
00202 void ft3x67_TS_GetXY(uint16_t DeviceAddr, uint16_t *X, uint16_t *Y)
00203 {
00204   uint8_t regAddress = 0U;
00205   uint8_t dataxy[4U];
00206   
00207   if(ft3x67_handle.currActiveTouchIdx < ft3x67_handle.currActiveTouchNb)
00208   {
00209     switch(ft3x67_handle.currActiveTouchIdx)
00210     {
00211     case 0U :
00212       regAddress = FT3X67_P1_XH_REG;
00213       break;
00214     
00215     case 1U :
00216       regAddress = FT3X67_P2_XH_REG;
00217       break;
00218     
00219     default :
00220       break;
00221     } /* end switch(ft3x67_handle.currActiveTouchIdx) */
00222     
00223     /* Read X and Y positions */
00224     TS_IO_ReadMultiple(DeviceAddr, regAddress, dataxy, sizeof(dataxy)); 
00225     
00226     /* Send back ready X position to caller */
00227     *X = ((dataxy[0U] & FT3X67_TOUCH_POS_MSB_MASK) << 8U) | dataxy[1U];
00228     
00229     /* Send back ready Y position to caller */
00230     *Y = ((dataxy[2U] & FT3X67_TOUCH_POS_MSB_MASK) << 8U) | dataxy[3U];
00231     
00232     /* Increment current touch index */
00233     ft3x67_handle.currActiveTouchIdx++;
00234   }
00235 }
00236 
00237 /**
00238   * @brief  Configure the FT3X67 device to generate IT on given INT pin
00239   *         connected to MCU as EXTI.
00240   * @param  DeviceAddr: Device address on communication Bus (Slave I2C address of FT3X67).
00241   * @retval None
00242   */
00243 void ft3x67_TS_EnableIT(uint16_t DeviceAddr)
00244 {
00245   /* Set interrupt trigger mode in FT3X67_GMODE_REG */
00246   TS_IO_Write(DeviceAddr, FT3X67_GMODE_REG, FT3X67_G_MODE_INTERRUPT_TRIGGER);
00247 }
00248 
00249 /**
00250   * @brief  Configure the FT3X67 device to stop generating IT on the given INT pin
00251   *         connected to MCU as EXTI.
00252   * @param  DeviceAddr: Device address on communication Bus (Slave I2C address of FT3X67).
00253   * @retval None
00254   */
00255 void ft3x67_TS_DisableIT(uint16_t DeviceAddr)
00256 {
00257   /* Set interrupt polling mode in FT3X67_GMODE_REG */
00258   TS_IO_Write(DeviceAddr, FT3X67_GMODE_REG, FT3X67_G_MODE_INTERRUPT_POLLING);
00259 }
00260 
00261 /**
00262   * @brief  Get IT status from FT3X67 interrupt status registers
00263   *         Should be called Following an EXTI coming to the MCU to know the detailed
00264   *         reason of the interrupt.
00265   *         @note : This feature is not applicable to FT3X67.
00266   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT3X67).
00267   * @retval TS interrupts status : always return 0 here
00268   */
00269 uint8_t ft3x67_TS_ITStatus(uint16_t DeviceAddr)
00270 {
00271   /* Always return 0 as feature not applicable to FT3X67 */
00272   return 0U;
00273 }
00274 
00275 /**
00276   * @brief  Clear IT status in FT3X67 interrupt status clear registers
00277   *         Should be called Following an EXTI coming to the MCU.
00278   *         @note : This feature is not applicable to FT3X67.
00279   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT3X67).
00280   * @retval None
00281   */
00282 void ft3x67_TS_ClearIT(uint16_t DeviceAddr)
00283 {
00284   /* Nothing to be done here for FT3X67 */
00285 }
00286 
00287 /**
00288   * @brief  Configure gesture feature (enable/disable).
00289   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT3X67).
00290   * @param  Activation : Enable or disable gesture feature. Possible values are
00291   *         FT3X67_GESTURE_DISABLE or FT3X67_GESTURE_ENABLE.
00292   * @retval None.
00293   */
00294 void ft3x67_TS_GestureConfig(uint16_t DeviceAddr, uint32_t Activation)
00295 {
00296   if(Activation == FT3X67_GESTURE_ENABLE)
00297   {
00298     /* Enable gesture feature. */
00299     TS_IO_Write(DeviceAddr, FT3X67_GESTURE_FLAG_REG, FT3X67_GEST_ALL_FLAGS_ENABLE);
00300     TS_IO_Write(DeviceAddr, FT3X67_GESTURE_ENABLE_REG, FT3X67_GESTURE_ENABLE);
00301   }
00302   else
00303   {
00304     /* Disable gesture feature. */
00305     TS_IO_Write(DeviceAddr, FT3X67_GESTURE_FLAG_REG, FT3X67_GEST_ALL_FLAGS_DISABLE);
00306     TS_IO_Write(DeviceAddr, FT3X67_GESTURE_ENABLE_REG, FT3X67_GESTURE_DISABLE);
00307   }
00308 }
00309 
00310 /**
00311   * @brief  Get the last touch gesture identification (zoom, move up/down...).
00312   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT3X67).
00313   * @param  pGestureId : Pointer to get last touch gesture Identification.
00314   * @retval None.
00315   */
00316 void ft3x67_TS_GetGestureID(uint16_t DeviceAddr, uint32_t * pGestureId)
00317 {
00318   volatile uint8_t ucReadData = 0U;
00319   
00320   ucReadData = TS_IO_Read(DeviceAddr, FT3X67_GEST_ID_REG);
00321   
00322   *pGestureId = ucReadData;
00323 }
00324 
00325 /**
00326   * @brief  Get the touch detailed informations on touch number 'touchIdx' (0..1)
00327   *         This touch detailed information contains :
00328   *         - weight that was applied to this touch
00329   *         - sub-area of the touch in the touch panel
00330   *         - event of linked to the touch (press down, lift up, ...)
00331   * @param  DeviceAddr: Device address on communication Bus (I2C slave address of FT3X67).
00332   * @param  touchIdx : Passed index of the touch (0..1) on which we want to get the
00333   *                    detailed information.
00334   * @param  pWeight : Pointer to to get the weight information of 'touchIdx'.
00335   * @param  pArea   : Pointer to to get the sub-area information of 'touchIdx'.
00336   * @param  pEvent  : Pointer to to get the event information of 'touchIdx'.
00337   * @note   Area and Weight features are not supported by FT3X67. Return always 0 value.
00338   * @retval None.
00339   */
00340 void ft3x67_TS_GetTouchInfo(uint16_t   DeviceAddr,
00341                             uint32_t   touchIdx,
00342                             uint32_t * pWeight,
00343                             uint32_t * pArea,
00344                             uint32_t * pEvent)
00345 {
00346   volatile uint8_t ucReadData = 0U;
00347   uint8_t regAddressXHigh     = 0U;
00348   
00349   if(touchIdx < ft3x67_handle.currActiveTouchNb)
00350   {
00351     switch(touchIdx)
00352     {
00353     case 0U :
00354       regAddressXHigh   = FT3X67_P1_XH_REG;
00355       break;
00356     
00357     case 1U :
00358       regAddressXHigh   = FT3X67_P2_XH_REG;
00359       break;
00360     default :
00361       break;
00362     } /* end switch(touchIdx) */
00363     
00364     /* Read Event Id of touch index */
00365     ucReadData = TS_IO_Read(DeviceAddr, regAddressXHigh);
00366     *pEvent = (ucReadData & FT3X67_TOUCH_EVT_FLAG_MASK) >> FT3X67_TOUCH_EVT_FLAG_SHIFT;
00367     
00368     /* Weight and area of touch index not supported by FT3X67 */
00369     *pWeight = 0;
00370     *pArea   = 0;
00371   } /* of if(touchIdx < ft3x67_handle.currActiveTouchNb) */
00372 }
00373 
00374 /**
00375   * @}
00376   */
00377 
00378 /* Private functions bodies---------------------------------------------------*/
00379 
00380 /** @addtogroup FT3X67_Private_Functions
00381   * @{
00382   */
00383 
00384 /**
00385   * @brief  Return the status of I2C was initialized or not.
00386   * @param  None.
00387   * @retval : I2C initialization status.
00388   */
00389 static uint8_t ft3x67_Get_I2C_InitializedStatus(void)
00390 {
00391   return(ft3x67_handle.i2cInitialized);
00392 }
00393 
00394 /**
00395   * @brief  I2C initialize if needed.
00396   * @param  None.
00397   * @retval : None.
00398   */
00399 static void ft3x67_I2C_InitializeIfRequired(void)
00400 {
00401   if(ft3x67_Get_I2C_InitializedStatus() == FT3X67_I2C_NOT_INITIALIZED)
00402   {
00403     /* Initialize TS IO BUS layer (I2C) */
00404     TS_IO_Init();
00405     
00406     /* Set state to initialized */
00407     ft3x67_handle.i2cInitialized = FT3X67_I2C_INITIALIZED;
00408   }
00409 }
00410 
00411 /**
00412   * @brief  Basic static configuration of TouchScreen
00413   * @param  DeviceAddr: FT3X67 Device address for communication on I2C Bus.
00414   * @retval Status FT3X67_STATUS_OK or FT3X67_STATUS_NOT_OK.
00415   */
00416 static uint32_t ft3x67_TS_Configure(uint16_t DeviceAddr)
00417 {
00418   uint32_t status = FT3X67_STATUS_OK;
00419   
00420   /* Disable gesture feature */
00421   TS_IO_Write(DeviceAddr, FT3X67_GESTURE_ENABLE_REG, FT3X67_GESTURE_DISABLE);
00422   
00423   return(status);
00424 }
00425 
00426 /**
00427   * @}
00428   */
00429 
00430 /**
00431   * @}
00432   */
00433 
00434 /**
00435   * @}
00436   */
00437 
00438 /**
00439   * @}
00440   */
00441 
00442 
00443 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/