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