Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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