drv_touch.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * File : drv_touch.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2018 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2018-10-03 xuzhuoyi first implementation.
  13. */
  14. #include "drv_touch.h"
  15. #include "drivers/i2c.h"
  16. #ifdef PKG_USING_LITTLEVGL2RTT
  17. #include "littlevgl2rtt.h"
  18. #endif
  19. #define TSC_I2C_ADDR 0x41 /* 7-bit I2C address */
  20. static struct rt_i2c_bus_device *stmpe811_i2c_bus;
  21. /**
  22. \fn int32_t touch_readRegister (uint8_t reg, uint8_t *val)
  23. \brief Read register value from Touchscreen controller
  24. \param[in] reg Register address
  25. \param[out] val Pointer where data will be read from register
  26. \returns
  27. - \b 0: function succeeded
  28. - \b -1: function failed
  29. */
  30. static int32_t touch_read (uint8_t reg, uint8_t *val)
  31. {
  32. struct rt_i2c_msg msgs[2];
  33. msgs[0].addr = TSC_I2C_ADDR;
  34. msgs[0].flags = RT_I2C_WR;
  35. msgs[0].buf = ®
  36. msgs[0].len = 1;
  37. msgs[1].addr = TSC_I2C_ADDR;
  38. msgs[1].flags = RT_I2C_RD;
  39. msgs[1].buf = val;
  40. msgs[1].len = 1;
  41. if (rt_i2c_transfer(stmpe811_i2c_bus, msgs, 2) == 2)
  42. {
  43. return RT_EOK;
  44. }
  45. else
  46. {
  47. return -RT_ERROR;
  48. }
  49. }
  50. /**
  51. \fn int32_t touch_writeData (uint8_t reg, const uint8_t *val)
  52. \brief Write value to Touchscreen controller register
  53. \param[in] reg Register address
  54. \param[in] val Pointer with data to write to register
  55. \returns
  56. - \b 0: function succeeded
  57. - \b -1: function failed
  58. */
  59. static int32_t touch_write (uint8_t reg, uint8_t val)
  60. {
  61. struct rt_i2c_msg msgs;
  62. rt_uint8_t buf[2] = {reg, val};
  63. msgs.addr = TSC_I2C_ADDR;
  64. msgs.flags = RT_I2C_WR;
  65. msgs.buf = buf;
  66. msgs.len = 2;
  67. if (rt_i2c_transfer(stmpe811_i2c_bus, &msgs, 1) == 1)
  68. {
  69. return RT_EOK;
  70. }
  71. else
  72. {
  73. return -RT_ERROR;
  74. }
  75. }
  76. /**
  77. \fn int32_t Touch_Initialize (void)
  78. \brief Initialize touchscreen
  79. \returns
  80. - \b 0: function succeeded
  81. - \b -1: function failed
  82. */
  83. static rt_err_t stmpe811_touch_init(rt_device_t dev)
  84. {
  85. stmpe811_i2c_bus = rt_i2c_bus_device_find(STMPE811_I2CBUS_NAME);
  86. // ptrI2C->Initialize (NULL);
  87. // ptrI2C->PowerControl(ARM_POWER_FULL);
  88. // ptrI2C->Control (ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
  89. // ptrI2C->Control (ARM_I2C_BUS_CLEAR, 0);
  90. touch_write(STMPE811_SYS_CTRL1, 0x02); /* Reset Touch-screen controller */
  91. rt_thread_mdelay(10); /* Wait 10ms */
  92. touch_write(STMPE811_SYS_CTRL2, 0x0C); /* Enable TSC and ADC */
  93. touch_write(STMPE811_ADC_CTRL1, 0x68); /* Set sample time , 12-bit mode */
  94. rt_thread_mdelay(1); /* Wait 1ms */
  95. touch_write(STMPE811_ADC_CTRL2, 0x01); /* ADC frequency 3.25 MHz */
  96. touch_write(STMPE811_TSC_CFG, 0xC2); /* Detect delay 10us,
  97. Settle time 500us */
  98. touch_write(STMPE811_FIFO_TH, 0x01); /* Threshold for FIFO */
  99. touch_write(STMPE811_FIFO_STA, 0x01); /* FIFO reset */
  100. touch_write(STMPE811_FIFO_STA, 0x00); /* FIFO not reset */
  101. touch_write(STMPE811_TSC_FRACTION_Z, 0x07); /* Fraction z */
  102. touch_write(STMPE811_TSC_I_DRIVE, 0x01); /* Drive 50 mA typical */
  103. touch_write(STMPE811_GPIO_AF, 0x00); /* Pins are used for touchscreen */
  104. touch_write(STMPE811_TSC_CTRL, 0x01); /* Enable TSC */
  105. return 0;
  106. }
  107. /**
  108. \fn int32_t Touch_Uninitialize (void)
  109. \brief De-initialize touchscreen
  110. \returns
  111. - \b 0: function succeeded
  112. - \b -1: function failed
  113. */
  114. int32_t touch_uninitialize (void) {
  115. touch_write(STMPE811_SYS_CTRL1, 0x02); /* Reset Touch-screen controller */
  116. return 0;
  117. }
  118. /**
  119. \fn int32_t Touch_GetState (TOUCH_STATE *pState)
  120. \brief Get touchscreen state
  121. \param[out] pState pointer to TOUCH_STATE structure
  122. \returns
  123. - \b 0: function succeeded
  124. - \b -1: function failed
  125. */
  126. int32_t touch_get_state(struct touch_state *pState)
  127. {
  128. uint8_t val;
  129. uint8_t num;
  130. uint8_t xyz[4];
  131. int32_t res;
  132. struct rt_i2c_msg msgs[2];
  133. /* Read touch status */
  134. res = touch_read(STMPE811_TSC_CTRL, &val);
  135. if (res < 0) return -1;
  136. pState->pressed = (val & (1 << 7)) ? 1 : 0;
  137. if (pState->pressed)
  138. {
  139. val = STMPE811_TSC_DATA;
  140. /* If FIFO overflow, discard all samples except the last one */
  141. res = touch_read(STMPE811_FIFO_SIZE, &num);
  142. if (res < 0 || num == 0) return -1;
  143. while (num--)
  144. {
  145. msgs[0].addr = TSC_I2C_ADDR;
  146. msgs[0].flags = RT_I2C_WR;
  147. msgs[0].buf = &val;
  148. msgs[0].len = 1;
  149. //rt_i2c_transfer(stmpe811_i2c_bus, &msgs, 1);
  150. //ptrI2C->MasterTransmit (TSC_I2C_ADDR, &val, 1, true);
  151. //while (ptrI2C->GetStatus().busy);
  152. msgs[1].addr = TSC_I2C_ADDR;
  153. msgs[1].flags = RT_I2C_RD;
  154. msgs[1].buf = xyz;
  155. msgs[1].len = 4;
  156. rt_i2c_transfer(stmpe811_i2c_bus, msgs, 2);
  157. //ptrI2C->MasterReceive (TSC_I2C_ADDR, xyz, 4, false);
  158. //while (ptrI2C->GetStatus().busy);
  159. }
  160. pState->x = (int16_t)((xyz[0] << 4) | ((xyz[1] & 0xF0) >> 4));
  161. pState->y = (int16_t) (xyz[2] | ((xyz[1] & 0x0F) << 8));
  162. }
  163. else
  164. {
  165. /* Clear all data in FIFO */
  166. touch_write(STMPE811_FIFO_STA, 0x1);
  167. touch_write(STMPE811_FIFO_STA, 0x0);
  168. }
  169. return 0;
  170. }
  171. void touch_show_state()
  172. {
  173. int16_t x;
  174. int16_t y;
  175. struct touch_state ts;
  176. touch_get_state(&ts);
  177. x = (3706 - ts.x) / 14;
  178. y = (-461 + ts.y) / 10.5;
  179. rt_kprintf("[drv_touch] touch_show_state, x: %d, y: %d, pressed: %d, padding: %d\n", ts.x , ts.y, ts.pressed, ts.padding);
  180. rt_kprintf("[drv_touch] touch_show_state, phy x: %d, phy y: %d\n", x , y);
  181. }
  182. MSH_CMD_EXPORT(touch_show_state, show screen coordinate in touching);
  183. static int rt_hw_touch_init(void)
  184. {
  185. static struct rt_device touch;
  186. /* init device structure */
  187. touch.type = RT_Device_Class_Unknown;
  188. touch.init = stmpe811_touch_init;
  189. touch.user_data = RT_NULL;
  190. /* register touch device to RT-Thread */
  191. rt_device_register(&touch, "touch", RT_DEVICE_FLAG_RDWR);
  192. return RT_EOK;
  193. }
  194. INIT_BOARD_EXPORT(rt_hw_touch_init);