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.

337 lines
9.5KB

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