drv_touch.c 6.2 KB

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