您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

207 行
6.3KB

  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_adc.h"
  27. #include "utility/pdb.h"
  28. DMAMEM static uint16_t analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
  29. audio_block_t * AudioInputAnalog::block_left = NULL;
  30. uint16_t AudioInputAnalog::block_offset = 0;
  31. bool AudioInputAnalog::update_responsibility = false;
  32. DMAChannel AudioInputAnalog::dma(false);
  33. AudioInputAnalog::AudioInputAnalog() : AudioStream(0, NULL)
  34. {
  35. unsigned int pin = A2;
  36. uint32_t i, sum=0;
  37. // Configure the ADC and run at least one software-triggered
  38. // conversion. This completes the self calibration stuff and
  39. // leaves the ADC in a state that's mostly ready to use
  40. analogReadRes(16);
  41. analogReference(INTERNAL); // range 0 to 1.2 volts
  42. analogReadAveraging(8);
  43. // Actually, do many normal reads, to start with a nice DC level
  44. for (i=0; i < 1024; i++) {
  45. sum += analogRead(pin);
  46. }
  47. dc_average = sum >> 10;
  48. // set the programmable delay block to trigger the ADC at 44.1 kHz
  49. if (!(SIM_SCGC6 & SIM_SCGC6_PDB)
  50. || (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
  51. || PDB0_MOD != PDB_PERIOD
  52. || PDB0_IDLY != 1
  53. || PDB0_CH0C1 != 0x0101) {
  54. SIM_SCGC6 |= SIM_SCGC6_PDB;
  55. PDB0_IDLY = 1;
  56. PDB0_MOD = PDB_PERIOD;
  57. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  58. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  59. PDB0_CH0C1 = 0x0101;
  60. }
  61. // enable the ADC for hardware trigger and DMA
  62. ADC0_SC2 |= ADC_SC2_ADTRG | ADC_SC2_DMAEN;
  63. // set up a DMA channel to store the ADC data
  64. dma.begin(true);
  65. dma.TCD->SADDR = &ADC0_RA;
  66. dma.TCD->SOFF = 0;
  67. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  68. dma.TCD->NBYTES_MLNO = 2;
  69. dma.TCD->SLAST = 0;
  70. dma.TCD->DADDR = analog_rx_buffer;
  71. dma.TCD->DOFF = 2;
  72. dma.TCD->CITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
  73. dma.TCD->DLASTSGA = -sizeof(analog_rx_buffer);
  74. dma.TCD->BITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
  75. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  76. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_ADC0);
  77. update_responsibility = update_setup();
  78. dma.enable();
  79. dma.attachInterrupt(isr);
  80. }
  81. void AudioInputAnalog::isr(void)
  82. {
  83. uint32_t daddr, offset;
  84. const uint16_t *src, *end;
  85. uint16_t *dest_left;
  86. audio_block_t *left;
  87. digitalWriteFast(2, HIGH);
  88. daddr = (uint32_t)(dma.TCD->DADDR);
  89. dma.clearInterrupt();
  90. if (daddr < (uint32_t)analog_rx_buffer + sizeof(analog_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 = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  94. end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
  95. if (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 = (uint16_t *)&analog_rx_buffer[0];
  100. end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  101. }
  102. left = block_left;
  103. if (left != NULL) {
  104. offset = block_offset;
  105. if (offset > AUDIO_BLOCK_SAMPLES/2) offset = AUDIO_BLOCK_SAMPLES/2;
  106. //if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  107. dest_left = (uint16_t *)&(left->data[offset]);
  108. block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  109. do {
  110. *dest_left++ = *src++;
  111. } while (src < end);
  112. //}
  113. }
  114. digitalWriteFast(2, LOW);
  115. }
  116. #if 0
  117. void adc0_isr(void)
  118. {
  119. uint32_t tmp = ADC0_RA; // read ADC result to clear interrupt
  120. digitalWriteFast(3, HIGH);
  121. delayMicroseconds(1);
  122. digitalWriteFast(3, LOW);
  123. }
  124. #endif
  125. void AudioInputAnalog::update(void)
  126. {
  127. audio_block_t *new_left=NULL, *out_left=NULL;
  128. unsigned int dc, offset;
  129. int16_t s, *p, *end;
  130. //Serial.println("update");
  131. // allocate new block (ok if NULL)
  132. new_left = allocate();
  133. __disable_irq();
  134. offset = block_offset;
  135. if (offset < AUDIO_BLOCK_SAMPLES) {
  136. // the DMA didn't fill a block
  137. if (new_left != NULL) {
  138. // but we allocated a block
  139. if (block_left == NULL) {
  140. // the DMA doesn't have any blocks to fill, so
  141. // give it the one we just allocated
  142. block_left = new_left;
  143. block_offset = 0;
  144. __enable_irq();
  145. //Serial.println("fail1");
  146. } else {
  147. // the DMA already has blocks, doesn't need this
  148. __enable_irq();
  149. release(new_left);
  150. //Serial.print("fail2, offset=");
  151. //Serial.println(offset);
  152. }
  153. } else {
  154. // The DMA didn't fill a block, and we could not allocate
  155. // memory... the system is likely starving for memory!
  156. // Sadly, there's nothing we can do.
  157. __enable_irq();
  158. //Serial.println("fail3");
  159. }
  160. return;
  161. }
  162. // the DMA filled a block, so grab it and get the
  163. // new block to the DMA, as quickly as possible
  164. out_left = block_left;
  165. block_left = new_left;
  166. block_offset = 0;
  167. __enable_irq();
  168. // find and subtract DC offset....
  169. // TODO: this may not be correct, needs testing with more types of signals
  170. dc = dc_average;
  171. p = out_left->data;
  172. end = p + AUDIO_BLOCK_SAMPLES;
  173. do {
  174. s = (uint16_t)(*p) - dc; // TODO: should be saturating subtract
  175. *p++ = s;
  176. dc += s >> 13; // approx 5.38 Hz high pass filter
  177. } while (p < end);
  178. dc_average = dc;
  179. // then transmit the AC data
  180. transmit(out_left);
  181. release(out_left);
  182. }