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.

output_i2s.cpp 12KB

il y a 10 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 == 180000000
  249. #define MCLK_MULT 16
  250. #define MCLK_DIV 255
  251. #elif F_CPU == 192000000
  252. #define MCLK_MULT 1
  253. #define MCLK_DIV 17
  254. #elif F_CPU == 16000000
  255. #define MCLK_MULT 12
  256. #define MCLK_DIV 17
  257. #else
  258. #error "This CPU Clock Speed is not supported by the Audio library";
  259. #endif
  260. #if F_CPU >= 20000000
  261. #define MCLK_SRC 3 // the PLL
  262. #else
  263. #define MCLK_SRC 0 // system clock
  264. #endif
  265. void AudioOutputI2S::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. // enable MCLK output
  274. I2S0_MCR = I2S_MCR_MICS(MCLK_SRC) | I2S_MCR_MOE;
  275. while (I2S0_MCR & I2S_MCR_DUF) ;
  276. I2S0_MDR = I2S_MDR_FRACT((MCLK_MULT-1)) | I2S_MDR_DIVIDE((MCLK_DIV-1));
  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 | I2S_TCR2_MSEL(1)
  281. | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  282. I2S0_TCR3 = I2S_TCR3_TCE;
  283. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  284. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  285. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  286. // configure receiver (sync'd to transmitter clocks)
  287. I2S0_RMR = 0;
  288. I2S0_RCR1 = I2S_RCR1_RFW(1);
  289. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  290. | I2S_RCR2_BCD | I2S_RCR2_DIV(3);
  291. I2S0_RCR3 = I2S_RCR3_RCE;
  292. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  293. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  294. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  295. // configure pin mux for 3 clock signals
  296. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  297. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  298. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  299. }
  300. /******************************************************************/
  301. void AudioOutputI2Sslave::begin(void)
  302. {
  303. dma.begin(true); // Allocate the DMA channel first
  304. //pinMode(2, OUTPUT);
  305. block_left_1st = NULL;
  306. block_right_1st = NULL;
  307. AudioOutputI2Sslave::config_i2s();
  308. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  309. #if defined(KINETISK)
  310. dma.TCD->SADDR = i2s_tx_buffer;
  311. dma.TCD->SOFF = 2;
  312. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  313. dma.TCD->NBYTES_MLNO = 2;
  314. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  315. dma.TCD->DADDR = &I2S0_TDR0;
  316. dma.TCD->DOFF = 0;
  317. dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  318. dma.TCD->DLASTSGA = 0;
  319. dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  320. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  321. #endif
  322. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_TX);
  323. update_responsibility = update_setup();
  324. dma.enable();
  325. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  326. dma.attachInterrupt(isr);
  327. }
  328. void AudioOutputI2Sslave::config_i2s(void)
  329. {
  330. SIM_SCGC6 |= SIM_SCGC6_I2S;
  331. SIM_SCGC7 |= SIM_SCGC7_DMA;
  332. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  333. // if either transmitter or receiver is enabled, do nothing
  334. if (I2S0_TCSR & I2S_TCSR_TE) return;
  335. if (I2S0_RCSR & I2S_RCSR_RE) return;
  336. // Select input clock 0
  337. // Configure to input the bit-clock from pin, bypasses the MCLK divider
  338. I2S0_MCR = I2S_MCR_MICS(0);
  339. I2S0_MDR = 0;
  340. // configure transmitter
  341. I2S0_TMR = 0;
  342. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  343. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  344. I2S0_TCR3 = I2S_TCR3_TCE;
  345. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  346. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  347. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  348. // configure receiver (sync'd to transmitter clocks)
  349. I2S0_RMR = 0;
  350. I2S0_RCR1 = I2S_RCR1_RFW(1);
  351. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  352. I2S0_RCR3 = I2S_RCR3_RCE;
  353. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  354. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  355. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  356. // configure pin mux for 3 clock signals
  357. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  358. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  359. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  360. }