You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // 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. DMA_CR = 0;
  45. DMA_TCD_SADDR(AUDIO_IN_I2S_DMA_CHANNEL) = &I2S0_RDR0;
  46. DMA_TCD_SOFF(AUDIO_IN_I2S_DMA_CHANNEL) = 0;
  47. DMA_TCD_ATTR(AUDIO_IN_I2S_DMA_CHANNEL) = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  48. DMA_TCD_NBYTES_MLNO(AUDIO_IN_I2S_DMA_CHANNEL) = 2;
  49. DMA_TCD_SLAST(AUDIO_IN_I2S_DMA_CHANNEL) = 0;
  50. DMA_TCD_DADDR(AUDIO_IN_I2S_DMA_CHANNEL) = i2s_rx_buffer;
  51. DMA_TCD_DOFF(AUDIO_IN_I2S_DMA_CHANNEL) = 2;
  52. DMA_TCD_CITER_ELINKNO(AUDIO_IN_I2S_DMA_CHANNEL) = sizeof(i2s_rx_buffer) / 2;
  53. DMA_TCD_DLASTSGA(AUDIO_IN_I2S_DMA_CHANNEL) = -sizeof(i2s_rx_buffer);
  54. DMA_TCD_BITER_ELINKNO(AUDIO_IN_I2S_DMA_CHANNEL) = sizeof(i2s_rx_buffer) / 2;
  55. DMA_TCD_CSR(AUDIO_IN_I2S_DMA_CHANNEL) = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  56. DMAMUX0_CHCFG(AUDIO_IN_I2S_DMA_CHANNEL) = DMAMUX_DISABLE;
  57. DMAMUX0_CHCFG(AUDIO_IN_I2S_DMA_CHANNEL) = DMAMUX_SOURCE_I2S0_RX | DMAMUX_ENABLE;
  58. update_responsibility = update_setup();
  59. DMA_SERQ = AUDIO_IN_I2S_DMA_CHANNEL;
  60. // TODO: is I2S_RCSR_BCE appropriate if sync'd to transmitter clock?
  61. //I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  62. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  63. NVIC_ENABLE_IRQ(IRQ_DMA_CH(AUDIO_IN_I2S_DMA_CHANNEL));
  64. }
  65. void DMA_ISR(AUDIO_IN_I2S_DMA_CHANNEL)(void)
  66. {
  67. uint32_t daddr, offset;
  68. const int16_t *src, *end;
  69. int16_t *dest_left, *dest_right;
  70. audio_block_t *left, *right;
  71. //digitalWriteFast(3, HIGH);
  72. daddr = (uint32_t)(DMA_TCD_DADDR(AUDIO_IN_I2S_DMA_CHANNEL));
  73. DMA_CINT = AUDIO_IN_I2S_DMA_CHANNEL;
  74. if (daddr < (uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2) {
  75. // DMA is receiving to the first half of the buffer
  76. // need to remove data from the second half
  77. src = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  78. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES];
  79. if (AudioInputI2S::update_responsibility) AudioStream::update_all();
  80. } else {
  81. // DMA is receiving to the second half of the buffer
  82. // need to remove data from the first half
  83. src = (int16_t *)&i2s_rx_buffer[0];
  84. end = (int16_t *)&i2s_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  85. }
  86. left = AudioInputI2S::block_left;
  87. right = AudioInputI2S::block_right;
  88. if (left != NULL && right != NULL) {
  89. offset = AudioInputI2S::block_offset;
  90. if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  91. dest_left = &(left->data[offset]);
  92. dest_right = &(right->data[offset]);
  93. AudioInputI2S::block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  94. do {
  95. //n = *src++;
  96. //*dest_left++ = (int16_t)n;
  97. //*dest_right++ = (int16_t)(n >> 16);
  98. *dest_left++ = *src++;
  99. *dest_right++ = *src++;
  100. } while (src < end);
  101. }
  102. }
  103. //digitalWriteFast(3, LOW);
  104. }
  105. void AudioInputI2S::update(void)
  106. {
  107. audio_block_t *new_left=NULL, *new_right=NULL, *out_left=NULL, *out_right=NULL;
  108. // allocate 2 new blocks, but if one fails, allocate neither
  109. new_left = allocate();
  110. if (new_left != NULL) {
  111. new_right = allocate();
  112. if (new_right == NULL) {
  113. release(new_left);
  114. new_left = NULL;
  115. }
  116. }
  117. __disable_irq();
  118. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  119. // the DMA filled 2 blocks, so grab them and get the
  120. // 2 new blocks to the DMA, as quickly as possible
  121. out_left = block_left;
  122. block_left = new_left;
  123. out_right = block_right;
  124. block_right = new_right;
  125. block_offset = 0;
  126. __enable_irq();
  127. // then transmit the DMA's former blocks
  128. transmit(out_left, 0);
  129. release(out_left);
  130. transmit(out_right, 1);
  131. release(out_right);
  132. //Serial.print(".");
  133. } else if (new_left != NULL) {
  134. // the DMA didn't fill blocks, but we allocated blocks
  135. if (block_left == NULL) {
  136. // the DMA doesn't have any blocks to fill, so
  137. // give it the ones we just allocated
  138. block_left = new_left;
  139. block_right = new_right;
  140. block_offset = 0;
  141. __enable_irq();
  142. } else {
  143. // the DMA already has blocks, doesn't need these
  144. __enable_irq();
  145. release(new_left);
  146. release(new_right);
  147. }
  148. } else {
  149. // The DMA didn't fill blocks, and we could not allocate
  150. // memory... the system is likely starving for memory!
  151. // Sadly, there's nothing we can do.
  152. __enable_irq();
  153. }
  154. }
  155. /******************************************************************/
  156. void AudioInputI2Sslave::begin(void)
  157. {
  158. //block_left_1st = NULL;
  159. //block_right_1st = NULL;
  160. //pinMode(3, OUTPUT);
  161. //digitalWriteFast(3, HIGH);
  162. //delayMicroseconds(500);
  163. //digitalWriteFast(3, LOW);
  164. AudioOutputI2Sslave::config_i2s();
  165. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  166. DMA_CR = 0;
  167. DMA_TCD_SADDR(AUDIO_IN_I2S_DMA_CHANNEL) = &I2S0_RDR0;
  168. DMA_TCD_SOFF(AUDIO_IN_I2S_DMA_CHANNEL) = 0;
  169. DMA_TCD_ATTR(AUDIO_IN_I2S_DMA_CHANNEL) = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  170. DMA_TCD_NBYTES_MLNO(AUDIO_IN_I2S_DMA_CHANNEL) = 2;
  171. DMA_TCD_SLAST(AUDIO_IN_I2S_DMA_CHANNEL) = 0;
  172. DMA_TCD_DADDR(AUDIO_IN_I2S_DMA_CHANNEL) = i2s_rx_buffer;
  173. DMA_TCD_DOFF(AUDIO_IN_I2S_DMA_CHANNEL) = 2;
  174. DMA_TCD_CITER_ELINKNO(AUDIO_IN_I2S_DMA_CHANNEL) = sizeof(i2s_rx_buffer) / 2;
  175. DMA_TCD_DLASTSGA(AUDIO_IN_I2S_DMA_CHANNEL) = -sizeof(i2s_rx_buffer);
  176. DMA_TCD_BITER_ELINKNO(AUDIO_IN_I2S_DMA_CHANNEL) = sizeof(i2s_rx_buffer) / 2;
  177. DMA_TCD_CSR(AUDIO_IN_I2S_DMA_CHANNEL) = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  178. DMAMUX0_CHCFG(AUDIO_IN_I2S_DMA_CHANNEL) = DMAMUX_DISABLE;
  179. DMAMUX0_CHCFG(AUDIO_IN_I2S_DMA_CHANNEL) = DMAMUX_SOURCE_I2S0_RX | DMAMUX_ENABLE;
  180. update_responsibility = update_setup();
  181. DMA_SERQ = AUDIO_IN_I2S_DMA_CHANNEL;
  182. // TODO: is I2S_RCSR_BCE appropriate if sync'd to transmitter clock?
  183. //I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  184. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  185. NVIC_ENABLE_IRQ(IRQ_DMA_CH(AUDIO_IN_I2S_DMA_CHANNEL));
  186. }