Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

167 rindas
5.0KB

  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. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  36. DAC0_C0 = DAC_C0_DACEN; // 1.2V VDDA is DACREF_2
  37. //DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V 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. SIM_SCGC7 |= SIM_SCGC7_DMA;
  50. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  51. DMA_CR = 0;
  52. DMA_TCD4_SADDR = dac_buffer;
  53. DMA_TCD4_SOFF = 2;
  54. DMA_TCD4_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  55. DMA_TCD4_NBYTES_MLNO = 2;
  56. DMA_TCD4_SLAST = -sizeof(dac_buffer);
  57. DMA_TCD4_DADDR = &DAC0_DAT0L;
  58. DMA_TCD4_DOFF = 0;
  59. DMA_TCD4_CITER_ELINKNO = sizeof(dac_buffer) / 2;
  60. DMA_TCD4_DLASTSGA = 0;
  61. DMA_TCD4_BITER_ELINKNO = sizeof(dac_buffer) / 2;
  62. DMA_TCD4_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  63. DMAMUX0_CHCFG4 = DMAMUX_DISABLE;
  64. DMAMUX0_CHCFG4 = DMAMUX_SOURCE_PDB | DMAMUX_ENABLE;
  65. update_responsibility = update_setup();
  66. DMA_SERQ = 4;
  67. NVIC_ENABLE_IRQ(IRQ_DMA_CH4);
  68. }
  69. void AudioOutputAnalog::analogReference(int ref)
  70. {
  71. // TODO: this should ramp gradually to the new DC level
  72. if (ref == INTERNAL) {
  73. DAC0_C0 &= ~DAC_C0_DACRFS; // 1.2V
  74. } else {
  75. DAC0_C0 |= DAC_C0_DACRFS; // 3.3V
  76. }
  77. }
  78. void AudioOutputAnalog::update(void)
  79. {
  80. audio_block_t *block;
  81. block = receiveReadOnly(0); // input 0
  82. if (block) {
  83. __disable_irq();
  84. if (block_left_1st == NULL) {
  85. block_left_1st = block;
  86. __enable_irq();
  87. } else if (block_left_2nd == NULL) {
  88. block_left_2nd = block;
  89. __enable_irq();
  90. } else {
  91. audio_block_t *tmp = block_left_1st;
  92. block_left_1st = block_left_2nd;
  93. block_left_2nd = block;
  94. __enable_irq();
  95. release(tmp);
  96. }
  97. }
  98. }
  99. // TODO: the DAC has much higher bandwidth than the datasheet says
  100. // can we output a 2X oversampled output, for easier filtering?
  101. void dma_ch4_isr(void)
  102. {
  103. const int16_t *src, *end;
  104. int16_t *dest;
  105. audio_block_t *block;
  106. uint32_t saddr;
  107. saddr = (uint32_t)DMA_TCD4_SADDR;
  108. DMA_CINT = 4;
  109. if (saddr < (uint32_t)dac_buffer + sizeof(dac_buffer) / 2) {
  110. // DMA is transmitting the first half of the buffer
  111. // so we must fill the second half
  112. dest = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  113. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  114. } else {
  115. // DMA is transmitting the second half of the buffer
  116. // so we must fill the first half
  117. dest = (int16_t *)dac_buffer;
  118. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  119. }
  120. block = AudioOutputAnalog::block_left_1st;
  121. if (block) {
  122. src = block->data;
  123. do {
  124. // TODO: this should probably dither
  125. *dest++ = ((*src++) + 32767) >> 4;
  126. } while (dest < end);
  127. AudioStream::release(block);
  128. AudioOutputAnalog::block_left_1st = AudioOutputAnalog::block_left_2nd;
  129. AudioOutputAnalog::block_left_2nd = NULL;
  130. } else {
  131. do {
  132. *dest++ = 2047;
  133. } while (dest < end);
  134. }
  135. if (AudioOutputAnalog::update_responsibility) AudioStream::update_all();
  136. }
  137. #else
  138. void AudioOutputAnalog::begin(void)
  139. {
  140. }
  141. void AudioOutputAnalog::update(void)
  142. {
  143. audio_block_t *block;
  144. block = receiveReadOnly(0); // input 0
  145. if (block) release(block);
  146. }
  147. #endif // defined(__MK20DX256__)