STM32L4R9I_EVAL BSP User Manual: stm32l4r9i_eval_dsi_ts.c Source File

STM32L4R9I_EVAL BSP

stm32l4r9i_eval_dsi_ts.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4r9i_eval_dsi_ts.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of functions needed to manage the Touch
00006   *          Screen on DSI LCD of STM32L4R9I-EVAL evaluation board.
00007   ******************************************************************************
00008   * @attention
00009   *
00010   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00011   *
00012   * Redistribution and use in source and binary forms, with or without modification,
00013   * are permitted provided that the following conditions are met:
00014   *   1. Redistributions of source code must retain the above copyright notice,
00015   *      this list of conditions and the following disclaimer.
00016   *   2. Redistributions in binary form must reproduce the above copyright notice,
00017   *      this list of conditions and the following disclaimer in the documentation
00018   *      and/or other materials provided with the distribution.
00019   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00020   *      may be used to endorse or promote products derived from this software
00021   *      without specific prior written permission.
00022   *
00023   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00026   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00027   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00029   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00031   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033   *
00034   ******************************************************************************
00035   */
00036 
00037 /* File Info : -----------------------------------------------------------------
00038                                    User NOTES
00039 1. How To use this driver:
00040 --------------------------
00041    - This driver is used to drive the touch screen module of the STM32L4R9I-EVAL
00042      evaluation board on the DSI LCD mounted on MB1314 daughter board.
00043      The touch screen driver IC is a FT3X67 type.
00044 
00045 2. Driver description:
00046 ---------------------
00047   + Initialization steps:
00048      o Initialize the TS module using the BSP_TS_Init() function. This
00049        function includes the MSP layer hardware resources initialization and the
00050        communication layer configuration to start the TS use. The LCD size properties
00051        (x and y) are passed as parameters.
00052      o If TS interrupt mode is desired, you must configure the TS interrupt mode
00053        by calling the function BSP_TS_ITConfig(). The TS interrupt mode is generated
00054        as an external interrupt whenever a touch is detected.
00055 
00056   + Touch screen use
00057      o The touch screen state is captured whenever the function BSP_TS_GetState() is
00058        used. This function returns information about the last LCD touch occurred
00059        in the TS_StateTypeDef structure.
00060      o FT3X67 doesn't support weight and area features (always 0 value returned).
00061 
00062 ------------------------------------------------------------------------------*/
00063 
00064 /* Includes ------------------------------------------------------------------*/
00065 #include "stm32l4r9i_eval_ts.h"
00066 #include "math.h"
00067 
00068 /** @addtogroup BSP
00069   * @{
00070   */
00071 
00072 /** @addtogroup STM32L4R9I_EVAL
00073   * @{
00074   */
00075 
00076 #if defined(USE_GVO_390x390)
00077 
00078 /** @addtogroup STM32L4R9I_EVAL_DSI_TS STM32L4R9I_EVAL DSI TS
00079   * @{
00080   */
00081 
00082 /** @defgroup STM32L4R9I_EVAL_DSI_TS_Private_Variables Private Variables
00083   * @{
00084   */
00085 
00086 static TS_DrvTypeDef *tsDriver;
00087 static uint8_t        I2C_Address   = 0;
00088 static uint8_t        HwRotation    = 0;
00089 
00090 /**
00091   * @}
00092   */
00093 
00094 /** @addtogroup STM32L4R9I_EVAL_DSI_TS_Exported_Functions Exported Functions
00095   * @{
00096   */
00097 
00098 /**
00099   * @brief  Initializes and configures the touch screen functionalities and
00100   *         configures all necessary hardware resources (GPIOs, I2C, clocks..).
00101   * @param  ts_SizeX : Maximum X size of the TS area on LCD
00102   * @param  ts_SizeY : Maximum Y size of the TS area on LCD
00103   * @retval TS_OK if all initializations are OK. Other value if error.
00104   */
00105 uint8_t BSP_DSI_TS_Init(uint16_t ts_SizeX, uint16_t ts_SizeY)
00106 {
00107   uint8_t  status  = TS_OK;
00108 
00109   /* Initialize the communication channel to sensor (I2C) if necessary */
00110   /* that is initialization is done only once after a power up         */
00111   ft3x67_ts_drv.Init(TS_DSI_I2C_ADDRESS);
00112 
00113   /* Scan FT3X67 TouchScreen IC controller ID register by I2C Read */
00114   /* Verify this is a FT3X67, otherwise this is an error case      */
00115   if(ft3x67_ts_drv.ReadID(TS_DSI_I2C_ADDRESS) == FT3X67_ID_VALUE)
00116   {
00117     /* Found FT3X67 : Initialize the TS driver structure */
00118     tsDriver    = &ft3x67_ts_drv;
00119     I2C_Address = TS_DSI_I2C_ADDRESS;
00120 
00121     /* Calibrate, Configure and Start the TouchScreen driver */
00122     tsDriver->Start(I2C_Address);
00123 
00124     /* Read firmware register to know if HW rotation is implemented */
00125     if(TS_IO_Read(I2C_Address, FT3X67_FIRMID_REG) != 0x01)
00126     {
00127       HwRotation = 1;
00128     }
00129   }
00130   else
00131   {
00132     status = TS_DEVICE_NOT_FOUND;
00133   }
00134   return status;
00135 }
00136 
00137 /**
00138   * @brief  DeInitializes the TouchScreen.
00139   * @retval TS state
00140   */
00141 uint8_t BSP_DSI_TS_DeInit(void)
00142 {
00143   /* Actually ts_driver does not provide a DeInit function */
00144   return TS_OK;
00145 }
00146 
00147 /**
00148   * @brief  Returns status and positions of the touch screen.
00149   * @param  TS_State: Pointer to touch screen current state structure
00150   * @retval TS_OK if OK. Other value if error.
00151   * @note   FT3X67 doesn't support weight and area features (always 0 value returned).
00152   */
00153 uint8_t BSP_DSI_TS_GetState(TS_StateTypeDef *TS_State)
00154 {
00155   static uint32_t xf[TS_MAX_NB_TOUCH] = {0, 0}; /* Final x value */
00156   static uint32_t yf[TS_MAX_NB_TOUCH] = {0, 0}; /* Final y value */
00157   uint8_t  ts_status = TS_OK;
00158   uint16_t x[TS_MAX_NB_TOUCH]; /* Initial x value */
00159   uint16_t y[TS_MAX_NB_TOUCH]; /* Initial y value */
00160   int16_t  xc, yc;   /* Coordinates transfered to center screen */
00161   int16_t  xr, yr;   /* Coordinates after rotation */
00162   uint32_t index;
00163   uint32_t weight = 0;
00164   uint32_t area   = 0;
00165   uint32_t event  = 0;
00166 
00167   /* Check and update the number of touches active detected */
00168   TS_State->touchDetected = tsDriver->DetectTouch(I2C_Address);
00169   if(TS_State->touchDetected)
00170   {
00171     for(index = 0; index < TS_State->touchDetected; index++)
00172     {
00173       /* Get each touch coordinates */
00174       tsDriver->GetXY(I2C_Address, &(x[index]), &(y[index]));
00175 
00176       if(HwRotation == 0)
00177       {
00178         /* First translate coordonates to center screen */
00179         xc = x[index] - 195;
00180         yc = y[index] - 195;
00181 
00182         /* Apply rotation of 45� */
00183         xr = (int16_t) (sqrt(2) * (xc - yc) / 2);
00184         yr = (int16_t) (sqrt(2) * (xc + yc) / 2);
00185 
00186         /* Revert the initial translation */
00187         xf[index] = xr + 195;
00188         yf[index] = yr + 195;
00189 
00190         TS_State->touchX[index] = xf[index];
00191         TS_State->touchY[index] = yf[index];
00192       }
00193       else
00194       {
00195         TS_State->touchX[index] = x[index];
00196         TS_State->touchY[index] = y[index];
00197       }
00198 
00199       /* Get touch info related to the current touch */
00200       ft3x67_TS_GetTouchInfo(I2C_Address, index, &weight, &area, &event);
00201 
00202       /* Update TS_State structure */
00203       TS_State->touchWeight[index] = weight;
00204       TS_State->touchArea[index]   = area;
00205 
00206       /* Remap touch event */
00207       switch(event)
00208       {
00209         case FT3X67_TOUCH_EVT_FLAG_PRESS_DOWN  :
00210           TS_State->touchEventId[index] = TOUCH_EVENT_PRESS_DOWN;
00211           break;
00212         case FT3X67_TOUCH_EVT_FLAG_LIFT_UP :
00213           TS_State->touchEventId[index] = TOUCH_EVENT_LIFT_UP;
00214           break;
00215         case FT3X67_TOUCH_EVT_FLAG_CONTACT :
00216           TS_State->touchEventId[index] = TOUCH_EVENT_CONTACT;
00217           break;
00218         case FT3X67_TOUCH_EVT_FLAG_NO_EVENT :
00219           TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT;
00220           break;
00221         default :
00222           ts_status = TS_ERROR;
00223           break;
00224       } /* of switch(event) */
00225 
00226     } /* of for(index=0; index < TS_State->touchDetected; index++) */
00227   } /* end of if(TS_State->touchDetected != 0) */
00228 
00229   return (ts_status);
00230 }
00231 
00232 /**
00233   * @brief  Configure gesture feature.
00234   * @param  State: Enable/Disable gesture feature.
00235   * @retval TS_OK if OK. Other value if error.
00236   */
00237 uint8_t BSP_DSI_TS_GestureConfig(FunctionalState State)
00238 {
00239   uint8_t  ts_status = TS_OK;
00240   uint32_t Activation;
00241 
00242   /* Configure gesture feature */
00243   Activation = (State == ENABLE) ? FT3X67_GESTURE_ENABLE : FT3X67_GESTURE_DISABLE;
00244   ft3x67_TS_GestureConfig(I2C_Address, Activation);
00245 
00246   return(ts_status);
00247 }
00248 
00249 /**
00250   * @brief  Update gesture Id following a touch detected.
00251   * @param  TS_State: Pointer to touch screen current state structure
00252   * @retval TS_OK if OK. Other value if error.
00253   */
00254 uint8_t BSP_DSI_TS_Get_GestureId(TS_StateTypeDef *TS_State)
00255 {
00256   uint32_t gestureId = 0;
00257   uint8_t  ts_status = TS_OK;
00258 
00259   /* Get gesture Id */
00260   ft3x67_TS_GetGestureID(I2C_Address, &gestureId);
00261 
00262   /* Remap gesture Id to a TS_GestureIdTypeDef value */
00263   switch(gestureId)
00264   {
00265     case FT3X67_GEST_ID_NO_GESTURE :
00266       TS_State->gestureId = GEST_ID_NO_GESTURE;
00267       break;
00268     case FT3X67_GEST_ID_MOVE_UP :
00269       TS_State->gestureId = GEST_ID_MOVE_UP;
00270       break;
00271     case FT3X67_GEST_ID_MOVE_RIGHT :
00272       TS_State->gestureId = GEST_ID_MOVE_RIGHT;
00273       break;
00274     case FT3X67_GEST_ID_MOVE_DOWN :
00275       TS_State->gestureId = GEST_ID_MOVE_DOWN;
00276       break;
00277     case FT3X67_GEST_ID_MOVE_LEFT :
00278       TS_State->gestureId = GEST_ID_MOVE_LEFT;
00279       break;
00280     case FT3X67_GEST_ID_DOUBLE_CLICK :
00281       TS_State->gestureId = GEST_ID_DOUBLE_CLICK;
00282       break;
00283     default :
00284       ts_status = TS_ERROR;
00285       break;
00286   } /* of switch(gestureId) */
00287 
00288   return(ts_status);
00289 }
00290 
00291 /**
00292   * @brief  Function used to reset all touch data before a new acquisition
00293   *         of touch information.
00294   * @param  TS_State: Pointer to touch screen current state structure
00295   * @retval TS_OK if OK, TS_ERROR if problem found.
00296   */
00297 uint8_t BSP_DSI_TS_ResetTouchData(TS_StateTypeDef *TS_State)
00298 {
00299   uint8_t  ts_status = TS_ERROR;
00300   uint32_t index;
00301 
00302   if (TS_State != (TS_StateTypeDef *)NULL)
00303   {
00304     TS_State->gestureId     = GEST_ID_NO_GESTURE;
00305     TS_State->touchDetected = 0;
00306 
00307     for(index = 0; index < TS_MAX_NB_TOUCH; index++)
00308     {
00309       TS_State->touchX[index]       = 0;
00310       TS_State->touchY[index]       = 0;
00311       TS_State->touchArea[index]    = 0;
00312       TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT;
00313       TS_State->touchWeight[index]  = 0;
00314     }
00315 
00316     ts_status = TS_OK;
00317   } /* of if (TS_State != (TS_StateTypeDef *)NULL) */
00318 
00319   return (ts_status);
00320 }
00321 
00322 /**
00323   * @brief  Configures and enables the touch screen interrupts.
00324   * @retval TS_OK if OK. Other value if error.
00325   */
00326 uint8_t BSP_DSI_TS_ITConfig(void)
00327 {
00328   GPIO_InitTypeDef gpio_init_structure;
00329 
00330   /* Configure Interrupt mode for TS detection pin */
00331   gpio_init_structure.Pin = TS_DSI_INT_PIN;
00332   gpio_init_structure.Pull = GPIO_PULLUP;
00333   gpio_init_structure.Speed = GPIO_SPEED_FAST;
00334   gpio_init_structure.Mode = GPIO_MODE_IT_FALLING;
00335   HAL_GPIO_Init(TS_DSI_INT_GPIO_PORT, &gpio_init_structure);
00336 
00337   /* Enable and set Touch screen EXTI Interrupt to the lowest priority */
00338   HAL_NVIC_SetPriority((IRQn_Type)(TS_DSI_INT_EXTI_IRQn), 0x0F, 0x00);
00339   HAL_NVIC_EnableIRQ((IRQn_Type)(TS_DSI_INT_EXTI_IRQn));
00340 
00341   /* Enable the TS ITs */
00342   tsDriver->EnableIT(I2C_Address);
00343 
00344   return TS_OK;
00345 }
00346 
00347 /**
00348   * @brief  Clears all touch screen interrupts.
00349   */
00350 void BSP_DSI_TS_ITClear(void)
00351 {
00352   /* Empty function on component FT3X67 */
00353 }
00354 
00355 /**
00356   * @brief  Gets the touch screen interrupt status.
00357   * @retval TS_OK if OK. Other value if error.
00358   */
00359 uint8_t BSP_DSI_TS_ITGetStatus(void)
00360 {
00361   /* Return the TS_OK because feature not available on FT3X67 */
00362   return TS_OK;
00363 }
00364 
00365 /**
00366   * @}
00367   */
00368 
00369 /**
00370   * @}
00371   */
00372 
00373 #endif /* USE_GVO_390x390 */
00374 
00375 /**
00376   * @}
00377   */
00378 
00379 /**
00380   * @}
00381   */
00382 
00383 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Thu Oct 12 2017 10:53:59 for STM32L4R9I_EVAL BSP User Manual by   doxygen 1.7.6.1