Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

input_adcs.cpp 11KB

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