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.

367 lines
11KB

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