STM32746G-Discovery board drivers V1.0.0

Dependents:   F746_SD_GraphicEqualizer_ren0620

Fork of BSP_DISCO_F746NG by ST

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