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.

278 lines
7.7KB

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