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.

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