選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

output_dac.cpp 4.9KB

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