Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

374 lines
12KB

  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_adcs.h"
  27. #include "utility/pdb.h"
  28. #include "utility/dspinst.h"
  29. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  30. #define COEF_HPF_DCBLOCK (1048300<<10) // DC Removal filter coefficient in S1.30
  31. DMAMEM static uint16_t left_buffer[AUDIO_BLOCK_SAMPLES];
  32. DMAMEM static uint16_t right_buffer[AUDIO_BLOCK_SAMPLES];
  33. audio_block_t * AudioInputAnalogStereo::block_left = NULL;
  34. audio_block_t * AudioInputAnalogStereo::block_right = NULL;
  35. uint16_t AudioInputAnalogStereo::offset_left = 0;
  36. uint16_t AudioInputAnalogStereo::offset_right = 0;
  37. int32_t AudioInputAnalogStereo::hpf_y1[2] = { 0, 0 };
  38. int32_t AudioInputAnalogStereo::hpf_x1[2] = { 0, 0 };
  39. bool AudioInputAnalogStereo::update_responsibility = false;
  40. DMAChannel AudioInputAnalogStereo::dma0(false);
  41. DMAChannel AudioInputAnalogStereo::dma1(false);
  42. static int analogReadADC1(uint8_t pin);
  43. void AudioInputAnalogStereo::init(uint8_t pin0, uint8_t pin1)
  44. {
  45. uint32_t tmp;
  46. //pinMode(32, OUTPUT);
  47. //pinMode(33, OUTPUT);
  48. // Configure the ADC and run at least one software-triggered
  49. // conversion. This completes the self calibration stuff and
  50. // leaves the ADC in a state that's mostly ready to use
  51. analogReadRes(16);
  52. analogReference(INTERNAL); // range 0 to 1.2 volts
  53. #if F_BUS == 96000000 || F_BUS == 48000000 || F_BUS == 24000000
  54. analogReadAveraging(8);
  55. ADC1_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  56. #else
  57. analogReadAveraging(4);
  58. ADC1_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  59. #endif
  60. // Note for review:
  61. // Probably not useful to spin cycles here stabilizing
  62. // since DC blocking is similar to te external analog filters
  63. tmp = (uint16_t) analogRead(pin0);
  64. tmp = ( ((int32_t) tmp) << 14);
  65. hpf_x1[0] = tmp; // With constant DC level x1 would be x0
  66. hpf_y1[0] = 0; // Output will settle here when stable
  67. tmp = (uint16_t) analogReadADC1(pin1);
  68. tmp = ( ((int32_t) tmp) << 14);
  69. hpf_x1[1] = tmp; // With constant DC level x1 would be x0
  70. hpf_y1[1] = 0; // Output will settle here when stable
  71. // set the programmable delay block to trigger the ADC at 44.1 kHz
  72. //if (!(SIM_SCGC6 & SIM_SCGC6_PDB)
  73. //|| (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
  74. //|| PDB0_MOD != PDB_PERIOD
  75. //|| PDB0_IDLY != 1
  76. //|| PDB0_CH0C1 != 0x0101) {
  77. SIM_SCGC6 |= SIM_SCGC6_PDB;
  78. PDB0_IDLY = 1;
  79. PDB0_MOD = PDB_PERIOD;
  80. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  81. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  82. PDB0_CH0C1 = 0x0101;
  83. PDB0_CH1C1 = 0x0101;
  84. //}
  85. // enable the ADC for hardware trigger and DMA
  86. ADC0_SC2 |= ADC_SC2_ADTRG | ADC_SC2_DMAEN;
  87. ADC1_SC2 |= ADC_SC2_ADTRG | ADC_SC2_DMAEN;
  88. // set up a DMA channel to store the ADC data
  89. dma0.begin(true);
  90. dma1.begin(true);
  91. // ADC0_RA = 0x4003B010
  92. // ADC1_RA = 0x400BB010
  93. dma0.TCD->SADDR = &ADC0_RA;
  94. dma0.TCD->SOFF = 0;
  95. dma0.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  96. dma0.TCD->NBYTES_MLNO = 2;
  97. dma0.TCD->SLAST = 0;
  98. dma0.TCD->DADDR = left_buffer;
  99. dma0.TCD->DOFF = 2;
  100. dma0.TCD->CITER_ELINKNO = sizeof(left_buffer) / 2;
  101. dma0.TCD->DLASTSGA = -sizeof(left_buffer);
  102. dma0.TCD->BITER_ELINKNO = sizeof(left_buffer) / 2;
  103. dma0.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  104. dma1.TCD->SADDR = &ADC1_RA;
  105. dma1.TCD->SOFF = 0;
  106. dma1.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  107. dma1.TCD->NBYTES_MLNO = 2;
  108. dma1.TCD->SLAST = 0;
  109. dma1.TCD->DADDR = right_buffer;
  110. dma1.TCD->DOFF = 2;
  111. dma1.TCD->CITER_ELINKNO = sizeof(right_buffer) / 2;
  112. dma1.TCD->DLASTSGA = -sizeof(right_buffer);
  113. dma1.TCD->BITER_ELINKNO = sizeof(right_buffer) / 2;
  114. dma1.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  115. dma0.triggerAtHardwareEvent(DMAMUX_SOURCE_ADC0);
  116. //dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_ADC1);
  117. dma1.triggerAtTransfersOf(dma0);
  118. dma1.triggerAtCompletionOf(dma0);
  119. update_responsibility = update_setup();
  120. dma0.enable();
  121. dma1.enable();
  122. dma0.attachInterrupt(isr0);
  123. dma1.attachInterrupt(isr1);
  124. }
  125. void AudioInputAnalogStereo::isr0(void)
  126. {
  127. uint32_t daddr, offset;
  128. const uint16_t *src, *end;
  129. uint16_t *dest;
  130. daddr = (uint32_t)(dma0.TCD->DADDR);
  131. dma0.clearInterrupt();
  132. //digitalWriteFast(32, HIGH);
  133. if (daddr < (uint32_t)left_buffer + sizeof(left_buffer) / 2) {
  134. // DMA is receiving to the first half of the buffer
  135. // need to remove data from the second half
  136. src = (uint16_t *)&left_buffer[AUDIO_BLOCK_SAMPLES/2];
  137. end = (uint16_t *)&left_buffer[AUDIO_BLOCK_SAMPLES];
  138. } else {
  139. // DMA is receiving to the second half of the buffer
  140. // need to remove data from the first half
  141. src = (uint16_t *)&left_buffer[0];
  142. end = (uint16_t *)&left_buffer[AUDIO_BLOCK_SAMPLES/2];
  143. //if (update_responsibility) AudioStream::update_all();
  144. }
  145. if (block_left != NULL) {
  146. offset = offset_left;
  147. if (offset > AUDIO_BLOCK_SAMPLES/2) offset = AUDIO_BLOCK_SAMPLES/2;
  148. offset_left = offset + AUDIO_BLOCK_SAMPLES/2;
  149. dest = (uint16_t *)&(block_left->data[offset]);
  150. do {
  151. *dest++ = *src++;
  152. } while (src < end);
  153. }
  154. //digitalWriteFast(32, LOW);
  155. }
  156. void AudioInputAnalogStereo::isr1(void)
  157. {
  158. uint32_t daddr, offset;
  159. const uint16_t *src, *end;
  160. uint16_t *dest;
  161. daddr = (uint32_t)(dma1.TCD->DADDR);
  162. dma1.clearInterrupt();
  163. //digitalWriteFast(33, HIGH);
  164. if (daddr < (uint32_t)right_buffer + sizeof(right_buffer) / 2) {
  165. // DMA is receiving to the first half of the buffer
  166. // need to remove data from the second half
  167. src = (uint16_t *)&right_buffer[AUDIO_BLOCK_SAMPLES/2];
  168. end = (uint16_t *)&right_buffer[AUDIO_BLOCK_SAMPLES];
  169. if (update_responsibility) AudioStream::update_all();
  170. } else {
  171. // DMA is receiving to the second half of the buffer
  172. // need to remove data from the first half
  173. src = (uint16_t *)&right_buffer[0];
  174. end = (uint16_t *)&right_buffer[AUDIO_BLOCK_SAMPLES/2];
  175. }
  176. if (block_right != NULL) {
  177. offset = offset_right;
  178. if (offset > AUDIO_BLOCK_SAMPLES/2) offset = AUDIO_BLOCK_SAMPLES/2;
  179. offset_right = offset + AUDIO_BLOCK_SAMPLES/2;
  180. dest = (uint16_t *)&(block_right->data[offset]);
  181. do {
  182. *dest++ = *src++;
  183. } while (src < end);
  184. }
  185. //digitalWriteFast(33, LOW);
  186. }
  187. void AudioInputAnalogStereo::update(void)
  188. {
  189. audio_block_t *new_left=NULL, *out_left=NULL;
  190. audio_block_t *new_right=NULL, *out_right=NULL;
  191. int32_t tmp;
  192. int16_t s, *p, *end;
  193. //Serial.println("update");
  194. // allocate new block (ok if both NULL)
  195. new_left = allocate();
  196. if (new_left == NULL) {
  197. new_right = NULL;
  198. } else {
  199. new_right = allocate();
  200. if (new_right == NULL) {
  201. release(new_left);
  202. new_left = NULL;
  203. }
  204. }
  205. __disable_irq();
  206. if (offset_left < AUDIO_BLOCK_SAMPLES || offset_right < AUDIO_BLOCK_SAMPLES) {
  207. // the DMA hasn't filled up both blocks
  208. if (block_left == NULL) {
  209. block_left = new_left;
  210. offset_left = 0;
  211. new_left = NULL;
  212. }
  213. if (block_right == NULL) {
  214. block_right = new_right;
  215. offset_right = 0;
  216. new_right = NULL;
  217. }
  218. __enable_irq();
  219. if (new_left) release(new_left);
  220. if (new_right) release(new_right);
  221. return;
  222. }
  223. // the DMA filled blocks, so grab them and get the
  224. // new blocks to the DMA, as quickly as possible
  225. out_left = block_left;
  226. out_right = block_right;
  227. block_left = new_left;
  228. block_right = new_right;
  229. offset_left = 0;
  230. offset_right = 0;
  231. __enable_irq();
  232. //
  233. // DC Offset Removal Filter
  234. // 1-pole digital high-pass filter implementation
  235. // y = a*(x[n] - x[n-1] + y[n-1])
  236. // The coefficient "a" is as follows:
  237. // a = UNITY*e^(-2*pi*fc/fs)
  238. // fc = 2 @ fs = 44100
  239. //
  240. // DC removal, LEFT
  241. p = out_left->data;
  242. end = p + AUDIO_BLOCK_SAMPLES;
  243. do {
  244. tmp = (uint16_t)(*p);
  245. tmp = ( ((int32_t) tmp) << 14);
  246. int32_t acc = hpf_y1[0] - hpf_x1[0];
  247. acc += tmp;
  248. hpf_y1[0] = FRACMUL_SHL(acc, COEF_HPF_DCBLOCK, 1);
  249. hpf_x1[0] = tmp;
  250. s = signed_saturate_rshift(hpf_y1[0], 16, 14);
  251. *p++ = s;
  252. } while (p < end);
  253. // DC removal, RIGHT
  254. p = out_right->data;
  255. end = p + AUDIO_BLOCK_SAMPLES;
  256. do {
  257. tmp = (uint16_t)(*p);
  258. tmp = ( ((int32_t) tmp) << 14);
  259. int32_t acc = hpf_y1[1] - hpf_x1[1];
  260. acc += tmp;
  261. hpf_y1[1]= FRACMUL_SHL(acc, COEF_HPF_DCBLOCK, 1);
  262. hpf_x1[1] = tmp;
  263. s = signed_saturate_rshift(hpf_y1[1], 16, 14);
  264. *p++ = s;
  265. } while (p < end);
  266. // then transmit the AC data
  267. transmit(out_left, 0);
  268. release(out_left);
  269. transmit(out_right, 1);
  270. release(out_right);
  271. }
  272. #if defined(__MK20DX256__)
  273. static const uint8_t pin2sc1a[] = {
  274. 5, 14, 8+128, 9+128, 13, 12, 6, 7, 15, 4, 0, 19, 3, 19+128, // 0-13 -> A0-A13
  275. 5, 14, 8+128, 9+128, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9
  276. 255, 255, // 24-25 are digital only
  277. 5+192, 5+128, 4+128, 6+128, 7+128, 4+192, // 26-31 are A15-A20
  278. 255, 255, // 32-33 are digital only
  279. 0, 19, 3, 19+128, // 34-37 are A10-A13
  280. 26, // 38 is temp sensor,
  281. 18+128, // 39 is vref
  282. 23 // 40 is A14
  283. };
  284. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  285. static const uint8_t pin2sc1a[] = {
  286. 5, 14, 8+128, 9+128, 13, 12, 6, 7, 15, 4, 3, 19+128, 14+128, 15+128, // 0-13 -> A0-A13
  287. 5, 14, 8+128, 9+128, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9
  288. 255, 255, 255, 255, 255, 255, 255, // 24-30 are digital only
  289. 14+128, 15+128, 17, 18, 4+128, 5+128, 6+128, 7+128, 17+128, // 31-39 are A12-A20
  290. 255, 255, 255, 255, 255, 255, 255, 255, 255, // 40-48 are digital only
  291. 10+128, 11+128, // 49-50 are A23-A24
  292. 255, 255, 255, 255, 255, 255, 255, // 51-57 are digital only
  293. 255, 255, 255, 255, 255, 255, // 58-63 (sd card pins) are digital only
  294. 3, 19+128, // 64-65 are A10-A11
  295. 23, 23+128,// 66-67 are A21-A22 (DAC pins)
  296. 1, 1+128, // 68-69 are A25-A26 (unused USB host port on Teensy 3.5)
  297. 26, // 70 is Temperature Sensor
  298. 18+128 // 71 is Vref
  299. };
  300. #endif
  301. static int analogReadADC1(uint8_t pin)
  302. {
  303. ADC1_SC1A = 9;
  304. while (1) {
  305. if ((ADC1_SC1A & ADC_SC1_COCO)) {
  306. return ADC1_RA;
  307. }
  308. }
  309. if (pin >= sizeof(pin2sc1a)) return 0;
  310. uint8_t channel = pin2sc1a[pin];
  311. if ((channel & 0x80) == 0) return 0;
  312. if (channel == 255) return 0;
  313. if (channel & 0x40) {
  314. ADC1_CFG2 &= ~ADC_CFG2_MUXSEL;
  315. } else {
  316. ADC1_CFG2 |= ADC_CFG2_MUXSEL;
  317. }
  318. ADC1_SC1A = channel & 0x3F;
  319. while (1) {
  320. if ((ADC1_SC1A & ADC_SC1_COCO)) {
  321. return ADC1_RA;
  322. }
  323. }
  324. }
  325. #else
  326. void AudioInputAnalogStereo::init(uint8_t pin0, uint8_t pin1)
  327. {
  328. }
  329. void AudioInputAnalogStereo::update(void)
  330. {
  331. }
  332. #endif