PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

420 Zeilen
11KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "output_i2s_oct.h"
  28. #include "output_i2s.h"
  29. #include "memcpy_audio.h"
  30. #if defined(__IMXRT1062__)
  31. #include "utility/imxrt_hw.h"
  32. audio_block_t * AudioOutputI2SOct::block_ch1_1st = NULL;
  33. audio_block_t * AudioOutputI2SOct::block_ch2_1st = NULL;
  34. audio_block_t * AudioOutputI2SOct::block_ch3_1st = NULL;
  35. audio_block_t * AudioOutputI2SOct::block_ch4_1st = NULL;
  36. audio_block_t * AudioOutputI2SOct::block_ch5_1st = NULL;
  37. audio_block_t * AudioOutputI2SOct::block_ch6_1st = NULL;
  38. audio_block_t * AudioOutputI2SOct::block_ch7_1st = NULL;
  39. audio_block_t * AudioOutputI2SOct::block_ch8_1st = NULL;
  40. audio_block_t * AudioOutputI2SOct::block_ch1_2nd = NULL;
  41. audio_block_t * AudioOutputI2SOct::block_ch2_2nd = NULL;
  42. audio_block_t * AudioOutputI2SOct::block_ch3_2nd = NULL;
  43. audio_block_t * AudioOutputI2SOct::block_ch4_2nd = NULL;
  44. audio_block_t * AudioOutputI2SOct::block_ch5_2nd = NULL;
  45. audio_block_t * AudioOutputI2SOct::block_ch6_2nd = NULL;
  46. audio_block_t * AudioOutputI2SOct::block_ch7_2nd = NULL;
  47. audio_block_t * AudioOutputI2SOct::block_ch8_2nd = NULL;
  48. uint16_t AudioOutputI2SOct::ch1_offset = 0;
  49. uint16_t AudioOutputI2SOct::ch2_offset = 0;
  50. uint16_t AudioOutputI2SOct::ch3_offset = 0;
  51. uint16_t AudioOutputI2SOct::ch4_offset = 0;
  52. uint16_t AudioOutputI2SOct::ch5_offset = 0;
  53. uint16_t AudioOutputI2SOct::ch6_offset = 0;
  54. uint16_t AudioOutputI2SOct::ch7_offset = 0;
  55. uint16_t AudioOutputI2SOct::ch8_offset = 0;
  56. bool AudioOutputI2SOct::update_responsibility = false;
  57. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES*4];
  58. DMAChannel AudioOutputI2SOct::dma(false);
  59. static const uint32_t zerodata[AUDIO_BLOCK_SAMPLES/4] = {0};
  60. void AudioOutputI2SOct::begin(void)
  61. {
  62. dma.begin(true); // Allocate the DMA channel first
  63. block_ch1_1st = NULL;
  64. block_ch2_1st = NULL;
  65. block_ch3_1st = NULL;
  66. block_ch4_1st = NULL;
  67. block_ch5_1st = NULL;
  68. block_ch6_1st = NULL;
  69. memset(i2s_tx_buffer, 0, sizeof(i2s_tx_buffer));
  70. AudioOutputI2S::config_i2s();
  71. I2S1_TCR3 = I2S_TCR3_TCE_4CH;
  72. CORE_PIN7_CONFIG = 3;
  73. CORE_PIN32_CONFIG = 3;
  74. CORE_PIN6_CONFIG = 3;
  75. CORE_PIN9_CONFIG = 3;
  76. dma.TCD->SADDR = i2s_tx_buffer;
  77. dma.TCD->SOFF = 2;
  78. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  79. dma.TCD->NBYTES_MLOFFYES = DMA_TCD_NBYTES_DMLOE |
  80. DMA_TCD_NBYTES_MLOFFYES_MLOFF(-16) |
  81. DMA_TCD_NBYTES_MLOFFYES_NBYTES(8);
  82. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  83. dma.TCD->DADDR = (void *)((uint32_t)&I2S1_TDR0 + 2);
  84. dma.TCD->DOFF = 4;
  85. dma.TCD->CITER_ELINKNO = AUDIO_BLOCK_SAMPLES * 2;
  86. dma.TCD->DLASTSGA = -16;
  87. dma.TCD->BITER_ELINKNO = AUDIO_BLOCK_SAMPLES * 2;
  88. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  89. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  90. dma.enable();
  91. I2S1_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE;
  92. I2S1_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  93. //I2S1_TCR3 = I2S_TCR3_TCE_4CH;
  94. update_responsibility = update_setup();
  95. dma.attachInterrupt(isr);
  96. }
  97. void AudioOutputI2SOct::isr(void)
  98. {
  99. uint32_t saddr;
  100. const int16_t *src1, *src2, *src3, *src4, *src5, *src6, *src7, *src8;
  101. const int16_t *zeros = (const int16_t *)zerodata;
  102. int16_t *dest;
  103. saddr = (uint32_t)(dma.TCD->SADDR);
  104. dma.clearInterrupt();
  105. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  106. // DMA is transmitting the first half of the buffer
  107. // so we must fill the second half
  108. dest = (int16_t *)((uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2);
  109. if (update_responsibility) update_all();
  110. } else {
  111. dest = (int16_t *)i2s_tx_buffer;
  112. }
  113. src1 = (block_ch1_1st) ? block_ch1_1st->data + ch1_offset : zeros;
  114. src2 = (block_ch2_1st) ? block_ch2_1st->data + ch2_offset : zeros;
  115. src3 = (block_ch3_1st) ? block_ch3_1st->data + ch3_offset : zeros;
  116. src4 = (block_ch4_1st) ? block_ch4_1st->data + ch4_offset : zeros;
  117. src5 = (block_ch5_1st) ? block_ch5_1st->data + ch5_offset : zeros;
  118. src6 = (block_ch6_1st) ? block_ch6_1st->data + ch6_offset : zeros;
  119. src7 = (block_ch7_1st) ? block_ch7_1st->data + ch7_offset : zeros;
  120. src8 = (block_ch8_1st) ? block_ch8_1st->data + ch8_offset : zeros;
  121. #if 0
  122. // TODO: optimized 8 channel copy...
  123. memcpy_tointerleaveQuad(dest, src1, src2, src3, src4);
  124. #else
  125. int16_t *p=dest;
  126. for (int i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  127. *p++ = *src1++;
  128. *p++ = *src3++;
  129. *p++ = *src5++;
  130. *p++ = *src7++;
  131. *p++ = *src2++;
  132. *p++ = *src4++;
  133. *p++ = *src6++;
  134. *p++ = *src8++;
  135. }
  136. #endif
  137. arm_dcache_flush_delete(dest, sizeof(i2s_tx_buffer) / 2);
  138. if (block_ch1_1st) {
  139. if (ch1_offset == 0) {
  140. ch1_offset = AUDIO_BLOCK_SAMPLES/2;
  141. } else {
  142. ch1_offset = 0;
  143. release(block_ch1_1st);
  144. block_ch1_1st = block_ch1_2nd;
  145. block_ch1_2nd = NULL;
  146. }
  147. }
  148. if (block_ch2_1st) {
  149. if (ch2_offset == 0) {
  150. ch2_offset = AUDIO_BLOCK_SAMPLES/2;
  151. } else {
  152. ch2_offset = 0;
  153. release(block_ch2_1st);
  154. block_ch2_1st = block_ch2_2nd;
  155. block_ch2_2nd = NULL;
  156. }
  157. }
  158. if (block_ch3_1st) {
  159. if (ch3_offset == 0) {
  160. ch3_offset = AUDIO_BLOCK_SAMPLES/2;
  161. } else {
  162. ch3_offset = 0;
  163. release(block_ch3_1st);
  164. block_ch3_1st = block_ch3_2nd;
  165. block_ch3_2nd = NULL;
  166. }
  167. }
  168. if (block_ch4_1st) {
  169. if (ch4_offset == 0) {
  170. ch4_offset = AUDIO_BLOCK_SAMPLES/2;
  171. } else {
  172. ch4_offset = 0;
  173. release(block_ch4_1st);
  174. block_ch4_1st = block_ch4_2nd;
  175. block_ch4_2nd = NULL;
  176. }
  177. }
  178. if (block_ch5_1st) {
  179. if (ch5_offset == 0) {
  180. ch5_offset = AUDIO_BLOCK_SAMPLES/2;
  181. } else {
  182. ch5_offset = 0;
  183. release(block_ch5_1st);
  184. block_ch5_1st = block_ch5_2nd;
  185. block_ch5_2nd = NULL;
  186. }
  187. }
  188. if (block_ch6_1st) {
  189. if (ch6_offset == 0) {
  190. ch6_offset = AUDIO_BLOCK_SAMPLES/2;
  191. } else {
  192. ch6_offset = 0;
  193. release(block_ch6_1st);
  194. block_ch6_1st = block_ch6_2nd;
  195. block_ch6_2nd = NULL;
  196. }
  197. }
  198. if (block_ch7_1st) {
  199. if (ch7_offset == 0) {
  200. ch7_offset = AUDIO_BLOCK_SAMPLES/2;
  201. } else {
  202. ch7_offset = 0;
  203. release(block_ch7_1st);
  204. block_ch7_1st = block_ch7_2nd;
  205. block_ch7_2nd = NULL;
  206. }
  207. }
  208. if (block_ch8_1st) {
  209. if (ch8_offset == 0) {
  210. ch8_offset = AUDIO_BLOCK_SAMPLES/2;
  211. } else {
  212. ch8_offset = 0;
  213. release(block_ch8_1st);
  214. block_ch8_1st = block_ch8_2nd;
  215. block_ch8_2nd = NULL;
  216. }
  217. }
  218. }
  219. void AudioOutputI2SOct::update(void)
  220. {
  221. audio_block_t *block, *tmp;
  222. block = receiveReadOnly(0); // channel 1
  223. if (block) {
  224. __disable_irq();
  225. if (block_ch1_1st == NULL) {
  226. block_ch1_1st = block;
  227. ch1_offset = 0;
  228. __enable_irq();
  229. } else if (block_ch1_2nd == NULL) {
  230. block_ch1_2nd = block;
  231. __enable_irq();
  232. } else {
  233. tmp = block_ch1_1st;
  234. block_ch1_1st = block_ch1_2nd;
  235. block_ch1_2nd = block;
  236. ch1_offset = 0;
  237. __enable_irq();
  238. release(tmp);
  239. }
  240. }
  241. block = receiveReadOnly(1); // channel 2
  242. if (block) {
  243. __disable_irq();
  244. if (block_ch2_1st == NULL) {
  245. block_ch2_1st = block;
  246. ch2_offset = 0;
  247. __enable_irq();
  248. } else if (block_ch2_2nd == NULL) {
  249. block_ch2_2nd = block;
  250. __enable_irq();
  251. } else {
  252. tmp = block_ch2_1st;
  253. block_ch2_1st = block_ch2_2nd;
  254. block_ch2_2nd = block;
  255. ch2_offset = 0;
  256. __enable_irq();
  257. release(tmp);
  258. }
  259. }
  260. block = receiveReadOnly(2); // channel 3
  261. if (block) {
  262. __disable_irq();
  263. if (block_ch3_1st == NULL) {
  264. block_ch3_1st = block;
  265. ch3_offset = 0;
  266. __enable_irq();
  267. } else if (block_ch3_2nd == NULL) {
  268. block_ch3_2nd = block;
  269. __enable_irq();
  270. } else {
  271. tmp = block_ch3_1st;
  272. block_ch3_1st = block_ch3_2nd;
  273. block_ch3_2nd = block;
  274. ch3_offset = 0;
  275. __enable_irq();
  276. release(tmp);
  277. }
  278. }
  279. block = receiveReadOnly(3); // channel 4
  280. if (block) {
  281. __disable_irq();
  282. if (block_ch4_1st == NULL) {
  283. block_ch4_1st = block;
  284. ch4_offset = 0;
  285. __enable_irq();
  286. } else if (block_ch4_2nd == NULL) {
  287. block_ch4_2nd = block;
  288. __enable_irq();
  289. } else {
  290. tmp = block_ch4_1st;
  291. block_ch4_1st = block_ch4_2nd;
  292. block_ch4_2nd = block;
  293. ch4_offset = 0;
  294. __enable_irq();
  295. release(tmp);
  296. }
  297. }
  298. block = receiveReadOnly(4); // channel 5
  299. if (block) {
  300. __disable_irq();
  301. if (block_ch5_1st == NULL) {
  302. block_ch5_1st = block;
  303. ch5_offset = 0;
  304. __enable_irq();
  305. } else if (block_ch5_2nd == NULL) {
  306. block_ch5_2nd = block;
  307. __enable_irq();
  308. } else {
  309. tmp = block_ch5_1st;
  310. block_ch5_1st = block_ch5_2nd;
  311. block_ch5_2nd = block;
  312. ch5_offset = 0;
  313. __enable_irq();
  314. release(tmp);
  315. }
  316. }
  317. block = receiveReadOnly(5); // channel 6
  318. if (block) {
  319. __disable_irq();
  320. if (block_ch6_1st == NULL) {
  321. block_ch6_1st = block;
  322. ch6_offset = 0;
  323. __enable_irq();
  324. } else if (block_ch6_2nd == NULL) {
  325. block_ch6_2nd = block;
  326. __enable_irq();
  327. } else {
  328. tmp = block_ch6_1st;
  329. block_ch6_1st = block_ch6_2nd;
  330. block_ch6_2nd = block;
  331. ch6_offset = 0;
  332. __enable_irq();
  333. release(tmp);
  334. }
  335. }
  336. block = receiveReadOnly(6); // channel 7
  337. if (block) {
  338. __disable_irq();
  339. if (block_ch7_1st == NULL) {
  340. block_ch7_1st = block;
  341. ch7_offset = 0;
  342. __enable_irq();
  343. } else if (block_ch7_2nd == NULL) {
  344. block_ch7_2nd = block;
  345. __enable_irq();
  346. } else {
  347. tmp = block_ch7_1st;
  348. block_ch7_1st = block_ch7_2nd;
  349. block_ch7_2nd = block;
  350. ch7_offset = 0;
  351. __enable_irq();
  352. release(tmp);
  353. }
  354. }
  355. block = receiveReadOnly(7); // channel 8
  356. if (block) {
  357. __disable_irq();
  358. if (block_ch8_1st == NULL) {
  359. block_ch8_1st = block;
  360. ch8_offset = 0;
  361. __enable_irq();
  362. } else if (block_ch8_2nd == NULL) {
  363. block_ch8_2nd = block;
  364. __enable_irq();
  365. } else {
  366. tmp = block_ch8_1st;
  367. block_ch8_1st = block_ch8_2nd;
  368. block_ch8_2nd = block;
  369. ch8_offset = 0;
  370. __enable_irq();
  371. release(tmp);
  372. }
  373. }
  374. }
  375. #else // not supported
  376. void AudioOutputI2SOct::begin(void)
  377. {
  378. }
  379. void AudioOutputI2SOct::update(void)
  380. {
  381. audio_block_t *block;
  382. block = receiveReadOnly(0);
  383. if (block) release(block);
  384. block = receiveReadOnly(1);
  385. if (block) release(block);
  386. block = receiveReadOnly(2);
  387. if (block) release(block);
  388. block = receiveReadOnly(3);
  389. if (block) release(block);
  390. block = receiveReadOnly(4);
  391. if (block) release(block);
  392. block = receiveReadOnly(5);
  393. if (block) release(block);
  394. block = receiveReadOnly(6);
  395. if (block) release(block);
  396. block = receiveReadOnly(7);
  397. if (block) release(block);
  398. }
  399. #endif