No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

213 líneas
6.7KB

  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. #define COEF_HPF_DCBLOCK (1048300<<10) // DC Removal filter coefficient in S1.30
  30. DMAMEM static uint16_t analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
  31. audio_block_t * AudioInputAnalog::block_left = NULL;
  32. uint16_t AudioInputAnalog::block_offset = 0;
  33. int32_t AudioInputAnalog::hpf_y1 = 0;
  34. int32_t AudioInputAnalog::hpf_x1 = 0;
  35. bool AudioInputAnalog::update_responsibility = false;
  36. DMAChannel AudioInputAnalog::dma(false);
  37. void AudioInputAnalog::init(uint8_t pin)
  38. {
  39. int32_t tmp;
  40. // Configure the ADC and run at least one software-triggered
  41. // conversion. This completes the self calibration stuff and
  42. // leaves the ADC in a state that's mostly ready to use
  43. analogReadRes(16);
  44. analogReference(INTERNAL); // range 0 to 1.2 volts
  45. #if F_BUS == 96000000 || F_BUS == 48000000 || F_BUS == 24000000
  46. analogReadAveraging(8);
  47. #else
  48. analogReadAveraging(4);
  49. #endif
  50. // Note for review:
  51. // Probably not useful to spin cycles here stabilizing
  52. // since DC blocking is similar to te external analog filters
  53. tmp = (uint16_t) analogRead(pin);
  54. tmp = ( ((int32_t) tmp) << 14);
  55. hpf_x1 = tmp; // With constant DC level x1 would be x0
  56. hpf_y1 = 0; // Output will settle here when stable
  57. // set the programmable delay block to trigger the ADC at 44.1 kHz
  58. #if defined(KINETISK)
  59. if (!(SIM_SCGC6 & SIM_SCGC6_PDB)
  60. || (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
  61. || PDB0_MOD != PDB_PERIOD
  62. || PDB0_IDLY != 1
  63. || PDB0_CH0C1 != 0x0101) {
  64. SIM_SCGC6 |= SIM_SCGC6_PDB;
  65. PDB0_IDLY = 1;
  66. PDB0_MOD = PDB_PERIOD;
  67. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  68. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  69. PDB0_CH0C1 = 0x0101;
  70. }
  71. #endif
  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.begin(true);
  76. #if defined(KINETISK)
  77. dma.TCD->SADDR = &ADC0_RA;
  78. dma.TCD->SOFF = 0;
  79. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  80. dma.TCD->NBYTES_MLNO = 2;
  81. dma.TCD->SLAST = 0;
  82. dma.TCD->DADDR = analog_rx_buffer;
  83. dma.TCD->DOFF = 2;
  84. dma.TCD->CITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
  85. dma.TCD->DLASTSGA = -sizeof(analog_rx_buffer);
  86. dma.TCD->BITER_ELINKNO = sizeof(analog_rx_buffer) / 2;
  87. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  88. #endif
  89. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_ADC0);
  90. update_responsibility = update_setup();
  91. dma.enable();
  92. dma.attachInterrupt(isr);
  93. }
  94. void AudioInputAnalog::isr(void)
  95. {
  96. uint32_t daddr, offset;
  97. const uint16_t *src, *end;
  98. uint16_t *dest_left;
  99. audio_block_t *left;
  100. #if defined(KINETISK)
  101. daddr = (uint32_t)(dma.TCD->DADDR);
  102. #endif
  103. dma.clearInterrupt();
  104. if (daddr < (uint32_t)analog_rx_buffer + sizeof(analog_rx_buffer) / 2) {
  105. // DMA is receiving to the first half of the buffer
  106. // need to remove data from the second half
  107. src = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  108. end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES];
  109. if (update_responsibility) AudioStream::update_all();
  110. } else {
  111. // DMA is receiving to the second half of the buffer
  112. // need to remove data from the first half
  113. src = (uint16_t *)&analog_rx_buffer[0];
  114. end = (uint16_t *)&analog_rx_buffer[AUDIO_BLOCK_SAMPLES/2];
  115. }
  116. left = block_left;
  117. if (left != NULL) {
  118. offset = block_offset;
  119. if (offset > AUDIO_BLOCK_SAMPLES/2) offset = AUDIO_BLOCK_SAMPLES/2;
  120. dest_left = (uint16_t *)&(left->data[offset]);
  121. block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  122. do {
  123. *dest_left++ = *src++;
  124. } while (src < end);
  125. }
  126. }
  127. void AudioInputAnalog::update(void)
  128. {
  129. audio_block_t *new_left=NULL, *out_left=NULL;
  130. uint32_t offset;
  131. int32_t tmp;
  132. int16_t s, *p, *end;
  133. //Serial.println("update");
  134. // allocate new block (ok if NULL)
  135. new_left = allocate();
  136. __disable_irq();
  137. offset = block_offset;
  138. if (offset < AUDIO_BLOCK_SAMPLES) {
  139. // the DMA didn't fill a block
  140. if (new_left != NULL) {
  141. // but we allocated a block
  142. if (block_left == NULL) {
  143. // the DMA doesn't have any blocks to fill, so
  144. // give it the one we just allocated
  145. block_left = new_left;
  146. block_offset = 0;
  147. __enable_irq();
  148. //Serial.println("fail1");
  149. } else {
  150. // the DMA already has blocks, doesn't need this
  151. __enable_irq();
  152. release(new_left);
  153. //Serial.print("fail2, offset=");
  154. //Serial.println(offset);
  155. }
  156. } else {
  157. // The DMA didn't fill a block, and we could not allocate
  158. // memory... the system is likely starving for memory!
  159. // Sadly, there's nothing we can do.
  160. __enable_irq();
  161. //Serial.println("fail3");
  162. }
  163. return;
  164. }
  165. // the DMA filled a block, so grab it and get the
  166. // new block to the DMA, as quickly as possible
  167. out_left = block_left;
  168. block_left = new_left;
  169. block_offset = 0;
  170. __enable_irq();
  171. //
  172. // DC Offset Removal Filter
  173. // 1-pole digital high-pass filter implementation
  174. // y = a*(x[n] - x[n-1] + y[n-1])
  175. // The coefficient "a" is as follows:
  176. // a = UNITY*e^(-2*pi*fc/fs)
  177. // fc = 2 @ fs = 44100
  178. //
  179. p = out_left->data;
  180. end = p + AUDIO_BLOCK_SAMPLES;
  181. do {
  182. tmp = (uint16_t)(*p);
  183. tmp = ( ((int32_t) tmp) << 14);
  184. int32_t acc = hpf_y1 - hpf_x1;
  185. acc += tmp;
  186. hpf_y1 = FRACMUL_SHL(acc, COEF_HPF_DCBLOCK, 1);
  187. hpf_x1 = tmp;
  188. s = signed_saturate_rshift(hpf_y1, 16, 14);
  189. *p++ = s;
  190. } while (p < end);
  191. // then transmit the AC data
  192. transmit(out_left);
  193. release(out_left);
  194. }