Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

206 lines
6.0KB

  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_quad.h"
  27. #include "output_i2s_quad.h"
  28. DMAMEM static uint32_t i2s_rx_buffer[AUDIO_BLOCK_SAMPLES*2];
  29. audio_block_t * AudioInputI2SQuad::block_ch1 = NULL;
  30. audio_block_t * AudioInputI2SQuad::block_ch2 = NULL;
  31. audio_block_t * AudioInputI2SQuad::block_ch3 = NULL;
  32. audio_block_t * AudioInputI2SQuad::block_ch4 = NULL;
  33. uint16_t AudioInputI2SQuad::block_offset = 0;
  34. bool AudioInputI2SQuad::update_responsibility = false;
  35. DMAChannel AudioInputI2SQuad::dma(false);
  36. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  37. void AudioInputI2SQuad::begin(void)
  38. {
  39. dma.begin(true); // Allocate the DMA channel first
  40. // TODO: should we set & clear the I2S_RCSR_SR bit here?
  41. AudioOutputI2SQuad::config_i2s();
  42. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  43. #if defined(__MK20DX256__)
  44. CORE_PIN30_CONFIG = PORT_PCR_MUX(4); // pin 30, PTC11, I2S0_RXD1
  45. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  46. CORE_PIN38_CONFIG = PORT_PCR_MUX(4); // pin 38, PTC11, I2S0_RXD1
  47. #endif
  48. #if defined(KINETISK)
  49. dma.TCD->SADDR = &I2S0_RDR0;
  50. dma.TCD->SOFF = 4;
  51. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_SMOD(3) | DMA_TCD_ATTR_DSIZE(1);
  52. dma.TCD->NBYTES_MLNO = 4;
  53. dma.TCD->SLAST = 0;
  54. dma.TCD->DADDR = i2s_rx_buffer;
  55. dma.TCD->DOFF = 2;
  56. dma.TCD->CITER_ELINKNO = sizeof(i2s_rx_buffer) / 4;
  57. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  58. dma.TCD->BITER_ELINKNO = sizeof(i2s_rx_buffer) / 4;
  59. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  60. #endif
  61. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  62. update_responsibility = update_setup();
  63. dma.enable();
  64. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  65. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX
  66. dma.attachInterrupt(isr);
  67. }
  68. void AudioInputI2SQuad::isr(void)
  69. {
  70. uint32_t daddr, offset;
  71. const int16_t *src;
  72. int16_t *dest1, *dest2, *dest3, *dest4;
  73. //digitalWriteFast(3, HIGH);
  74. daddr = (uint32_t)(dma.TCD->DADDR);
  75. dma.clearInterrupt();
  76. if (daddr < (uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2) {
  77. // DMA is receiving to the first half of the buffer
  78. // need to remove data from the second half
  79. src = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  80. if (update_responsibility) update_all();
  81. } else {
  82. // DMA is receiving to the second half of the buffer
  83. // need to remove data from the first half
  84. src = (int16_t *)&i2s_rx_buffer[0];
  85. }
  86. if (block_ch1) {
  87. offset = block_offset;
  88. if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  89. block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  90. dest1 = &(block_ch1->data[offset]);
  91. dest2 = &(block_ch2->data[offset]);
  92. dest3 = &(block_ch3->data[offset]);
  93. dest4 = &(block_ch4->data[offset]);
  94. for (int i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  95. *dest1++ = *src++;
  96. *dest3++ = *src++;
  97. *dest2++ = *src++;
  98. *dest4++ = *src++;
  99. }
  100. }
  101. }
  102. //digitalWriteFast(3, LOW);
  103. }
  104. void AudioInputI2SQuad::update(void)
  105. {
  106. audio_block_t *new1, *new2, *new3, *new4;
  107. audio_block_t *out1, *out2, *out3, *out4;
  108. // allocate 4 new blocks
  109. new1 = allocate();
  110. new2 = allocate();
  111. new3 = allocate();
  112. new4 = allocate();
  113. // but if any fails, allocate none
  114. if (!new1 || !new2 || !new3 || !new4) {
  115. if (new1) {
  116. release(new1);
  117. new1 = NULL;
  118. }
  119. if (new2) {
  120. release(new2);
  121. new2 = NULL;
  122. }
  123. if (new3) {
  124. release(new3);
  125. new3 = NULL;
  126. }
  127. if (new4) {
  128. release(new4);
  129. new4 = NULL;
  130. }
  131. }
  132. __disable_irq();
  133. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  134. // the DMA filled 4 blocks, so grab them and get the
  135. // 4 new blocks to the DMA, as quickly as possible
  136. out1 = block_ch1;
  137. block_ch1 = new1;
  138. out2 = block_ch2;
  139. block_ch2 = new2;
  140. out3 = block_ch3;
  141. block_ch3 = new3;
  142. out4 = block_ch4;
  143. block_ch4 = new4;
  144. block_offset = 0;
  145. __enable_irq();
  146. // then transmit the DMA's former blocks
  147. transmit(out1, 0);
  148. release(out1);
  149. transmit(out2, 1);
  150. release(out2);
  151. transmit(out3, 2);
  152. release(out3);
  153. transmit(out4, 3);
  154. release(out4);
  155. } else if (new1 != NULL) {
  156. // the DMA didn't fill blocks, but we allocated blocks
  157. if (block_ch1 == NULL) {
  158. // the DMA doesn't have any blocks to fill, so
  159. // give it the ones we just allocated
  160. block_ch1 = new1;
  161. block_ch2 = new2;
  162. block_ch3 = new3;
  163. block_ch4 = new4;
  164. block_offset = 0;
  165. __enable_irq();
  166. } else {
  167. // the DMA already has blocks, doesn't need these
  168. __enable_irq();
  169. release(new1);
  170. release(new2);
  171. release(new3);
  172. release(new4);
  173. }
  174. } else {
  175. // The DMA didn't fill blocks, and we could not allocate
  176. // memory... the system is likely starving for memory!
  177. // Sadly, there's nothing we can do.
  178. __enable_irq();
  179. }
  180. }
  181. #else // not __MK20DX256__
  182. void AudioInputI2SQuad::begin(void)
  183. {
  184. }
  185. #endif