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.

output_dac.cpp 7.7KB

10 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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__)