Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

output_i2s.cpp 13KB

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