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.

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