luat_tp_cst820.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "luat_base.h"
  2. #include "luat_tp.h"
  3. #include "luat_gpio.h"
  4. #include "luat_mem.h"
  5. #include "luat_rtos.h"
  6. #include "luat_tp_reg.h"
  7. #define LUAT_LOG_TAG "cst820"
  8. #include "luat_log.h"
  9. #define CST820_ADDRESS (0x15)
  10. #define CST820_CHIP_ID_CODE (0xB7)
  11. #define CST820_CHIP_ID (0xA7)
  12. #define CST820_STATUS (0x02)
  13. #define CST820_POINT1_REG (0x03)
  14. #define CST820_NORSCANPER_REG (0xEE)
  15. #define CST820_POINT_INFO_NUM (4)
  16. #define CST820_TOUCH_NUMBER_MIN (1)
  17. #define CST820_TOUCH_NUMBER_MAX (1)
  18. #define CST820_REFRESH_RATE_MIN (5)
  19. #define CST820_REFRESH_RATE_MAX (20)
  20. typedef struct luat_touch_info{
  21. union{
  22. struct {
  23. uint64_t version:8;
  24. uint64_t x_max:16;
  25. uint64_t y_max:16;
  26. uint64_t touch_num:4;
  27. uint64_t :4;
  28. uint64_t int_type:2;
  29. uint64_t :1;
  30. uint64_t x2y:1;
  31. uint64_t stretch_rank:2;
  32. uint64_t :2;
  33. uint64_t :8;
  34. };
  35. uint64_t info;
  36. };
  37. }luat_tp_info_t;
  38. static uint8_t cst820_init_state = 0;
  39. static int tp_cst820_detect(luat_tp_config_t* luat_tp_config){
  40. uint8_t chip_id = 0;
  41. luat_tp_config->address = CST820_ADDRESS;
  42. tp_i2c_read_reg8(luat_tp_config, CST820_CHIP_ID, &chip_id, 1, 0);
  43. if (chip_id == CST820_CHIP_ID_CODE){
  44. LLOGI("TP find device CST820 ,address:0x%02X",luat_tp_config->address);
  45. return 0;
  46. }else{
  47. return -1;
  48. }
  49. }
  50. static int luat_tp_irq_cb(int pin, void *args){
  51. if (cst820_init_state == 0){
  52. return -1;
  53. }
  54. luat_tp_config_t* luat_tp_config = (luat_tp_config_t*)args;
  55. luat_tp_irq_enable(luat_tp_config, 0);
  56. luat_rtos_message_send(luat_tp_config->task_handle, 1, args);
  57. return 0;
  58. }
  59. static int tp_cst820_hw_reset(luat_tp_config_t* luat_tp_config){
  60. if (luat_tp_config->pin_rst != LUAT_GPIO_NONE){
  61. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_LOW);
  62. luat_rtos_task_sleep(10);
  63. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_HIGH);
  64. luat_rtos_task_sleep(100);
  65. }
  66. return 0;
  67. }
  68. static int tp_cst820_gpio_init(luat_tp_config_t* luat_tp_config){
  69. luat_gpio_mode(luat_tp_config->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  70. luat_gpio_mode(luat_tp_config->pin_int, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  71. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_HIGH);
  72. luat_gpio_set(luat_tp_config->pin_int, Luat_GPIO_HIGH);
  73. tp_cst820_hw_reset(luat_tp_config);
  74. return 0;
  75. }
  76. static int tp_cst820_init(luat_tp_config_t* luat_tp_config){
  77. int ret = 0;
  78. luat_rtos_task_sleep(100);
  79. tp_cst820_gpio_init(luat_tp_config);
  80. luat_rtos_task_sleep(10);
  81. ret = tp_cst820_detect(luat_tp_config);
  82. if (ret){
  83. LLOGE("tp detect fail!");
  84. return ret;
  85. }
  86. luat_rtos_task_sleep(20);
  87. tp_i2c_write_reg8(luat_tp_config, CST820_NORSCANPER_REG, (uint8_t[]){0x02}, 1);
  88. luat_tp_config->int_type = Luat_GPIO_FALLING;
  89. luat_gpio_t gpio = {0};
  90. gpio.pin = luat_tp_config->pin_int;
  91. gpio.mode = Luat_GPIO_IRQ;
  92. gpio.pull = Luat_GPIO_PULLUP;
  93. gpio.irq = luat_tp_config->int_type;
  94. gpio.irq_cb = luat_tp_irq_cb;
  95. gpio.irq_args = luat_tp_config;
  96. luat_gpio_setup(&gpio);
  97. cst820_init_state = 1;
  98. return ret;
  99. }
  100. static int tp_cst820_deinit(luat_tp_config_t* luat_tp_config){
  101. cst820_init_state = 0;
  102. if (luat_tp_config->pin_int != LUAT_GPIO_NONE){
  103. luat_gpio_close(luat_tp_config->pin_int);
  104. }
  105. if (luat_tp_config->pin_rst != LUAT_GPIO_NONE){
  106. luat_gpio_close(luat_tp_config->pin_rst);
  107. }
  108. return 0;
  109. }
  110. static void tp_cst820_read_done(luat_tp_config_t * luat_tp_config){
  111. luat_tp_irq_enable(luat_tp_config, 1);
  112. }
  113. // cst820 get tp info.
  114. typedef struct {
  115. uint8_t x_h : 4;
  116. uint8_t : 2;
  117. uint8_t event: 2;
  118. uint8_t x_l;
  119. uint8_t y_h : 4;
  120. uint8_t id : 4;
  121. uint8_t y_l;
  122. } point_data_t;
  123. static int16_t pre_x[CST820_TOUCH_NUMBER_MAX] = {-1};
  124. static int16_t pre_y[CST820_TOUCH_NUMBER_MAX] = {-1};
  125. static int16_t pre_w[CST820_TOUCH_NUMBER_MAX] = {-1};
  126. static uint8_t s_tp_down[CST820_TOUCH_NUMBER_MAX];
  127. static uint8_t read_buff[CST820_POINT_INFO_NUM * CST820_TOUCH_NUMBER_MAX];
  128. void cst820_touch_up(void *buf, int8_t id){
  129. luat_tp_data_t *read_data = (luat_tp_data_t *)buf;
  130. if(s_tp_down[id] == 1){
  131. s_tp_down[id] = 0;
  132. read_data[id].event = TP_EVENT_TYPE_UP;
  133. }else{
  134. read_data[id].event = TP_EVENT_TYPE_NONE;
  135. }
  136. read_data[id].timestamp = luat_mcu_ticks();
  137. read_data[id].width = pre_w[id];
  138. read_data[id].x_coordinate = pre_x[id];
  139. read_data[id].y_coordinate = pre_y[id];
  140. read_data[id].track_id = id;
  141. pre_x[id] = -1; /* last point is none */
  142. pre_y[id] = -1;
  143. pre_w[id] = -1;
  144. }
  145. void cst820_touch_down(void *buf, int8_t id, int16_t x, int16_t y, int16_t w){
  146. luat_tp_data_t *read_data = (luat_tp_data_t *)buf;
  147. if (s_tp_down[id] == 1){
  148. read_data[id].event = TP_EVENT_TYPE_MOVE;
  149. }else{
  150. read_data[id].event = TP_EVENT_TYPE_DOWN;
  151. s_tp_down[id] = 1;
  152. }
  153. read_data[id].timestamp = luat_mcu_ticks();
  154. read_data[id].width = w;
  155. read_data[id].x_coordinate = x;
  156. read_data[id].y_coordinate = y;
  157. read_data[id].track_id = id;
  158. pre_x[id] = x; /* save last point */
  159. pre_y[id] = y;
  160. pre_w[id] = w;
  161. }
  162. void cst820_read_point(uint8_t *input_buff, void *buf, uint8_t touch_num){
  163. uint8_t *read_buf = input_buff;
  164. uint8_t read_index;
  165. int8_t read_id = 0;
  166. int16_t input_x = 0;
  167. int16_t input_y = 0;
  168. int16_t input_w = 0;
  169. static uint8_t pre_touch = 0;
  170. static int8_t pre_id[CST820_TOUCH_NUMBER_MAX] = {0};
  171. if (pre_touch > touch_num){ /* point up */
  172. for (read_index = 0; read_index < pre_touch; read_index++){
  173. uint8_t j;
  174. for (j = 0; j < touch_num; j++){ /* this time touch num */
  175. read_id = j;
  176. if (read_id >= CST820_POINT_INFO_NUM){
  177. LLOGE("%s, touch ID %d is out range!\r\n", __func__, read_id);
  178. return;
  179. }
  180. if (pre_id[read_index] == read_id) /* this id is not free */
  181. break;
  182. if (j >= touch_num - 1){
  183. uint8_t up_id;
  184. up_id = pre_id[read_index];
  185. cst820_touch_up(buf, up_id);
  186. }
  187. }
  188. }
  189. }
  190. if (touch_num){ /* point down */
  191. uint8_t off_set;
  192. for (read_index = 0; read_index < touch_num; read_index++){
  193. off_set = read_index * CST820_POINT_INFO_NUM;
  194. point_data_t* point_buff = &read_buf[off_set];
  195. read_id = point_buff->id;
  196. if (read_id >= CST820_POINT_INFO_NUM){
  197. LLOGE("%s, touch ID %d is out range!\r\n", __func__, read_id);
  198. return;
  199. }
  200. pre_id[read_index] = read_id;
  201. // LLOGD("%s, id %d event:%d ", __func__, point_buff->id,point_buff->event);
  202. input_x = point_buff->x_h << 8 | point_buff->x_l; /* x */
  203. input_y = point_buff->y_h << 8 | point_buff->y_l; /* y */
  204. cst820_touch_down(buf, read_id, input_x, input_y, input_w);
  205. }
  206. }else if (pre_touch){
  207. for(read_index = 0; read_index < pre_touch; read_index++){
  208. cst820_touch_up(buf, pre_id[read_index]);
  209. }
  210. }
  211. pre_touch = touch_num;
  212. }
  213. static int tp_cst820_read(luat_tp_config_t* luat_tp_config, luat_tp_data_t *luat_tp_data){
  214. uint8_t touch_num=0;
  215. tp_i2c_read_reg8(luat_tp_config, CST820_STATUS, &touch_num, 1, 0);
  216. // LLOGD("touch_num = %d",touch_num);
  217. if (touch_num > CST820_TOUCH_NUMBER_MAX) {/* point num is not correct */
  218. touch_num = 0;
  219. goto exit_;
  220. }
  221. memset(read_buff, 0x00, sizeof(read_buff));
  222. if (touch_num){
  223. tp_i2c_read_reg8(luat_tp_config, CST820_POINT1_REG, read_buff, touch_num * CST820_POINT_INFO_NUM, 0);
  224. }
  225. cst820_read_point(read_buff, luat_tp_data, touch_num);
  226. exit_:
  227. return touch_num;
  228. }
  229. luat_tp_opts_t tp_config_cst820 = {
  230. .name = "cst820",
  231. .init = tp_cst820_init,
  232. .deinit = tp_cst820_deinit,
  233. .read = tp_cst820_read,
  234. .read_done = tp_cst820_read_done,
  235. };