PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

input_adcs.cpp 12KB

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