STM32769I_EVAL BSP User Manual: stm32f769i_eval_ts.c Source File

STM32769I EVAL BSP Drivers

stm32f769i_eval_ts.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f769i_eval_ts.c
00004   * @author  MCD Application Team
00005   * @version V2.0.0
00006   * @date    30-December-2016
00007   * @brief   This file provides a set of functions needed to manage the Touch
00008   *          Screen on STM32F769I-EVAL evaluation board.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013   *
00014   * Redistribution and use in source and binary forms, with or without modification,
00015   * are permitted provided that the following conditions are met:
00016   *   1. Redistributions of source code must retain the above copyright notice,
00017   *      this list of conditions and the following disclaimer.
00018   *   2. Redistributions in binary form must reproduce the above copyright notice,
00019   *      this list of conditions and the following disclaimer in the documentation
00020   *      and/or other materials provided with the distribution.
00021   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022   *      may be used to endorse or promote products derived from this software
00023   *      without specific prior written permission.
00024   *
00025   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035   *
00036   ******************************************************************************
00037   */
00038 
00039 /* File Info : -----------------------------------------------------------------
00040                                    User NOTES
00041 1. How To use this driver:
00042 --------------------------
00043    - This driver is used to drive the touch screen module of the STM32F769I-EVAL
00044      evaluation board on the K.O.D Optica Technology 480x800 TFT-LCD mounted on
00045      MB1166 daughter board. The touch screen driver IC inside the K.O.D module KM-040TMP-02
00046      is a FT6206 by Focal Tech.
00047 
00048 2. Driver description:
00049 ---------------------
00050   + Initialization steps:
00051      o Initialize the TS module using the BSP_TS_Init() function. This
00052        function includes the MSP layer hardware resources initialization and the
00053        communication layer configuration to start the TS use. The LCD size properties
00054        (x and y) are passed as parameters.
00055      o If TS interrupt mode is desired, you must configure the TS interrupt mode
00056        by calling the function BSP_TS_ITConfig(). The TS interrupt mode is generated
00057        as an external interrupt whenever a touch is detected.
00058        The interrupt mode internally uses the IO functionalities driver driven by
00059        the IO expander, to configure the IT line.
00060 
00061   + Touch screen use
00062      o The touch screen state is captured whenever the function BSP_TS_GetState() is
00063        used. This function returns information about the last LCD touch occurred
00064        in the TS_StateTypeDef structure.
00065      o If TS interrupt mode is used, the function BSP_TS_ITGetStatus() is needed to get
00066        the interrupt status. To clear the IT pending bits, you should call the
00067        function BSP_TS_ITClear().
00068      o The IT is handled using the corresponding external interrupt IRQ handler,
00069        the user IT callback treatment is implemented on the same external interrupt
00070        callback.
00071 
00072 ------------------------------------------------------------------------------*/
00073 
00074 /* Includes ------------------------------------------------------------------*/
00075 #include "stm32f769i_eval.h"
00076 #include "stm32f769i_eval_ts.h"
00077 
00078 /** @addtogroup BSP
00079   * @{
00080   */
00081 
00082 /** @addtogroup STM32F769I_EVAL
00083   * @{
00084   */
00085 
00086 /** @defgroup STM32F769I_EVAL_TS STM32F769I_EVAL TS
00087   * @{
00088   */
00089 
00090 /** @defgroup STM32F769I_EVAL_TS_Private_Types_Definitions TS Private Types Definitions
00091   * @{
00092   */
00093 /**
00094   * @}
00095   */
00096 
00097 /** @defgroup STM32F769I_EVAL_TS_Private_Defines TS Private Types Defines
00098   * @{
00099   */
00100 /**
00101   * @}
00102   */
00103 
00104 /** @defgroup STM32F769I_EVAL_TS_Private_Macros TS Private Macros
00105   * @{
00106   */
00107 /**
00108   * @}
00109   */
00110 
00111 /** @defgroup STM32F769I_EVAL_TS_Imported_Variables TS Imported Variables
00112   * @{
00113   */
00114   /**
00115     * @}
00116     */
00117 
00118 /** @defgroup STM32F769I_EVAL_TS_Private_Variables TS Private Variables
00119   * @{
00120   */
00121 static TS_DrvTypeDef *ts_driver;
00122 static uint8_t  ts_orientation;
00123 uint8_t  I2C_Address = 0;
00124 
00125 /* Table for touchscreen event information display on LCD : table indexed on enum @ref TS_TouchEventTypeDef information */
00126 char * ts_event_string_tab[TOUCH_EVENT_NB_MAX] = { "None",
00127                                                    "Press down",
00128                                                    "Lift up",
00129                                                    "Contact"
00130                                                   };
00131 
00132 /* Table for touchscreen gesture Id information display on LCD : table indexed on enum @ref TS_GestureIdTypeDef information */
00133 char * ts_gesture_id_string_tab[GEST_ID_NB_MAX] = { "None",
00134                                                     "Move Up",
00135                                                     "Move Right",
00136                                                     "Move Down",
00137                                                     "Move Left",
00138                                                     "Zoom In",
00139                                                     "Zoom Out"
00140                                                   };
00141 
00142 /**
00143   * @}
00144   */
00145 
00146 /** @defgroup STM32F769I_EVAL_TS_Private_Function_Prototypes TS Private Function Prototypes
00147   * @{
00148   */
00149 
00150 /**
00151   * @}
00152   */
00153 
00154 /** @defgroup STM32F769I_EVAL_TS_Public_Functions TS Public Functions
00155   * @{
00156   */
00157 
00158 /**
00159   * @brief  Initializes and configures the touch screen functionalities and
00160   *         configures all necessary hardware resources (GPIOs, I2C, clocks..).
00161   * @param  ts_SizeX : Maximum X size of the TS area on LCD
00162   * @param  ts_SizeY : Maximum Y size of the TS area on LCD
00163   * @retval TS_OK if all initializations are OK. Other value if error.
00164   */
00165 uint8_t BSP_TS_Init(uint16_t ts_SizeX, uint16_t ts_SizeY)
00166 {
00167   uint8_t ts_status = TS_OK;
00168   uint8_t ts_id1, ts_id2 = 0;
00169   /* Note : I2C_Address is un-initialized here, but is not used at all in init function */
00170   /* but the prototype of Init() is like that in template and should be respected       */
00171 
00172   /* Initialize the communication channel to sensor (I2C) if necessary */
00173   /* that is initialization is done only once after a power up         */
00174   ft6x06_ts_drv.Init(I2C_Address);
00175 
00176   ts_id1 = ft6x06_ts_drv.ReadID(TS_I2C_ADDRESS);
00177   if(ts_id1 != FT6206_ID_VALUE)
00178   {
00179     ts_id2 = ft6x06_ts_drv.ReadID(TS_I2C_ADDRESS_A02);
00180     I2C_Address    = TS_I2C_ADDRESS_A02;    
00181   }
00182   else
00183   {
00184     I2C_Address    = TS_I2C_ADDRESS;    
00185   }
00186   
00187   /* Scan FT6xx6 TouchScreen IC controller ID register by I2C Read       */
00188   /* Verify this is a FT6206 or FT6336G, otherwise this is an error case */
00189   if((ts_id1 == FT6206_ID_VALUE) || (ts_id2 == FT6206_ID_VALUE))
00190   {
00191     /* Found FT6206 : Initialize the TS driver structure */
00192     ts_driver = &ft6x06_ts_drv;
00193 
00194     /* Get LCD chosen orientation */
00195     if(ts_SizeX < ts_SizeY)
00196     {
00197       ts_orientation = TS_SWAP_NONE;                
00198     }
00199     else
00200     {
00201       ts_orientation = TS_SWAP_XY | TS_SWAP_Y;                 
00202     }
00203 
00204     if(ts_status == TS_OK)
00205     {
00206       /* Software reset the TouchScreen */
00207       ts_driver->Reset(I2C_Address);
00208 
00209       /* Calibrate, Configure and Start the TouchScreen driver */
00210       ts_driver->Start(I2C_Address);
00211 
00212     } /* of if(ts_status == TS_OK) */
00213   }
00214   else
00215   {
00216     ts_status = TS_DEVICE_NOT_FOUND;
00217   }
00218 
00219   return (ts_status);
00220 }
00221 
00222 /**
00223   * @brief  Configures and enables the touch screen interrupts.
00224   * @retval TS_OK if all initializations are OK. Other value if error.
00225   */
00226 uint8_t BSP_TS_ITConfig(void)
00227 {
00228   uint8_t ts_status = TS_ERROR;
00229   uint8_t io_status = IO_ERROR;
00230 
00231   /* Initialize the IO */
00232   io_status = BSP_IO_Init();
00233   if(io_status != IO_OK)
00234   {
00235     return (ts_status);
00236   }
00237 
00238   /* Configure TS IT line IO : is active low on FT6206 (see data sheet)                           */
00239   /* Configure TS_INT_PIN (MFX_IO_14) low level to generate MFX_IRQ_OUT in EXTI on MCU            */
00240   io_status = BSP_IO_ConfigPin(TS_INT_PIN, IO_MODE_IT_LOW_LEVEL_PU);
00241   if(io_status != IO_OK)
00242   {
00243     return (ts_status);
00244   }
00245 
00246   /* Enable the TS in interrupt mode */
00247   /* In that case the INT output of FT6206 when new touch is available */
00248   /* is active low and directed on MFX IO14 */
00249   ts_driver->EnableIT(I2C_Address);
00250 
00251   /* If arrived here : set good status on exit */
00252   ts_status = TS_OK;
00253 
00254   return (ts_status);
00255 }
00256 
00257 /**
00258   * @brief  Gets the touch screen interrupt status.
00259   * @retval TS_IRQ_PENDING if touchscreen IRQ is pending, TS_NO_IRQ_PENDING when no IRQ TS is pending.
00260   */
00261 uint8_t BSP_TS_ITGetStatus(void)
00262 {
00263   uint8_t itStatus = TS_NO_IRQ_PENDING; /* By default no IRQ TS pending */
00264   uint32_t mfx_irq_status = 0; /* No MFX IRQ by default */
00265 
00266   /* Check status of MFX_IO14 in particular which is the Touch Screen INT pin active low */
00267   mfx_irq_status = BSP_IO_ITGetStatus(TS_INT_PIN);
00268   if(mfx_irq_status != 0) /* Note : returned mfx_irq_status = 0x4000 == (1<<TS_INT_PIN) == (1<<14) */
00269   {
00270       /* This is Touch Screen INT case : so this is a new touch available that produced the IRQ EXTI */
00271       itStatus = TS_IRQ_PENDING;
00272   }
00273 
00274   /* Return the TS IT status */
00275   return (itStatus);
00276 }
00277 
00278 /**
00279   * @brief  Returns status and positions of the touch screen.
00280   * @param  TS_State: Pointer to touch screen current state structure
00281   * @retval TS_OK if all initializations are OK. Other value if error.
00282   */
00283 uint8_t BSP_TS_GetState(TS_StateTypeDef *TS_State)
00284 {
00285   static uint32_t _x[TS_MAX_NB_TOUCH] = {0, 0};
00286   static uint32_t _y[TS_MAX_NB_TOUCH] = {0, 0};
00287   uint8_t ts_status = TS_OK;
00288   uint16_t tmp;
00289   uint16_t Raw_x[TS_MAX_NB_TOUCH];
00290   uint16_t Raw_y[TS_MAX_NB_TOUCH];
00291   uint16_t xDiff;
00292   uint16_t yDiff;
00293   uint32_t index;
00294 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00295   uint32_t weight = 0;
00296   uint32_t area = 0;
00297   uint32_t event = 0;
00298 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00299 
00300   /* Check and update the number of touches active detected */
00301   TS_State->touchDetected = ts_driver->DetectTouch(I2C_Address);
00302   if(TS_State->touchDetected)
00303   {
00304     for(index=0; index < TS_State->touchDetected; index++)
00305     {
00306       /* Get each touch coordinates */
00307       ts_driver->GetXY(I2C_Address, &(Raw_x[index]), &(Raw_y[index]));
00308 
00309       if(ts_orientation & TS_SWAP_XY)
00310       {
00311         tmp = Raw_x[index];
00312         Raw_x[index] = Raw_y[index]; 
00313         Raw_y[index] = tmp;
00314       }
00315       
00316       if(ts_orientation & TS_SWAP_X)
00317       {
00318         Raw_x[index] = FT_6206_MAX_WIDTH - 1 - Raw_x[index];
00319       }
00320 
00321       if(ts_orientation & TS_SWAP_Y)
00322       {
00323         Raw_y[index] = FT_6206_MAX_HEIGHT - 1 - Raw_y[index];
00324       }
00325             
00326       xDiff = Raw_x[index] > _x[index]? (Raw_x[index] - _x[index]): (_x[index] - Raw_x[index]);
00327       yDiff = Raw_y[index] > _y[index]? (Raw_y[index] - _y[index]): (_y[index] - Raw_y[index]);
00328 
00329       if ((xDiff + yDiff) > 5)
00330       {
00331         _x[index] = Raw_x[index];
00332         _y[index] = Raw_y[index];
00333       }
00334 
00335 
00336       TS_State->touchX[index] = _x[index];
00337       TS_State->touchY[index] = _y[index];
00338 
00339 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00340 
00341       /* Get touch info related to the current touch */
00342       ft6x06_TS_GetTouchInfo(I2C_Address, index, &weight, &area, &event);
00343 
00344       /* Update TS_State structure */
00345       TS_State->touchWeight[index] = weight;
00346       TS_State->touchArea[index]   = area;
00347 
00348       /* Remap touch event */
00349       switch(event)
00350       {
00351         case FT6206_TOUCH_EVT_FLAG_PRESS_DOWN  :
00352           TS_State->touchEventId[index] = TOUCH_EVENT_PRESS_DOWN;
00353           break;
00354         case FT6206_TOUCH_EVT_FLAG_LIFT_UP :
00355           TS_State->touchEventId[index] = TOUCH_EVENT_LIFT_UP;
00356           break;
00357         case FT6206_TOUCH_EVT_FLAG_CONTACT :
00358           TS_State->touchEventId[index] = TOUCH_EVENT_CONTACT;
00359           break;
00360         case FT6206_TOUCH_EVT_FLAG_NO_EVENT :
00361           TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT;
00362           break;
00363         default :
00364           ts_status = TS_ERROR;
00365           break;
00366       } /* of switch(event) */
00367 
00368 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00369 
00370     } /* of for(index=0; index < TS_State->touchDetected; index++) */
00371 
00372 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00373     /* Get gesture Id */
00374     ts_status = BSP_TS_Get_GestureId(TS_State);
00375 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00376 
00377   } /* end of if(TS_State->touchDetected != 0) */
00378 
00379   return (ts_status);
00380 }
00381 
00382 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00383 /**
00384   * @brief  Update gesture Id following a touch detected.
00385   * @param  TS_State: Pointer to touch screen current state structure
00386   * @retval TS_OK if all initializations are OK. Other value if error.
00387   */
00388 uint8_t BSP_TS_Get_GestureId(TS_StateTypeDef *TS_State)
00389 {
00390   uint32_t gestureId = 0;
00391   uint8_t  ts_status = TS_OK;
00392 
00393   /* Get gesture Id */
00394   ft6x06_TS_GetGestureID(I2C_Address, &gestureId);
00395 
00396   /* Remap gesture Id to a TS_GestureIdTypeDef value */
00397   switch(gestureId)
00398   {
00399     case FT6206_GEST_ID_NO_GESTURE :
00400       TS_State->gestureId = GEST_ID_NO_GESTURE;
00401       break;
00402     case FT6206_GEST_ID_MOVE_UP :
00403       TS_State->gestureId = GEST_ID_MOVE_UP;
00404       break;
00405     case FT6206_GEST_ID_MOVE_RIGHT :
00406       TS_State->gestureId = GEST_ID_MOVE_RIGHT;
00407       break;
00408     case FT6206_GEST_ID_MOVE_DOWN :
00409       TS_State->gestureId = GEST_ID_MOVE_DOWN;
00410       break;
00411     case FT6206_GEST_ID_MOVE_LEFT :
00412       TS_State->gestureId = GEST_ID_MOVE_LEFT;
00413       break;
00414     case FT6206_GEST_ID_ZOOM_IN :
00415       TS_State->gestureId = GEST_ID_ZOOM_IN;
00416       break;
00417     case FT6206_GEST_ID_ZOOM_OUT :
00418       TS_State->gestureId = GEST_ID_ZOOM_OUT;
00419       break;
00420     default :
00421       ts_status = TS_ERROR;
00422       break;
00423   } /* of switch(gestureId) */
00424 
00425   return(ts_status);
00426 }
00427 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00428 
00429 
00430 /**
00431   * @brief  Clears all touch screen interrupts.
00432   */
00433 void BSP_TS_ITClear(void)
00434 {
00435   /* Clear TS_INT_PIN IRQ in MFX */
00436   BSP_IO_ITClearPin(TS_INT_PIN);
00437 }
00438 
00439 
00440 /** @defgroup STM32F769I_EVAL_TS_Private_Functions TS Private Functions
00441   * @{
00442   */
00443 
00444 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00445 /**
00446   * @brief  Function used to reset all touch data before a new acquisition
00447   *         of touch information.
00448   * @param  TS_State: Pointer to touch screen current state structure
00449   * @retval TS_OK if OK, TE_ERROR if problem found.
00450   */
00451 uint8_t BSP_TS_ResetTouchData(TS_StateTypeDef *TS_State)
00452 {
00453   uint8_t ts_status = TS_ERROR;
00454   uint32_t index;
00455 
00456   if (TS_State != (TS_StateTypeDef *)NULL)
00457   {
00458     TS_State->gestureId = GEST_ID_NO_GESTURE;
00459     TS_State->touchDetected = 0;
00460 
00461     for(index = 0; index < TS_MAX_NB_TOUCH; index++)
00462     {
00463       TS_State->touchX[index]       = 0;
00464       TS_State->touchY[index]       = 0;
00465       TS_State->touchArea[index]    = 0;
00466       TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT;
00467       TS_State->touchWeight[index]  = 0;
00468     }
00469 
00470     ts_status = TS_OK;
00471 
00472   } /* of if (TS_State != (TS_StateTypeDef *)NULL) */
00473 
00474   return (ts_status);
00475 }
00476 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00477 /**
00478   * @}
00479   */
00480 
00481 /**
00482   * @}
00483   */
00484 
00485 /**
00486   * @}
00487   */
00488 
00489 /**
00490   * @}
00491   */
00492 
00493 /**
00494   * @}
00495   */
00496 
00497 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Fri Dec 30 2016 20:55:35 for STM32769I_EVAL BSP User Manual by   doxygen 1.7.6.1