Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.4KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2019 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_dev.h"
  31. #include "usb_touch.h"
  32. #include "core_pins.h" // for millis()
  33. #include <string.h> // for memcpy()
  34. #include "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
  35. #include "debug/printf.h"
  36. #include "core_pins.h"
  37. #ifdef MULTITOUCH_INTERFACE // defined by usb_dev.h -> usb_desc.h
  38. static uint8_t prev_id=0;
  39. static uint8_t scan_index=0;
  40. static uint8_t scan_count=0;
  41. static uint8_t contactid[MULTITOUCH_FINGERS];
  42. static uint8_t pressure[MULTITOUCH_FINGERS];
  43. static uint16_t xpos[MULTITOUCH_FINGERS];
  44. static uint16_t ypos[MULTITOUCH_FINGERS];
  45. static uint16_t scan_timestamp;
  46. #define TX_NUM 20
  47. #define TX_BUFSIZE 32
  48. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  49. DMAMEM static uint8_t txbuffer[TX_NUM * TX_BUFSIZE] __attribute__ ((aligned(32)));
  50. static uint8_t tx_head=0;
  51. #if MULTITOUCH_SIZE > TX_BUFSIZE
  52. #error "Internal error, transmit buffer size is too small for touchscreen endpoint"
  53. #endif
  54. void usb_touchscreen_configure(void)
  55. {
  56. memset(tx_transfer, 0, sizeof(tx_transfer));
  57. tx_head = 0;
  58. usb_config_tx(MULTITOUCH_ENDPOINT, MULTITOUCH_SIZE, 0, NULL);
  59. // TODO: need to cause usb_touchscreen_update_callback to be run on SOF
  60. }
  61. void usb_touchscreen_press(uint8_t finger, uint32_t x, uint32_t y, uint32_t press)
  62. {
  63. if (finger >= MULTITOUCH_FINGERS) return;
  64. if (press > 255) press = 255;
  65. if (x > 32767) x = 32767;
  66. if (y > 32767) y = 32767;
  67. __disable_irq();
  68. if (pressure[finger] == 0 && press > 0) {
  69. // Begin a new finger touch, assign a
  70. // new Contact Identifier (usage 0x51)
  71. uint8_t id = prev_id + 1;
  72. if (id == 0 || id > 127) id = 1;
  73. prev_id = id;
  74. contactid[finger] = (id << 1) | 1;
  75. }
  76. xpos[finger] = x;
  77. ypos[finger] = y;
  78. pressure[finger] = press;
  79. __enable_irq();
  80. }
  81. // User should allow at least 12 ms after release before starting a
  82. // new touch with press, in case the host's driver does not properly
  83. // parse a change in contact identifier to mean a new touch.
  84. void usb_touchscreen_release(uint8_t finger)
  85. {
  86. if (finger >= MULTITOUCH_FINGERS) return;
  87. pressure[finger] = 0;
  88. }
  89. static int transmit(const uint8_t *data)
  90. {
  91. if (!usb_configuration) return 0;
  92. uint32_t head = tx_head;
  93. transfer_t *xfer = tx_transfer + head;
  94. uint32_t status = usb_transfer_status(xfer);
  95. if ((status & 0x80)) return 0;
  96. if (status & 0x68) {
  97. // TODO: what if status has errors???
  98. }
  99. uint8_t *buffer = txbuffer + head * TX_BUFSIZE;
  100. memcpy(buffer, data, MULTITOUCH_SIZE);
  101. usb_prepare_transfer(xfer, buffer, MULTITOUCH_SIZE, 0);
  102. arm_dcache_flush_delete(buffer, TX_BUFSIZE);
  103. usb_transmit(MULTITOUCH_ENDPOINT, xfer);
  104. if (++head >= TX_NUM) head = 0;
  105. tx_head = head;
  106. return 1;
  107. }
  108. // touch report
  109. // 0: contact id + on/off
  110. // 1: pressure
  111. // 2: X lsb
  112. // 3: X msb
  113. // 4: Y lsb
  114. // 5: Y msb
  115. // 6: scan time lsb
  116. // 7: scan time msb
  117. // Called by the start-of-frame interrupt.
  118. //
  119. void usb_touchscreen_update_callback(void)
  120. {
  121. // TODO: if 480 speed, run only every 8th time
  122. if (scan_index == 0) {
  123. if (usb_tx_packet_count(MULTITOUCH_ENDPOINT) > 1) {
  124. // wait to begin another scan if if more than
  125. // one prior packet remains to transmit
  126. return;
  127. }
  128. scan_timestamp = millis() * 10;
  129. scan_count = 0;
  130. }
  131. while (scan_index < MULTITOUCH_FINGERS) {
  132. uint32_t press = pressure[scan_index];
  133. uint32_t id = contactid[scan_index];
  134. if (id) {
  135. if (press == 0) {
  136. // End a finger touch. One final packet sent
  137. // with same Contact Identifier (usage 0x51)
  138. // and Tip Switch (usage 0x42) = zero
  139. id &= 0xFE;
  140. contactid[scan_index] = 0;
  141. }
  142. uint8_t buffer[MULTITOUCH_SIZE]; // MULTITOUCH_SIZE = 8
  143. buffer[0] = id;
  144. buffer[1] = press;
  145. buffer[2] = xpos[scan_index];
  146. buffer[3] = xpos[scan_index] >> 8;
  147. buffer[4] = ypos[scan_index];
  148. buffer[5] = ypos[scan_index] >> 8;
  149. buffer[6] = scan_timestamp;
  150. buffer[7] = scan_timestamp >> 8;
  151. if (transmit(buffer)) scan_index++;
  152. return;
  153. }
  154. scan_index++;
  155. }
  156. if (++scan_count >= MULTITOUCH_FINGERS) {
  157. // when done early, wait to restart another scan
  158. scan_index = 0;
  159. }
  160. }
  161. #endif // MULTITOUCH_INTERFACE