選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

196 行
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. 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 (!(SIM_SCGC6 & SIM_SCGC6_PDB)
  51. || (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
  52. || PDB0_MOD != PDB_PERIOD
  53. || PDB0_IDLY != 1
  54. || PDB0_CH0C1 != 0x0101) {
  55. SIM_SCGC6 |= SIM_SCGC6_PDB;
  56. PDB0_IDLY = 1;
  57. PDB0_MOD = PDB_PERIOD;
  58. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  59. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  60. PDB0_CH0C1 = 0x0101;
  61. }
  62. // enable the ADC for hardware trigger and DMA
  63. ADC0_SC2 |= ADC_SC2_ADTRG | ADC_SC2_DMAEN;
  64. // set up a DMA channel to store the ADC data
  65. dma.begin(true);
  66. dma.TCD->SADDR = &ADC0_RA;
  67. dma.TCD->SOFF = 0;
  68. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  69. dma.TCD->NBYTES_MLNO = 2;
  70. dma.TCD->SLAST = 0;
  71. dma.TCD->DADDR = analog_rx_buffer;
  72. dma.TCD->DOFF = 2;
  73. dma.TCD->CITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
  74. dma.TCD->DLASTSGA = -sizeof(analog_rx_buffer);
  75. dma.TCD->BITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
  76. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  77. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_ADC0);
  78. update_responsibility = update_setup();
  79. dma.enable();
  80. dma.attachInterrupt(isr);
  81. }
  82. void AudioInputAnalog::isr(void)
  83. {
  84. uint32_t daddr, offset;
  85. const uint16_t *src, *end;
  86. uint16_t *dest_left;
  87. audio_block_t *left;
  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. dest_left = (uint16_t *)&(left->data[offset]);
  107. block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  108. do {
  109. *dest_left++ = *src++;
  110. } while (src < end);
  111. }
  112. }
  113. void AudioInputAnalog::update(void)
  114. {
  115. audio_block_t *new_left=NULL, *out_left=NULL;
  116. unsigned int dc, offset;
  117. int32_t tmp;
  118. int16_t s, *p, *end;
  119. //Serial.println("update");
  120. // allocate new block (ok if NULL)
  121. new_left = allocate();
  122. __disable_irq();
  123. offset = block_offset;
  124. if (offset < AUDIO_BLOCK_SAMPLES) {
  125. // the DMA didn't fill a block
  126. if (new_left != NULL) {
  127. // but we allocated a block
  128. if (block_left == NULL) {
  129. // the DMA doesn't have any blocks to fill, so
  130. // give it the one we just allocated
  131. block_left = new_left;
  132. block_offset = 0;
  133. __enable_irq();
  134. //Serial.println("fail1");
  135. } else {
  136. // the DMA already has blocks, doesn't need this
  137. __enable_irq();
  138. release(new_left);
  139. //Serial.print("fail2, offset=");
  140. //Serial.println(offset);
  141. }
  142. } else {
  143. // The DMA didn't fill a block, and we could not allocate
  144. // memory... the system is likely starving for memory!
  145. // Sadly, there's nothing we can do.
  146. __enable_irq();
  147. //Serial.println("fail3");
  148. }
  149. return;
  150. }
  151. // the DMA filled a block, so grab it and get the
  152. // new block to the DMA, as quickly as possible
  153. out_left = block_left;
  154. block_left = new_left;
  155. block_offset = 0;
  156. __enable_irq();
  157. // find and subtract DC offset....
  158. // TODO: this may not be correct, needs testing with more types of signals
  159. dc = dc_average;
  160. p = out_left->data;
  161. end = p + AUDIO_BLOCK_SAMPLES;
  162. do {
  163. tmp = (uint16_t)(*p) - (int32_t)dc;
  164. s = signed_saturate_rshift(tmp, 16, 0);
  165. *p++ = s;
  166. dc += s / 12000; // slow response, remove DC component
  167. } while (p < end);
  168. dc_average = dc;
  169. // then transmit the AC data
  170. transmit(out_left);
  171. release(out_left);
  172. }