Contains the BSP driver for the DISCO_F413ZH board.

Dependents:   DISCO_F413ZH-LCD-demo DISCO_F413ZH-touch-screen-demo DISCO_F413ZH-SD-demo DISCO_F413ZH-PSRAM-demo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f413h_discovery_ts.c Source File

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