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.

402 satır
12KB

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