Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

196 lines
5.9KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2016 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_audio.h"
  31. #include "HardwareSerial.h"
  32. #include <string.h> // for memcpy()
  33. #ifdef AUDIO_INTERFACE // defined by usb_dev.h -> usb_desc.h
  34. #if F_CPU >= 20000000
  35. bool AudioInputUSB::update_responsibility;
  36. audio_block_t * AudioInputUSB::incoming_left;
  37. audio_block_t * AudioInputUSB::incoming_right;
  38. audio_block_t * AudioInputUSB::ready_left;
  39. audio_block_t * AudioInputUSB::ready_right;
  40. uint16_t AudioInputUSB::incoming_count;
  41. //uint16_t AudioInputUSB::underflow_flag;
  42. #define DMABUFATTR __attribute__ ((section(".dmabuffers"), aligned (4)))
  43. uint16_t usb_audio_receive_buffer[AUDIO_RX_SIZE/2] DMABUFATTR;
  44. uint16_t usb_audio_transmit_buffer[AUDIO_TX_SIZE/2] DMABUFATTR;
  45. uint32_t usb_audio_sync_feedback DMABUFATTR;
  46. void AudioInputUSB::begin(void)
  47. {
  48. incoming_count = 0;
  49. incoming_left = NULL;
  50. incoming_right = NULL;
  51. ready_left = NULL;
  52. ready_right = NULL;
  53. // update_responsibility = update_setup();
  54. // TODO: update responsibility is tough, partly because the USB
  55. // interrupts aren't sychronous to the audio library block size,
  56. // but also because the PC may stop transmitting data, which
  57. // means we no longer get receive callbacks from usb_dev.
  58. update_responsibility = false;
  59. usb_audio_sync_feedback = 722824;
  60. usb_audio_sync_feedback = 723700; // too fast?
  61. usb_audio_sync_feedback = 722534; // too slow
  62. }
  63. static void copy_to_buffers(const uint32_t *src, int16_t *left, int16_t *right, unsigned int len)
  64. {
  65. // TODO: optimize...
  66. while (len > 0) {
  67. uint32_t n = *src++;
  68. *left++ = n & 0xFFFF;
  69. *right++ = n >> 16;
  70. len--;
  71. }
  72. }
  73. // Called from the USB interrupt when an isochronous packet arrives
  74. // we must completely remove it from the receive buffer before returning
  75. //
  76. void usb_audio_receive_callback(unsigned int len)
  77. {
  78. unsigned int count, avail;
  79. audio_block_t *left, *right;
  80. const uint32_t *data;
  81. len >>= 2; // 1 sample = 4 bytes: 2 left, 2 right
  82. data = (const uint32_t *)usb_audio_receive_buffer;
  83. count = AudioInputUSB::incoming_count;
  84. left = AudioInputUSB::incoming_left;
  85. right = AudioInputUSB::incoming_right;
  86. if (left == NULL) {
  87. left = AudioStream::allocate();
  88. if (left == NULL) return;
  89. AudioInputUSB::incoming_left = left;
  90. }
  91. if (right == NULL) {
  92. right = AudioStream::allocate();
  93. if (right == NULL) return;
  94. AudioInputUSB::incoming_right = right;
  95. }
  96. while (len > 0) {
  97. avail = AUDIO_BLOCK_SAMPLES - count;
  98. if (len < avail) {
  99. copy_to_buffers(data, left->data + count, right->data + count, len);
  100. AudioInputUSB::incoming_count = count + len;
  101. return;
  102. } else if (avail > 0) {
  103. copy_to_buffers(data, left->data + count, right->data + count, avail);
  104. data += avail;
  105. len -= avail;
  106. if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) {
  107. // buffer overrun, PC sending too fast
  108. AudioInputUSB::incoming_count = count + avail;
  109. if (len > 0) {
  110. serial_print("!");
  111. serial_phex(len);
  112. }
  113. return;
  114. }
  115. send:
  116. AudioInputUSB::ready_left = left;
  117. AudioInputUSB::ready_right = right;
  118. //if (AudioInputUSB::update_responsibility) AudioStream::update_all();
  119. left = AudioStream::allocate();
  120. if (left == NULL) {
  121. AudioInputUSB::incoming_left = NULL;
  122. AudioInputUSB::incoming_right = NULL;
  123. AudioInputUSB::incoming_count = 0;
  124. return;
  125. }
  126. right = AudioStream::allocate();
  127. if (right == NULL) {
  128. AudioStream::release(left);
  129. AudioInputUSB::incoming_left = NULL;
  130. AudioInputUSB::incoming_right = NULL;
  131. AudioInputUSB::incoming_count = 0;
  132. return;
  133. }
  134. AudioInputUSB::incoming_left = left;
  135. AudioInputUSB::incoming_right = right;
  136. count = 0;
  137. } else {
  138. if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) return;
  139. goto send; // recover from buffer overrun
  140. }
  141. }
  142. AudioInputUSB::incoming_count = count;
  143. }
  144. void AudioInputUSB::update(void)
  145. {
  146. audio_block_t *left, *right;
  147. __disable_irq();
  148. left = ready_left;
  149. ready_left = NULL;
  150. right = ready_right;
  151. ready_right = NULL;
  152. // TODO: use incoming_count for USB isochronous feedback
  153. uint16_t c = incoming_count;
  154. __enable_irq();
  155. //serial_phex(c);
  156. //serial_print(".");
  157. if (!left || !right) {
  158. serial_print("#"); // buffer underrun - PC sending too slow
  159. }
  160. if (left) {
  161. transmit(left, 0);
  162. release(left);
  163. }
  164. if (right) {
  165. transmit(right, 1);
  166. release(right);
  167. }
  168. }
  169. // Called from the USB interrupt when ready to transmit another
  170. // isochronous packet. If we place data into the transmit buffer,
  171. // the return is the number of bytes. Otherwise, return 0 means
  172. // no data to transmit
  173. unsigned int usb_audio_transmit_callback(void)
  174. {
  175. return 0;
  176. }
  177. #endif // F_CPU
  178. #endif // AUDIO_INTERFACE