Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

360 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 8
  257. #define MCLK_DIV 153
  258. #define MCLK_SRC 0
  259. #elif F_CPU == 240000000
  260. #define MCLK_MULT 4
  261. #define MCLK_DIV 85
  262. #elif F_CPU == 16000000
  263. #define MCLK_MULT 12
  264. #define MCLK_DIV 17
  265. #else
  266. #error "This CPU Clock Speed is not supported by the Audio library";
  267. #endif
  268. #ifndef MCLK_SRC
  269. #if F_CPU >= 20000000
  270. #define MCLK_SRC 3 // the PLL
  271. #else
  272. #define MCLK_SRC 0 // system clock
  273. #endif
  274. #endif
  275. void AudioOutputI2SQuad::config_i2s(void)
  276. {
  277. SIM_SCGC6 |= SIM_SCGC6_I2S;
  278. SIM_SCGC7 |= SIM_SCGC7_DMA;
  279. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  280. // if either transmitter or receiver is enabled, do nothing
  281. if (I2S0_TCSR & I2S_TCSR_TE) return;
  282. if (I2S0_RCSR & I2S_RCSR_RE) return;
  283. // enable MCLK output
  284. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  285. while (I2S0_MCR & I2S_MCR_DUF) ;
  286. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  287. // configure transmitter
  288. I2S0_TMR = 0;
  289. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  290. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  291. | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  292. I2S0_TCR3 = I2S_TCR3_TCE_2CH;
  293. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  294. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  295. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  296. // configure receiver (sync'd to transmitter clocks)
  297. I2S0_RMR = 0;
  298. I2S0_RCR1 = I2S_RCR1_RFW(1);
  299. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  300. | I2S_RCR2_BCD | I2S_RCR2_DIV(3);
  301. I2S0_RCR3 = I2S_RCR3_RCE_2CH;
  302. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  303. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  304. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  305. // configure pin mux for 3 clock signals
  306. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  307. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  308. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  309. }
  310. #else // not __MK20DX256__
  311. void AudioOutputI2SQuad::begin(void)
  312. {
  313. }
  314. void AudioOutputI2SQuad::update(void)
  315. {
  316. audio_block_t *block;
  317. block = receiveReadOnly(0);
  318. if (block) release(block);
  319. block = receiveReadOnly(1);
  320. if (block) release(block);
  321. block = receiveReadOnly(2);
  322. if (block) release(block);
  323. block = receiveReadOnly(3);
  324. if (block) release(block);
  325. }
  326. #endif