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.

218 Zeilen
6.8KB

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