luat_tp_cst9220.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. #include "cst92xx_fw.h"
  8. #define LUAT_LOG_TAG "cst92xx"
  9. #include "luat_log.h"
  10. #define HYN_POWER_ON_UPDATA (0) //touch fw updata
  11. #define CST92XX_ADDRESS (0x5A)
  12. #define CST9217_CHIP_ID_CODE (0x9217)
  13. #define CST9220_CHIP_ID_CODE (0x9220)
  14. #define CST92XX_CHIP_ID (0xA7)
  15. #define CST92XX_STATUS (0x02)
  16. #define CST92XX_POINT1_REG (0x03)
  17. #define CST92XX_CONFIG_SIZE (CST92XX_CONFIG_FRESH - CST92XX_CONFIG_REG + 1)
  18. #define CST92XX_POINT_INFO_NUM (5)
  19. #define CST92XX_TOUCH_NUMBER_MIN (1)
  20. #define CST92XX_TOUCH_NUMBER_MAX (2)
  21. #define CST92XX_REFRESH_RATE_MIN (5)
  22. #define CST92XX_REFRESH_RATE_MAX (20)
  23. #define U8TO16(x1,x2) ((((x1)&0xFF)<<8)|((x2)&0xFF))
  24. #define U8TO32(x1,x2,x3,x4) ((((x1)&0xFF)<<24)|(((x2)&0xFF)<<16)|(((x3)&0xFF)<<8)|((x4)&0xFF))
  25. #define U16REV(x) ((((x)<<8)&0xFF00)|(((x)>>8)&0x00FF))
  26. #define DISABLE (0)
  27. #define ENABLE (1)
  28. enum work_mode{
  29. NOMAL_MODE = 0,
  30. GESTURE_MODE = 1,
  31. LP_MODE = 2,
  32. DEEPSLEEP = 3,
  33. DIFF_MODE = 4,
  34. RAWDATA_MODE = 5,
  35. BASELINE_MODE = 6,
  36. CALIBRATE_MODE = 7,
  37. FAC_TEST_MODE = 8,
  38. ENTER_BOOT_MODE = 0xCA,
  39. };
  40. struct tp_info{
  41. uint8_t fw_sensor_txnum;
  42. uint8_t fw_sensor_rxnum;
  43. uint8_t fw_key_num;
  44. uint8_t reserve;
  45. uint16_t fw_res_y;
  46. uint16_t fw_res_x;
  47. uint32_t fw_boot_time;
  48. uint32_t fw_project_id;
  49. uint32_t fw_chip_type;
  50. uint32_t fw_ver;
  51. uint32_t ic_fw_checksum;
  52. uint32_t fw_module_id;
  53. };
  54. typedef struct hyn_ts_data {
  55. enum work_mode work_mode;
  56. struct tp_info hw_info;
  57. int boot_is_pass;
  58. int need_updata_fw;
  59. }hyn_ts_data_t;
  60. static hyn_ts_data_t hyn_92xxdata = {0};
  61. typedef struct luat_touch_info{
  62. union{
  63. struct {
  64. uint64_t version:8;
  65. uint64_t x_max:16;
  66. uint64_t y_max:16;
  67. uint64_t touch_num:4;
  68. uint64_t :4;
  69. uint64_t int_type:2;
  70. uint64_t :1;
  71. uint64_t x2y:1;
  72. uint64_t stretch_rank:2;
  73. uint64_t :2;
  74. uint64_t :8;
  75. };
  76. uint64_t info;
  77. };
  78. }luat_tp_info_t;
  79. static uint8_t cst92xx_init_state = 0;
  80. static int hyn_wr_reg(luat_tp_config_t* luat_tp_config, uint32_t reg_addr, uint8_t reg_len, uint8_t *rbuf, uint16_t rlen){
  81. int ret = 0,i=0;
  82. uint8_t wbuf[4] = {0};
  83. i = reg_len;
  84. while(i){
  85. i--;
  86. wbuf[i] = reg_addr;
  87. reg_addr >>= 8;
  88. }
  89. if (luat_tp_config->soft_i2c != NULL){
  90. ret = i2c_soft_send(luat_tp_config->soft_i2c, luat_tp_config->address, (char *)wbuf, reg_len, 1);
  91. }else{
  92. ret = luat_i2c_send(luat_tp_config->i2c_id, luat_tp_config->address, wbuf, reg_len, 1);
  93. }
  94. if(rlen){
  95. if (luat_tp_config->soft_i2c != NULL){
  96. ret |= i2c_soft_recv(luat_tp_config->soft_i2c, luat_tp_config->address, (char *)rbuf, rlen);
  97. }else{
  98. ret |= luat_i2c_recv(luat_tp_config->i2c_id, luat_tp_config->address, rbuf, rlen);
  99. }
  100. }
  101. return ret;
  102. }
  103. static int tp_cst92xx_detect(luat_tp_config_t* luat_tp_config){
  104. struct tp_info *ic = &hyn_92xxdata.hw_info;
  105. // LLOGD("module_id: 0x%04x", ic->fw_chip_type);
  106. if ((ic->fw_chip_type != CST9217_CHIP_ID_CODE) && (ic->fw_chip_type != CST9220_CHIP_ID_CODE)){
  107. LLOGE("fw_chip_type error 0x%04x", ic->fw_chip_type);
  108. return -1;
  109. }else{
  110. LLOGI("TP find device CST9220 ,address:0x%02X",luat_tp_config->address);
  111. }
  112. return 0;
  113. }
  114. static int luat_tp_irq_cb(int pin, void *args){
  115. if (cst92xx_init_state == 0){
  116. return -1;
  117. }
  118. luat_tp_config_t* luat_tp_config = (luat_tp_config_t*)args;
  119. luat_tp_irq_enable(luat_tp_config, 0);
  120. luat_rtos_message_send(luat_tp_config->task_handle, 1, args);
  121. return 0;
  122. }
  123. static int tp_cst92xx_set_workmode(luat_tp_config_t* luat_tp_config, enum work_mode mode,uint8_t enable){
  124. int ret = -1;
  125. uint8_t i2c_buf[4] = {0};
  126. uint8_t i = 0;
  127. hyn_92xxdata.work_mode = mode;
  128. if(mode != NOMAL_MODE){
  129. }
  130. for(i=0;i<3;i++){
  131. luat_rtos_task_sleep(2);
  132. if (hyn_wr_reg(luat_tp_config,0xD11E,2,i2c_buf,0)) {
  133. luat_rtos_task_sleep(1);
  134. continue;
  135. }
  136. luat_rtos_task_sleep(2);
  137. if (hyn_wr_reg(luat_tp_config,0x0002,2,i2c_buf,2)) {
  138. luat_rtos_task_sleep(1);
  139. continue;
  140. }
  141. if(i2c_buf[1] == 0x1E){
  142. break;
  143. }
  144. }
  145. switch(mode){
  146. case NOMAL_MODE:
  147. luat_tp_irq_enable(luat_tp_config, 1);
  148. if (hyn_wr_reg(luat_tp_config,0xD109,2,i2c_buf,0)) {
  149. return -1;
  150. }
  151. break;
  152. case GESTURE_MODE:
  153. if (hyn_wr_reg(luat_tp_config,0xD104,2,i2c_buf,0)) {
  154. return -1;
  155. }
  156. break;
  157. case LP_MODE:
  158. if (hyn_wr_reg(luat_tp_config,0xD107,2,i2c_buf,0)) {
  159. return -1;
  160. }
  161. break;
  162. case DIFF_MODE:
  163. if (hyn_wr_reg(luat_tp_config,0xD10D,2,i2c_buf,0)) {
  164. return -1;
  165. }
  166. break;
  167. case RAWDATA_MODE:
  168. if (hyn_wr_reg(luat_tp_config,0xD10A,2,i2c_buf,0)) {
  169. return -1;
  170. }
  171. break;
  172. case FAC_TEST_MODE:
  173. hyn_wr_reg(luat_tp_config,0xD114,2,i2c_buf,0);
  174. break;
  175. case DEEPSLEEP:
  176. hyn_wr_reg(luat_tp_config,0xD105,2,i2c_buf,0);
  177. break;
  178. default :
  179. hyn_92xxdata.work_mode = NOMAL_MODE;
  180. break;
  181. }
  182. return 0;
  183. }
  184. static int tp_cst92xx_updata_tpinfo(luat_tp_config_t* luat_tp_config){
  185. uint8_t buf[30] = {0};
  186. struct tp_info *ic = &hyn_92xxdata.hw_info;
  187. tp_cst92xx_set_workmode(luat_tp_config, 0xff,DISABLE);
  188. if(hyn_wr_reg(luat_tp_config,0xD101,2,buf,0)){
  189. return -1;
  190. }
  191. luat_rtos_task_sleep(5);
  192. //firmware_project_id firmware_ic_type
  193. if(hyn_wr_reg(luat_tp_config,0xD1F4,2,buf,28)){
  194. return -1;
  195. }
  196. ic->fw_project_id = ((uint16_t)buf[17] <<8) + buf[16];
  197. ic->fw_chip_type = ((uint16_t)buf[19] <<8) + buf[18];
  198. //firmware_version
  199. ic->fw_ver = (buf[23]<<24)|(buf[22]<<16)|(buf[21]<<8)|buf[20];
  200. //tx_num rx_num key_num
  201. ic->fw_sensor_txnum = ((uint16_t)buf[1]<<8) + buf[0];
  202. ic->fw_sensor_rxnum = buf[2];
  203. ic->fw_key_num = buf[3];
  204. ic->fw_res_y = (buf[7]<<8)|buf[6];
  205. ic->fw_res_x = (buf[5]<<8)|buf[4];
  206. //fw_checksum
  207. ic->ic_fw_checksum = (buf[27]<<24)|(buf[26]<<16)|(buf[25]<<8)|buf[24];
  208. LLOGD("IC_info project_id:%04x ictype:%04x fw_ver:%x checksum:%#x",ic->fw_project_id,ic->fw_chip_type,ic->fw_ver,ic->ic_fw_checksum);
  209. tp_cst92xx_set_workmode(luat_tp_config,NOMAL_MODE,ENABLE);
  210. return 0;
  211. }
  212. static int tp_cst92xx_hw_reset(luat_tp_config_t* luat_tp_config){
  213. if (luat_tp_config->pin_rst != LUAT_GPIO_NONE){
  214. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_LOW);
  215. luat_rtos_task_sleep(8);
  216. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_HIGH);
  217. }
  218. return 0;
  219. }
  220. static int16_t read_word_from_mem(luat_tp_config_t* luat_tp_config, uint8_t type, uint16_t addr, uint32_t *value){
  221. uint8_t i2c_buf[4] = {0};
  222. i2c_buf[0] = 0xA0;
  223. i2c_buf[1] = 0x10;
  224. i2c_buf[2] = type;
  225. if (tp_i2c_write(luat_tp_config, i2c_buf, 3, NULL, 0)){
  226. return -1;
  227. }
  228. i2c_buf[0] = 0xA0;
  229. i2c_buf[1] = 0x0C;
  230. i2c_buf[2] = addr;
  231. i2c_buf[3] = addr >> 8;
  232. if (tp_i2c_write(luat_tp_config, i2c_buf, 4, NULL, 0)){
  233. return -1;
  234. }
  235. i2c_buf[0] = 0xA0;
  236. i2c_buf[1] = 0x04;
  237. i2c_buf[2] = 0xE4;
  238. if (tp_i2c_write(luat_tp_config, i2c_buf, 3, NULL, 0)){
  239. return -1;
  240. }
  241. for (uint8_t t = 0;; t++){
  242. if (t >= 100){
  243. return -1;
  244. }
  245. if (hyn_wr_reg(luat_tp_config, 0xA004,2,i2c_buf,1)){
  246. continue;
  247. }
  248. if (i2c_buf[0] == 0x00){
  249. break;
  250. }
  251. }
  252. if (hyn_wr_reg(luat_tp_config, 0xA018,2, i2c_buf, 4)){
  253. return -1;
  254. }
  255. *value = ((uint32_t)(i2c_buf[0])) |
  256. (((uint32_t)(i2c_buf[1])) << 8) |
  257. (((uint32_t)(i2c_buf[2])) << 16) |
  258. (((uint32_t)(i2c_buf[3])) << 24);
  259. return 0;
  260. }
  261. static int tp_cst92xx_enter_boot(luat_tp_config_t* luat_tp_config){
  262. uint8_t i2c_buf[4] = {0};
  263. for (uint8_t t = 10; t < 30; t += 2){
  264. tp_cst92xx_hw_reset(luat_tp_config);
  265. luat_rtos_task_sleep(t);
  266. if(hyn_wr_reg(luat_tp_config, 0xA001AA, 3, i2c_buf, 0)){
  267. continue;
  268. }
  269. luat_rtos_task_sleep(1);
  270. if(hyn_wr_reg(luat_tp_config, 0xA002, 2, i2c_buf, 2)){
  271. continue;
  272. }
  273. if ((i2c_buf[0] == 0x55) && (i2c_buf[1] == 0xB0)) {
  274. break;
  275. }
  276. }
  277. if(hyn_wr_reg(luat_tp_config, 0xA00100, 3, i2c_buf, 0)){
  278. return -1;
  279. }
  280. return 0;
  281. }
  282. static int tp_cst92xx_read_chip_id(luat_tp_config_t* luat_tp_config){
  283. int16_t ret = 0;
  284. uint8_t retry = 3;
  285. uint32_t partno_chip_type,module_id;
  286. if (tp_cst92xx_enter_boot(luat_tp_config)){
  287. LLOGE("enter_bootloader error");
  288. return -1;
  289. }
  290. for (; retry > 0; retry--){
  291. // partno
  292. ret = read_word_from_mem(luat_tp_config, 1, 0x077C, &partno_chip_type);
  293. if (ret){
  294. continue;
  295. }
  296. // module id
  297. ret = read_word_from_mem(luat_tp_config, 0, 0x7FC0, &module_id);
  298. if (ret){
  299. continue;
  300. }
  301. if ((partno_chip_type >> 16) == 0xCACA){
  302. partno_chip_type &= 0xffff;
  303. break;
  304. }
  305. }
  306. tp_cst92xx_hw_reset(luat_tp_config);
  307. luat_rtos_task_sleep(30);
  308. LLOGD("partno_chip_type: 0x%04x", partno_chip_type);
  309. LLOGD("module_id: 0x%04x", module_id);
  310. if ((partno_chip_type != CST9217_CHIP_ID_CODE) && (partno_chip_type != CST9220_CHIP_ID_CODE)){
  311. LLOGE("partno_chip_type error 0x%04x", partno_chip_type);
  312. // return -1;
  313. }
  314. return 0;
  315. }
  316. static uint32_t cst92xx_read_checksum(luat_tp_config_t* luat_tp_config){
  317. uint8_t i2c_buf[4] = {0};
  318. uint32_t chip_checksum = 0;
  319. uint8_t retry = 5;
  320. hyn_92xxdata.boot_is_pass = 0;
  321. if (hyn_wr_reg(luat_tp_config,0xA00300,3,i2c_buf,0)) {
  322. return -1;
  323. }
  324. luat_rtos_task_sleep(2);
  325. while(retry--){
  326. luat_rtos_task_sleep(5);
  327. if (hyn_wr_reg(luat_tp_config,0xA000,2,i2c_buf,1)) {
  328. continue;
  329. }
  330. if(i2c_buf[0]!=0) break;
  331. }
  332. luat_rtos_task_sleep(1);
  333. if(i2c_buf[0] == 0x01){
  334. hyn_92xxdata.boot_is_pass = 1;
  335. memset(i2c_buf,0,sizeof(i2c_buf));
  336. if (hyn_wr_reg(luat_tp_config,0xA008,2,i2c_buf,4)) {
  337. return -1;
  338. }
  339. chip_checksum = ((uint32_t)(i2c_buf[0])) |
  340. (((uint32_t)(i2c_buf[1])) << 8) |
  341. (((uint32_t)(i2c_buf[2])) << 16) |
  342. (((uint32_t)(i2c_buf[3])) << 24);
  343. }
  344. else{
  345. hyn_92xxdata.need_updata_fw = 1;
  346. }
  347. return chip_checksum;
  348. }
  349. static int cst92xx_updata_judge(luat_tp_config_t* luat_tp_config, uint8_t *p_fw, uint16_t len){
  350. uint32_t f_checksum,f_fw_ver,f_ictype,f_fw_project_id;
  351. uint8_t *p_data = p_fw + len - 28; //7F64
  352. struct tp_info *ic = &hyn_92xxdata.hw_info;
  353. if (tp_cst92xx_enter_boot(luat_tp_config)){
  354. LLOGI("cst92xx_enter_boot fail,need update");
  355. return -1;
  356. }
  357. hyn_92xxdata.hw_info.ic_fw_checksum = cst92xx_read_checksum(luat_tp_config);
  358. if(hyn_92xxdata.boot_is_pass == 0){
  359. LLOGI("boot_is_pass %d,need force update",hyn_92xxdata.boot_is_pass);
  360. return -1; //need updata
  361. }
  362. f_fw_project_id = U8TO16(p_data[1],p_data[0]);
  363. f_ictype = U8TO16(p_data[3],p_data[2]);
  364. f_fw_ver = U8TO16(p_data[7],p_data[6]);
  365. f_fw_ver = (f_fw_ver<<16)|U8TO16(p_data[5],p_data[4]);
  366. f_checksum = U8TO16(p_data[11],p_data[10]);
  367. f_checksum = (f_checksum << 16)|U8TO16(p_data[9],p_data[8]);
  368. LLOGI("Bin_info project_id:0x%04x ictype:0x%04x fw_ver:0x%x checksum:0x%x",f_fw_project_id,f_ictype,f_fw_ver,f_checksum);
  369. if(f_ictype != ic->fw_chip_type || f_fw_project_id != ic->fw_project_id){
  370. LLOGE("not update,please confirm: ic_type 0x%04x,ic_project_id 0x%04x",ic->fw_chip_type,ic->fw_project_id);
  371. return 0; //not updata
  372. }
  373. if(f_checksum != ic->ic_fw_checksum && f_fw_ver > ic->fw_ver){
  374. LLOGI("need update!");
  375. return -1; //need updata
  376. }
  377. LLOGI("cst92xx_updata_judge done, no need update");
  378. return 0;
  379. }
  380. static int tp_cst92xx_gpio_init(luat_tp_config_t* luat_tp_config){
  381. luat_gpio_mode(luat_tp_config->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  382. luat_gpio_mode(luat_tp_config->pin_int, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  383. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_HIGH);
  384. luat_gpio_set(luat_tp_config->pin_int, Luat_GPIO_HIGH);
  385. luat_tp_config->int_type = Luat_GPIO_FALLING;
  386. luat_gpio_t gpio = {0};
  387. gpio.pin = luat_tp_config->pin_int;
  388. gpio.mode = Luat_GPIO_IRQ;
  389. gpio.pull = Luat_GPIO_PULLUP;
  390. gpio.irq = luat_tp_config->int_type;
  391. gpio.irq_cb = luat_tp_irq_cb;
  392. gpio.irq_args = luat_tp_config;
  393. luat_gpio_setup(&gpio);
  394. luat_rtos_task_sleep(10);
  395. return 0;
  396. }
  397. static int tp_cst92xx_init(luat_tp_config_t* luat_tp_config){
  398. int ret = 0;
  399. luat_rtos_task_sleep(50);
  400. luat_tp_config->address = CST92XX_ADDRESS;
  401. tp_cst92xx_gpio_init(luat_tp_config);
  402. tp_cst92xx_hw_reset(luat_tp_config);
  403. luat_rtos_task_sleep(40);
  404. #if HYN_POWER_ON_UPDATA
  405. if(tp_cst92xx_read_chip_id(luat_tp_config)){
  406. LLOGE("cst92xx_read_chip_id failed");
  407. return ret;
  408. }
  409. ret = tp_cst92xx_updata_tpinfo(luat_tp_config);
  410. if(ret){
  411. LLOGE("cst92xx_updata_tpinfo failed");
  412. return ret;
  413. }
  414. cst92xx_updata_judge(luat_tp_config,(uint8_t*)fw_bin,CST92XX_BIN_SIZE);
  415. tp_cst92xx_hw_reset(luat_tp_config);
  416. luat_rtos_task_sleep(40);
  417. #endif
  418. ret = tp_cst92xx_updata_tpinfo(luat_tp_config);
  419. if(ret){
  420. LLOGE("cst92xx_updata_tpinfo failed");
  421. return ret;
  422. }
  423. ret = tp_cst92xx_detect(luat_tp_config);
  424. if (ret){
  425. LLOGE("tp detect fail!");
  426. return ret;
  427. }
  428. ret |= tp_cst92xx_set_workmode(luat_tp_config, NOMAL_MODE,0);
  429. luat_rtos_task_sleep(10);
  430. cst92xx_init_state = 1;
  431. return ret;
  432. }
  433. static int tp_cst92xx_deinit(luat_tp_config_t* luat_tp_config){
  434. cst92xx_init_state = 0;
  435. if (luat_tp_config->pin_int != LUAT_GPIO_NONE){
  436. luat_gpio_close(luat_tp_config->pin_int);
  437. }
  438. if (luat_tp_config->pin_rst != LUAT_GPIO_NONE){
  439. luat_gpio_close(luat_tp_config->pin_rst);
  440. }
  441. return 0;
  442. }
  443. static void tp_cst92xx_read_done(luat_tp_config_t * luat_tp_config){
  444. luat_tp_irq_enable(luat_tp_config, 1);
  445. }
  446. // cst92xx get tp info.
  447. typedef struct {
  448. uint8_t switch_ : 4;
  449. uint8_t id : 4;
  450. uint8_t x_h : 4;
  451. uint8_t : 4;
  452. uint8_t y_h : 4;
  453. uint8_t : 4;
  454. uint8_t y_l : 4;
  455. uint8_t x_l : 4;
  456. uint8_t z : 7;
  457. uint8_t : 1;
  458. } point_data_t;
  459. static int16_t pre_x[CST92XX_TOUCH_NUMBER_MAX] = {-1, -1};
  460. static int16_t pre_y[CST92XX_TOUCH_NUMBER_MAX] = {-1, -1};
  461. static int16_t pre_w[CST92XX_TOUCH_NUMBER_MAX] = {-1, -1};
  462. static uint8_t s_tp_down[CST92XX_TOUCH_NUMBER_MAX];
  463. static uint8_t read_buff[CST92XX_POINT_INFO_NUM * CST92XX_TOUCH_NUMBER_MAX + 5] = {0};
  464. void cst92xx_touch_up(void *buf, int8_t id){
  465. luat_tp_data_t *read_data = (luat_tp_data_t *)buf;
  466. if(s_tp_down[id] == 1){
  467. s_tp_down[id] = 0;
  468. read_data[id].event = TP_EVENT_TYPE_UP;
  469. }else{
  470. read_data[id].event = TP_EVENT_TYPE_NONE;
  471. }
  472. read_data[id].timestamp = luat_mcu_ticks();
  473. read_data[id].width = pre_w[id];
  474. read_data[id].x_coordinate = pre_x[id];
  475. read_data[id].y_coordinate = pre_y[id];
  476. read_data[id].track_id = id;
  477. pre_x[id] = -1; /* last point is none */
  478. pre_y[id] = -1;
  479. pre_w[id] = -1;
  480. }
  481. void cst92xx_touch_down(void *buf, int8_t id, int16_t x, int16_t y, int16_t w){
  482. luat_tp_data_t *read_data = (luat_tp_data_t *)buf;
  483. if (s_tp_down[id] == 1){
  484. read_data[id].event = TP_EVENT_TYPE_MOVE;
  485. }else{
  486. read_data[id].event = TP_EVENT_TYPE_DOWN;
  487. s_tp_down[id] = 1;
  488. }
  489. read_data[id].timestamp = luat_mcu_ticks();
  490. read_data[id].width = w;
  491. read_data[id].x_coordinate = x;
  492. read_data[id].y_coordinate = y;
  493. read_data[id].track_id = id;
  494. pre_x[id] = x; /* save last point */
  495. pre_y[id] = y;
  496. pre_w[id] = w;
  497. }
  498. void cst92xx_read_point(uint8_t *input_buff, void *buf, uint8_t touch_num){
  499. uint8_t *read_buf = input_buff;
  500. uint8_t read_index;
  501. int8_t read_id = 0;
  502. int16_t input_x = 0;
  503. int16_t input_y = 0;
  504. int16_t input_w = 0;
  505. static uint8_t pre_touch = 0;
  506. static int8_t pre_id[CST92XX_TOUCH_NUMBER_MAX] = {0};
  507. if (pre_touch > touch_num){ /* point up */
  508. for (read_index = 0; read_index < pre_touch; read_index++){
  509. uint8_t j;
  510. for (j = 0; j < touch_num; j++){ /* this time touch num */
  511. read_id = j;
  512. if (read_id >= CST92XX_POINT_INFO_NUM){
  513. LLOGE("%s, touch ID %d is out range!\r\n", __func__, read_id);
  514. return;
  515. }
  516. if (pre_id[read_index] == read_id) /* this id is not free */
  517. break;
  518. if (j >= touch_num - 1){
  519. uint8_t up_id;
  520. up_id = pre_id[read_index];
  521. cst92xx_touch_up(buf, up_id);
  522. }
  523. }
  524. }
  525. }
  526. if (touch_num){ /* point down */
  527. uint8_t off_set = 0;
  528. for (read_index = 0; read_index < touch_num; read_index++){
  529. if (read_index){
  530. off_set = read_index * CST92XX_POINT_INFO_NUM + 2;
  531. }
  532. point_data_t* point_buff = &read_buf[off_set];
  533. read_id = point_buff->id;
  534. if (read_id >= CST92XX_POINT_INFO_NUM){
  535. LLOGE("%s, touch ID %d is out range!", __func__, read_id);
  536. return;
  537. }
  538. pre_id[read_index] = read_id;
  539. input_x = point_buff->x_h << 4 | point_buff->x_l; /* x */
  540. input_y = point_buff->y_h << 4 | point_buff->y_l; /* y */
  541. // LLOGD("%s, id %d switch:0x%02x x:%d y:%d z:%d", __func__, point_buff->id,point_buff->switch_,input_x,input_y,point_buff->z);
  542. if (point_buff->switch_ == 0){
  543. cst92xx_touch_up(buf, pre_id[read_index]);
  544. }else{
  545. cst92xx_touch_down(buf, read_id, input_x, input_y, input_w);
  546. }
  547. }
  548. }else if (pre_touch){
  549. for(read_index = 0; read_index < pre_touch; read_index++){
  550. cst92xx_touch_up(buf, pre_id[read_index]);
  551. }
  552. }
  553. pre_touch = touch_num;
  554. }
  555. static int tp_cst92xx_read(luat_tp_config_t* luat_tp_config, luat_tp_data_t *luat_tp_data){
  556. uint8_t touch_num=0, point_status=0;
  557. // uint8_t i2c_buf[CST92XX_TOUCH_NUMBER_MAX*5+5] = {0};
  558. memset(read_buff, 0x00, sizeof(read_buff));
  559. if (hyn_wr_reg(luat_tp_config, 0xD000,2,read_buff,sizeof(read_buff))){
  560. goto exit_;
  561. }
  562. if (hyn_wr_reg(luat_tp_config, 0xD000AB,3,read_buff,0)){
  563. goto exit_;
  564. }
  565. // luat_rtos_task_sleep(8);
  566. // for (size_t i = 0; i < sizeof(read_buff); i++){
  567. // LLOGD("read_buff[%d] = 0x%02X", i, read_buff[i]);
  568. // }
  569. if (read_buff[6] != 0xAB) {
  570. // LLOGE("fail buf[6]=0x%02x",i2c_buf[6]);
  571. goto exit_;
  572. }
  573. touch_num = read_buff[5] & 0x7F;
  574. if (touch_num > CST92XX_TOUCH_NUMBER_MAX) {
  575. LLOGE("fail touch_num=%d",touch_num);
  576. goto exit_;
  577. }
  578. // LLOGD("touch_num = %d",touch_num);
  579. if (touch_num > CST92XX_TOUCH_NUMBER_MAX) {/* point num is not correct */
  580. touch_num = 0;
  581. goto exit_;
  582. }
  583. if (touch_num){
  584. // tp_i2c_read_reg8(luat_tp_config, CST92XX_POINT1_REG, read_buff, touch_num * CST92XX_POINT_INFO_NUM, 0);
  585. }else{
  586. memset(read_buff, 0x00, sizeof(read_buff));
  587. }
  588. cst92xx_read_point(read_buff, luat_tp_data, touch_num);
  589. exit_:
  590. return touch_num;
  591. }
  592. luat_tp_opts_t tp_config_cst92xx = {
  593. .name = "cst92xx",
  594. .init = tp_cst92xx_init,
  595. .deinit = tp_cst92xx_deinit,
  596. .read = tp_cst92xx_read,
  597. .read_done = tp_cst92xx_read_done,
  598. };