You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.9KB

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