Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

input_i2s.cpp 11KB

10 anos atrás
4 anos atrás
4 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "input_i2s.h"
  27. #include "output_i2s.h"
  28. #if !defined(KINETISL)
  29. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  30. audio_block_t * AudioInputI2S::block_left = NULL;
  31. audio_block_t * AudioInputI2S::block_right = NULL;
  32. uint16_t AudioInputI2S::block_offset = 0;
  33. bool AudioInputI2S::update_responsibility = false;
  34. DMAChannel AudioInputI2S::dma(false);
  35. void AudioInputI2S::begin(void)
  36. {
  37. dma.begin(true); // Allocate the DMA channel first
  38. //block_left_1st = NULL;
  39. //block_right_1st = NULL;
  40. // TODO: should we set & clear the I2S_RCSR_SR bit here?
  41. AudioOutputI2S::config_i2s();
  42. #if defined(KINETISK)
  43. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  44. dma.TCD->SADDR = (void *)((uint32_t)&I2S0_RDR0 + 2);
  45. dma.TCD->SOFF = 0;
  46. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  47. dma.TCD->NBYTES_MLNO = 2;
  48. dma.TCD->SLAST = 0;
  49. dma.TCD->DADDR = i2s_rx_buffer;
  50. dma.TCD->DOFF = 2;
  51. dma.TCD->CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  52. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  53. dma.TCD->BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  54. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  55. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  56. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  57. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX
  58. #elif defined(__IMXRT1062__)
  59. CORE_PIN8_CONFIG = 3; //1:RX_DATA0
  60. IOMUXC_SAI1_RX_DATA0_SELECT_INPUT = 2;
  61. dma.TCD->SADDR = (void *)((uint32_t)&I2S1_RDR0 + 2);
  62. dma.TCD->SOFF = 0;
  63. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  64. dma.TCD->NBYTES_MLNO = 2;
  65. dma.TCD->SLAST = 0;
  66. dma.TCD->DADDR = i2s_rx_buffer;
  67. dma.TCD->DOFF = 2;
  68. dma.TCD->CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  69. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  70. dma.TCD->BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  71. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  72. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_RX);
  73. I2S1_RCSR = I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  74. #endif
  75. update_responsibility = update_setup();
  76. dma.enable();
  77. dma.attachInterrupt(isr);
  78. }
  79. void AudioInputI2S::isr(void)
  80. {
  81. uint32_t daddr, offset;
  82. const int16_t *src, *end;
  83. int16_t *dest_left, *dest_right;
  84. audio_block_t *left, *right;
  85. #if defined(KINETISK) || defined(__IMXRT1062__)
  86. daddr = (uint32_t)(dma.TCD->DADDR);
  87. dma.clearInterrupt();
  88. //Serial.println("isr");
  89. if (daddr < (uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2) {
  90. // DMA is receiving to the first half of the buffer
  91. // need to remove data from the second half
  92. src = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  93. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  94. if (AudioInputI2S::update_responsibility) AudioStream::update_all();
  95. } else {
  96. // DMA is receiving to the second half of the buffer
  97. // need to remove data from the first half
  98. src = (int16_t *)&i2s_rx_buffer[0];
  99. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  100. }
  101. left = AudioInputI2S::block_left;
  102. right = AudioInputI2S::block_right;
  103. if (left != NULL && right != NULL) {
  104. offset = AudioInputI2S::block_offset;
  105. if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  106. dest_left = &(left->data[offset]);
  107. dest_right = &(right->data[offset]);
  108. AudioInputI2S::block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  109. arm_dcache_delete((void*)src, sizeof(i2s_rx_buffer) / 2);
  110. do {
  111. *dest_left++ = *src++;
  112. *dest_right++ = *src++;
  113. } while (src < end);
  114. }
  115. }
  116. #endif
  117. }
  118. void AudioInputI2S::update(void)
  119. {
  120. audio_block_t *new_left=NULL, *new_right=NULL, *out_left=NULL, *out_right=NULL;
  121. // allocate 2 new blocks, but if one fails, allocate neither
  122. new_left = allocate();
  123. if (new_left != NULL) {
  124. new_right = allocate();
  125. if (new_right == NULL) {
  126. release(new_left);
  127. new_left = NULL;
  128. }
  129. }
  130. __disable_irq();
  131. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  132. // the DMA filled 2 blocks, so grab them and get the
  133. // 2 new blocks to the DMA, as quickly as possible
  134. out_left = block_left;
  135. block_left = new_left;
  136. out_right = block_right;
  137. block_right = new_right;
  138. block_offset = 0;
  139. __enable_irq();
  140. // then transmit the DMA's former blocks
  141. transmit(out_left, 0);
  142. release(out_left);
  143. transmit(out_right, 1);
  144. release(out_right);
  145. //Serial.print(".");
  146. } else if (new_left != NULL) {
  147. // the DMA didn't fill blocks, but we allocated blocks
  148. if (block_left == NULL) {
  149. // the DMA doesn't have any blocks to fill, so
  150. // give it the ones we just allocated
  151. block_left = new_left;
  152. block_right = new_right;
  153. block_offset = 0;
  154. __enable_irq();
  155. } else {
  156. // the DMA already has blocks, doesn't need these
  157. __enable_irq();
  158. release(new_left);
  159. release(new_right);
  160. }
  161. } else {
  162. // The DMA didn't fill blocks, and we could not allocate
  163. // memory... the system is likely starving for memory!
  164. // Sadly, there's nothing we can do.
  165. __enable_irq();
  166. }
  167. }
  168. /******************************************************************/
  169. void AudioInputI2Sslave::begin(void)
  170. {
  171. dma.begin(true); // Allocate the DMA channel first
  172. AudioOutputI2Sslave::config_i2s();
  173. #if defined(KINETISK)
  174. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  175. dma.TCD->SADDR = (void *)((uint32_t)&I2S0_RDR0 + 2);
  176. dma.TCD->SOFF = 0;
  177. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  178. dma.TCD->NBYTES_MLNO = 2;
  179. dma.TCD->SLAST = 0;
  180. dma.TCD->DADDR = i2s_rx_buffer;
  181. dma.TCD->DOFF = 2;
  182. dma.TCD->CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  183. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  184. dma.TCD->BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  185. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  186. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  187. update_responsibility = update_setup();
  188. dma.enable();
  189. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  190. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX
  191. dma.attachInterrupt(isr);
  192. #elif defined(__IMXRT1062__)
  193. CORE_PIN8_CONFIG = 3; //1:RX_DATA0
  194. IOMUXC_SAI1_RX_DATA0_SELECT_INPUT = 2;
  195. dma.TCD->SADDR = (void *)((uint32_t)&I2S1_RDR0 + 2);
  196. dma.TCD->SOFF = 0;
  197. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  198. dma.TCD->NBYTES_MLNO = 2;
  199. dma.TCD->SLAST = 0;
  200. dma.TCD->DADDR = i2s_rx_buffer;
  201. dma.TCD->DOFF = 2;
  202. dma.TCD->CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  203. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  204. dma.TCD->BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  205. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  206. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_RX);
  207. dma.enable();
  208. I2S1_RCSR = 0;
  209. I2S1_RCSR = I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  210. update_responsibility = update_setup();
  211. dma.attachInterrupt(isr);
  212. #endif
  213. }
  214. #elif defined(KINETISL)
  215. /**************************************************************************************
  216. * Teensy LC
  217. ***************************************************************************************/
  218. #define NUM_SAMPLES (AUDIO_BLOCK_SAMPLES / 2)
  219. DMAMEM static int16_t i2s_rx_buffer1[NUM_SAMPLES * 2];
  220. DMAMEM static int16_t i2s_rx_buffer2[NUM_SAMPLES * 2];
  221. audio_block_t * AudioInputI2S::block_left = NULL;
  222. audio_block_t * AudioInputI2S::block_right = NULL;
  223. DMAChannel AudioInputI2S::dma1(false);
  224. DMAChannel AudioInputI2S::dma2(false);
  225. bool AudioInputI2S::update_responsibility = false;
  226. void AudioInputI2S::begin(void)
  227. {
  228. // TODO
  229. }
  230. void AudioInputI2S::update(void)
  231. {
  232. //Keep it simple
  233. //If we have a block, transmit and release it.
  234. if (block_left) {
  235. transmit(block_left, 0);
  236. release(block_left);
  237. block_left = nullptr;
  238. }
  239. if (block_right) {
  240. transmit(block_right, 1);
  241. release(block_right);
  242. block_right = nullptr;
  243. }
  244. // allocate 2 new blocks, but if one fails, allocate neither
  245. block_left = allocate();
  246. if (block_left != nullptr) {
  247. block_right = allocate();
  248. if (block_right == nullptr) {
  249. release(block_left);
  250. block_left = nullptr;
  251. }
  252. }
  253. }
  254. //todo : ("unroll-loops") or optimize better
  255. inline __attribute__((always_inline, hot, optimize("O2") ))
  256. static void deinterleave(const int16_t *src,audio_block_t *block_left, audio_block_t *block_right, const unsigned offset)
  257. {
  258. //we can assume that we have either two blocks or none
  259. if (!block_left) return;
  260. for (unsigned i=0; i < NUM_SAMPLES; i++) {
  261. block_left->data[i + offset] = src[i*2];
  262. block_right->data[i + offset] = src[i*2+1];
  263. }
  264. }
  265. void AudioInputI2S::isr1(void)
  266. {
  267. //DMA Channel 1 Interrupt
  268. //Start Channel 2:
  269. dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  270. dma2.enable();
  271. //Reset & Copy Data Channel 1
  272. dma1.clearInterrupt();
  273. dma1.destinationBuffer(i2s_rx_buffer1, sizeof(i2s_rx_buffer1));
  274. deinterleave(&i2s_rx_buffer1[0], AudioInputI2S::block_left, AudioInputI2S::block_right, 0);
  275. //Serial.print(".");
  276. }
  277. void AudioInputI2S::isr2(void)
  278. {
  279. //DMA Channel 1 Interrupt
  280. //Start Channel 2:
  281. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  282. dma1.enable();
  283. //Reset & Copy Data Channel 1
  284. dma2.clearInterrupt();
  285. dma2.destinationBuffer(i2s_rx_buffer2, sizeof(i2s_rx_buffer2));
  286. deinterleave(&i2s_rx_buffer2[0], AudioInputI2S::block_left, AudioInputI2S::block_right, NUM_SAMPLES);
  287. if (AudioInputI2S::update_responsibility) AudioStream::update_all();
  288. //Serial.print("!");
  289. }
  290. void AudioInputI2Sslave::begin(void)
  291. {
  292. memset(i2s_rx_buffer1, 0, sizeof( i2s_rx_buffer1 ) );
  293. memset(i2s_rx_buffer2, 0, sizeof( i2s_rx_buffer2 ) );
  294. dma1.begin(true);
  295. dma2.begin(true);
  296. AudioOutputI2Sslave::config_i2s();
  297. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  298. //configure both DMA channels
  299. dma1.CFG->SAR = (void *)((uint32_t)&I2S0_RDR0 + 2);
  300. dma1.CFG->DCR = (dma1.CFG->DCR & 0xF08E0FFF) | DMA_DCR_SSIZE(2);
  301. dma1.destinationBuffer(i2s_rx_buffer1, sizeof(i2s_rx_buffer1));
  302. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  303. dma1.interruptAtCompletion();
  304. dma1.disableOnCompletion();
  305. dma1.attachInterrupt(isr1);
  306. dma2.CFG->SAR = dma1.CFG->SAR;
  307. dma2.CFG->DCR = dma1.CFG->DCR;
  308. dma2.destinationBuffer(i2s_rx_buffer2, sizeof(i2s_rx_buffer2));
  309. dma2.interruptAtCompletion();
  310. dma2.disableOnCompletion();
  311. dma2.attachInterrupt(isr2);
  312. I2S0_RCSR = 0;
  313. I2S0_RCSR = I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FWDE /*| I2S_RCSR_FR*/;
  314. update_responsibility = update_setup();
  315. dma1.enable();
  316. }
  317. #endif