BSP files for STM32H747I-Discovery Copy from ST Cube delivery
Dependents: DISCO_H747I_LCD_demo DISCO_H747I_AUDIO_demo
stm32h747i_discovery_ts.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32h747i_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 STM32H747I_DISCOVERY discovery board. 00007 ****************************************************************************** 00008 * @attention 00009 * 00010 * <h2><center>© Copyright (c) 2019 STMicroelectronics. 00011 * All rights reserved.</center></h2> 00012 * 00013 * This software component is licensed by ST under BSD 3-Clause license, 00014 * the "License"; You may not use this file except in compliance with the 00015 * License. You may obtain a copy of the License at: 00016 * opensource.org/licenses/BSD-3-Clause 00017 * 00018 ****************************************************************************** 00019 */ 00020 00021 /* File Info : ----------------------------------------------------------------- 00022 User NOTES 00023 1. How To use this driver: 00024 -------------------------- 00025 - This driver is used to drive the touch screen module of the STM32H747I_DISCOVERY 00026 discovery board on the K.O.D Optica Technology 480x800 TFT-LCD mounted on 00027 MB1166 daughter board. The touch screen driver IC inside the K.O.D module KM-040TMP-02 00028 is a FT6206 by Focal Tech. 00029 00030 2. Driver description: 00031 --------------------- 00032 + Initialization steps: 00033 o Initialize the TS module using the BSP_TS_Init() function. This 00034 function includes the MSP layer hardware resources initialization and the 00035 communication layer configuration to start the TS use. The LCD size properties 00036 (x and y) are passed as parameters. 00037 o If TS interrupt mode is desired, you must configure the TS interrupt mode 00038 by calling the function BSP_TS_ITConfig(). The TS interrupt mode is generated 00039 as an external interrupt whenever a touch is detected. 00040 The interrupt mode internally uses the IO functionalities driver driven by 00041 the IO expander, to configure the IT line. 00042 00043 + Touch screen use 00044 o The touch screen state is captured whenever the function BSP_TS_GetState() is 00045 used. This function returns information about the last LCD touch occurred 00046 in the TS_StateTypeDef structure. 00047 o The IT is handled using the corresponding external interrupt IRQ handler, 00048 the user IT callback treatment is implemented on the same external interrupt 00049 callback. 00050 00051 ------------------------------------------------------------------------------*/ 00052 00053 /* Includes ------------------------------------------------------------------*/ 00054 #include "stm32h747i_discovery.h" 00055 #include "stm32h747i_discovery_ts.h" 00056 00057 /** @addtogroup BSP 00058 * @{ 00059 */ 00060 00061 /** @addtogroup STM32H747I_DISCOVERY 00062 * @{ 00063 */ 00064 00065 /** @defgroup STM32H747I_DISCOVERY_TS STM32H747I_DISCOVERY_TS 00066 * @{ 00067 */ 00068 00069 /** @defgroup STM32H747I_DISCOVERY_TS_Private_Variables Private Variables 00070 * @{ 00071 */ 00072 static TS_DrvTypeDef *ts_driver; 00073 static uint8_t ts_orientation; 00074 static uint8_t I2C_Address = 0; 00075 00076 /* Table for touchscreen event information display on LCD : table indexed on enum @ref TS_TouchEventTypeDef information */ 00077 char * ts_event_string_tab[TOUCH_EVENT_NB_MAX] = { "None", 00078 "Press down", 00079 "Lift up", 00080 "Contact" 00081 }; 00082 00083 /* Table for touchscreen gesture Id information display on LCD : table indexed on enum @ref TS_GestureIdTypeDef information */ 00084 char * ts_gesture_id_string_tab[GEST_ID_NB_MAX] = { "None", 00085 "Move Up", 00086 "Move Right", 00087 "Move Down", 00088 "Move Left", 00089 "Zoom In", 00090 "Zoom Out" 00091 }; 00092 00093 /** 00094 * @} 00095 */ 00096 00097 /** @defgroup STM32H747I_DISCOVERY_TS_Exported_Functions Exported Functions 00098 * @{ 00099 */ 00100 00101 /** 00102 * @brief Initializes and configures the touch screen functionalities and 00103 * configures all necessary hardware resources (GPIOs, I2C, clocks..). 00104 * @param ts_SizeX : Maximum X size of the TS area on LCD 00105 * @param ts_SizeY : Maximum Y size of the TS area on LCD 00106 * @retval TS_OK if all initializations are OK. Other value if error. 00107 */ 00108 uint8_t BSP_TS_Init(uint16_t ts_SizeX, uint16_t ts_SizeY) 00109 { 00110 uint8_t ts_status = TS_OK; 00111 uint8_t ts_id1, ts_id2 = 0; 00112 /* Note : I2C_Address is un-initialized here, but is not used at all in init function */ 00113 /* but the prototype of Init() is like that in template and should be respected */ 00114 00115 /* Initialize the communication channel to sensor (I2C) if necessary */ 00116 /* that is initialization is done only once after a power up */ 00117 ft6x06_ts_drv.Init(I2C_Address); 00118 00119 ts_id1 = ft6x06_ts_drv.ReadID(TS_I2C_ADDRESS); 00120 if(ts_id1 != FT6206_ID_VALUE) 00121 { 00122 ts_id2 = ft6x06_ts_drv.ReadID(TS_I2C_ADDRESS_A02); 00123 I2C_Address = TS_I2C_ADDRESS_A02; 00124 } 00125 else 00126 { 00127 I2C_Address = TS_I2C_ADDRESS; 00128 } 00129 00130 /* Scan FT6xx6 TouchScreen IC controller ID register by I2C Read */ 00131 /* Verify this is a FT6206 or FT6336G, otherwise this is an error case */ 00132 if((ts_id1 == FT6206_ID_VALUE) || (ts_id2 == FT6206_ID_VALUE)) 00133 { 00134 /* Found FT6206 : Initialize the TS driver structure */ 00135 ts_driver = &ft6x06_ts_drv; 00136 00137 /* Get LCD chosen orientation */ 00138 if(ts_SizeX < ts_SizeY) 00139 { 00140 ts_orientation = TS_SWAP_NONE; 00141 } 00142 else 00143 { 00144 ts_orientation = TS_SWAP_XY | TS_SWAP_Y; 00145 } 00146 00147 if(ts_status == TS_OK) 00148 { 00149 /* Software reset the TouchScreen */ 00150 ts_driver->Reset(I2C_Address); 00151 00152 /* Calibrate, Configure and Start the TouchScreen driver */ 00153 ts_driver->Start(I2C_Address); 00154 00155 } /* of if(ts_status == TS_OK) */ 00156 } 00157 else 00158 { 00159 ts_status = TS_DEVICE_NOT_FOUND; 00160 } 00161 00162 return (ts_status); 00163 } 00164 00165 /** 00166 * @brief Configures and enables the touch screen interrupts. 00167 * @retval TS_OK if all initializations are OK. Other value if error. 00168 */ 00169 uint8_t BSP_TS_ITConfig(void) 00170 { 00171 uint8_t ts_status = TS_OK; 00172 GPIO_InitTypeDef gpio_init_structure; 00173 00174 /* Msp Init of GPIO used for TS_INT pin coming from TouchScreen driver IC FT6x06 */ 00175 /* When touchscreen is operated in interrupt mode */ 00176 BSP_TS_INT_MspInit(); 00177 00178 /* Configure Interrupt mode for TS_INT pin falling edge : when a new touch is available */ 00179 /* TS_INT pin is active on low level on new touch available */ 00180 gpio_init_structure.Pin = TS_INT_PIN; 00181 gpio_init_structure.Pull = GPIO_PULLUP; 00182 gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH; 00183 gpio_init_structure.Mode = GPIO_MODE_IT_FALLING; 00184 HAL_GPIO_Init(TS_INT_GPIO_PORT, &gpio_init_structure); 00185 00186 /* Enable and set the TS_INT EXTI Interrupt to an intermediate priority */ 00187 HAL_NVIC_SetPriority((IRQn_Type)(TS_INT_EXTI_IRQn), 0x0F, 0x00); 00188 HAL_NVIC_EnableIRQ((IRQn_Type)(TS_INT_EXTI_IRQn)); 00189 00190 /* Enable the TS in interrupt mode */ 00191 /* In that case the INT output of FT6206 when new touch is available */ 00192 /* is active on low level and directed on EXTI */ 00193 ts_driver->EnableIT(I2C_Address); 00194 00195 return (ts_status); 00196 } 00197 00198 /** 00199 * @brief Returns status and positions of the touch screen. 00200 * @param TS_State: Pointer to touch screen current state structure 00201 * @retval TS_OK if all initializations are OK. Other value if error. 00202 */ 00203 uint8_t BSP_TS_GetState(TS_StateTypeDef *TS_State) 00204 { 00205 static uint32_t _x[TS_MAX_NB_TOUCH] = {0, 0}; 00206 static uint32_t _y[TS_MAX_NB_TOUCH] = {0, 0}; 00207 uint8_t ts_status = TS_OK; 00208 uint16_t tmp; 00209 uint16_t Raw_x[TS_MAX_NB_TOUCH]; 00210 uint16_t Raw_y[TS_MAX_NB_TOUCH]; 00211 uint16_t xDiff; 00212 uint16_t yDiff; 00213 uint32_t index; 00214 #if (TS_MULTI_TOUCH_SUPPORTED == 1) 00215 uint32_t weight = 0; 00216 uint32_t area = 0; 00217 uint32_t event = 0; 00218 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */ 00219 00220 /* Check and update the number of touches active detected */ 00221 TS_State->touchDetected = ts_driver->DetectTouch(I2C_Address); 00222 if(TS_State->touchDetected) 00223 { 00224 for(index=0; index < TS_State->touchDetected; index++) 00225 { 00226 /* Get each touch coordinates */ 00227 ts_driver->GetXY(I2C_Address, &(Raw_x[index]), &(Raw_y[index])); 00228 00229 if(ts_orientation & TS_SWAP_XY) 00230 { 00231 tmp = Raw_x[index]; 00232 Raw_x[index] = Raw_y[index]; 00233 Raw_y[index] = tmp; 00234 } 00235 00236 if(ts_orientation & TS_SWAP_X) 00237 { 00238 Raw_x[index] = FT_6206_MAX_WIDTH - 1 - Raw_x[index]; 00239 } 00240 00241 if(ts_orientation & TS_SWAP_Y) 00242 { 00243 Raw_y[index] = FT_6206_MAX_HEIGHT - 1 - Raw_y[index]; 00244 } 00245 00246 xDiff = Raw_x[index] > _x[index]? (Raw_x[index] - _x[index]): (_x[index] - Raw_x[index]); 00247 yDiff = Raw_y[index] > _y[index]? (Raw_y[index] - _y[index]): (_y[index] - Raw_y[index]); 00248 00249 if ((xDiff + yDiff) > 5) 00250 { 00251 _x[index] = Raw_x[index]; 00252 _y[index] = Raw_y[index]; 00253 } 00254 00255 00256 TS_State->touchX[index] = _x[index]; 00257 TS_State->touchY[index] = _y[index]; 00258 00259 #if (TS_MULTI_TOUCH_SUPPORTED == 1) 00260 00261 /* Get touch info related to the current touch */ 00262 ft6x06_TS_GetTouchInfo(I2C_Address, index, &weight, &area, &event); 00263 00264 /* Update TS_State structure */ 00265 TS_State->touchWeight[index] = weight; 00266 TS_State->touchArea[index] = area; 00267 00268 /* Remap touch event */ 00269 switch(event) 00270 { 00271 case FT6206_TOUCH_EVT_FLAG_PRESS_DOWN : 00272 TS_State->touchEventId[index] = TOUCH_EVENT_PRESS_DOWN; 00273 break; 00274 case FT6206_TOUCH_EVT_FLAG_LIFT_UP : 00275 TS_State->touchEventId[index] = TOUCH_EVENT_LIFT_UP; 00276 break; 00277 case FT6206_TOUCH_EVT_FLAG_CONTACT : 00278 TS_State->touchEventId[index] = TOUCH_EVENT_CONTACT; 00279 break; 00280 case FT6206_TOUCH_EVT_FLAG_NO_EVENT : 00281 TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT; 00282 break; 00283 default : 00284 ts_status = TS_ERROR; 00285 break; 00286 } /* of switch(event) */ 00287 00288 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */ 00289 00290 } /* of for(index=0; index < TS_State->touchDetected; index++) */ 00291 00292 #if (TS_MULTI_TOUCH_SUPPORTED == 1) 00293 /* Get gesture Id */ 00294 ts_status = BSP_TS_Get_GestureId(TS_State); 00295 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */ 00296 00297 } /* end of if(TS_State->touchDetected != 0) */ 00298 00299 return (ts_status); 00300 } 00301 00302 #if (TS_MULTI_TOUCH_SUPPORTED == 1) 00303 /** 00304 * @brief Update gesture Id following a touch detected. 00305 * @param TS_State: Pointer to touch screen current state structure 00306 * @retval TS_OK if all initializations are OK. Other value if error. 00307 */ 00308 uint8_t BSP_TS_Get_GestureId(TS_StateTypeDef *TS_State) 00309 { 00310 uint32_t gestureId = 0; 00311 uint8_t ts_status = TS_OK; 00312 00313 /* Get gesture Id */ 00314 ft6x06_TS_GetGestureID(I2C_Address, &gestureId); 00315 00316 /* Remap gesture Id to a TS_GestureIdTypeDef value */ 00317 switch(gestureId) 00318 { 00319 case FT6206_GEST_ID_NO_GESTURE : 00320 TS_State->gestureId = GEST_ID_NO_GESTURE; 00321 break; 00322 case FT6206_GEST_ID_MOVE_UP : 00323 TS_State->gestureId = GEST_ID_MOVE_UP; 00324 break; 00325 case FT6206_GEST_ID_MOVE_RIGHT : 00326 TS_State->gestureId = GEST_ID_MOVE_RIGHT; 00327 break; 00328 case FT6206_GEST_ID_MOVE_DOWN : 00329 TS_State->gestureId = GEST_ID_MOVE_DOWN; 00330 break; 00331 case FT6206_GEST_ID_MOVE_LEFT : 00332 TS_State->gestureId = GEST_ID_MOVE_LEFT; 00333 break; 00334 case FT6206_GEST_ID_ZOOM_IN : 00335 TS_State->gestureId = GEST_ID_ZOOM_IN; 00336 break; 00337 case FT6206_GEST_ID_ZOOM_OUT : 00338 TS_State->gestureId = GEST_ID_ZOOM_OUT; 00339 break; 00340 default : 00341 ts_status = TS_ERROR; 00342 break; 00343 } /* of switch(gestureId) */ 00344 00345 return(ts_status); 00346 } 00347 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */ 00348 00349 00350 #if (TS_MULTI_TOUCH_SUPPORTED == 1) 00351 /** 00352 * @brief Function used to reset all touch data before a new acquisition 00353 * of touch information. 00354 * @param TS_State: Pointer to touch screen current state structure 00355 * @retval TS_OK if OK, TE_ERROR if problem found. 00356 */ 00357 uint8_t BSP_TS_ResetTouchData(TS_StateTypeDef *TS_State) 00358 { 00359 uint8_t ts_status = TS_ERROR; 00360 uint32_t index; 00361 00362 if (TS_State != (TS_StateTypeDef *)NULL) 00363 { 00364 TS_State->gestureId = GEST_ID_NO_GESTURE; 00365 TS_State->touchDetected = 0; 00366 00367 for(index = 0; index < TS_MAX_NB_TOUCH; index++) 00368 { 00369 TS_State->touchX[index] = 0; 00370 TS_State->touchY[index] = 0; 00371 TS_State->touchArea[index] = 0; 00372 TS_State->touchEventId[index] = TOUCH_EVENT_NO_EVT; 00373 TS_State->touchWeight[index] = 0; 00374 } 00375 00376 ts_status = TS_OK; 00377 00378 } /* of if (TS_State != (TS_StateTypeDef *)NULL) */ 00379 00380 return (ts_status); 00381 } 00382 #endif /* TS_MULTI_TOUCH_SUPPORTED == 1 */ 00383 00384 /** 00385 * @brief Initializes the TS_INT pin MSP. 00386 * @retval None 00387 */ 00388 __weak void BSP_TS_INT_MspInit(void) 00389 { 00390 GPIO_InitTypeDef gpio_init_structure; 00391 00392 TS_INT_GPIO_CLK_ENABLE(); 00393 00394 /* GPIO configuration in input for TouchScreen interrupt signal on TS_INT pin */ 00395 gpio_init_structure.Pin = TS_INT_PIN; 00396 00397 gpio_init_structure.Mode = GPIO_MODE_INPUT; 00398 gpio_init_structure.Pull = GPIO_PULLUP; 00399 gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00400 HAL_GPIO_Init(TS_INT_GPIO_PORT, &gpio_init_structure); 00401 } 00402 00403 /** 00404 * @} 00405 */ 00406 00407 /** 00408 * @} 00409 */ 00410 00411 /** 00412 * @} 00413 */ 00414 00415 /** 00416 * @} 00417 */ 00418 00419 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 18:45:41 by 1.7.2