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.

213 lines
6.5KB

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