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

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