Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

input_adc.cpp 6.4KB

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