drv_dsi_touch.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-11-26 bigmagic first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <touch.h>
  13. #include "mbox.h"
  14. #include "drv_dsi_touch.h"
  15. #define DBG_TAG "dsi_touch"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. static rt_touch_t touch_device = RT_NULL;
  19. static struct rt_semaphore dsi_touch_ack;
  20. static rt_uint32_t touch_x;
  21. static rt_uint32_t touch_y;
  22. static rt_uint32_t touch_state;
  23. static rt_thread_t dsi_touch_tid = RT_NULL;
  24. #define DSI_TOUCH_THREAD_STACK_SIZE (4096)
  25. #define DSI_TOUCH_THREAD_PRIORITY (25)
  26. #define DSI_TOUCH_THREAD_TIMESLICE (10)
  27. #define MAXIMUM_SUPPORTED_POINTS (10)
  28. struct touch_regs
  29. {
  30. uint8_t device_mode;
  31. uint8_t gesture_id;
  32. uint8_t num_points;
  33. struct touch
  34. {
  35. uint8_t xh;
  36. uint8_t xl;
  37. uint8_t yh;
  38. uint8_t yl;
  39. uint8_t res1;
  40. uint8_t res2;
  41. } point[MAXIMUM_SUPPORTED_POINTS];
  42. };
  43. static void dsi_touch_thread_entry(void *param)
  44. {
  45. static volatile uint32_t touchbuf;
  46. touchbuf = bcm271x_mbox_get_touch(); //0x0f436000
  47. if(touchbuf == RT_NULL)
  48. {
  49. rt_kprintf("init dsi touch err!\n");
  50. return;
  51. }
  52. while (1)
  53. {
  54. struct touch_regs *regs = (struct touch_regs *)touchbuf;
  55. if ((regs->num_points > 0) && (regs->num_points < MAXIMUM_SUPPORTED_POINTS))
  56. {
  57. //only one touch point
  58. touch_x = (((int)regs->point[0].xh & 0xf) << 8) + regs->point[0].xl;
  59. touch_y = (((int)regs->point[0].yh & 0xf) << 8) + regs->point[0].yl;
  60. touch_state = 1;
  61. }
  62. else
  63. {
  64. touch_state = 0;
  65. }
  66. rt_thread_mdelay(50);
  67. }
  68. }
  69. static rt_size_t dsi_read_point(struct rt_touch_device *touch, void *buf, rt_size_t read_num)
  70. {
  71. rt_uint16_t* touchxy = (rt_uint16_t *)buf;
  72. if((read_num != 0) && (touch_state == 1))
  73. {
  74. touchxy[0] = touch_x;
  75. touchxy[1] = touch_y;
  76. touch_state = 0;
  77. return read_num;
  78. }
  79. else
  80. {
  81. return 0;
  82. }
  83. }
  84. static rt_err_t dsi_control(struct rt_touch_device *device, int cmd, void *data)
  85. {
  86. return RT_EOK;
  87. }
  88. static struct rt_touch_ops dsi_touch_ops =
  89. {
  90. .touch_readpoint = dsi_read_point,
  91. .touch_control = dsi_control,
  92. };
  93. static int hw_dsi_touch_init(void)
  94. {
  95. //touch sem
  96. rt_sem_init(&dsi_touch_ack, "dsi_touch_ack", 0, RT_IPC_FLAG_FIFO);
  97. dsi_touch_tid = rt_thread_create("dsi_touch",
  98. dsi_touch_thread_entry, RT_NULL,
  99. DSI_TOUCH_THREAD_STACK_SIZE,
  100. DSI_TOUCH_THREAD_PRIORITY, DSI_TOUCH_THREAD_TIMESLICE);
  101. if (dsi_touch_tid != RT_NULL)
  102. rt_thread_startup(dsi_touch_tid);
  103. touch_device = (rt_touch_t)rt_calloc(1, sizeof(struct rt_touch_device));
  104. if (touch_device == RT_NULL)
  105. return -RT_ERROR;
  106. /* register touch device */
  107. touch_device->info.type = RT_TOUCH_TYPE_RESISTANCE;
  108. touch_device->info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
  109. //rt_memcpy(&touch_device->config, cfg, sizeof(struct rt_touch_config));
  110. touch_device->ops = &dsi_touch_ops;
  111. rt_hw_touch_register(touch_device, "dsi_touch", RT_DEVICE_FLAG_INT_RX, RT_NULL);
  112. return 0;
  113. }
  114. INIT_DEVICE_EXPORT(hw_dsi_touch_init);