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.

output_i2s_quad.cpp 12KB

пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 8 година
пре 8 година
пре 4 година
пре 8 година
пре 4 година
пре 4 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 4 година
пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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_quad.h"
  28. #include "output_i2s.h"
  29. #include "memcpy_audio.h"
  30. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1062__)
  31. #if defined(__IMXRT1062__)
  32. #include "utility/imxrt_hw.h"
  33. #endif
  34. audio_block_t * AudioOutputI2SQuad::block_ch1_1st = NULL;
  35. audio_block_t * AudioOutputI2SQuad::block_ch2_1st = NULL;
  36. audio_block_t * AudioOutputI2SQuad::block_ch3_1st = NULL;
  37. audio_block_t * AudioOutputI2SQuad::block_ch4_1st = NULL;
  38. audio_block_t * AudioOutputI2SQuad::block_ch1_2nd = NULL;
  39. audio_block_t * AudioOutputI2SQuad::block_ch2_2nd = NULL;
  40. audio_block_t * AudioOutputI2SQuad::block_ch3_2nd = NULL;
  41. audio_block_t * AudioOutputI2SQuad::block_ch4_2nd = NULL;
  42. uint16_t AudioOutputI2SQuad::ch1_offset = 0;
  43. uint16_t AudioOutputI2SQuad::ch2_offset = 0;
  44. uint16_t AudioOutputI2SQuad::ch3_offset = 0;
  45. uint16_t AudioOutputI2SQuad::ch4_offset = 0;
  46. bool AudioOutputI2SQuad::update_responsibility = false;
  47. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES*2];
  48. DMAChannel AudioOutputI2SQuad::dma(false);
  49. static const uint32_t zerodata[AUDIO_BLOCK_SAMPLES/4] = {0};
  50. void AudioOutputI2SQuad::begin(void)
  51. {
  52. dma.begin(true); // Allocate the DMA channel first
  53. block_ch1_1st = NULL;
  54. block_ch2_1st = NULL;
  55. block_ch3_1st = NULL;
  56. block_ch4_1st = NULL;
  57. #if defined(KINETISK)
  58. // TODO: can we call normal config_i2s, and then just enable the extra output?
  59. config_i2s();
  60. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0 -> ch1 & ch2
  61. CORE_PIN15_CONFIG = PORT_PCR_MUX(6); // pin 15, PTC0, I2S0_TXD1 -> ch3 & ch4
  62. dma.TCD->SADDR = i2s_tx_buffer;
  63. dma.TCD->SOFF = 2;
  64. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1) | DMA_TCD_ATTR_DMOD(3);
  65. dma.TCD->NBYTES_MLNO = 4;
  66. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  67. dma.TCD->DADDR = &I2S0_TDR0;
  68. dma.TCD->DOFF = 4;
  69. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 4;
  70. dma.TCD->DLASTSGA = 0;
  71. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 4;
  72. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  73. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  74. update_responsibility = update_setup();
  75. dma.enable();
  76. I2S0_TCSR = I2S_TCSR_SR;
  77. I2S0_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  78. dma.attachInterrupt(isr);
  79. #elif defined(__IMXRT1062__)
  80. const int pinoffset = 0; // TODO: make this configurable...
  81. memset(i2s_tx_buffer, 0, sizeof(i2s_tx_buffer));
  82. AudioOutputI2S::config_i2s();
  83. I2S1_TCR3 = I2S_TCR3_TCE_2CH << pinoffset;
  84. switch (pinoffset) {
  85. case 0:
  86. CORE_PIN7_CONFIG = 3;
  87. CORE_PIN32_CONFIG = 3;
  88. break;
  89. case 1:
  90. CORE_PIN32_CONFIG = 3;
  91. CORE_PIN9_CONFIG = 3;
  92. break;
  93. case 2:
  94. CORE_PIN9_CONFIG = 3;
  95. CORE_PIN6_CONFIG = 3;
  96. }
  97. dma.TCD->SADDR = i2s_tx_buffer;
  98. dma.TCD->SOFF = 2;
  99. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  100. dma.TCD->NBYTES_MLOFFYES = DMA_TCD_NBYTES_DMLOE |
  101. DMA_TCD_NBYTES_MLOFFYES_MLOFF(-8) |
  102. DMA_TCD_NBYTES_MLOFFYES_NBYTES(4);
  103. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  104. dma.TCD->DADDR = (void *)((uint32_t)&I2S1_TDR0 + 2 + pinoffset * 4);
  105. dma.TCD->DOFF = 4;
  106. dma.TCD->CITER_ELINKNO = AUDIO_BLOCK_SAMPLES * 2;
  107. dma.TCD->DLASTSGA = -8;
  108. dma.TCD->BITER_ELINKNO = AUDIO_BLOCK_SAMPLES * 2;
  109. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  110. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  111. dma.enable();
  112. I2S1_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE;
  113. I2S1_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  114. I2S1_TCR3 = I2S_TCR3_TCE_2CH << pinoffset;
  115. update_responsibility = update_setup();
  116. dma.attachInterrupt(isr);
  117. #endif
  118. }
  119. void AudioOutputI2SQuad::isr(void)
  120. {
  121. uint32_t saddr;
  122. const int16_t *src1, *src2, *src3, *src4;
  123. const int16_t *zeros = (const int16_t *)zerodata;
  124. int16_t *dest;
  125. saddr = (uint32_t)(dma.TCD->SADDR);
  126. dma.clearInterrupt();
  127. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  128. // DMA is transmitting the first half of the buffer
  129. // so we must fill the second half
  130. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  131. if (update_responsibility) update_all();
  132. } else {
  133. dest = (int16_t *)i2s_tx_buffer;
  134. }
  135. src1 = (block_ch1_1st) ? block_ch1_1st->data + ch1_offset : zeros;
  136. src2 = (block_ch2_1st) ? block_ch2_1st->data + ch2_offset : zeros;
  137. src3 = (block_ch3_1st) ? block_ch3_1st->data + ch3_offset : zeros;
  138. src4 = (block_ch4_1st) ? block_ch4_1st->data + ch4_offset : zeros;
  139. #if 1
  140. memcpy_tointerleaveQuad(dest, src1, src2, src3, src4);
  141. #else
  142. for (int i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  143. *dest++ = *src1++;
  144. *dest++ = *src3++;
  145. *dest++ = *src2++;
  146. *dest++ = *src4++;
  147. }
  148. #endif
  149. arm_dcache_flush_delete(dest, sizeof(i2s_tx_buffer) / 2 );
  150. if (block_ch1_1st) {
  151. if (ch1_offset == 0) {
  152. ch1_offset = AUDIO_BLOCK_SAMPLES/2;
  153. } else {
  154. ch1_offset = 0;
  155. release(block_ch1_1st);
  156. block_ch1_1st = block_ch1_2nd;
  157. block_ch1_2nd = NULL;
  158. }
  159. }
  160. if (block_ch2_1st) {
  161. if (ch2_offset == 0) {
  162. ch2_offset = AUDIO_BLOCK_SAMPLES/2;
  163. } else {
  164. ch2_offset = 0;
  165. release(block_ch2_1st);
  166. block_ch2_1st = block_ch2_2nd;
  167. block_ch2_2nd = NULL;
  168. }
  169. }
  170. if (block_ch3_1st) {
  171. if (ch3_offset == 0) {
  172. ch3_offset = AUDIO_BLOCK_SAMPLES/2;
  173. } else {
  174. ch3_offset = 0;
  175. release(block_ch3_1st);
  176. block_ch3_1st = block_ch3_2nd;
  177. block_ch3_2nd = NULL;
  178. }
  179. }
  180. if (block_ch4_1st) {
  181. if (ch4_offset == 0) {
  182. ch4_offset = AUDIO_BLOCK_SAMPLES/2;
  183. } else {
  184. ch4_offset = 0;
  185. release(block_ch4_1st);
  186. block_ch4_1st = block_ch4_2nd;
  187. block_ch4_2nd = NULL;
  188. }
  189. }
  190. }
  191. void AudioOutputI2SQuad::update(void)
  192. {
  193. audio_block_t *block, *tmp;
  194. block = receiveReadOnly(0); // channel 1
  195. if (block) {
  196. __disable_irq();
  197. if (block_ch1_1st == NULL) {
  198. block_ch1_1st = block;
  199. ch1_offset = 0;
  200. __enable_irq();
  201. } else if (block_ch1_2nd == NULL) {
  202. block_ch1_2nd = block;
  203. __enable_irq();
  204. } else {
  205. tmp = block_ch1_1st;
  206. block_ch1_1st = block_ch1_2nd;
  207. block_ch1_2nd = block;
  208. ch1_offset = 0;
  209. __enable_irq();
  210. release(tmp);
  211. }
  212. }
  213. block = receiveReadOnly(1); // channel 2
  214. if (block) {
  215. __disable_irq();
  216. if (block_ch2_1st == NULL) {
  217. block_ch2_1st = block;
  218. ch2_offset = 0;
  219. __enable_irq();
  220. } else if (block_ch2_2nd == NULL) {
  221. block_ch2_2nd = block;
  222. __enable_irq();
  223. } else {
  224. tmp = block_ch2_1st;
  225. block_ch2_1st = block_ch2_2nd;
  226. block_ch2_2nd = block;
  227. ch2_offset = 0;
  228. __enable_irq();
  229. release(tmp);
  230. }
  231. }
  232. block = receiveReadOnly(2); // channel 3
  233. if (block) {
  234. __disable_irq();
  235. if (block_ch3_1st == NULL) {
  236. block_ch3_1st = block;
  237. ch3_offset = 0;
  238. __enable_irq();
  239. } else if (block_ch3_2nd == NULL) {
  240. block_ch3_2nd = block;
  241. __enable_irq();
  242. } else {
  243. tmp = block_ch3_1st;
  244. block_ch3_1st = block_ch3_2nd;
  245. block_ch3_2nd = block;
  246. ch3_offset = 0;
  247. __enable_irq();
  248. release(tmp);
  249. }
  250. }
  251. block = receiveReadOnly(3); // channel 4
  252. if (block) {
  253. __disable_irq();
  254. if (block_ch4_1st == NULL) {
  255. block_ch4_1st = block;
  256. ch4_offset = 0;
  257. __enable_irq();
  258. } else if (block_ch4_2nd == NULL) {
  259. block_ch4_2nd = block;
  260. __enable_irq();
  261. } else {
  262. tmp = block_ch4_1st;
  263. block_ch4_1st = block_ch4_2nd;
  264. block_ch4_2nd = block;
  265. ch4_offset = 0;
  266. __enable_irq();
  267. release(tmp);
  268. }
  269. }
  270. }
  271. #if defined(KINETISK)
  272. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  273. //
  274. #if F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  275. // PLL is at 96 MHz in these modes
  276. #define MCLK_MULT 2
  277. #define MCLK_DIV 17
  278. #elif F_CPU == 72000000
  279. #define MCLK_MULT 8
  280. #define MCLK_DIV 51
  281. #elif F_CPU == 120000000
  282. #define MCLK_MULT 8
  283. #define MCLK_DIV 85
  284. #elif F_CPU == 144000000
  285. #define MCLK_MULT 4
  286. #define MCLK_DIV 51
  287. #elif F_CPU == 168000000
  288. #define MCLK_MULT 8
  289. #define MCLK_DIV 119
  290. #elif F_CPU == 180000000
  291. #define MCLK_MULT 16
  292. #define MCLK_DIV 255
  293. #define MCLK_SRC 0
  294. #elif F_CPU == 192000000
  295. #define MCLK_MULT 1
  296. #define MCLK_DIV 17
  297. #elif F_CPU == 216000000
  298. #define MCLK_MULT 12
  299. #define MCLK_DIV 17
  300. #define MCLK_SRC 1
  301. #elif F_CPU == 240000000
  302. #define MCLK_MULT 2
  303. #define MCLK_DIV 85
  304. #define MCLK_SRC 0
  305. #elif F_CPU == 256000000
  306. #define MCLK_MULT 12
  307. #define MCLK_DIV 17
  308. #define MCLK_SRC 1
  309. #elif F_CPU == 16000000
  310. #define MCLK_MULT 12
  311. #define MCLK_DIV 17
  312. #else
  313. #error "This CPU Clock Speed is not supported by the Audio library";
  314. #endif
  315. #ifndef MCLK_SRC
  316. #if F_CPU >= 20000000
  317. #define MCLK_SRC 3 // the PLL
  318. #else
  319. #define MCLK_SRC 0 // system clock
  320. #endif
  321. #endif
  322. void AudioOutputI2SQuad::config_i2s(void)
  323. {
  324. SIM_SCGC6 |= SIM_SCGC6_I2S;
  325. SIM_SCGC7 |= SIM_SCGC7_DMA;
  326. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  327. // if either transmitter or receiver is enabled, do nothing
  328. if (I2S0_TCSR & I2S_TCSR_TE) return;
  329. if (I2S0_RCSR & I2S_RCSR_RE) return;
  330. // enable MCLK output
  331. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  332. while (I2S0_MCR & I2S_MCR_DUF) ;
  333. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  334. // configure transmitter
  335. I2S0_TMR = 0;
  336. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  337. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  338. | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  339. I2S0_TCR3 = I2S_TCR3_TCE_2CH;
  340. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  341. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  342. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  343. // configure receiver (sync'd to transmitter clocks)
  344. I2S0_RMR = 0;
  345. I2S0_RCR1 = I2S_RCR1_RFW(1);
  346. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  347. | I2S_RCR2_BCD | I2S_RCR2_DIV(3);
  348. I2S0_RCR3 = I2S_RCR3_RCE_2CH;
  349. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  350. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  351. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  352. // configure pin mux for 3 clock signals
  353. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  354. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  355. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  356. }
  357. #endif // KINETISK
  358. #else // not supported
  359. void AudioOutputI2SQuad::begin(void)
  360. {
  361. }
  362. void AudioOutputI2SQuad::update(void)
  363. {
  364. audio_block_t *block;
  365. block = receiveReadOnly(0);
  366. if (block) release(block);
  367. block = receiveReadOnly(1);
  368. if (block) release(block);
  369. block = receiveReadOnly(2);
  370. if (block) release(block);
  371. block = receiveReadOnly(3);
  372. if (block) release(block);
  373. }
  374. #endif