STM32F413H-Discovery BSP User Manual: stm32f413h_discovery_ts.c Source File

STM32F413H-Discovery BSP Drivers

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