Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

195 Zeilen
6.0KB

  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. #include "utility/dspinst.h"
  29. DMAMEM static uint16_t analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
  30. audio_block_t * AudioInputAnalog::block_left = NULL;
  31. uint16_t AudioInputAnalog::block_offset = 0;
  32. bool AudioInputAnalog::update_responsibility = false;
  33. DMAChannel AudioInputAnalog::dma(false);
  34. AudioInputAnalog::AudioInputAnalog(uint8_t pin) : AudioStream(0, NULL)
  35. {
  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. daddr = (uint32_t)(dma.TCD->DADDR);
  88. dma.clearInterrupt();
  89. if (daddr < (uint32_t)analog_rx_buffer + sizeof(analog_rx_buffer) / 2) {
  90. // DMA is receiving to the first half of the buffer
  91. // need to remove data from the second half
  92. src = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  93. end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
  94. if (update_responsibility) AudioStream::update_all();
  95. } else {
  96. // DMA is receiving to the second half of the buffer
  97. // need to remove data from the first half
  98. src = (uint16_t *)&analog_rx_buffer[0];
  99. end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  100. }
  101. left = block_left;
  102. if (left != NULL) {
  103. offset = block_offset;
  104. if (offset > AUDIO_BLOCK_SAMPLES/2) offset = AUDIO_BLOCK_SAMPLES/2;
  105. dest_left = (uint16_t *)&(left->data[offset]);
  106. block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  107. do {
  108. *dest_left++ = *src++;
  109. } while (src < end);
  110. }
  111. }
  112. void AudioInputAnalog::update(void)
  113. {
  114. audio_block_t *new_left=NULL, *out_left=NULL;
  115. unsigned int dc, offset;
  116. int32_t tmp;
  117. int16_t s, *p, *end;
  118. //Serial.println("update");
  119. // allocate new block (ok if NULL)
  120. new_left = allocate();
  121. __disable_irq();
  122. offset = block_offset;
  123. if (offset < AUDIO_BLOCK_SAMPLES) {
  124. // the DMA didn't fill a block
  125. if (new_left != NULL) {
  126. // but we allocated a block
  127. if (block_left == NULL) {
  128. // the DMA doesn't have any blocks to fill, so
  129. // give it the one we just allocated
  130. block_left = new_left;
  131. block_offset = 0;
  132. __enable_irq();
  133. //Serial.println("fail1");
  134. } else {
  135. // the DMA already has blocks, doesn't need this
  136. __enable_irq();
  137. release(new_left);
  138. //Serial.print("fail2, offset=");
  139. //Serial.println(offset);
  140. }
  141. } else {
  142. // The DMA didn't fill a block, and we could not allocate
  143. // memory... the system is likely starving for memory!
  144. // Sadly, there's nothing we can do.
  145. __enable_irq();
  146. //Serial.println("fail3");
  147. }
  148. return;
  149. }
  150. // the DMA filled a block, so grab it and get the
  151. // new block to the DMA, as quickly as possible
  152. out_left = block_left;
  153. block_left = new_left;
  154. block_offset = 0;
  155. __enable_irq();
  156. // find and subtract DC offset....
  157. // TODO: this may not be correct, needs testing with more types of signals
  158. dc = dc_average;
  159. p = out_left->data;
  160. end = p + AUDIO_BLOCK_SAMPLES;
  161. do {
  162. tmp = (uint16_t)(*p) - (int32_t)dc;
  163. s = signed_saturate_rshift(tmp, 16, 0);
  164. *p++ = s;
  165. dc += s / 12000; // slow response, remove DC component
  166. } while (p < end);
  167. dc_average = dc;
  168. // then transmit the AC data
  169. transmit(out_left);
  170. release(out_left);
  171. }