Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

277 lines
7.6KB

  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 "output_dac.h"
  27. #include "utility/pdb.h"
  28. #if defined(__MK20DX256__)
  29. DMAMEM static uint16_t dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  30. audio_block_t * AudioOutputAnalog::block_left_1st = NULL;
  31. audio_block_t * AudioOutputAnalog::block_left_2nd = NULL;
  32. bool AudioOutputAnalog::update_responsibility = false;
  33. DMAChannel AudioOutputAnalog::dma(false);
  34. void AudioOutputAnalog::begin(void)
  35. {
  36. dma.begin(true); // Allocate the DMA channel first
  37. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  38. DAC0_C0 = DAC_C0_DACEN; // 1.2V VDDA is DACREF_2
  39. // slowly ramp up to DC voltage, approx 1/4 second
  40. for (int16_t i=0; i<2048; i+=8) {
  41. *(int16_t *)&(DAC0_DAT0L) = i;
  42. delay(1);
  43. }
  44. // set the programmable delay block to trigger DMA requests
  45. if (!(SIM_SCGC6 & SIM_SCGC6_PDB)
  46. || (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
  47. || PDB0_MOD != PDB_PERIOD
  48. || PDB0_IDLY != 1
  49. || PDB0_CH0C1 != 0x0101) {
  50. SIM_SCGC6 |= SIM_SCGC6_PDB;
  51. PDB0_IDLY = 1;
  52. PDB0_MOD = PDB_PERIOD;
  53. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  54. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  55. PDB0_CH0C1 = 0x0101;
  56. }
  57. dma.TCD->SADDR = dac_buffer;
  58. dma.TCD->SOFF = 2;
  59. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  60. dma.TCD->NBYTES_MLNO = 2;
  61. dma.TCD->SLAST = -sizeof(dac_buffer);
  62. dma.TCD->DADDR = &DAC0_DAT0L;
  63. dma.TCD->DOFF = 0;
  64. dma.TCD->CITER_ELINKNO = sizeof(dac_buffer) / 2;
  65. dma.TCD->DLASTSGA = 0;
  66. dma.TCD->BITER_ELINKNO = sizeof(dac_buffer) / 2;
  67. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  68. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
  69. update_responsibility = update_setup();
  70. dma.enable();
  71. dma.attachInterrupt(isr);
  72. }
  73. void AudioOutputAnalog::analogReference(int ref)
  74. {
  75. // TODO: this should ramp gradually to the new DC level
  76. if (ref == INTERNAL) {
  77. DAC0_C0 &= ~DAC_C0_DACRFS; // 1.2V
  78. } else {
  79. DAC0_C0 |= DAC_C0_DACRFS; // 3.3V
  80. }
  81. }
  82. void AudioOutputAnalog::update(void)
  83. {
  84. audio_block_t *block;
  85. block = receiveReadOnly(0); // input 0
  86. if (block) {
  87. __disable_irq();
  88. if (block_left_1st == NULL) {
  89. block_left_1st = block;
  90. __enable_irq();
  91. } else if (block_left_2nd == NULL) {
  92. block_left_2nd = block;
  93. __enable_irq();
  94. } else {
  95. audio_block_t *tmp = block_left_1st;
  96. block_left_1st = block_left_2nd;
  97. block_left_2nd = block;
  98. __enable_irq();
  99. release(tmp);
  100. }
  101. }
  102. }
  103. // TODO: the DAC has much higher bandwidth than the datasheet says
  104. // can we output a 2X oversampled output, for easier filtering?
  105. void AudioOutputAnalog::isr(void)
  106. {
  107. const int16_t *src, *end;
  108. int16_t *dest;
  109. audio_block_t *block;
  110. uint32_t saddr;
  111. saddr = (uint32_t)(dma.TCD->SADDR);
  112. dma.clearInterrupt();
  113. if (saddr < (uint32_t)dac_buffer + sizeof(dac_buffer) / 2) {
  114. // DMA is transmitting the first half of the buffer
  115. // so we must fill the second half
  116. dest = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  117. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  118. } else {
  119. // DMA is transmitting the second half of the buffer
  120. // so we must fill the first half
  121. dest = (int16_t *)dac_buffer;
  122. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  123. }
  124. block = AudioOutputAnalog::block_left_1st;
  125. if (block) {
  126. src = block->data;
  127. do {
  128. // TODO: this should probably dither
  129. *dest++ = ((*src++) + 32767) >> 4;
  130. } while (dest < end);
  131. AudioStream::release(block);
  132. AudioOutputAnalog::block_left_1st = AudioOutputAnalog::block_left_2nd;
  133. AudioOutputAnalog::block_left_2nd = NULL;
  134. } else {
  135. do {
  136. *dest++ = 2047;
  137. } while (dest < end);
  138. }
  139. if (AudioOutputAnalog::update_responsibility) AudioStream::update_all();
  140. }
  141. #elif defined (__MKL26Z64__)
  142. DMAMEM static uint16_t dac_buffer1[AUDIO_BLOCK_SAMPLES];
  143. DMAMEM static uint16_t dac_buffer2[AUDIO_BLOCK_SAMPLES];
  144. audio_block_t * AudioOutputAnalog::block_left_1st = NULL;
  145. bool AudioOutputAnalog::update_responsibility = false;
  146. DMAChannel AudioOutputAnalog::dma1(false);
  147. DMAChannel AudioOutputAnalog::dma2(false);
  148. void AudioOutputAnalog::begin(void)
  149. {
  150. dma1.begin(true); // Allocate the DMA channels first
  151. dma2.begin(true); // Allocate the DMA channels first
  152. delay(2500);
  153. Serial.println("AudioOutputAnalog begin");
  154. delay(10);
  155. SIM_SCGC6 |= SIM_SCGC6_DAC0;
  156. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // VDDA (3.3V) ref
  157. // slowly ramp up to DC voltage, approx 1/4 second
  158. for (int16_t i=0; i<2048; i+=8) {
  159. *(int16_t *)&(DAC0_DAT0L) = i;
  160. delay(1);
  161. }
  162. // commandeer FTM1 for timing (PWM on pin 3 & 4 will become 22 kHz)
  163. FTM1_SC = 0;
  164. FTM1_CNT = 0;
  165. FTM1_MOD = (uint32_t)((F_PLL/2) / AUDIO_SAMPLE_RATE_EXACT + 0.5);
  166. FTM1_SC = FTM_SC_CLKS(1);
  167. dma1.sourceBuffer(dac_buffer1, sizeof(dac_buffer1));
  168. dma1.destination(*(int16_t *)&DAC0_DAT0L);
  169. dma1.interruptAtCompletion();
  170. dma1.disableOnCompletion();
  171. dma1.triggerAtCompletionOf(dma2);
  172. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_OV);
  173. dma1.attachInterrupt(isr1);
  174. dma2.sourceBuffer(dac_buffer2, sizeof(dac_buffer2));
  175. dma2.destination(*(int16_t *)&DAC0_DAT0L);
  176. dma2.interruptAtCompletion();
  177. dma2.disableOnCompletion();
  178. dma2.triggerAtCompletionOf(dma1);
  179. dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_OV);
  180. dma2.attachInterrupt(isr2);
  181. update_responsibility = update_setup();
  182. /*
  183. dma.TCD->SADDR = dac_buffer;
  184. dma.TCD->SOFF = 2;
  185. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  186. dma.TCD->NBYTES_MLNO = 2;
  187. dma.TCD->SLAST = -sizeof(dac_buffer);
  188. dma.TCD->DADDR = &DAC0_DAT0L;
  189. dma.TCD->DOFF = 0;
  190. dma.TCD->CITER_ELINKNO = sizeof(dac_buffer) / 2;
  191. dma.TCD->DLASTSGA = 0;
  192. dma.TCD->BITER_ELINKNO = sizeof(dac_buffer) / 2;
  193. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  194. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
  195. update_responsibility = update_setup();
  196. dma.enable();
  197. dma.attachInterrupt(isr);
  198. */
  199. }
  200. void AudioOutputAnalog::isr1(void)
  201. {
  202. dma1.clearInterrupt();
  203. }
  204. void AudioOutputAnalog::isr2(void)
  205. {
  206. dma2.clearInterrupt();
  207. }
  208. void AudioOutputAnalog::update(void)
  209. {
  210. audio_block_t *block;
  211. block = receiveReadOnly();
  212. if (block) {
  213. __disable_irq();
  214. if (block_left_1st == NULL) {
  215. block_left_1st = block;
  216. __enable_irq();
  217. } else {
  218. audio_block_t *tmp = block_left_1st;
  219. block_left_1st = block;
  220. __enable_irq();
  221. release(tmp);
  222. }
  223. }
  224. }
  225. #else
  226. void AudioOutputAnalog::begin(void)
  227. {
  228. }
  229. void AudioOutputAnalog::update(void)
  230. {
  231. audio_block_t *block;
  232. block = receiveReadOnly(0); // input 0
  233. if (block) release(block);
  234. }
  235. #endif // defined(__MK20DX256__)