STM32746G-Discovery board drivers V1.0.0

Dependents:   DISCO-F746NG_LCDTS_CC3000_NTP DISCO-F746NG_ROPE_WIFI F746_SpectralAnalysis_NoPhoto ecte433 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32746g_discovery_ts.c Source File

stm32746g_discovery_ts.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32746g_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 STM32746G-Discovery board.
00007   @verbatim
00008    1. How To use this driver:
00009    --------------------------
00010       - This driver is used to drive the touch screen module of the STM32746G-Discovery
00011         board on the RK043FN48H-CT672B 480x272 LCD screen with capacitive touch screen.
00012       - The FT5336 component driver must be included in project files according to
00013         the touch screen driver present on this board.
00014    
00015    2. Driver description:
00016    ---------------------
00017      + Initialization steps:
00018         o Initialize the TS module using the BSP_TS_Init() function. This 
00019           function includes the MSP layer hardware resources initialization and the
00020           communication layer configuration to start the TS use. The LCD size properties
00021           (x and y) are passed as parameters.
00022         o If TS interrupt mode is desired, you must configure the TS interrupt mode
00023           by calling the function BSP_TS_ITConfig(). The TS interrupt mode is generated
00024           as an external interrupt whenever a touch is detected. 
00025           The interrupt mode internally uses the IO functionalities driver driven by
00026           the IO expander, to configure the IT line.
00027      
00028      + Touch screen use
00029         o The touch screen state is captured whenever the function BSP_TS_GetState() is 
00030           used. This function returns information about the last LCD touch occurred
00031           in the TS_StateTypeDef structure.
00032         o If TS interrupt mode is used, the function BSP_TS_ITGetStatus() is needed to get
00033           the interrupt status. To clear the IT pending bits, you should call the 
00034           function BSP_TS_ITClear().
00035         o The IT is handled using the corresponding external interrupt IRQ handler,
00036           the user IT callback treatment is implemented on the same external interrupt
00037           callback.
00038   @endverbatim
00039   ******************************************************************************
00040   * @attention
00041   *
00042   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00043   *
00044   * Redistribution and use in source and binary forms, with or without modification,
00045   * are permitted provided that the following conditions are met:
00046   *   1. Redistributions of source code must retain the above copyright notice,
00047   *      this list of conditions and the following disclaimer.
00048   *   2. Redistributions in binary form must reproduce the above copyright notice,
00049   *      this list of conditions and the following disclaimer in the documentation
00050   *      and/or other materials provided with the distribution.
00051   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00052   *      may be used to endorse or promote products derived from this software
00053   *      without specific prior written permission.
00054   *
00055   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00056   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00057   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00058   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00059   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00060   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00061   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00062   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00063   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00064   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00065   *
00066   ******************************************************************************
00067   */ 
00068 
00069 /* Dependencies
00070 - stm32746g_discovery_lcd.c
00071 - ft5336.c
00072 EndDependencies */
00073 
00074 /* Includes ------------------------------------------------------------------*/
00075 #include "stm32746g_discovery_ts.h"
00076 
00077 /** @addtogroup BSP
00078   * @{
00079   */
00080 
00081 /** @addtogroup STM32746G_DISCOVERY
00082   * @{
00083   */ 
00084   
00085 /** @defgroup STM32746G_DISCOVERY_TS STM32746G_DISCOVERY_TS
00086   * @{
00087   */   
00088 
00089 /** @defgroup STM32746G_DISCOVERY_TS_Private_Types_Definitions STM32746G_DISCOVERY_TS Types Definitions
00090   * @{
00091   */ 
00092 /**
00093   * @}
00094   */ 
00095 
00096 /** @defgroup STM32746G_DISCOVERY_TS_Private_Defines STM32746G_DISCOVERY_TS Types Defines
00097   * @{
00098   */ 
00099 /**
00100   * @}
00101   */ 
00102 
00103 /** @defgroup STM32746G_DISCOVERY_TS_Private_Macros STM32746G_DISCOVERY_TS Private Macros
00104   * @{
00105   */ 
00106 /**
00107   * @}
00108   */
00109 
00110 /** @defgroup STM32746G_DISCOVERY_TS_Imported_Variables STM32746G_DISCOVERY_TS Imported Variables
00111   * @{
00112   */
00113   /**
00114     * @}
00115     */
00116 
00117 /** @defgroup STM32746G_DISCOVERY_TS_Private_Variables STM32746G_DISCOVERY_TS Private Variables
00118   * @{
00119   */ 
00120 static TS_DrvTypeDef *tsDriver;
00121 static uint16_t tsXBoundary, tsYBoundary; 
00122 static uint8_t  tsOrientation;
00123 static uint8_t  I2cAddress;
00124 /**
00125   * @}
00126   */ 
00127 
00128 /** @defgroup STM32746G_DISCOVERY_TS_Private_Function_Prototypes STM32746G_DISCOVERY_TS Private Function Prototypes
00129   * @{
00130   */ 
00131 /**
00132   * @}
00133   */ 
00134 
00135 /** @defgroup STM32746G_DISCOVERY_TS_Exported_Functions STM32746G_DISCOVERY_TS Exported Functions
00136   * @{
00137   */ 
00138 
00139 /**
00140   * @brief  Initializes and configures the touch screen functionalities and 
00141   *         configures all necessary hardware resources (GPIOs, I2C, clocks..).
00142   * @param  ts_SizeX: Maximum X size of the TS area on LCD
00143   * @param  ts_SizeY: Maximum Y size of the TS area on LCD
00144   * @retval TS_OK if all initializations are OK. Other value if error.
00145   */
00146 uint8_t BSP_TS_Init(uint16_t ts_SizeX, uint16_t ts_SizeY)
00147 {
00148   uint8_t status = TS_OK;
00149   tsXBoundary = ts_SizeX;
00150   tsYBoundary = ts_SizeY;
00151   
00152   /* Read ID and verify if the touch screen driver is ready */
00153   ft5336_ts_drv.Init(TS_I2C_ADDRESS);
00154   if(ft5336_ts_drv.ReadID(TS_I2C_ADDRESS) == FT5336_ID_VALUE)
00155   { 
00156     /* Initialize the TS driver structure */
00157     tsDriver = &ft5336_ts_drv;
00158     I2cAddress = TS_I2C_ADDRESS;
00159     tsOrientation = TS_SWAP_XY;
00160 
00161     /* Initialize the TS driver */
00162     tsDriver->Start(I2cAddress);
00163   }
00164   else
00165   {
00166     status = TS_DEVICE_NOT_FOUND;
00167   }
00168 
00169   return status;
00170 }
00171 
00172 /**
00173   * @brief  DeInitializes the TouchScreen.
00174   * @retval TS state
00175   */
00176 uint8_t BSP_TS_DeInit(void)
00177 { 
00178   /* Actually ts_driver does not provide a DeInit function */
00179   return TS_OK;
00180 }
00181 
00182 /**
00183   * @brief  Configures and enables the touch screen interrupts.
00184   * @retval TS_OK if all initializations are OK. Other value if error.
00185   */
00186 uint8_t BSP_TS_ITConfig(void)
00187 {
00188   GPIO_InitTypeDef gpio_init_structure;
00189 
00190   /* Configure Interrupt mode for SD detection pin */
00191   gpio_init_structure.Pin = TS_INT_PIN;
00192   gpio_init_structure.Pull = GPIO_NOPULL;
00193   gpio_init_structure.Speed = GPIO_SPEED_FAST;
00194   gpio_init_structure.Mode = GPIO_MODE_IT_RISING;
00195   HAL_GPIO_Init(TS_INT_GPIO_PORT, &gpio_init_structure);
00196 
00197   /* Enable and set Touch screen EXTI Interrupt to the lowest priority */
00198   HAL_NVIC_SetPriority((IRQn_Type)(TS_INT_EXTI_IRQn), 0x0F, 0x00);
00199   HAL_NVIC_EnableIRQ((IRQn_Type)(TS_INT_EXTI_IRQn));
00200 
00201   /* Enable the TS ITs */
00202   tsDriver->EnableIT(I2cAddress);
00203 
00204   return TS_OK;  
00205 }
00206 
00207 /**
00208   * @brief  Gets the touch screen interrupt status.
00209   * @retval TS_OK if all initializations are OK. Other value if error.
00210   */
00211 uint8_t BSP_TS_ITGetStatus(void)
00212 {
00213   /* Return the TS IT status */
00214   return (tsDriver->GetITStatus(I2cAddress));
00215 }
00216 
00217 /**
00218   * @brief  Returns status and positions of the touch screen.
00219   * @param  TS_State: Pointer to touch screen current state structure
00220   * @retval TS_OK if all initializations are OK. Other value if error.
00221   */
00222 uint8_t BSP_TS_GetState(TS_StateTypeDef *TS_State)
00223 {
00224   static uint32_t _x[TS_MAX_NB_TOUCH] = {0, 0};
00225   static uint32_t _y[TS_MAX_NB_TOUCH] = {0, 0};
00226   uint8_t ts_status = TS_OK;
00227   uint16_t x[TS_MAX_NB_TOUCH];
00228   uint16_t y[TS_MAX_NB_TOUCH];
00229   uint16_t brute_x[TS_MAX_NB_TOUCH];
00230   uint16_t brute_y[TS_MAX_NB_TOUCH];
00231   uint16_t x_diff;
00232   uint16_t y_diff;
00233   uint32_t index;
00234 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00235   uint32_t weight = 0;
00236   uint32_t area = 0;
00237   uint32_t event = 0;
00238 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00239 
00240   /* Check and update the number of touches active detected */
00241   TS_State->touchDetected = tsDriver->DetectTouch(I2cAddress);
00242   
00243   if(TS_State->touchDetected)
00244   {
00245     for(index=0; index < TS_State->touchDetected; index++)
00246     {
00247       /* Get each touch coordinates */
00248       tsDriver->GetXY(I2cAddress, &(brute_x[index]), &(brute_y[index]));
00249 
00250       if(tsOrientation == TS_SWAP_NONE)
00251       {
00252         x[index] = brute_x[index];
00253         y[index] = brute_y[index];
00254       }
00255 
00256       if(tsOrientation & TS_SWAP_X)
00257       {
00258         x[index] = 4096 - brute_x[index];
00259       }
00260 
00261       if(tsOrientation & TS_SWAP_Y)
00262       {
00263         y[index] = 4096 - brute_y[index];
00264       }
00265 
00266       if(tsOrientation & TS_SWAP_XY)
00267       {
00268         y[index] = brute_x[index];
00269         x[index] = brute_y[index];
00270       }
00271 
00272       x_diff = x[index] > _x[index]? (x[index] - _x[index]): (_x[index] - x[index]);
00273       y_diff = y[index] > _y[index]? (y[index] - _y[index]): (_y[index] - y[index]);
00274 
00275       if ((x_diff + y_diff) > 5)
00276       {
00277         _x[index] = x[index];
00278         _y[index] = y[index];
00279       }
00280 
00281       if(I2cAddress == FT5336_I2C_SLAVE_ADDRESS)
00282       {
00283         TS_State->touchX[index] = x[index];
00284         TS_State->touchY[index] = y[index];
00285       }
00286       else
00287       {
00288         /* 2^12 = 4096 : indexes are expressed on a dynamic of 4096 */
00289         TS_State->touchX[index] = (tsXBoundary * _x[index]) >> 12;
00290         TS_State->touchY[index] = (tsYBoundary * _y[index]) >> 12;
00291       }
00292 
00293 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00294 
00295       /* Get touch info related to the current touch */
00296       ft5336_TS_GetTouchInfo(I2cAddress, index, &weight, &area, &event);
00297 
00298       /* Update TS_State structure */
00299       TS_State->touchWeight[index] = weight;
00300       TS_State->touchArea[index]   = area;
00301 
00302       /* Remap touch event */
00303       switch(event)
00304       {
00305         case FT5336_TOUCH_EVT_FLAG_PRESS_DOWN   :
00306           TS_State->touchEventId[index] = TOUCH_EVENT_PRESS_DOWN;
00307           break;
00308         case FT5336_TOUCH_EVT_FLAG_LIFT_UP :
00309           TS_State->touchEventId[index] = TOUCH_EVENT_LIFT_UP;
00310           break;
00311         case FT5336_TOUCH_EVT_FLAG_CONTACT :
00312           TS_State->touchEventId[index] = TOUCH_EVENT_CONTACT;
00313           break;
00314         case FT5336_TOUCH_EVT_FLAG_NO_EVENT :
00315           TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT;
00316           break;
00317         default :
00318           ts_status = TS_ERROR;
00319           break;
00320       } /* of switch(event) */
00321 
00322 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00323 
00324     } /* of for(index=0; index < TS_State->touchDetected; index++) */
00325 
00326 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00327     /* Get gesture Id */
00328     ts_status = BSP_TS_Get_GestureId(TS_State);
00329 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00330 
00331   } /* end of if(TS_State->touchDetected != 0) */
00332 
00333   return (ts_status);
00334 }
00335 
00336 #if (TS_MULTI_TOUCH_SUPPORTED == 1)
00337 /**
00338   * @brief  Update gesture Id following a touch detected.
00339   * @param  TS_State: Pointer to touch screen current state structure
00340   * @retval TS_OK if all initializations are OK. Other value if error.
00341   */
00342 uint8_t BSP_TS_Get_GestureId(TS_StateTypeDef *TS_State)
00343 {
00344   uint32_t gestureId = 0;
00345   uint8_t  ts_status = TS_OK;
00346 
00347   /* Get gesture Id */
00348   ft5336_TS_GetGestureID(I2cAddress, &gestureId);
00349 
00350   /* Remap gesture Id to a TS_GestureIdTypeDef value */
00351   switch(gestureId)
00352   {
00353     case FT5336_GEST_ID_NO_GESTURE :
00354       TS_State->gestureId = GEST_ID_NO_GESTURE;
00355       break;
00356     case FT5336_GEST_ID_MOVE_UP :
00357       TS_State->gestureId = GEST_ID_MOVE_UP;
00358       break;
00359     case FT5336_GEST_ID_MOVE_RIGHT :
00360       TS_State->gestureId = GEST_ID_MOVE_RIGHT;
00361       break;
00362     case FT5336_GEST_ID_MOVE_DOWN :
00363       TS_State->gestureId = GEST_ID_MOVE_DOWN;
00364       break;
00365     case FT5336_GEST_ID_MOVE_LEFT :
00366       TS_State->gestureId = GEST_ID_MOVE_LEFT;
00367       break;
00368     case FT5336_GEST_ID_ZOOM_IN :
00369       TS_State->gestureId = GEST_ID_ZOOM_IN;
00370       break;
00371     case FT5336_GEST_ID_ZOOM_OUT :
00372       TS_State->gestureId = GEST_ID_ZOOM_OUT;
00373       break;
00374     default :
00375       ts_status = TS_ERROR;
00376       break;
00377   } /* of switch(gestureId) */
00378 
00379   return(ts_status);
00380 }
00381 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */
00382 
00383 /**
00384   * @brief  Clears all touch screen interrupts.
00385   */
00386 void BSP_TS_ITClear(void)
00387 {
00388   /* Clear TS IT pending bits */
00389   tsDriver->ClearIT(I2cAddress); 
00390 }
00391 
00392 
00393 /** @defgroup STM32756G_DISCOVERY_TS_Private_Functions TS Private Functions
00394   * @{
00395   */
00396 
00397 
00398 /**
00399   * @brief  Function used to reset all touch data before a new acquisition
00400   *         of touch information.
00401   * @param  TS_State: Pointer to touch screen current state structure
00402   * @retval TS_OK if OK, TE_ERROR if problem found.
00403   */
00404 uint8_t BSP_TS_ResetTouchData(TS_StateTypeDef *TS_State)
00405 {
00406   uint8_t ts_status = TS_ERROR;
00407   uint32_t index;
00408 
00409   if (TS_State != (TS_StateTypeDef *)NULL)
00410   {
00411     TS_State->gestureId = GEST_ID_NO_GESTURE;
00412     TS_State->touchDetected = 0;
00413 
00414     for(index = 0; index < TS_MAX_NB_TOUCH; index++)
00415     {
00416       TS_State->touchX[index]       = 0;
00417       TS_State->touchY[index]       = 0;
00418       TS_State->touchArea[index]    = 0;
00419       TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT;
00420       TS_State->touchWeight[index]  = 0;
00421     }
00422 
00423     ts_status = TS_OK;
00424 
00425   } /* of if (TS_State != (TS_StateTypeDef *)NULL) */
00426 
00427   return (ts_status);
00428 }
00429 
00430 /**
00431   * @}
00432   */ 
00433 
00434 /**
00435   * @}
00436   */ 
00437 
00438 /**
00439   * @}
00440   */ 
00441 
00442 /**
00443   * @}
00444   */  
00445 
00446 /**
00447   * @}
00448   */
00449 
00450 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/