Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

usb_rawhid.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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_rawhid.h"
  32. #include "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
  33. #include "core_pins.h" // for yield(), millis()
  34. #include <string.h> // for memcpy()
  35. //#include "HardwareSerial.h"
  36. #include "debug/printf.h"
  37. #ifdef RAWHID_INTERFACE // defined by usb_dev.h -> usb_desc.h
  38. #define TX_NUM 4
  39. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  40. DMAMEM static uint8_t txbuffer[RAWHID_TX_SIZE * TX_NUM];
  41. static uint8_t tx_head=0;
  42. #define RX_NUM 4
  43. static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
  44. DMAMEM static uint8_t rx_buffer[RAWHID_RX_SIZE * RX_NUM] __attribute__ ((aligned(32)));
  45. static volatile uint8_t rx_head;
  46. static volatile uint8_t rx_tail;
  47. static uint8_t rx_list[RX_NUM + 1];
  48. static volatile uint32_t rx_available;
  49. static void rx_queue_transfer(int i);
  50. static void rx_event(transfer_t *t);
  51. extern volatile uint8_t usb_configuration;
  52. void usb_rawhid_configure(void)
  53. {
  54. printf("usb_rawhid_configure\n");
  55. memset(tx_transfer, 0, sizeof(tx_transfer));
  56. memset(rx_transfer, 0, sizeof(rx_transfer));
  57. tx_head = 0;
  58. rx_head = 0;
  59. rx_tail = 0;
  60. usb_config_tx(RAWHID_TX_ENDPOINT, RAWHID_TX_SIZE, 0, NULL);
  61. usb_config_rx(RAWHID_RX_ENDPOINT, RAWHID_RX_SIZE, 0, rx_event);
  62. int i;
  63. for (i=0; i < RX_NUM; i++) rx_queue_transfer(i);
  64. }
  65. /*************************************************************************/
  66. /** Receive **/
  67. /*************************************************************************/
  68. static void rx_queue_transfer(int i)
  69. {
  70. void *buffer = rx_buffer + i * RAWHID_RX_SIZE;
  71. arm_dcache_delete(buffer, RAWHID_RX_SIZE);
  72. //memset(buffer, )
  73. NVIC_DISABLE_IRQ(IRQ_USB1);
  74. usb_prepare_transfer(rx_transfer + i, buffer, RAWHID_RX_SIZE, i);
  75. usb_receive(RAWHID_RX_ENDPOINT, rx_transfer + i);
  76. NVIC_ENABLE_IRQ(IRQ_USB1);
  77. }
  78. static void rx_event(transfer_t *t)
  79. {
  80. int i = t->callback_param;
  81. //printf("rx event i=%d\n", i);
  82. // received a packet with data
  83. uint32_t head = rx_head;
  84. if (++head > RX_NUM) head = 0;
  85. rx_list[head] = i;
  86. rx_head = head;
  87. }
  88. int usb_rawhid_recv(void *buffer, uint32_t timeout)
  89. {
  90. uint32_t wait_begin_at = systick_millis_count;
  91. uint32_t tail = rx_tail;
  92. while (1) {
  93. if (!usb_configuration) return -1; // usb not enumerated by host
  94. if (tail != rx_head) break;
  95. if (systick_millis_count - wait_begin_at > timeout) {
  96. return 0;
  97. }
  98. yield();
  99. }
  100. // digitalWriteFast(0, LOW);
  101. if (++tail > RX_NUM) tail = 0;
  102. uint32_t i = rx_list[tail];
  103. rx_tail = tail;
  104. memcpy(buffer, rx_buffer + i * RAWHID_RX_SIZE, RAWHID_RX_SIZE);
  105. rx_queue_transfer(i);
  106. //memset(rx_transfer, 0, sizeof(rx_transfer));
  107. //usb_prepare_transfer(rx_transfer + 0, rx_buffer, RAWHID_RX_SIZE, 0);
  108. //usb_receive(RAWHID_RX_ENDPOINT, rx_transfer + 0);
  109. return RAWHID_RX_SIZE;
  110. }
  111. int usb_rawhid_send(const void *buffer, uint32_t timeout)
  112. {
  113. transfer_t *xfer = tx_transfer + tx_head;
  114. uint32_t wait_begin_at = systick_millis_count;
  115. while (1) {
  116. if (!usb_configuration) return -1; // usb not enumerated by host
  117. uint32_t status = usb_transfer_status(xfer);
  118. if (!(status & 0x80)) break; // transfer descriptor ready
  119. if (systick_millis_count - wait_begin_at > timeout) return 0;
  120. yield();
  121. }
  122. uint8_t *txdata = txbuffer + (tx_head * RAWHID_TX_SIZE);
  123. memcpy(txdata, buffer, RAWHID_TX_SIZE);
  124. arm_dcache_flush_delete(txdata, RAWHID_TX_SIZE );
  125. usb_prepare_transfer(xfer, txdata, RAWHID_TX_SIZE, 0);
  126. usb_transmit(RAWHID_TX_ENDPOINT, xfer);
  127. if (++tx_head >= TX_NUM) tx_head = 0;
  128. return RAWHID_TX_SIZE;
  129. }
  130. int usb_rawhid_available(void)
  131. {
  132. if (!usb_configuration) return 0;
  133. if (rx_head != rx_tail) return RAWHID_RX_SIZE;
  134. //if (!(usb_transfer_status(rx_transfer) & 0x80)) return RAWHID_RX_SIZE;
  135. return 0;
  136. }
  137. #endif // RAWHID_INTERFACE