Teensy 4.1 core updated for C++20
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_mtp.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 MTP_INTERFACE // defined by usb_dev.h -> usb_desc.h
  38. extern volatile uint8_t usb_high_speed;
  39. #define TX_NUM 4
  40. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  41. DMAMEM static uint8_t txbuffer[MTP_TX_SIZE_480 * TX_NUM];
  42. static uint8_t tx_head=0;
  43. static uint16_t tx_packet_size=0;
  44. #define RX_NUM 4
  45. static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
  46. DMAMEM static uint8_t rx_buffer[MTP_RX_SIZE_480 * RX_NUM] __attribute__ ((aligned(32)));
  47. static volatile uint8_t rx_head;
  48. static volatile uint8_t rx_tail;
  49. static uint8_t rx_list[RX_NUM + 1];
  50. static volatile uint32_t rx_available;
  51. static uint16_t rx_packet_size=0;
  52. static void rx_queue_transfer(int i);
  53. static void rx_event(transfer_t *t);
  54. extern volatile uint8_t usb_configuration;
  55. void usb_mtp_configure(void)
  56. {
  57. printf("usb_mtp_configure\n");
  58. if (usb_high_speed) {
  59. tx_packet_size = MTP_TX_SIZE_480;
  60. rx_packet_size = MTP_RX_SIZE_480;
  61. } else {
  62. tx_packet_size = MTP_TX_SIZE_12;
  63. rx_packet_size = MTP_RX_SIZE_12;
  64. }
  65. memset(tx_transfer, 0, sizeof(tx_transfer));
  66. memset(rx_transfer, 0, sizeof(rx_transfer));
  67. tx_head = 0;
  68. rx_head = 0;
  69. rx_tail = 0;
  70. usb_config_tx(MTP_TX_ENDPOINT, tx_packet_size, 0, NULL);
  71. usb_config_rx(MTP_RX_ENDPOINT, rx_packet_size, 0, rx_event);
  72. int i;
  73. for (i=0; i < RX_NUM; i++) rx_queue_transfer(i);
  74. }
  75. /*************************************************************************/
  76. /** Receive **/
  77. /*************************************************************************/
  78. static void rx_queue_transfer(int i)
  79. {
  80. void *buffer = rx_buffer + i * MTP_RX_SIZE_480;
  81. arm_dcache_delete(buffer, rx_packet_size);
  82. //memset(buffer, )
  83. NVIC_DISABLE_IRQ(IRQ_USB1);
  84. usb_prepare_transfer(rx_transfer + i, buffer, rx_packet_size, i);
  85. usb_receive(MTP_RX_ENDPOINT, rx_transfer + i);
  86. NVIC_ENABLE_IRQ(IRQ_USB1);
  87. }
  88. static void rx_event(transfer_t *t)
  89. {
  90. int i = t->callback_param;
  91. //printf("rx event i=%d\n", i);
  92. // received a packet with data
  93. uint32_t head = rx_head;
  94. if (++head > RX_NUM) head = 0;
  95. rx_list[head] = i;
  96. rx_head = head;
  97. }
  98. int usb_mtp_recv(void *buffer, uint32_t timeout)
  99. {
  100. uint32_t wait_begin_at = systick_millis_count;
  101. uint32_t tail = rx_tail;
  102. while (1) {
  103. if (!usb_configuration) return -1; // usb not enumerated by host
  104. if (tail != rx_head) break;
  105. if (systick_millis_count - wait_begin_at > timeout) {
  106. return 0;
  107. }
  108. yield();
  109. }
  110. // digitalWriteFast(0, LOW);
  111. if (++tail > RX_NUM) tail = 0;
  112. uint32_t i = rx_list[tail];
  113. rx_tail = tail;
  114. memcpy(buffer, rx_buffer + i * MTP_RX_SIZE_480, rx_packet_size);
  115. rx_queue_transfer(i);
  116. //memset(rx_transfer, 0, sizeof(rx_transfer));
  117. //usb_prepare_transfer(rx_transfer + 0, rx_buffer, rx_packet_size, 0);
  118. //usb_receive(MTP_RX_ENDPOINT, rx_transfer + 0);
  119. return rx_packet_size;
  120. }
  121. int usb_mtp_send(const void *buffer, uint32_t len, uint32_t timeout)
  122. {
  123. transfer_t *xfer = tx_transfer + tx_head;
  124. uint32_t wait_begin_at = systick_millis_count;
  125. while (1) {
  126. if (!usb_configuration) return -1; // usb not enumerated by host
  127. uint32_t status = usb_transfer_status(xfer);
  128. if (!(status & 0x80)) break; // transfer descriptor ready
  129. if (systick_millis_count - wait_begin_at > timeout) return 0;
  130. yield();
  131. }
  132. uint8_t *txdata = txbuffer + (tx_head * MTP_TX_SIZE_480);
  133. memcpy(txdata, buffer, len);
  134. arm_dcache_flush_delete(txdata, tx_packet_size );
  135. usb_prepare_transfer(xfer, txdata, len, 0);
  136. usb_transmit(MTP_TX_ENDPOINT, xfer);
  137. if (++tx_head >= TX_NUM) tx_head = 0;
  138. return len;
  139. }
  140. int usb_mtp_available(void)
  141. {
  142. if (!usb_configuration) return 0;
  143. if (rx_head != rx_tail) return rx_packet_size;
  144. //if (!(usb_transfer_status(rx_transfer) & 0x80)) return MTP_RX_SIZE;
  145. return 0;
  146. }
  147. #endif // MTP_INTERFACE