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.

327 lines
9.9KB

  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. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  28. // Possible to create using fractional divider for all USB-compatible Kinetis:
  29. // MCLK = 16e6 * 12 / 17
  30. // MCLK = 24e6 * 8 / 17
  31. // MCLK = 48e6 * 4 / 17
  32. // MCLK = 72e6 * 8 / 51
  33. // MCLK = 96e6 * 2 / 17
  34. // MCLK = 120e6 * 8 / 85
  35. // TODO: instigate using I2S0_MCR to select the crystal directly instead of the system
  36. // clock, which has audio band jitter from the PLL
  37. audio_block_t * AudioOutputI2S::block_left_1st = NULL;
  38. audio_block_t * AudioOutputI2S::block_right_1st = NULL;
  39. audio_block_t * AudioOutputI2S::block_left_2nd = NULL;
  40. audio_block_t * AudioOutputI2S::block_right_2nd = NULL;
  41. uint16_t AudioOutputI2S::block_left_offset = 0;
  42. uint16_t AudioOutputI2S::block_right_offset = 0;
  43. bool AudioOutputI2S::update_responsibility = false;
  44. DMAMEM static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  45. void AudioOutputI2S::begin(void)
  46. {
  47. //pinMode(2, OUTPUT);
  48. block_left_1st = NULL;
  49. block_right_1st = NULL;
  50. config_i2s();
  51. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  52. DMA_CR = 0;
  53. DMA_TCD0_SADDR = i2s_tx_buffer;
  54. DMA_TCD0_SOFF = 2;
  55. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  56. DMA_TCD0_NBYTES_MLNO = 2;
  57. DMA_TCD0_SLAST = -sizeof(i2s_tx_buffer);
  58. DMA_TCD0_DADDR = &I2S0_TDR0;
  59. DMA_TCD0_DOFF = 0;
  60. DMA_TCD0_CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  61. DMA_TCD0_DLASTSGA = 0;
  62. DMA_TCD0_BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  63. DMA_TCD0_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  64. DMAMUX0_CHCFG0 = DMAMUX_DISABLE;
  65. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_I2S0_TX | DMAMUX_ENABLE;
  66. update_responsibility = update_setup();
  67. DMA_SERQ = 0;
  68. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  69. NVIC_ENABLE_IRQ(IRQ_DMA_CH0);
  70. }
  71. void dma_ch0_isr(void)
  72. {
  73. const int16_t *src, *end;
  74. int16_t *dest;
  75. audio_block_t *block;
  76. uint32_t saddr, offset;
  77. saddr = (uint32_t)DMA_TCD0_SADDR;
  78. DMA_CINT = 0;
  79. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  80. // DMA is transmitting the first half of the buffer
  81. // so we must fill the second half
  82. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  83. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  84. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  85. } else {
  86. // DMA is transmitting the second half of the buffer
  87. // so we must fill the first half
  88. dest = (int16_t *)i2s_tx_buffer;
  89. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  90. }
  91. // TODO: these copy routines could be merged and optimized, maybe in assembly?
  92. block = AudioOutputI2S::block_left_1st;
  93. if (block) {
  94. offset = AudioOutputI2S::block_left_offset;
  95. src = &block->data[offset];
  96. do {
  97. *dest = *src++;
  98. dest += 2;
  99. } while (dest < end);
  100. offset += AUDIO_BLOCK_SAMPLES/2;
  101. if (offset < AUDIO_BLOCK_SAMPLES) {
  102. AudioOutputI2S::block_left_offset = offset;
  103. } else {
  104. AudioOutputI2S::block_left_offset = 0;
  105. AudioStream::release(block);
  106. AudioOutputI2S::block_left_1st = AudioOutputI2S::block_left_2nd;
  107. AudioOutputI2S::block_left_2nd = NULL;
  108. }
  109. } else {
  110. do {
  111. *dest = 0;
  112. dest += 2;
  113. } while (dest < end);
  114. }
  115. dest -= AUDIO_BLOCK_SAMPLES - 1;
  116. block = AudioOutputI2S::block_right_1st;
  117. if (block) {
  118. offset = AudioOutputI2S::block_right_offset;
  119. src = &block->data[offset];
  120. do {
  121. *dest = *src++;
  122. dest += 2;
  123. } while (dest < end);
  124. offset += AUDIO_BLOCK_SAMPLES/2;
  125. if (offset < AUDIO_BLOCK_SAMPLES) {
  126. AudioOutputI2S::block_right_offset = offset;
  127. } else {
  128. AudioOutputI2S::block_right_offset = 0;
  129. AudioStream::release(block);
  130. AudioOutputI2S::block_right_1st = AudioOutputI2S::block_right_2nd;
  131. AudioOutputI2S::block_right_2nd = NULL;
  132. }
  133. } else {
  134. do {
  135. *dest = 0;
  136. dest += 2;
  137. } while (dest < end);
  138. }
  139. }
  140. void AudioOutputI2S::update(void)
  141. {
  142. // null audio device: discard all incoming data
  143. //if (!active) return;
  144. //audio_block_t *block = receiveReadOnly();
  145. //if (block) release(block);
  146. audio_block_t *block;
  147. block = receiveReadOnly(0); // input 0 = left channel
  148. if (block) {
  149. __disable_irq();
  150. if (block_left_1st == NULL) {
  151. block_left_1st = block;
  152. block_left_offset = 0;
  153. __enable_irq();
  154. } else if (block_left_2nd == NULL) {
  155. block_left_2nd = block;
  156. __enable_irq();
  157. } else {
  158. audio_block_t *tmp = block_left_1st;
  159. block_left_1st = block_left_2nd;
  160. block_left_2nd = block;
  161. block_left_offset = 0;
  162. __enable_irq();
  163. release(tmp);
  164. }
  165. }
  166. block = receiveReadOnly(1); // input 1 = right channel
  167. if (block) {
  168. __disable_irq();
  169. if (block_right_1st == NULL) {
  170. block_right_1st = block;
  171. block_right_offset = 0;
  172. __enable_irq();
  173. } else if (block_right_2nd == NULL) {
  174. block_right_2nd = block;
  175. __enable_irq();
  176. } else {
  177. audio_block_t *tmp = block_right_1st;
  178. block_right_1st = block_right_2nd;
  179. block_right_2nd = block;
  180. block_right_offset = 0;
  181. __enable_irq();
  182. release(tmp);
  183. }
  184. }
  185. }
  186. void AudioOutputI2S::config_i2s(void)
  187. {
  188. SIM_SCGC6 |= SIM_SCGC6_I2S;
  189. SIM_SCGC7 |= SIM_SCGC7_DMA;
  190. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  191. // if either transmitter or receiver is enabled, do nothing
  192. if (I2S0_TCSR & I2S_TCSR_TE) return;
  193. if (I2S0_RCSR & I2S_RCSR_RE) return;
  194. // enable MCLK output
  195. I2S0_MCR = I2S_MCR_MICS(3) | I2S_MCR_MOE;
  196. I2S0_MDR = I2S_MDR_FRACT(1) | I2S_MDR_DIVIDE(16);
  197. // configure transmitter
  198. I2S0_TMR = 0;
  199. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  200. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  201. | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  202. I2S0_TCR3 = I2S_TCR3_TCE;
  203. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  204. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  205. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  206. // configure receiver (sync'd to transmitter clocks)
  207. I2S0_RMR = 0;
  208. I2S0_RCR1 = I2S_RCR1_RFW(1);
  209. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  210. | I2S_RCR2_BCD | I2S_RCR2_DIV(3);
  211. I2S0_RCR3 = I2S_RCR3_RCE;
  212. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  213. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  214. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  215. // configure pin mux for 3 clock signals
  216. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  217. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  218. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  219. }
  220. /******************************************************************/
  221. void AudioOutputI2Sslave::begin(void)
  222. {
  223. //pinMode(2, OUTPUT);
  224. block_left_1st = NULL;
  225. block_right_1st = NULL;
  226. AudioOutputI2Sslave::config_i2s();
  227. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  228. DMA_CR = 0;
  229. DMA_TCD0_SADDR = i2s_tx_buffer;
  230. DMA_TCD0_SOFF = 2;
  231. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  232. DMA_TCD0_NBYTES_MLNO = 2;
  233. DMA_TCD0_SLAST = -sizeof(i2s_tx_buffer);
  234. DMA_TCD0_DADDR = &I2S0_TDR0;
  235. DMA_TCD0_DOFF = 0;
  236. DMA_TCD0_CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  237. DMA_TCD0_DLASTSGA = 0;
  238. DMA_TCD0_BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  239. DMA_TCD0_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  240. DMAMUX0_CHCFG0 = DMAMUX_DISABLE;
  241. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_I2S0_TX | DMAMUX_ENABLE;
  242. update_responsibility = update_setup();
  243. DMA_SERQ = 0;
  244. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  245. NVIC_ENABLE_IRQ(IRQ_DMA_CH0);
  246. }
  247. void AudioOutputI2Sslave::config_i2s(void)
  248. {
  249. SIM_SCGC6 |= SIM_SCGC6_I2S;
  250. SIM_SCGC7 |= SIM_SCGC7_DMA;
  251. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  252. // if either transmitter or receiver is enabled, do nothing
  253. if (I2S0_TCSR & I2S_TCSR_TE) return;
  254. if (I2S0_RCSR & I2S_RCSR_RE) return;
  255. // Select input clock 0
  256. // Configure to input the bit-clock from pin, bypasses the MCLK divider
  257. I2S0_MCR = I2S_MCR_MICS(0);
  258. I2S0_MDR = 0;
  259. // configure transmitter
  260. I2S0_TMR = 0;
  261. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  262. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  263. I2S0_TCR3 = I2S_TCR3_TCE;
  264. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  265. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  266. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  267. // configure receiver (sync'd to transmitter clocks)
  268. I2S0_RMR = 0;
  269. I2S0_RCR1 = I2S_RCR1_RFW(1);
  270. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  271. I2S0_RCR3 = I2S_RCR3_RCE;
  272. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  273. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  274. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  275. // configure pin mux for 3 clock signals
  276. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  277. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  278. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  279. }