drv_dsi_touch.c 3.3 KB

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