Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

output_dac.cpp 5.1KB

10 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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(false);
  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. if (!(SIM_SCGC6 & SIM_SCGC6_PDB)
  46. || (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
  47. || PDB0_MOD != PDB_PERIOD
  48. || PDB0_IDLY != 1
  49. || PDB0_CH0C1 != 0x0101) {
  50. SIM_SCGC6 |= SIM_SCGC6_PDB;
  51. PDB0_IDLY = 1;
  52. PDB0_MOD = PDB_PERIOD;
  53. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  54. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  55. PDB0_CH0C1 = 0x0101;
  56. }
  57. dma.TCD->SADDR = dac_buffer;
  58. dma.TCD->SOFF = 2;
  59. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  60. dma.TCD->NBYTES_MLNO = 2;
  61. dma.TCD->SLAST = -sizeof(dac_buffer);
  62. dma.TCD->DADDR = &DAC0_DAT0L;
  63. dma.TCD->DOFF = 0;
  64. dma.TCD->CITER_ELINKNO = sizeof(dac_buffer) / 2;
  65. dma.TCD->DLASTSGA = 0;
  66. dma.TCD->BITER_ELINKNO = sizeof(dac_buffer) / 2;
  67. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  68. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
  69. update_responsibility = update_setup();
  70. dma.enable();
  71. dma.attachInterrupt(isr);
  72. }
  73. void AudioOutputAnalog::analogReference(int ref)
  74. {
  75. // TODO: this should ramp gradually to the new DC level
  76. if (ref == INTERNAL) {
  77. DAC0_C0 &= ~DAC_C0_DACRFS; // 1.2V
  78. } else {
  79. DAC0_C0 |= DAC_C0_DACRFS; // 3.3V
  80. }
  81. }
  82. void AudioOutputAnalog::update(void)
  83. {
  84. audio_block_t *block;
  85. block = receiveReadOnly(0); // input 0
  86. if (block) {
  87. __disable_irq();
  88. if (block_left_1st == NULL) {
  89. block_left_1st = block;
  90. __enable_irq();
  91. } else if (block_left_2nd == NULL) {
  92. block_left_2nd = block;
  93. __enable_irq();
  94. } else {
  95. audio_block_t *tmp = block_left_1st;
  96. block_left_1st = block_left_2nd;
  97. block_left_2nd = block;
  98. __enable_irq();
  99. release(tmp);
  100. }
  101. }
  102. }
  103. // TODO: the DAC has much higher bandwidth than the datasheet says
  104. // can we output a 2X oversampled output, for easier filtering?
  105. void AudioOutputAnalog::isr(void)
  106. {
  107. const int16_t *src, *end;
  108. int16_t *dest;
  109. audio_block_t *block;
  110. uint32_t saddr;
  111. saddr = (uint32_t)(dma.TCD->SADDR);
  112. dma.clearInterrupt();
  113. if (saddr < (uint32_t)dac_buffer + sizeof(dac_buffer) / 2) {
  114. // DMA is transmitting the first half of the buffer
  115. // so we must fill the second half
  116. dest = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  117. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  118. } else {
  119. // DMA is transmitting the second half of the buffer
  120. // so we must fill the first half
  121. dest = (int16_t *)dac_buffer;
  122. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  123. }
  124. block = AudioOutputAnalog::block_left_1st;
  125. if (block) {
  126. src = block->data;
  127. do {
  128. // TODO: this should probably dither
  129. *dest++ = ((*src++) + 32767) >> 4;
  130. } while (dest < end);
  131. AudioStream::release(block);
  132. AudioOutputAnalog::block_left_1st = AudioOutputAnalog::block_left_2nd;
  133. AudioOutputAnalog::block_left_2nd = NULL;
  134. } else {
  135. do {
  136. *dest++ = 2047;
  137. } while (dest < end);
  138. }
  139. if (AudioOutputAnalog::update_responsibility) AudioStream::update_all();
  140. }
  141. #else
  142. void AudioOutputAnalog::begin(void)
  143. {
  144. }
  145. void AudioOutputAnalog::update(void)
  146. {
  147. audio_block_t *block;
  148. block = receiveReadOnly(0); // input 0
  149. if (block) release(block);
  150. }
  151. #endif // defined(__MK20DX256__)