Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

217 Zeilen
6.7KB

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