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.

359 Zeilen
10KB

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