luat_lcd_jd9261t.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "luat_base.h"
  2. #include "luat_lcd.h"
  3. #include "luat_gpio.h"
  4. #include "luat_mem.h"
  5. #include "luat_rtos.h"
  6. #include "luat_i2c.h"
  7. #define LUAT_LOG_TAG "jd9261t"
  8. #include "luat_log.h"
  9. typedef struct
  10. {
  11. luat_lcd_conf_t* lcd;
  12. luat_rtos_timer_t scan_timer;
  13. uint8_t scan_cnt;
  14. }jd9261t_tp_ctrl_t;
  15. static jd9261t_tp_ctrl_t jd9261t_tp;
  16. static void jd9261t_get_tp_point(void *param, uint32_t param_len)
  17. {
  18. uint16_t tp_x, tp_y;
  19. int res;
  20. uint8_t buff[60];
  21. buff[0] = 0x20;
  22. buff[1] = 0x01;
  23. buff[2] = 0x11;
  24. buff[3] = 0x20;
  25. res = luat_i2c_send(jd9261t_tp.lcd->tp_i2c_id, 0x68, buff, 4, 1);
  26. if (res)
  27. {
  28. res = luat_i2c_send(jd9261t_tp.lcd->tp_i2c_id, 0x68, buff, 4, 1);
  29. if (res)
  30. {
  31. LLOGE("TP read point failed");
  32. return;
  33. }
  34. }
  35. luat_timer_us_delay(15);
  36. res = luat_i2c_recv(jd9261t_tp.lcd->tp_i2c_id, 0x68, buff, 60);
  37. if (res)
  38. {
  39. res = luat_i2c_recv(jd9261t_tp.lcd->tp_i2c_id, 0x68, buff, 60);
  40. if (res)
  41. {
  42. LLOGE("TP read point failed");
  43. return;
  44. }
  45. }
  46. if (!buff[0]) return;
  47. for (uint8_t i = 0; i < 10; i++)
  48. {
  49. if (buff[i * 5 + 3] != 0xff)
  50. {
  51. //LLOGI("%d, %x %x %x %x", i * 5 + 3, buff[i * 5 + 3], buff[i * 5 + 4], buff[i * 5 + 5], buff[i * 5 + 6]);
  52. tp_x = buff[i * 5 + 3] & 0x01;
  53. tp_x = (tp_x << 8) | buff[i * 5 + 4];
  54. tp_y = buff[i * 5 + 5] & 0x01;
  55. tp_y = (tp_y << 8) | buff[i * 5 + 6];
  56. LLOGI("TP point %d x %d y %d", i+1, tp_x, tp_y);
  57. }
  58. }
  59. }
  60. static LUAT_RT_RET_TYPE jd9261t_scan_once(LUAT_RT_CB_PARAM)
  61. {
  62. jd9261t_tp.scan_cnt++;
  63. luat_lcd_run_api_in_service(jd9261t_get_tp_point, jd9261t_tp.lcd, 0);
  64. if (jd9261t_tp.scan_cnt > 10)
  65. {
  66. luat_rtos_timer_stop(jd9261t_tp.scan_timer);
  67. }
  68. }
  69. static int jd9261t_irq_cb(int pin, void* args)
  70. {
  71. if (jd9261t_tp.scan_cnt > 10)
  72. {
  73. jd9261t_tp.scan_cnt = 0;
  74. luat_start_rtos_timer(jd9261t_tp.scan_timer, 20, 1);
  75. }
  76. return 0;
  77. }
  78. static int jd9261t_inited_init(luat_lcd_conf_t* conf)
  79. {
  80. if (!conf->buff)
  81. {
  82. conf->buff = luat_heap_opt_zalloc(LUAT_HEAP_AUTO, conf->w * conf->h * ((conf->bpp <= 16)?2:4));
  83. conf->flush_y_min = conf->h;
  84. conf->flush_y_max = 0;
  85. }
  86. luat_lcd_qspi_conf_t auto_flush =
  87. {
  88. .write_4line_cmd = 0xde,
  89. .vsync_reg = 0x61,
  90. .hsync_cmd = 0xde,
  91. .hsync_reg = 0x60,
  92. .write_1line_cmd = 0xde,
  93. };
  94. luat_gpio_set(conf->tp_pin_rst, Luat_GPIO_HIGH);
  95. luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
  96. // luat_rtos_task_sleep(5);
  97. // luat_gpio_set(conf->tp_pin_rst, Luat_GPIO_HIGH);
  98. luat_rtos_task_sleep(5);
  99. luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
  100. // luat_gpio_set(conf->tp_pin_rst, Luat_GPIO_LOW);
  101. // luat_rtos_task_sleep(50);
  102. // luat_gpio_set(conf->tp_pin_rst, Luat_GPIO_HIGH);
  103. // luat_rtos_task_sleep(100);
  104. luat_rtos_task_sleep(150);
  105. luat_lcd_qspi_config(conf, &auto_flush); //必须在第一个命令发送前就准备好
  106. luat_lcd_set_direction(conf,conf->direction);
  107. luat_rtos_task_sleep(5);
  108. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  109. if ((conf->tp_pin_rst != LUAT_GPIO_NONE) && (conf->tp_pin_rst || conf->tp_pin_irq))
  110. {
  111. uint8_t ID[4];
  112. ID[0] = 0x40;
  113. ID[1] = 0x00;
  114. ID[2] = 0x80;
  115. ID[3] = 0x76;
  116. luat_i2c_setup(conf->tp_i2c_id, 200000);
  117. luat_i2c_set_polling_mode(conf->tp_i2c_id, 1);
  118. if (luat_i2c_transfer(conf->tp_i2c_id, 0x68, ID, 4, ID, 2))
  119. {
  120. LLOGE("TP not detect");
  121. }
  122. else
  123. {
  124. LLOGI("TP detect %02x%02x", ID[1], ID[0]);
  125. jd9261t_tp.lcd = conf;
  126. jd9261t_tp.scan_cnt = 100;
  127. jd9261t_tp.scan_timer = luat_create_rtos_timer(jd9261t_scan_once, NULL, NULL);
  128. luat_gpio_t gpio = {0};
  129. gpio.pin = conf->tp_pin_irq;
  130. gpio.mode = LUAT_GPIO_IRQ;
  131. gpio.alt_func = -1;
  132. gpio.pull = LUAT_GPIO_PULLUP;
  133. gpio.irq = LUAT_GPIO_FALLING_IRQ;
  134. gpio.irq_cb = jd9261t_irq_cb;
  135. luat_gpio_setup(&gpio);
  136. }
  137. }
  138. else
  139. {
  140. LLOGI("TP not work");
  141. }
  142. luat_lcd_qspi_auto_flush_on_off(conf, 1);
  143. return 0;
  144. }
  145. luat_lcd_opts_t lcd_opts_jd9261t_inited = {
  146. .name = "jd9261t_inited",
  147. .init_cmds_len = 0,
  148. .init_cmds = NULL,
  149. .direction0 = 0x00,
  150. .direction90 = 0x00,
  151. .direction180 = 0x03,
  152. .direction270 = 0x03,
  153. .rb_swap = 1,
  154. .no_ram_mode = 1,
  155. .user_ctrl_init = jd9261t_inited_init,
  156. };