stm322xg_eval_ts.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. ******************************************************************************
  3. * @file stm322xg_eval_ts.c
  4. * @author MCD Application Team
  5. * @brief This file provides a set of functions needed to manage the touch
  6. * screen on STM322xG-EVAL evaluation board.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* File Info : -----------------------------------------------------------------
  37. User NOTES
  38. 1. How To use this driver:
  39. --------------------------
  40. - This driver is used to drive the touch screen module of the STM322xG-EVAL
  41. evaluation board on the ILI9325 LCD mounted on MB785 daughter board .
  42. - The STMPE811 IO expander device component driver must be included with this
  43. driver in order to run the TS module commanded by the IO expander device
  44. mounted on the evaluation board.
  45. 2. Driver description:
  46. ---------------------
  47. + Initialization steps:
  48. o Initialize the TS module using the BSP_TS_Init() function. This
  49. function includes the MSP layer hardware resources initialization and the
  50. communication layer configuration to start the TS use. The LCD size properties
  51. (x and y) are passed as parameters.
  52. o If TS interrupt mode is desired, you must configure the TS interrupt mode
  53. by calling the function BSP_TS_ITConfig(). The TS interrupt mode is generated
  54. as an external interrupt whenever a touch is detected.
  55. + Touch screen use
  56. o The touch screen state is captured whenever the function BSP_TS_GetState() is
  57. used. This function returns information about the last LCD touch occurred
  58. in the TS_StateTypeDef structure.
  59. o If TS interrupt mode is used, the function BSP_TS_ITGetStatus() is needed to get
  60. the interrupt status. To clear the IT pending bits, you should call the
  61. function BSP_TS_ITClear().
  62. o The IT is handled using the corresponding external interrupt IRQ handler,
  63. the user IT callback treatment is implemented on the same external interrupt
  64. callback.
  65. ------------------------------------------------------------------------------*/
  66. /* Includes ------------------------------------------------------------------*/
  67. #include "stm322xg_eval_ts.h"
  68. /** @addtogroup BSP
  69. * @{
  70. */
  71. /** @addtogroup STM322xG_EVAL
  72. * @{
  73. */
  74. /** @defgroup STM322xG_EVAL_TS STM322xG EVAL TS
  75. * @{
  76. */
  77. /** @defgroup STM322xG_EVAL_TS_Private_Variables STM322xG EVAL TS Private Variables
  78. * @{
  79. */
  80. static TS_DrvTypeDef *ts_driver;
  81. static uint16_t ts_x_boundary, ts_y_boundary;
  82. static uint8_t ts_orientation;
  83. /**
  84. * @}
  85. */
  86. /** @defgroup STM322xG_EVAL_TS_Private_Function_Prototypes STM322xG EVAL TS Private Function Prototypes
  87. * @{
  88. */
  89. /**
  90. * @}
  91. */
  92. /** @defgroup STM322xG_EVAL_TS_Private_Functions STM322xG EVAL TS Private Functions
  93. * @{
  94. */
  95. /**
  96. * @brief Initializes and configures the touch screen functionalities and
  97. * configures all necessary hardware resources (GPIOs, clocks..).
  98. * @param xSize: Maximum X size of the TS area on LCD
  99. * @param ySize: Maximum Y size of the TS area on LCD
  100. * @retval TS_OK if all initializations are OK. Other value if error.
  101. */
  102. uint8_t BSP_TS_Init(uint16_t xSize, uint16_t ySize)
  103. {
  104. uint8_t ret = TS_ERROR;
  105. if(stmpe811_ts_drv.ReadID(TS_I2C_ADDRESS) == STMPE811_ID)
  106. {
  107. /* Initialize the TS driver structure */
  108. ts_driver = &stmpe811_ts_drv;
  109. /* Initialize x and y positions boundaries */
  110. ts_x_boundary = xSize;
  111. ts_y_boundary = ySize;
  112. ts_orientation = TS_SWAP_XY;
  113. ret = TS_OK;
  114. }
  115. if(ret == TS_OK)
  116. {
  117. /* Initialize the LL TS Driver */
  118. ts_driver->Init(TS_I2C_ADDRESS);
  119. ts_driver->Start(TS_I2C_ADDRESS);
  120. }
  121. return ret;
  122. }
  123. /**
  124. * @brief Configures and enables the touch screen interrupts.
  125. * @retval TS_OK if all initializations are OK. Other value if error.
  126. */
  127. uint8_t BSP_TS_ITConfig(void)
  128. {
  129. /* Call component driver to enable TS ITs */
  130. ts_driver->EnableIT(TS_I2C_ADDRESS);
  131. return TS_OK;
  132. }
  133. /**
  134. * @brief Gets the touch screen interrupt status.
  135. * @retval TS_OK if all initializations are OK. Other value if error.
  136. */
  137. uint8_t BSP_TS_ITGetStatus(void)
  138. {
  139. /* Call component driver to enable TS ITs */
  140. return (ts_driver->GetITStatus(TS_I2C_ADDRESS));
  141. }
  142. /**
  143. * @brief Returns status and positions of the touch screen.
  144. * @param TS_State: Pointer to touch screen current state structure
  145. * @retval TS_OK if all initializations are OK. Other value if error.
  146. */
  147. uint8_t BSP_TS_GetState(TS_StateTypeDef *TS_State)
  148. {
  149. static uint32_t _x = 0, _y = 0;
  150. uint16_t xDiff, yDiff , x , y;
  151. uint16_t swap;
  152. TS_State->TouchDetected = ts_driver->DetectTouch(TS_I2C_ADDRESS);
  153. if(TS_State->TouchDetected)
  154. {
  155. ts_driver->GetXY(TS_I2C_ADDRESS, &x, &y);
  156. if(ts_orientation & TS_SWAP_X)
  157. {
  158. x = 4096 - x;
  159. }
  160. if(ts_orientation & TS_SWAP_Y)
  161. {
  162. y = 4096 - y;
  163. }
  164. if(ts_orientation & TS_SWAP_XY)
  165. {
  166. swap = y;
  167. y = x;
  168. x = swap;
  169. }
  170. xDiff = x > _x? (x - _x): (_x - x);
  171. yDiff = y > _y? (y - _y): (_y - y);
  172. if (xDiff + yDiff > 5)
  173. {
  174. _x = x;
  175. _y = y;
  176. }
  177. TS_State->x = (ts_x_boundary * _x) >> 12;
  178. TS_State->y = (ts_y_boundary * _y) >> 12;
  179. }
  180. return TS_OK;
  181. }
  182. /**
  183. * @brief Clears all touch screen interrupts.
  184. */
  185. void BSP_TS_ITClear(void)
  186. {
  187. ts_driver->ClearIT(TS_I2C_ADDRESS);
  188. }
  189. /**
  190. * @}
  191. */
  192. /**
  193. * @}
  194. */
  195. /**
  196. * @}
  197. */
  198. /**
  199. * @}
  200. */
  201. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/