您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

output_dacs.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2016, 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_dacs.h"
  28. #include "utility/pdb.h"
  29. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  30. DMAMEM static uint32_t dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  31. audio_block_t * AudioOutputAnalogStereo::block_left_1st = NULL;
  32. audio_block_t * AudioOutputAnalogStereo::block_left_2nd = NULL;
  33. audio_block_t * AudioOutputAnalogStereo::block_right_1st = NULL;
  34. audio_block_t * AudioOutputAnalogStereo::block_right_2nd = NULL;
  35. audio_block_t AudioOutputAnalogStereo::block_silent;
  36. bool AudioOutputAnalogStereo::update_responsibility = false;
  37. DMAChannel AudioOutputAnalogStereo::dma(false);
  38. void AudioOutputAnalogStereo::begin(void)
  39. {
  40. dma.begin(true); // Allocate the DMA channel first
  41. SIM_SCGC2 |= SIM_SCGC2_DAC0 | SIM_SCGC2_DAC1;
  42. DAC0_C0 = DAC_C0_DACEN; // 1.2V VDDA is DACREF_2
  43. DAC1_C0 = DAC_C0_DACEN;
  44. memset(&block_silent, 0, sizeof(block_silent));
  45. // slowly ramp up to DC voltage, approx 1/4 second
  46. for (int16_t i=0; i<=2048; i+=8) {
  47. *(int16_t *)&(DAC0_DAT0L) = i;
  48. *(int16_t *)&(DAC1_DAT0L) = i;
  49. delay(1);
  50. }
  51. // set the programmable delay block to trigger DMA requests
  52. if (!(SIM_SCGC6 & SIM_SCGC6_PDB)
  53. || (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
  54. || PDB0_MOD != PDB_PERIOD
  55. || PDB0_IDLY != 1
  56. || PDB0_CH0C1 != 0x0101) {
  57. SIM_SCGC6 |= SIM_SCGC6_PDB;
  58. PDB0_IDLY = 1;
  59. PDB0_MOD = PDB_PERIOD;
  60. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  61. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  62. PDB0_CH0C1 = 0x0101;
  63. }
  64. dma.TCD->SADDR = dac_buffer;
  65. dma.TCD->SOFF = 4;
  66. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(DMA_TCD_ATTR_SIZE_32BIT) |
  67. DMA_TCD_ATTR_DSIZE(DMA_TCD_ATTR_SIZE_16BIT);
  68. dma.TCD->NBYTES_MLNO = DMA_TCD_NBYTES_MLOFFYES_NBYTES(4) | DMA_TCD_NBYTES_DMLOE |
  69. DMA_TCD_NBYTES_MLOFFYES_MLOFF((&DAC0_DAT0L - &DAC1_DAT0L) * 2);
  70. dma.TCD->SLAST = -sizeof(dac_buffer);
  71. dma.TCD->DADDR = &DAC0_DAT0L;
  72. dma.TCD->DOFF = &DAC1_DAT0L - &DAC0_DAT0L;
  73. dma.TCD->CITER_ELINKNO = sizeof(dac_buffer) / 4;
  74. dma.TCD->DLASTSGA = (&DAC0_DAT0L - &DAC1_DAT0L) * 2;
  75. dma.TCD->BITER_ELINKNO = sizeof(dac_buffer) / 4;
  76. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  77. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
  78. update_responsibility = update_setup();
  79. dma.enable();
  80. dma.attachInterrupt(isr);
  81. }
  82. void AudioOutputAnalogStereo::analogReference(int ref)
  83. {
  84. // TODO: this should ramp gradually to the new DC level
  85. if (ref == INTERNAL) {
  86. DAC0_C0 &= ~DAC_C0_DACRFS; // 1.2V
  87. DAC1_C0 &= ~DAC_C0_DACRFS;
  88. } else {
  89. DAC0_C0 |= DAC_C0_DACRFS; // 3.3V
  90. DAC1_C0 |= DAC_C0_DACRFS;
  91. }
  92. }
  93. void AudioOutputAnalogStereo::update(void)
  94. {
  95. audio_block_t *block_left, *block_right;
  96. block_left = receiveReadOnly(0); // input 0
  97. block_right = receiveReadOnly(1); // input 1
  98. __disable_irq();
  99. if (block_left) {
  100. if (block_left_1st == NULL) {
  101. block_left_1st = block_left;
  102. block_left = NULL;
  103. } else if (block_left_2nd == NULL) {
  104. block_left_2nd = block_left;
  105. block_left = NULL;
  106. } else {
  107. audio_block_t *tmp = block_left_1st;
  108. block_left_1st = block_left_2nd;
  109. block_left_2nd = block_left;
  110. block_left = tmp;
  111. }
  112. }
  113. if (block_right) {
  114. if (block_right_1st == NULL) {
  115. block_right_1st = block_right;
  116. block_right = NULL;
  117. } else if (block_right_2nd == NULL) {
  118. block_right_2nd = block_right;
  119. block_right = NULL;
  120. } else {
  121. audio_block_t *tmp = block_right_1st;
  122. block_right_1st = block_right_2nd;
  123. block_right_2nd = block_right;
  124. block_right = tmp;
  125. }
  126. }
  127. __enable_irq();
  128. if (block_left) release(block_left);
  129. if (block_right) release(block_right);
  130. }
  131. // TODO: the DAC has much higher bandwidth than the datasheet says
  132. // can we output a 2X oversampled output, for easier filtering?
  133. void AudioOutputAnalogStereo::isr(void)
  134. {
  135. const uint32_t *src_left, *src_right, *end;
  136. uint32_t *dest;
  137. audio_block_t *block_left, *block_right;
  138. uint32_t saddr;
  139. saddr = (uint32_t)(dma.TCD->SADDR);
  140. dma.clearInterrupt();
  141. if (saddr < (uint32_t)dac_buffer + sizeof(dac_buffer) / 2) {
  142. // DMA is transmitting the first half of the buffer
  143. // so we must fill the second half
  144. dest = dac_buffer + AUDIO_BLOCK_SAMPLES;
  145. end = dac_buffer + AUDIO_BLOCK_SAMPLES*2;
  146. } else {
  147. // DMA is transmitting the second half of the buffer
  148. // so we must fill the first half
  149. dest = dac_buffer;
  150. end = dac_buffer + AUDIO_BLOCK_SAMPLES;
  151. }
  152. block_left = block_left_1st;
  153. if (!block_left) block_left = &block_silent;
  154. block_right = block_right_1st;
  155. if (!block_right) block_right = &block_silent;
  156. src_left = (const uint32_t *)(block_left->data);
  157. src_right = (const uint32_t *)(block_right->data);
  158. do {
  159. // TODO: can this be optimized?
  160. uint32_t left = *src_left++;
  161. uint32_t right = *src_right++;
  162. uint32_t out1 = ((left & 0xFFFF) + 32768) >> 4;
  163. out1 |= (((right & 0xFFFF) + 32768) >> 4) << 16;
  164. uint32_t out2 = ((left >> 16) + 32768) >> 4;
  165. out2 |= (((right >> 16) + 32768) >> 4) << 16;
  166. *dest++ = out1;
  167. *dest++ = out2;
  168. } while (dest < end);
  169. if (block_left != &block_silent) {
  170. release(block_left);
  171. block_left_1st = block_left_2nd;
  172. block_left_2nd = NULL;
  173. }
  174. if (block_right != &block_silent) {
  175. release(block_right);
  176. block_right_1st = block_right_2nd;
  177. block_right_2nd = NULL;
  178. }
  179. if (update_responsibility) update_all();
  180. }
  181. #else // not __MK64FX512__ or __MK66FX1M0__
  182. void AudioOutputAnalogStereo::begin(void)
  183. {
  184. }
  185. void AudioOutputAnalogStereo::update(void)
  186. {
  187. audio_block_t *block;
  188. block = receiveReadOnly(0); // input 0
  189. if (block) release(block);
  190. block = receiveReadOnly(1); // input 1
  191. if (block) release(block);
  192. }
  193. #endif