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.

166 lines
4.9KB

  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 "output_dac.h"
  27. #include "utility/pdb.h"
  28. #if defined(__MK20DX256__)
  29. DMAMEM static uint16_t dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  30. audio_block_t * AudioOutputAnalog::block_left_1st = NULL;
  31. audio_block_t * AudioOutputAnalog::block_left_2nd = NULL;
  32. bool AudioOutputAnalog::update_responsibility = false;
  33. DMAChannel AudioOutputAnalog::dma;
  34. void AudioOutputAnalog::begin(void)
  35. {
  36. dma.begin(true); // Allocate the DMA channel first
  37. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  38. DAC0_C0 = DAC_C0_DACEN; // 1.2V VDDA is DACREF_2
  39. // slowly ramp up to DC voltage, approx 1/4 second
  40. for (int16_t i=0; i<2048; i+=8) {
  41. *(int16_t *)&(DAC0_DAT0L) = i;
  42. delay(1);
  43. }
  44. // set the programmable delay block to trigger DMA requests
  45. SIM_SCGC6 |= SIM_SCGC6_PDB;
  46. PDB0_IDLY = 1;
  47. PDB0_MOD = PDB_PERIOD;
  48. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  49. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG | PDB_SC_PDBIE | PDB_SC_DMAEN;
  50. dma.TCD->SADDR = dac_buffer;
  51. dma.TCD->SOFF = 2;
  52. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  53. dma.TCD->NBYTES_MLNO = 2;
  54. dma.TCD->SLAST = -sizeof(dac_buffer);
  55. dma.TCD->DADDR = &DAC0_DAT0L;
  56. dma.TCD->DOFF = 0;
  57. dma.TCD->CITER_ELINKNO = sizeof(dac_buffer) / 2;
  58. dma.TCD->DLASTSGA = 0;
  59. dma.TCD->BITER_ELINKNO = sizeof(dac_buffer) / 2;
  60. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  61. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
  62. update_responsibility = update_setup();
  63. dma.enable();
  64. dma.attachInterrupt(isr);
  65. }
  66. void AudioOutputAnalog::analogReference(int ref)
  67. {
  68. // TODO: this should ramp gradually to the new DC level
  69. if (ref == INTERNAL) {
  70. DAC0_C0 &= ~DAC_C0_DACRFS; // 1.2V
  71. } else {
  72. DAC0_C0 |= DAC_C0_DACRFS; // 3.3V
  73. }
  74. }
  75. void AudioOutputAnalog::update(void)
  76. {
  77. audio_block_t *block;
  78. block = receiveReadOnly(0); // input 0
  79. if (block) {
  80. __disable_irq();
  81. if (block_left_1st == NULL) {
  82. block_left_1st = block;
  83. __enable_irq();
  84. } else if (block_left_2nd == NULL) {
  85. block_left_2nd = block;
  86. __enable_irq();
  87. } else {
  88. audio_block_t *tmp = block_left_1st;
  89. block_left_1st = block_left_2nd;
  90. block_left_2nd = block;
  91. __enable_irq();
  92. release(tmp);
  93. }
  94. }
  95. }
  96. // TODO: the DAC has much higher bandwidth than the datasheet says
  97. // can we output a 2X oversampled output, for easier filtering?
  98. void AudioOutputAnalog::isr(void)
  99. {
  100. const int16_t *src, *end;
  101. int16_t *dest;
  102. audio_block_t *block;
  103. uint32_t saddr;
  104. saddr = (uint32_t)(dma.TCD->SADDR);
  105. dma.clearInterrupt();
  106. if (saddr < (uint32_t)dac_buffer + sizeof(dac_buffer) / 2) {
  107. // DMA is transmitting the first half of the buffer
  108. // so we must fill the second half
  109. dest = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  110. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  111. } else {
  112. // DMA is transmitting the second half of the buffer
  113. // so we must fill the first half
  114. dest = (int16_t *)dac_buffer;
  115. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  116. }
  117. block = AudioOutputAnalog::block_left_1st;
  118. if (block) {
  119. src = block->data;
  120. do {
  121. // TODO: this should probably dither
  122. *dest++ = ((*src++) + 32767) >> 4;
  123. } while (dest < end);
  124. AudioStream::release(block);
  125. AudioOutputAnalog::block_left_1st = AudioOutputAnalog::block_left_2nd;
  126. AudioOutputAnalog::block_left_2nd = NULL;
  127. } else {
  128. do {
  129. *dest++ = 2047;
  130. } while (dest < end);
  131. }
  132. if (AudioOutputAnalog::update_responsibility) AudioStream::update_all();
  133. }
  134. #else
  135. void AudioOutputAnalog::begin(void)
  136. {
  137. }
  138. void AudioOutputAnalog::update(void)
  139. {
  140. audio_block_t *block;
  141. block = receiveReadOnly(0); // input 0
  142. if (block) release(block);
  143. }
  144. #endif // defined(__MK20DX256__)