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.

215 lines
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 = 2;
  65. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(DMA_TCD_ATTR_SIZE_16BIT) |
  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. } else {
  87. DAC0_C0 |= DAC_C0_DACRFS; // 3.3V
  88. }
  89. }
  90. void AudioOutputAnalogStereo::update(void)
  91. {
  92. audio_block_t *block_left, *block_right;
  93. block_left = receiveReadOnly(0); // input 0
  94. block_right = receiveReadOnly(1); // input 1
  95. __disable_irq();
  96. if (block_left) {
  97. if (block_left_1st == NULL) {
  98. block_left_1st = block_left;
  99. block_left = NULL;
  100. } else if (block_left_2nd == NULL) {
  101. block_left_2nd = block_left;
  102. block_left = NULL;
  103. } else {
  104. audio_block_t *tmp = block_left_1st;
  105. block_left_1st = block_left_2nd;
  106. block_left_2nd = block_left;
  107. block_left = tmp;
  108. }
  109. }
  110. if (block_right) {
  111. if (block_right_1st == NULL) {
  112. block_right_1st = block_right;
  113. block_right = NULL;
  114. } else if (block_right_2nd == NULL) {
  115. block_right_2nd = block_right;
  116. block_right = NULL;
  117. } else {
  118. audio_block_t *tmp = block_right_1st;
  119. block_right_1st = block_right_2nd;
  120. block_right_2nd = block_right;
  121. block_right = tmp;
  122. }
  123. }
  124. __enable_irq();
  125. if (block_left) release(block_left);
  126. if (block_right) release(block_right);
  127. }
  128. // TODO: the DAC has much higher bandwidth than the datasheet says
  129. // can we output a 2X oversampled output, for easier filtering?
  130. void AudioOutputAnalogStereo::isr(void)
  131. {
  132. const uint32_t *src_left, *src_right, *end;
  133. uint32_t *dest;
  134. audio_block_t *block_left, *block_right;
  135. uint32_t saddr;
  136. saddr = (uint32_t)(dma.TCD->SADDR);
  137. dma.clearInterrupt();
  138. if (saddr < (uint32_t)dac_buffer + sizeof(dac_buffer) / 2) {
  139. // DMA is transmitting the first half of the buffer
  140. // so we must fill the second half
  141. dest = dac_buffer + AUDIO_BLOCK_SAMPLES;
  142. end = dac_buffer + AUDIO_BLOCK_SAMPLES*2;
  143. } else {
  144. // DMA is transmitting the second half of the buffer
  145. // so we must fill the first half
  146. dest = dac_buffer;
  147. end = dac_buffer + AUDIO_BLOCK_SAMPLES;
  148. }
  149. block_left = block_left_1st;
  150. if (!block_left) block_left = &block_silent;
  151. block_right = block_right_1st;
  152. if (!block_right) block_right = &block_silent;
  153. src_left = (const uint32_t *)(block_left->data);
  154. src_right = (const uint32_t *)(block_right->data);
  155. do {
  156. // TODO: can this be optimized?
  157. uint32_t left = *src_left++;
  158. uint32_t right = *src_right++;
  159. uint32_t out1 = ((left & 0xFFFF) + 32767) >> 4;
  160. out1 |= (((right & 0xFFFF) + 32767) >> 4) << 16;
  161. uint32_t out2 = ((left >> 16) + 32767) >> 4;
  162. out2 |= (((right >> 16) + 32767) >> 4) << 16;
  163. *dest++ = out1;
  164. *dest++ = out2;
  165. } while (dest < end);
  166. if (block_left != &block_silent) {
  167. release(block_left);
  168. block_left_1st = block_left_2nd;
  169. block_left_2nd = NULL;
  170. }
  171. if (block_right != &block_silent) {
  172. release(block_right);
  173. block_left_1st = block_left_2nd;
  174. block_left_2nd = NULL;
  175. }
  176. if (update_responsibility) update_all();
  177. }
  178. #else // not __MK64FX512__ or __MK66FX1M0__
  179. void AudioOutputAnalogStereo::begin(void)
  180. {
  181. }
  182. void AudioOutputAnalogStereo::update(void)
  183. {
  184. audio_block_t *block;
  185. block = receiveReadOnly(0); // input 0
  186. if (block) release(block);
  187. block = receiveReadOnly(1); // input 1
  188. if (block) release(block);
  189. }
  190. #endif