您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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