Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

237 rindas
6.8KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. //#define SEREMU_PRINT_DEBUG
  26. void USBSerialEmu::init()
  27. {
  28. USBHost::contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  29. USBHIDParser::driver_ready_for_hid_collection(this);
  30. }
  31. hidclaim_t USBSerialEmu::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  32. {
  33. // only claim SerEMU devices currently: 16c0:0486
  34. #ifdef SEREMU_PRINT_DEBUG
  35. USBHDBGSerial.printf("SerEMU Claim: %x:%x usage: %x\n", dev->idVendor, dev->idProduct, topusage);
  36. #endif
  37. if (dev->idVendor != 0x16c0) return CLAIM_NO; // NOT PJRC
  38. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  39. if (usage_) return CLAIM_NO; // Only claim one
  40. // make sure it is the SEREMU usage
  41. if (topusage != 0xffc90004) return CLAIM_NO; // Not the SEREMU
  42. mydevice = dev;
  43. collections_claimed++;
  44. usage_ = topusage;
  45. driver_ = driver; // remember the driver.
  46. rx_head_ = 0;// receive head
  47. rx_tail_ = 0;// receive tail
  48. tx_head_ = 0;
  49. rx_pipe_size_ = driver->inSize();
  50. tx_pipe_size_ = driver->outSize();
  51. tx_out_data_pending_ = 0;
  52. //if (setup.word1 == 0x03000921 && setup.word2 == ((4<<16)|SEREMU_INTERFACE)) {
  53. tx_buffer_[0] = 0;
  54. tx_buffer_[1] = 0;
  55. tx_buffer_[2] = 0;
  56. tx_buffer_[3] = 0;
  57. driver_->sendControlPacket( 0x21, 0x9, 0x300, driver_->interfaceNumber(), 4, tx_buffer_);
  58. return CLAIM_INTERFACE; // We wa
  59. }
  60. void USBSerialEmu::disconnect_collection(Device_t *dev)
  61. {
  62. if (--collections_claimed == 0) {
  63. mydevice = NULL;
  64. usage_ = 0;
  65. }
  66. }
  67. bool USBSerialEmu::hid_process_in_data(const Transfer_t *transfer)
  68. {
  69. uint16_t len = transfer->length;
  70. const uint8_t *buffer = (const uint8_t *)transfer->buffer;
  71. #ifdef SEREMU_PRINT_DEBUG
  72. USBHDBGSerial.printf("USBSerialEmu::hid_process_in_data: %x %d: %x %x %x\n", usage_, len, buffer[0], buffer[1], buffer[2]);
  73. #endif
  74. const uint8_t *buffer_end = buffer + len -1;
  75. while ((buffer_end > buffer) && (*buffer_end == 0))buffer_end--;
  76. // lets trim off the trailing null characters.
  77. // Now lets move the bytes onto our queue.
  78. uint16_t tail = rx_tail_;
  79. while (buffer <= buffer_end) {
  80. uint16_t new_head = rx_head_ + 1;
  81. if (new_head == RX_BUFFER_SIZE) new_head = 0;
  82. if (new_head == tail) break; // we don't have room so bail out.
  83. rx_buffer_[rx_head_] = *buffer++;
  84. rx_head_ = new_head; // point off to the new next head.
  85. }
  86. return true;
  87. }
  88. bool USBSerialEmu::hid_process_out_data(const Transfer_t *transfer)
  89. {
  90. #ifdef SEREMU_PRINT_DEBUG
  91. USBHDBGSerial.printf("USBSerialEmu::hid_process_out_data: %x\n", usage_);
  92. #endif
  93. if (tx_out_data_pending_) {
  94. tx_out_data_pending_--;
  95. }
  96. return true;
  97. }
  98. bool USBSerialEmu::sendPacket()
  99. {
  100. USBHDBGSerial.printf("SEMU: SendPacket\n");
  101. if (!driver_) return false;
  102. if (!driver_->sendPacket(tx_buffer_)) return false;
  103. tx_out_data_pending_++;
  104. tx_head_ = 0;
  105. return true;
  106. }
  107. int USBSerialEmu::available(void)
  108. {
  109. if (!driver_) return 0;
  110. uint32_t head = rx_head_;
  111. uint32_t tail = rx_tail_;
  112. if (head >= tail) return head - tail;
  113. return RX_BUFFER_SIZE + head - tail;
  114. }
  115. int USBSerialEmu::peek(void)
  116. {
  117. if (!driver_) return -1;
  118. if (rx_head_ == rx_tail_) return -1;
  119. return rx_buffer_[rx_tail_];
  120. }
  121. int USBSerialEmu::read(void)
  122. {
  123. if (!driver_) return -1;
  124. if (rx_head_ == rx_tail_) return -1;
  125. int c = rx_buffer_[rx_tail_];
  126. if (++rx_tail_ >= RX_BUFFER_SIZE) rx_tail_ = 0;
  127. return c;
  128. }
  129. int USBSerialEmu::availableForWrite()
  130. {
  131. if (!driver_) return 0;
  132. return tx_pipe_size_ - tx_head_ + (2-tx_out_data_pending_)*tx_pipe_size_;
  133. }
  134. size_t USBSerialEmu::write(uint8_t c)
  135. {
  136. // Single buffer, as our HID device has double buffers.
  137. if (c >= ' ') USBHDBGSerial.printf("SEMU: %c\n", c);
  138. else USBHDBGSerial.printf("SEMU: 0x%x\n", c);
  139. if (!driver_) return 0;
  140. if (tx_head_ == tx_pipe_size_) {
  141. while (!sendPacket()) yield(); // wait until the device above queues this packet
  142. }
  143. tx_buffer_[tx_head_++] = c;
  144. // if this character filled it. then try to queue it again
  145. if (tx_head_ == tx_pipe_size_) sendPacket();
  146. driver_->stopTimer();
  147. driver_->startTimer(write_timeout_);
  148. return 1;
  149. }
  150. void USBSerialEmu::flush(void)
  151. {
  152. if (!driver_) return;
  153. USBHDBGSerial.printf("SEMU: flush\n");
  154. driver_->stopTimer(); // Stop longer timer.
  155. driver_->startTimer(100); // Start a mimimal timeout
  156. if (tx_head_) sendPacket();
  157. // And wait for HID to say they were all sent.
  158. elapsedMillis em = 0;
  159. while (tx_out_data_pending_ && (em < 10000)) yield(); // wait up to 10 seconds?
  160. }
  161. void USBSerialEmu::hid_timer_event(USBDriverTimer *whichTimer)
  162. {
  163. USBHDBGSerial.printf("SEMU: Timer\n");
  164. if (!driver_) return;
  165. driver_->stopTimer();
  166. if (tx_head_) {
  167. memset(tx_buffer_ + tx_head_, 0, tx_pipe_size_ - tx_head_); // clear the rest of bytes in buffer.
  168. sendPacket();
  169. }
  170. }
  171. void USBSerialEmu::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  172. {
  173. // These should not be called as we are claiming the whole interface and not
  174. // allowing the parse to happen
  175. #ifdef SEREMU_PRINT_DEBUG
  176. USBHDBGSerial.printf("SerEMU::hid_input_begin %x %x %x %x\n", topusage, type, lgmin, lgmax);
  177. #endif
  178. //hid_input_begin_ = true;
  179. }
  180. void USBSerialEmu::hid_input_data(uint32_t usage, int32_t value)
  181. {
  182. // These should not be called as we are claiming the whole interface and not
  183. // allowing the parse to happen
  184. #ifdef SEREMU_PRINT_DEBUG
  185. USBHDBGSerial.printf("SerEMU: usage=%X, value=%d", usage, value);
  186. if ((value >= ' ') && (value <='~')) USBHDBGSerial.printf("(%c)", value);
  187. USBHDBGSerial.println();
  188. #endif
  189. }
  190. void USBSerialEmu::hid_input_end()
  191. {
  192. // These should not be called as we are claiming the whole interface and not
  193. // allowing the parse to happen
  194. #ifdef SEREMU_PRINT_DEBUG
  195. USBHDBGSerial.println("SerEMU::hid_input_end");
  196. #endif
  197. // if (hid_input_begin_) {
  198. // hid_input_begin_ = false;
  199. // }
  200. }