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 9.8KB

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