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.

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