STM32F469I-Discovery BSP User Manual: stm32469i_discovery_ts.c Source File

STM32F469I-Discovery BSP Drivers

stm32469i_discovery_ts.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32469i_discovery_ts.c
00004   * @author  MCD Application Team
00005   * @version V1.0.2
00006   * @date    13-January-2016
00007   * @brief   This file provides a set of functions needed to manage the Touch
00008   *          Screen on STM32469I-Discovery board.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT(c) 2015 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 STM32469I-Discovery
00044      board on the K.O.D Optica Technology 480x800 TFT-LCD mounted on
00045      MB1189 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 "stm32469i_discovery_ts.h"
00076 
00077 /** @addtogroup BSP
00078   * @{
00079   */
00080 
00081 /** @addtogroup STM32469I_Discovery
00082   * @{
00083   */
00084 
00085 /** @defgroup STM32469I-Discovery_TS STM32469I Discovery TS
00086   * @{
00087   */
00088 
00089 /** @defgroup STM32469I-Discovery_TS_Private_Types_Definitions STM32469I Discovery TS Private Types Definitions
00090   * @{
00091   */
00092 /**
00093   * @}
00094   */
00095 
00096 /** @defgroup STM32469I-Discovery_TS_Private_Defines STM32469I Discovery TS Private Types Defines
00097   * @{
00098   */
00099 /**
00100   * @}
00101   */
00102 
00103 /** @defgroup STM32469I-Discovery_TS_Private_Macros STM32469I Discovery TS Private Macros
00104   * @{
00105   */
00106 /**
00107   * @}
00108   */
00109 
00110 /** @defgroup STM32469I-Discovery_TS_Imported_Variables STM32469I Discovery TS Imported Variables
00111   * @{
00112   */
00113   /**
00114     * @}
00115     */
00116 
00117 /** @defgroup STM32469I-Discovery_TS_Private_Variables STM32469I Discovery TS Private Variables
00118   * @{
00119   */
00120 static TS_DrvTypeDef *ts_driver;
00121 static uint8_t  ts_orientation;
00122 static uint8_t  I2C_Address = 0;
00123 
00124 /* Table for touchscreen event information display on LCD : table indexed on enum @ref TS_TouchEventTypeDef information */
00125 char * ts_event_string_tab[TOUCH_EVENT_NB_MAX] = { "None",
00126                                                    "Press down",
00127                                                    "Lift up",
00128                                                    "Contact"
00129                                                   };
00130 
00131 /* Table for touchscreen gesture Id information display on LCD : table indexed on enum @ref TS_GestureIdTypeDef information */
00132 char * ts_gesture_id_string_tab[GEST_ID_NB_MAX] = { "None",
00133                                                     "Move Up",
00134                                                     "Move Right",
00135                                                     "Move Down",
00136                                                     "Move Left",
00137                                                     "Zoom In",
00138                                                     "Zoom Out"
00139                                                   };
00140 
00141 /**
00142   * @}
00143   */
00144 
00145 /** @defgroup STM32469I-Discovery_TS_Private_Function_Prototypes STM32469I Discovery TS Private Function Prototypes
00146   * @{
00147   */
00148 
00149 /**
00150   * @}
00151   */
00152 
00153 /** @defgroup STM32469I-Discovery_TS_Public_Functions STM32469I Discovery TS Public Functions
00154   * @{
00155   */
00156 
00157 /**
00158   * @brief  Initializes and configures the touch screen functionalities and
00159   *         configures all necessary hardware resources (GPIOs, I2C, clocks..).
00160   * @param  ts_SizeX : Maximum X size of the TS area on LCD
00161   * @param  ts_SizeY : Maximum Y size of the TS area on LCD
00162   * @retval TS_OK if all initializations are OK. Other value if error.
00163   */
00164 uint8_t BSP_TS_Init(uint16_t ts_SizeX, uint16_t ts_SizeY)
00165 {
00166   uint8_t ts_status = TS_OK;
00167 
00168   /* Note : I2C_Address is un-initialized here, but is not used at all in init function */
00169   /* but the prototype of Init() is like that in template and should be respected       */
00170 
00171   /* Initialize the communication channel to sensor (I2C) if necessary */
00172   /* that is initialization is done only once after a power up         */
00173   ft6x06_ts_drv.Init(I2C_Address);
00174 
00175   /* Scan FT6x06 TouchScreen IC controller ID register by I2C Read */
00176   /* Verify this is a FT6x06, otherwise this is an error case      */
00177   if(ft6x06_ts_drv.ReadID(TS_I2C_ADDRESS) == FT6206_ID_VALUE)
00178   {
00179     /* Found FT6206 : Initialize the TS driver structure */
00180     ts_driver = &ft6x06_ts_drv;
00181 
00182     I2C_Address    = TS_I2C_ADDRESS;
00183 
00184     /* Get LCD chosen orientation */
00185     if(ts_SizeX < ts_SizeY)
00186     {
00187       ts_orientation = TS_SWAP_NONE;                
00188     }
00189     else
00190     {
00191       ts_orientation = TS_SWAP_XY | TS_SWAP_Y;                 
00192     }
00193 
00194     if(ts_status == TS_OK)
00195     {
00196       /* Software reset the TouchScreen */
00197       ts_driver->Reset(I2C_Address);
00198 
00199       /* Calibrate, Configure and Start the TouchScreen driver */
00200       ts_driver->Start(I2C_Address);
00201 
00202     } /* of if(ts_status == TS_OK) */
00203   }
00204   else
00205   {
00206     ts_status = TS_DEVICE_NOT_FOUND;
00207   }
00208 
00209   return (ts_status);
00210 }
00211 
00212 /**
00213   * @brief  Configures and enables the touch screen interrupts both at GPIO level and
00214   * in TouchScreen IC driver configuration.
00215   * @retval TS_OK if all initializations are OK.
00216   */
00217 uint8_t BSP_TS_ITConfig(void)
00218 {
00219   uint8_t ts_status = TS_OK;
00220   GPIO_InitTypeDef gpio_init_structure;
00221 
00222   /* Msp Init of GPIO used for TS_INT pin coming from TouchScreen driver IC FT6x06 */
00223   /* When touchscreen is operated in interrupt mode */
00224   BSP_TS_INT_MspInit();
00225 
00226   /* Configure Interrupt mode for TS_INT pin falling edge : when a new touch is available */
00227   /* TS_INT pin is active on low level on new touch available */
00228   gpio_init_structure.Pin = TS_INT_PIN;
00229   gpio_init_structure.Pull = GPIO_PULLUP;
00230   gpio_init_structure.Speed = GPIO_SPEED_FAST;
00231   gpio_init_structure.Mode = GPIO_MODE_IT_FALLING;
00232   HAL_GPIO_Init(TS_INT_GPIO_PORT, &gpio_init_structure);
00233 
00234   /* Enable and set the TS_INT EXTI Interrupt to an intermediate priority */
00235   HAL_NVIC_SetPriority((IRQn_Type)(TS_INT_EXTI_IRQn), 0x05, 0x00);
00236   HAL_NVIC_EnableIRQ((IRQn_Type)(TS_INT_EXTI_IRQn));
00237 
00238   /* Enable the TS in interrupt mode */
00239   /* In that case the INT output of FT6206 when new touch is available */
00240   /* is active on low level and directed on EXTI */
00241   ts_driver->EnableIT(I2C_Address);
00242 
00243   return (ts_status);
00244 }
00245 
00246 /**
00247   * @brief  Returns status and positions of the touch screen.
00248   * @param  TS_State: Pointer to touch screen current state structure
00249   * @retval TS_OK if all initializations are OK. Other value if error.
00250   */
00251 uint8_t BSP_TS_GetState(TS_StateTypeDef *TS_State)
00252 {
00253   static uint32_t _x[TS_MAX_NB_TOUCH] = {0, 0};
00254   static uint32_t _y[TS_MAX_NB_TOUCH] = {0, 0};
00255   uint8_t ts_status = TS_OK;
00256   uint16_t tmp;
00257   uint16_t Raw_x[TS_MAX_NB_TOUCH];
00258   uint16_t Raw_y[TS_MAX_NB_TOUCH];
00259   uint16_t xDiff;
00260   uint16_t yDiff;
00261   uint32_t index;
00262 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00263   uint32_t weight = 0;
00264   uint32_t area = 0;
00265   uint32_t event = 0;
00266 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00267 
00268   /* Check and update the number of touches active detected */
00269   TS_State->touchDetected = ts_driver->DetectTouch(I2C_Address);
00270   if(TS_State->touchDetected)
00271   {
00272     for(index=0; index < TS_State->touchDetected; index++)
00273     {
00274       /* Get each touch coordinates */
00275       ts_driver->GetXY(I2C_Address, &(Raw_x[index]), &(Raw_y[index]));
00276 
00277       if(ts_orientation & TS_SWAP_XY)
00278       {
00279         tmp = Raw_x[index];
00280         Raw_x[index] = Raw_y[index]; 
00281         Raw_y[index] = tmp;
00282       }
00283       
00284       if(ts_orientation & TS_SWAP_X)
00285       {
00286         Raw_x[index] = FT_6206_MAX_WIDTH - 1 - Raw_x[index];
00287       }
00288 
00289       if(ts_orientation & TS_SWAP_Y)
00290       {
00291         Raw_y[index] = FT_6206_MAX_HEIGHT - 1 - Raw_y[index];
00292       }
00293             
00294       xDiff = Raw_x[index] > _x[index]? (Raw_x[index] - _x[index]): (_x[index] - Raw_x[index]);
00295       yDiff = Raw_y[index] > _y[index]? (Raw_y[index] - _y[index]): (_y[index] - Raw_y[index]);
00296 
00297       if ((xDiff + yDiff) > 5)
00298       {
00299         _x[index] = Raw_x[index];
00300         _y[index] = Raw_y[index];
00301       }
00302 
00303 
00304       TS_State->touchX[index] = _x[index];
00305       TS_State->touchY[index] = _y[index];
00306 
00307 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00308 
00309       /* Get touch info related to the current touch */
00310       ft6x06_TS_GetTouchInfo(I2C_Address, index, &weight, &area, &event);
00311 
00312       /* Update TS_State structure */
00313       TS_State->touchWeight[index] = weight;
00314       TS_State->touchArea[index]   = area;
00315 
00316       /* Remap touch event */
00317       switch(event)
00318       {
00319         case FT6206_TOUCH_EVT_FLAG_PRESS_DOWN  :
00320           TS_State->touchEventId[index] = TOUCH_EVENT_PRESS_DOWN;
00321           break;
00322         case FT6206_TOUCH_EVT_FLAG_LIFT_UP :
00323           TS_State->touchEventId[index] = TOUCH_EVENT_LIFT_UP;
00324           break;
00325         case FT6206_TOUCH_EVT_FLAG_CONTACT :
00326           TS_State->touchEventId[index] = TOUCH_EVENT_CONTACT;
00327           break;
00328         case FT6206_TOUCH_EVT_FLAG_NO_EVENT :
00329           TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT;
00330           break;
00331         default :
00332           ts_status = TS_ERROR;
00333           break;
00334       } /* of switch(event) */
00335 
00336 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00337 
00338     } /* of for(index=0; index < TS_State->touchDetected; index++) */
00339 
00340 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00341     /* Get gesture Id */
00342     ts_status = BSP_TS_Get_GestureId(TS_State);
00343 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00344 
00345   } /* end of if(TS_State->touchDetected != 0) */
00346 
00347   return (ts_status);
00348 }
00349 
00350 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00351 /**
00352   * @brief  Update gesture Id following a touch detected.
00353   * @param  TS_State: Pointer to touch screen current state structure
00354   * @retval TS_OK if all initializations are OK. Other value if error.
00355   */
00356 uint8_t BSP_TS_Get_GestureId(TS_StateTypeDef *TS_State)
00357 {
00358   uint32_t gestureId = 0;
00359   uint8_t  ts_status = TS_OK;
00360 
00361   /* Get gesture Id */
00362   ft6x06_TS_GetGestureID(I2C_Address, &gestureId);
00363 
00364   /* Remap gesture Id to a TS_GestureIdTypeDef value */
00365   switch(gestureId)
00366   {
00367     case FT6206_GEST_ID_NO_GESTURE :
00368       TS_State->gestureId = GEST_ID_NO_GESTURE;
00369       break;
00370     case FT6206_GEST_ID_MOVE_UP :
00371       TS_State->gestureId = GEST_ID_MOVE_UP;
00372       break;
00373     case FT6206_GEST_ID_MOVE_RIGHT :
00374       TS_State->gestureId = GEST_ID_MOVE_RIGHT;
00375       break;
00376     case FT6206_GEST_ID_MOVE_DOWN :
00377       TS_State->gestureId = GEST_ID_MOVE_DOWN;
00378       break;
00379     case FT6206_GEST_ID_MOVE_LEFT :
00380       TS_State->gestureId = GEST_ID_MOVE_LEFT;
00381       break;
00382     case FT6206_GEST_ID_ZOOM_IN :
00383       TS_State->gestureId = GEST_ID_ZOOM_IN;
00384       break;
00385     case FT6206_GEST_ID_ZOOM_OUT :
00386       TS_State->gestureId = GEST_ID_ZOOM_OUT;
00387       break;
00388     default :
00389       ts_status = TS_ERROR;
00390       break;
00391   } /* of switch(gestureId) */
00392 
00393   return(ts_status);
00394 }
00395 
00396 
00397 /** @defgroup STM32469I-Discovery_TS_Private_Functions STM32469I Discovery TS Private Functions
00398   * @{
00399   */
00400 
00401 
00402 /**
00403   * @brief  Function used to reset all touch data before a new acquisition
00404   *         of touch information.
00405   * @param  TS_State: Pointer to touch screen current state structure
00406   * @retval TS_OK if OK, TE_ERROR if problem found.
00407   */
00408 uint8_t BSP_TS_ResetTouchData(TS_StateTypeDef *TS_State)
00409 {
00410   uint8_t ts_status = TS_ERROR;
00411   uint32_t index;
00412 
00413   if (TS_State != (TS_StateTypeDef *)NULL)
00414   {
00415     TS_State->gestureId = GEST_ID_NO_GESTURE;
00416     TS_State->touchDetected = 0;
00417 
00418     for(index = 0; index < TS_MAX_NB_TOUCH; index++)
00419     {
00420       TS_State->touchX[index]       = 0;
00421       TS_State->touchY[index]       = 0;
00422       TS_State->touchArea[index]    = 0;
00423       TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT;
00424       TS_State->touchWeight[index]  = 0;
00425     }
00426 
00427     ts_status = TS_OK;
00428 
00429   } /* of if (TS_State != (TS_StateTypeDef *)NULL) */
00430 
00431   return (ts_status);
00432 }
00433 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00434 /**
00435   * @brief  Initializes the TS_INT pin MSP.
00436   */
00437 __weak void BSP_TS_INT_MspInit(void)
00438 {
00439   GPIO_InitTypeDef  gpio_init_structure;
00440 
00441   TS_INT_GPIO_CLK_ENABLE();
00442 
00443   /* GPIO configuration in input for TouchScreen interrupt signal on TS_INT pin */
00444   gpio_init_structure.Pin       = TS_INT_PIN;
00445 
00446   gpio_init_structure.Mode      = GPIO_MODE_INPUT;
00447   gpio_init_structure.Pull      = GPIO_PULLUP;
00448   gpio_init_structure.Speed     = GPIO_SPEED_HIGH;
00449   HAL_GPIO_Init(TS_INT_GPIO_PORT, &gpio_init_structure);
00450 }
00451 
00452 
00453 /**
00454   * @}
00455   */
00456 
00457 /**
00458   * @}
00459   */
00460 
00461 /**
00462   * @}
00463   */
00464 
00465 /**
00466   * @}
00467   */
00468 
00469 /**
00470   * @}
00471   */
00472 
00473 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Jan 13 2016 10:58:46 for STM32F469I-Discovery BSP User Manual by   doxygen 1.7.6.1