Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

209 lines
6.7KB

  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. #if !defined(__IMXRT1052__) && !defined(__IMXRT1062__)
  27. #include <Arduino.h>
  28. #include "input_i2s.h"
  29. #include "output_i2s.h"
  30. DMAMEM static uint32_t i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  31. audio_block_t * AudioInputI2S::block_left = NULL;
  32. audio_block_t * AudioInputI2S::block_right = NULL;
  33. uint16_t AudioInputI2S::block_offset = 0;
  34. bool AudioInputI2S::update_responsibility = false;
  35. DMAChannel AudioInputI2S::dma(false);
  36. void AudioInputI2S::begin(void)
  37. {
  38. dma.begin(true); // Allocate the DMA channel first
  39. //block_left_1st = NULL;
  40. //block_right_1st = NULL;
  41. // TODO: should we set & clear the I2S_RCSR_SR bit here?
  42. AudioOutputI2S::config_i2s();
  43. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  44. #if defined(KINETISK)
  45. dma.TCD->SADDR = (void *)((uint32_t)&I2S0_RDR0 + 2);
  46. dma.TCD->SOFF = 0;
  47. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  48. dma.TCD->NBYTES_MLNO = 2;
  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) / 2;
  53. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  54. dma.TCD->BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  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 AudioInputI2S::isr(void)
  65. {
  66. uint32_t daddr, offset;
  67. const int16_t *src, *end;
  68. int16_t *dest_left, *dest_right;
  69. audio_block_t *left, *right;
  70. //digitalWriteFast(3, HIGH);
  71. #if defined(KINETISK)
  72. daddr = (uint32_t)(dma.TCD->DADDR);
  73. #endif
  74. dma.clearInterrupt();
  75. if (daddr < (uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2) {
  76. // DMA is receiving to the first half of the buffer
  77. // need to remove data from the second half
  78. src = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  79. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  80. if (AudioInputI2S::update_responsibility) AudioStream::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. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  86. }
  87. left = AudioInputI2S::block_left;
  88. right = AudioInputI2S::block_right;
  89. if (left != NULL && right != NULL) {
  90. offset = AudioInputI2S::block_offset;
  91. if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  92. dest_left = &(left->data[offset]);
  93. dest_right = &(right->data[offset]);
  94. AudioInputI2S::block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  95. do {
  96. //n = *src++;
  97. //*dest_left++ = (int16_t)n;
  98. //*dest_right++ = (int16_t)(n >> 16);
  99. *dest_left++ = *src++;
  100. *dest_right++ = *src++;
  101. } while (src < end);
  102. }
  103. }
  104. //digitalWriteFast(3, LOW);
  105. }
  106. void AudioInputI2S::update(void)
  107. {
  108. audio_block_t *new_left=NULL, *new_right=NULL, *out_left=NULL, *out_right=NULL;
  109. // allocate 2 new blocks, but if one fails, allocate neither
  110. new_left = allocate();
  111. if (new_left != NULL) {
  112. new_right = allocate();
  113. if (new_right == NULL) {
  114. release(new_left);
  115. new_left = NULL;
  116. }
  117. }
  118. __disable_irq();
  119. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  120. // the DMA filled 2 blocks, so grab them and get the
  121. // 2 new blocks to the DMA, as quickly as possible
  122. out_left = block_left;
  123. block_left = new_left;
  124. out_right = block_right;
  125. block_right = new_right;
  126. block_offset = 0;
  127. __enable_irq();
  128. // then transmit the DMA's former blocks
  129. transmit(out_left, 0);
  130. release(out_left);
  131. transmit(out_right, 1);
  132. release(out_right);
  133. //Serial.print(".");
  134. } else if (new_left != NULL) {
  135. // the DMA didn't fill blocks, but we allocated blocks
  136. if (block_left == NULL) {
  137. // the DMA doesn't have any blocks to fill, so
  138. // give it the ones we just allocated
  139. block_left = new_left;
  140. block_right = new_right;
  141. block_offset = 0;
  142. __enable_irq();
  143. } else {
  144. // the DMA already has blocks, doesn't need these
  145. __enable_irq();
  146. release(new_left);
  147. release(new_right);
  148. }
  149. } else {
  150. // The DMA didn't fill blocks, and we could not allocate
  151. // memory... the system is likely starving for memory!
  152. // Sadly, there's nothing we can do.
  153. __enable_irq();
  154. }
  155. }
  156. /******************************************************************/
  157. void AudioInputI2Sslave::begin(void)
  158. {
  159. dma.begin(true); // Allocate the DMA channel first
  160. //block_left_1st = NULL;
  161. //block_right_1st = NULL;
  162. AudioOutputI2Sslave::config_i2s();
  163. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  164. #if defined(KINETISK)
  165. dma.TCD->SADDR = (void *)((uint32_t)&I2S0_RDR0 + 2);
  166. dma.TCD->SOFF = 0;
  167. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  168. dma.TCD->NBYTES_MLNO = 2;
  169. dma.TCD->SLAST = 0;
  170. dma.TCD->DADDR = i2s_rx_buffer;
  171. dma.TCD->DOFF = 2;
  172. dma.TCD->CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  173. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  174. dma.TCD->BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  175. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  176. #endif
  177. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  178. update_responsibility = update_setup();
  179. dma.enable();
  180. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  181. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX
  182. dma.attachInterrupt(isr);
  183. }
  184. #endif