Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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