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

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