Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

output_i2s.cpp 10KB

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