STM32F723E-Discovery BSP User Manual: stm32f723e_discovery_ts.c Source File

STM32F723E-Discovery BSP

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