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.

495 lines
13KB

  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 <Arduino.h>
  31. #include "usb_dev.h"
  32. #include "usb_audio.h"
  33. #include "debug/printf.h"
  34. #ifdef AUDIO_INTERFACE
  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. uint8_t AudioInputUSB::receive_flag;
  42. struct usb_audio_features_struct AudioInputUSB::features = {0,0,FEATURE_MAX_VOLUME/2};
  43. extern volatile uint8_t usb_high_speed;
  44. static void rx_event(transfer_t *t);
  45. static transfer_t rx_transfer __attribute__ ((used, aligned(32)));
  46. DMAMEM static uint8_t rx_buffer[AUDIO_RX_SIZE] __attribute__ ((aligned(32)));
  47. //#define DMABUFATTR __attribute__ ((section(".dmabuffers"), aligned (4)))
  48. //uint16_t usb_audio_receive_buffer[AUDIO_RX_SIZE/2] DMABUFATTR;
  49. //uint32_t usb_audio_sync_feedback DMABUFATTR;
  50. uint8_t usb_audio_receive_setting=0;
  51. uint8_t usb_audio_transmit_setting=0;
  52. static uint32_t feedback_accumulator = 185042824;
  53. static void rx_queue_transfer(void)
  54. {
  55. usb_prepare_transfer(&rx_transfer, rx_buffer, AUDIO_RX_SIZE, 0);
  56. arm_dcache_delete(&rx_transfer, AUDIO_RX_SIZE);
  57. usb_receive(AUDIO_RX_ENDPOINT, &rx_transfer);
  58. }
  59. void usb_audio_configure(void)
  60. {
  61. printf("usb_audio_configure\n");
  62. memset(&rx_transfer, 0, sizeof(rx_transfer));
  63. usb_config_rx_iso(AUDIO_RX_ENDPOINT, AUDIO_RX_SIZE, 1, rx_event);
  64. rx_queue_transfer();
  65. }
  66. void AudioInputUSB::begin(void)
  67. {
  68. incoming_count = 0;
  69. incoming_left = NULL;
  70. incoming_right = NULL;
  71. ready_left = NULL;
  72. ready_right = NULL;
  73. receive_flag = 0;
  74. // update_responsibility = update_setup();
  75. // TODO: update responsibility is tough, partly because the USB
  76. // interrupts aren't sychronous to the audio library block size,
  77. // but also because the PC may stop transmitting data, which
  78. // means we no longer get receive callbacks from usb.c
  79. update_responsibility = false;
  80. //usb_audio_sync_feedback = feedback_accumulator >> 8;
  81. }
  82. static void copy_to_buffers(const uint32_t *src, int16_t *left, int16_t *right, unsigned int len)
  83. {
  84. uint32_t *target = (uint32_t*) src + len;
  85. while ((src < target) && (((uintptr_t) left & 0x02) != 0)) {
  86. uint32_t n = *src++;
  87. *left++ = n & 0xFFFF;
  88. *right++ = n >> 16;
  89. }
  90. while ((src < target - 2)) {
  91. uint32_t n1 = *src++;
  92. uint32_t n = *src++;
  93. *(uint32_t *)left = (n1 & 0xFFFF) | ((n & 0xFFFF) << 16);
  94. left+=2;
  95. *(uint32_t *)right = (n1 >> 16) | ((n & 0xFFFF0000)) ;
  96. right+=2;
  97. }
  98. while ((src < target)) {
  99. uint32_t n = *src++;
  100. *left++ = n & 0xFFFF;
  101. *right++ = n >> 16;
  102. }
  103. }
  104. // Called from the USB interrupt when an isochronous packet arrives
  105. // we must completely remove it from the receive buffer before returning
  106. //
  107. static void rx_event(transfer_t *t)
  108. {
  109. unsigned int len = AUDIO_RX_SIZE - ((t->status >> 16) & 0x7FFF);
  110. //int len = AUDIO_RX_SIZE - ((rx_transfer.status >> 16) & 0x7FFF);
  111. printf("rx %u\n", len);
  112. // TODO: actually move the data from USB to audio lib
  113. usb_audio_receive_callback(len);
  114. rx_queue_transfer();
  115. }
  116. #if 1
  117. void usb_audio_receive_callback(unsigned int len)
  118. {
  119. unsigned int count, avail;
  120. audio_block_t *left, *right;
  121. const uint32_t *data;
  122. AudioInputUSB::receive_flag = 1;
  123. len >>= 2; // 1 sample = 4 bytes: 2 left, 2 right
  124. data = (const uint32_t *)rx_buffer;
  125. count = AudioInputUSB::incoming_count;
  126. left = AudioInputUSB::incoming_left;
  127. right = AudioInputUSB::incoming_right;
  128. if (left == NULL) {
  129. left = AudioStream::allocate();
  130. if (left == NULL) return;
  131. AudioInputUSB::incoming_left = left;
  132. }
  133. if (right == NULL) {
  134. right = AudioStream::allocate();
  135. if (right == NULL) return;
  136. AudioInputUSB::incoming_right = right;
  137. }
  138. while (len > 0) {
  139. avail = AUDIO_BLOCK_SAMPLES - count;
  140. if (len < avail) {
  141. copy_to_buffers(data, left->data + count, right->data + count, len);
  142. AudioInputUSB::incoming_count = count + len;
  143. return;
  144. } else if (avail > 0) {
  145. copy_to_buffers(data, left->data + count, right->data + count, avail);
  146. data += avail;
  147. len -= avail;
  148. if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) {
  149. // buffer overrun, PC sending too fast
  150. AudioInputUSB::incoming_count = count + avail;
  151. //if (len > 0) {
  152. //serial_print("!");
  153. //serial_phex(len);
  154. //}
  155. return;
  156. }
  157. send:
  158. AudioInputUSB::ready_left = left;
  159. AudioInputUSB::ready_right = right;
  160. //if (AudioInputUSB::update_responsibility) AudioStream::update_all();
  161. left = AudioStream::allocate();
  162. if (left == NULL) {
  163. AudioInputUSB::incoming_left = NULL;
  164. AudioInputUSB::incoming_right = NULL;
  165. AudioInputUSB::incoming_count = 0;
  166. return;
  167. }
  168. right = AudioStream::allocate();
  169. if (right == NULL) {
  170. AudioStream::release(left);
  171. AudioInputUSB::incoming_left = NULL;
  172. AudioInputUSB::incoming_right = NULL;
  173. AudioInputUSB::incoming_count = 0;
  174. return;
  175. }
  176. AudioInputUSB::incoming_left = left;
  177. AudioInputUSB::incoming_right = right;
  178. count = 0;
  179. } else {
  180. if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) return;
  181. goto send; // recover from buffer overrun
  182. }
  183. }
  184. AudioInputUSB::incoming_count = count;
  185. }
  186. #endif
  187. void AudioInputUSB::update(void)
  188. {
  189. audio_block_t *left, *right;
  190. __disable_irq();
  191. left = ready_left;
  192. ready_left = NULL;
  193. right = ready_right;
  194. ready_right = NULL;
  195. uint16_t c = incoming_count;
  196. uint8_t f = receive_flag;
  197. receive_flag = 0;
  198. __enable_irq();
  199. if (f) {
  200. int diff = AUDIO_BLOCK_SAMPLES/2 - (int)c;
  201. feedback_accumulator += diff / 3;
  202. uint32_t feedback = (feedback_accumulator >> 8) + diff * 100;
  203. #ifdef MACOSX_ADAPTIVE_LIMIT
  204. if (feedback > 722698) feedback = 722698;
  205. #endif
  206. //usb_audio_sync_feedback = feedback;
  207. //if (diff > 0) {
  208. //serial_print(".");
  209. //} else if (diff < 0) {
  210. //serial_print("^");
  211. //}
  212. }
  213. //serial_phex(c);
  214. //serial_print(".");
  215. if (!left || !right) {
  216. //serial_print("#"); // buffer underrun - PC sending too slow
  217. //if (f) feedback_accumulator += 10 << 8;
  218. }
  219. if (left) {
  220. transmit(left, 0);
  221. release(left);
  222. }
  223. if (right) {
  224. transmit(right, 1);
  225. release(right);
  226. }
  227. }
  228. #if 0
  229. bool AudioOutputUSB::update_responsibility;
  230. audio_block_t * AudioOutputUSB::left_1st;
  231. audio_block_t * AudioOutputUSB::left_2nd;
  232. audio_block_t * AudioOutputUSB::right_1st;
  233. audio_block_t * AudioOutputUSB::right_2nd;
  234. uint16_t AudioOutputUSB::offset_1st;
  235. uint16_t usb_audio_transmit_buffer[AUDIO_TX_SIZE/2] DMABUFATTR;
  236. void AudioOutputUSB::begin(void)
  237. {
  238. update_responsibility = false;
  239. left_1st = NULL;
  240. right_1st = NULL;
  241. }
  242. static void copy_from_buffers(uint32_t *dst, int16_t *left, int16_t *right, unsigned int len)
  243. {
  244. // TODO: optimize...
  245. while (len > 0) {
  246. *dst++ = (*right++ << 16) | (*left++ & 0xFFFF);
  247. len--;
  248. }
  249. }
  250. void AudioOutputUSB::update(void)
  251. {
  252. audio_block_t *left, *right;
  253. // TODO: we shouldn't be writing to these......
  254. //left = receiveReadOnly(0); // input 0 = left channel
  255. //right = receiveReadOnly(1); // input 1 = right channel
  256. left = receiveWritable(0); // input 0 = left channel
  257. right = receiveWritable(1); // input 1 = right channel
  258. if (usb_audio_transmit_setting == 0) {
  259. if (left) release(left);
  260. if (right) release(right);
  261. if (left_1st) { release(left_1st); left_1st = NULL; }
  262. if (left_2nd) { release(left_2nd); left_2nd = NULL; }
  263. if (right_1st) { release(right_1st); right_1st = NULL; }
  264. if (right_2nd) { release(right_2nd); right_2nd = NULL; }
  265. offset_1st = 0;
  266. return;
  267. }
  268. if (left == NULL) {
  269. left = allocate();
  270. if (left == NULL) {
  271. if (right) release(right);
  272. return;
  273. }
  274. memset(left->data, 0, sizeof(left->data));
  275. }
  276. if (right == NULL) {
  277. right = allocate();
  278. if (right == NULL) {
  279. release(left);
  280. return;
  281. }
  282. memset(right->data, 0, sizeof(right->data));
  283. }
  284. __disable_irq();
  285. if (left_1st == NULL) {
  286. left_1st = left;
  287. right_1st = right;
  288. offset_1st = 0;
  289. } else if (left_2nd == NULL) {
  290. left_2nd = left;
  291. right_2nd = right;
  292. } else {
  293. // buffer overrun - PC is consuming too slowly
  294. audio_block_t *discard1 = left_1st;
  295. left_1st = left_2nd;
  296. left_2nd = left;
  297. audio_block_t *discard2 = right_1st;
  298. right_1st = right_2nd;
  299. right_2nd = right;
  300. offset_1st = 0; // TODO: discard part of this data?
  301. //serial_print("*");
  302. release(discard1);
  303. release(discard2);
  304. }
  305. __enable_irq();
  306. }
  307. // Called from the USB interrupt when ready to transmit another
  308. // isochronous packet. If we place data into the transmit buffer,
  309. // the return is the number of bytes. Otherwise, return 0 means
  310. // no data to transmit
  311. unsigned int usb_audio_transmit_callback(void)
  312. {
  313. static uint32_t count=5;
  314. uint32_t avail, num, target, offset, len=0;
  315. audio_block_t *left, *right;
  316. if (++count < 9) { // TODO: dynamic adjust to match USB rate
  317. target = 44;
  318. } else {
  319. count = 0;
  320. target = 45;
  321. }
  322. while (len < target) {
  323. num = target - len;
  324. left = AudioOutputUSB::left_1st;
  325. if (left == NULL) {
  326. // buffer underrun - PC is consuming too quickly
  327. memset(usb_audio_transmit_buffer + len, 0, num * 4);
  328. //serial_print("%");
  329. break;
  330. }
  331. right = AudioOutputUSB::right_1st;
  332. offset = AudioOutputUSB::offset_1st;
  333. avail = AUDIO_BLOCK_SAMPLES - offset;
  334. if (num > avail) num = avail;
  335. copy_from_buffers((uint32_t *)usb_audio_transmit_buffer + len,
  336. left->data + offset, right->data + offset, num);
  337. len += num;
  338. offset += num;
  339. if (offset >= AUDIO_BLOCK_SAMPLES) {
  340. AudioStream::release(left);
  341. AudioStream::release(right);
  342. AudioOutputUSB::left_1st = AudioOutputUSB::left_2nd;
  343. AudioOutputUSB::left_2nd = NULL;
  344. AudioOutputUSB::right_1st = AudioOutputUSB::right_2nd;
  345. AudioOutputUSB::right_2nd = NULL;
  346. AudioOutputUSB::offset_1st = 0;
  347. } else {
  348. AudioOutputUSB::offset_1st = offset;
  349. }
  350. }
  351. return target * 4;
  352. }
  353. #endif
  354. struct setup_struct {
  355. union {
  356. struct {
  357. uint8_t bmRequestType;
  358. uint8_t bRequest;
  359. union {
  360. struct {
  361. uint8_t bChannel; // 0=main, 1=left, 2=right
  362. uint8_t bCS; // Control Selector
  363. };
  364. uint16_t wValue;
  365. };
  366. union {
  367. struct {
  368. uint8_t bIfEp; // type of entity
  369. uint8_t bEntityId; // UnitID, TerminalID, etc.
  370. };
  371. uint16_t wIndex;
  372. };
  373. uint16_t wLength;
  374. };
  375. };
  376. };
  377. int usb_audio_get_feature(void *stp, uint8_t *data, uint32_t *datalen)
  378. {
  379. struct setup_struct setup = *((struct setup_struct *)stp);
  380. if (setup.bmRequestType==0xA1) { // should check bRequest, bChannel, and UnitID
  381. if (setup.bCS==0x01) { // mute
  382. data[0] = AudioInputUSB::features.mute; // 1=mute, 0=unmute
  383. *datalen = 1;
  384. return 1;
  385. }
  386. else if (setup.bCS==0x02) { // volume
  387. if (setup.bRequest==0x81) { // GET_CURR
  388. data[0] = AudioInputUSB::features.volume & 0xFF;
  389. data[1] = (AudioInputUSB::features.volume>>8) & 0xFF;
  390. }
  391. else if (setup.bRequest==0x82) { // GET_MIN
  392. //serial_print("vol get_min\n");
  393. data[0] = 0; // min level is 0
  394. data[1] = 0;
  395. }
  396. else if (setup.bRequest==0x83) { // GET_MAX
  397. data[0] = FEATURE_MAX_VOLUME & 0xFF; // max level, for range of 0 to MAX
  398. data[1] = (FEATURE_MAX_VOLUME>>8) & 0x0F;
  399. }
  400. else if (setup.bRequest==0x84) { // GET_RES
  401. data[0] = 1; // increment vol by by 1
  402. data[1] = 0;
  403. }
  404. else { // pass over SET_MEM, etc.
  405. return 0;
  406. }
  407. *datalen = 2;
  408. return 1;
  409. }
  410. }
  411. return 0;
  412. }
  413. int usb_audio_set_feature(void *stp, uint8_t *buf)
  414. {
  415. struct setup_struct setup = *((struct setup_struct *)stp);
  416. if (setup.bmRequestType==0x21) { // should check bRequest, bChannel and UnitID
  417. if (setup.bCS==0x01) { // mute
  418. if (setup.bRequest==0x01) { // SET_CUR
  419. AudioInputUSB::features.mute = buf[0]; // 1=mute,0=unmute
  420. AudioInputUSB::features.change = 1;
  421. return 1;
  422. }
  423. }
  424. else if (setup.bCS==0x02) { // volume
  425. if (setup.bRequest==0x01) { // SET_CUR
  426. AudioInputUSB::features.volume = buf[0] + (buf[1]<<8);
  427. AudioInputUSB::features.change = 1;
  428. return 1;
  429. }
  430. }
  431. }
  432. return 0;
  433. }
  434. #endif // AUDIO_INTERFACE