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.

input_adc.cpp 6.1KB

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