Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

217 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. #include "input_i2s.h"
  27. #include "output_i2s.h"
  28. DMAMEM static uint32_t i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  29. audio_block_t * AudioInputI2S::block_left = NULL;
  30. audio_block_t * AudioInputI2S::block_right = NULL;
  31. uint16_t AudioInputI2S::block_offset = 0;
  32. bool AudioInputI2S::update_responsibility = false;
  33. void AudioInputI2S::begin(void)
  34. {
  35. //block_left_1st = NULL;
  36. //block_right_1st = NULL;
  37. //pinMode(3, OUTPUT);
  38. //digitalWriteFast(3, HIGH);
  39. //delayMicroseconds(500);
  40. //digitalWriteFast(3, LOW);
  41. AudioOutputI2S::config_i2s();
  42. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  43. DMA_CR = 0;
  44. DMA_TCD1_SADDR = &I2S0_RDR0;
  45. DMA_TCD1_SOFF = 0;
  46. DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  47. DMA_TCD1_NBYTES_MLNO = 2;
  48. DMA_TCD1_SLAST = 0;
  49. DMA_TCD1_DADDR = i2s_rx_buffer;
  50. DMA_TCD1_DOFF = 2;
  51. DMA_TCD1_CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  52. DMA_TCD1_DLASTSGA = -sizeof(i2s_rx_buffer);
  53. DMA_TCD1_BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  54. DMA_TCD1_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  55. DMAMUX0_CHCFG1 = DMAMUX_DISABLE;
  56. DMAMUX0_CHCFG1 = DMAMUX_SOURCE_I2S0_RX | DMAMUX_ENABLE;
  57. update_responsibility = update_setup();
  58. DMA_SERQ = 1;
  59. // TODO: is I2S_RCSR_BCE appropriate if sync'd to transmitter clock?
  60. //I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  61. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  62. NVIC_ENABLE_IRQ(IRQ_DMA_CH1);
  63. }
  64. void dma_ch1_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. daddr = (uint32_t)DMA_TCD1_DADDR;
  72. DMA_CINT = 1;
  73. if (daddr < (uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2) {
  74. // DMA is receiving to the first half of the buffer
  75. // need to remove data from the second half
  76. src = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  77. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  78. if (AudioInputI2S::update_responsibility) AudioStream::update_all();
  79. } else {
  80. // DMA is receiving to the second half of the buffer
  81. // need to remove data from the first half
  82. src = (int16_t *)&i2s_rx_buffer[0];
  83. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  84. }
  85. left = AudioInputI2S::block_left;
  86. right = AudioInputI2S::block_right;
  87. if (left != NULL && right != NULL) {
  88. offset = AudioInputI2S::block_offset;
  89. if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  90. dest_left = &(left->data[offset]);
  91. dest_right = &(right->data[offset]);
  92. AudioInputI2S::block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  93. do {
  94. //n = *src++;
  95. //*dest_left++ = (int16_t)n;
  96. //*dest_right++ = (int16_t)(n >> 16);
  97. *dest_left++ = *src++;
  98. *dest_right++ = *src++;
  99. } while (src < end);
  100. }
  101. }
  102. //digitalWriteFast(3, LOW);
  103. }
  104. void AudioInputI2S::update(void)
  105. {
  106. audio_block_t *new_left=NULL, *new_right=NULL, *out_left=NULL, *out_right=NULL;
  107. // allocate 2 new blocks, but if one fails, allocate neither
  108. new_left = allocate();
  109. if (new_left != NULL) {
  110. new_right = allocate();
  111. if (new_right == NULL) {
  112. release(new_left);
  113. new_left = NULL;
  114. }
  115. }
  116. __disable_irq();
  117. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  118. // the DMA filled 2 blocks, so grab them and get the
  119. // 2 new blocks to the DMA, as quickly as possible
  120. out_left = block_left;
  121. block_left = new_left;
  122. out_right = block_right;
  123. block_right = new_right;
  124. block_offset = 0;
  125. __enable_irq();
  126. // then transmit the DMA's former blocks
  127. transmit(out_left, 0);
  128. release(out_left);
  129. transmit(out_right, 1);
  130. release(out_right);
  131. //Serial.print(".");
  132. } else if (new_left != NULL) {
  133. // the DMA didn't fill blocks, but we allocated blocks
  134. if (block_left == NULL) {
  135. // the DMA doesn't have any blocks to fill, so
  136. // give it the ones we just allocated
  137. block_left = new_left;
  138. block_right = new_right;
  139. block_offset = 0;
  140. __enable_irq();
  141. } else {
  142. // the DMA already has blocks, doesn't need these
  143. __enable_irq();
  144. release(new_left);
  145. release(new_right);
  146. }
  147. } else {
  148. // The DMA didn't fill blocks, and we could not allocate
  149. // memory... the system is likely starving for memory!
  150. // Sadly, there's nothing we can do.
  151. __enable_irq();
  152. }
  153. }
  154. /******************************************************************/
  155. void AudioInputI2Sslave::begin(void)
  156. {
  157. //block_left_1st = NULL;
  158. //block_right_1st = NULL;
  159. //pinMode(3, OUTPUT);
  160. //digitalWriteFast(3, HIGH);
  161. //delayMicroseconds(500);
  162. //digitalWriteFast(3, LOW);
  163. AudioOutputI2Sslave::config_i2s();
  164. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  165. DMA_CR = 0;
  166. DMA_TCD1_SADDR = &I2S0_RDR0;
  167. DMA_TCD1_SOFF = 0;
  168. DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  169. DMA_TCD1_NBYTES_MLNO = 2;
  170. DMA_TCD1_SLAST = 0;
  171. DMA_TCD1_DADDR = i2s_rx_buffer;
  172. DMA_TCD1_DOFF = 2;
  173. DMA_TCD1_CITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  174. DMA_TCD1_DLASTSGA = -sizeof(i2s_rx_buffer);
  175. DMA_TCD1_BITER_ELINKNO = sizeof(i2s_rx_buffer) / 2;
  176. DMA_TCD1_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  177. DMAMUX0_CHCFG1 = DMAMUX_DISABLE;
  178. DMAMUX0_CHCFG1 = DMAMUX_SOURCE_I2S0_RX | DMAMUX_ENABLE;
  179. update_responsibility = update_setup();
  180. DMA_SERQ = 1;
  181. // TODO: is I2S_RCSR_BCE appropriate if sync'd to transmitter clock?
  182. //I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  183. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  184. NVIC_ENABLE_IRQ(IRQ_DMA_CH1);
  185. }