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

420 lines
13KB

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