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.

236 linhas
7.4KB

  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.h"
  28. #include "output_i2s.h"
  29. 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(__IMXRT1052__) || defined(__IMXRT1062__)
  59. CORE_PIN7_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;
  74. I2S1_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE;
  75. #endif
  76. update_responsibility = update_setup();
  77. dma.enable();
  78. dma.attachInterrupt(isr);
  79. //pinMode(13, OUTPUT);
  80. }
  81. void AudioInputI2S::isr(void)
  82. {
  83. uint32_t daddr, offset;
  84. const int16_t *src, *end;
  85. int16_t *dest_left, *dest_right;
  86. audio_block_t *left, *right;
  87. //digitalWriteFast(13, HIGH);
  88. #if defined(KINETISK) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
  89. daddr = (uint32_t)(dma.TCD->DADDR);
  90. #endif
  91. dma.clearInterrupt();
  92. if (daddr < (uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2) {
  93. // DMA is receiving to the first half of the buffer
  94. // need to remove data from the second half
  95. src = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  96. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  97. if (AudioInputI2S::update_responsibility) AudioStream::update_all();
  98. } else {
  99. // DMA is receiving to the second half of the buffer
  100. // need to remove data from the first half
  101. src = (int16_t *)&i2s_rx_buffer[0];
  102. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  103. }
  104. left = AudioInputI2S::block_left;
  105. right = AudioInputI2S::block_right;
  106. if (left != NULL && right != NULL) {
  107. offset = AudioInputI2S::block_offset;
  108. if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  109. dest_left = &(left->data[offset]);
  110. dest_right = &(right->data[offset]);
  111. AudioInputI2S::block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  112. do {
  113. //Serial.println(*src);
  114. //n = *src++;
  115. //*dest_left++ = (int16_t)n;
  116. //*dest_right++ = (int16_t)(n >> 16);
  117. *dest_left++ = *src++;
  118. *dest_right++ = *src++;
  119. } while (src < end);
  120. }
  121. }
  122. //digitalWriteFast(13, LOW);
  123. }
  124. void AudioInputI2S::update(void)
  125. {
  126. audio_block_t *new_left=NULL, *new_right=NULL, *out_left=NULL, *out_right=NULL;
  127. // allocate 2 new blocks, but if one fails, allocate neither
  128. new_left = allocate();
  129. if (new_left != NULL) {
  130. new_right = allocate();
  131. if (new_right == NULL) {
  132. release(new_left);
  133. new_left = NULL;
  134. }
  135. }
  136. __disable_irq();
  137. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  138. // the DMA filled 2 blocks, so grab them and get the
  139. // 2 new blocks to the DMA, as quickly as possible
  140. out_left = block_left;
  141. block_left = new_left;
  142. out_right = block_right;
  143. block_right = new_right;
  144. block_offset = 0;
  145. __enable_irq();
  146. // then transmit the DMA's former blocks
  147. transmit(out_left, 0);
  148. release(out_left);
  149. transmit(out_right, 1);
  150. release(out_right);
  151. //Serial.print(".");
  152. } else if (new_left != NULL) {
  153. // the DMA didn't fill blocks, but we allocated blocks
  154. if (block_left == NULL) {
  155. // the DMA doesn't have any blocks to fill, so
  156. // give it the ones we just allocated
  157. block_left = new_left;
  158. block_right = new_right;
  159. block_offset = 0;
  160. __enable_irq();
  161. } else {
  162. // the DMA already has blocks, doesn't need these
  163. __enable_irq();
  164. release(new_left);
  165. release(new_right);
  166. }
  167. } else {
  168. // The DMA didn't fill blocks, and we could not allocate
  169. // memory... the system is likely starving for memory!
  170. // Sadly, there's nothing we can do.
  171. __enable_irq();
  172. }
  173. }
  174. /******************************************************************/
  175. void AudioInputI2Sslave::begin(void)
  176. {
  177. dma.begin(true); // Allocate the DMA channel first
  178. //block_left_1st = NULL;
  179. //block_right_1st = NULL;
  180. AudioOutputI2Sslave::config_i2s();
  181. #if defined(KINETISK)
  182. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  183. dma.TCD->SADDR = (void *)((uint32_t)&I2S0_RDR0 + 2);
  184. dma.TCD->SOFF = 0;
  185. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  186. dma.TCD->NBYTES_MLNO = 2;
  187. dma.TCD->SLAST = 0;
  188. dma.TCD->DADDR = i2s_rx_buffer;
  189. dma.TCD->DOFF = 2;
  190. dma.TCD->CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  191. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  192. dma.TCD->BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  193. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  194. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  195. update_responsibility = update_setup();
  196. dma.enable();
  197. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  198. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX
  199. dma.attachInterrupt(isr);
  200. #endif
  201. }