PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

output_dac.cpp 8.2KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <Arduino.h>
  27. #include "output_dac.h"
  28. #include "utility/pdb.h"
  29. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  30. DMAMEM __attribute__((aligned(32))) static uint16_t dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  31. audio_block_t * AudioOutputAnalog::block_left_1st = NULL;
  32. audio_block_t * AudioOutputAnalog::block_left_2nd = NULL;
  33. bool AudioOutputAnalog::update_responsibility = false;
  34. DMAChannel AudioOutputAnalog::dma(false);
  35. void AudioOutputAnalog::begin(void)
  36. {
  37. dma.begin(true); // Allocate the DMA channel first
  38. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  39. DAC0_C0 = DAC_C0_DACEN; // 1.2V VDDA is DACREF_2
  40. // slowly ramp up to DC voltage, approx 1/4 second
  41. for (int16_t i=0; i<=2048; i+=8) {
  42. *(int16_t *)&(DAC0_DAT0L) = i;
  43. delay(1);
  44. }
  45. // set the programmable delay block to trigger DMA requests
  46. if (!(SIM_SCGC6 & SIM_SCGC6_PDB)
  47. || (PDB0_SC & PDB_CONFIG) != PDB_CONFIG
  48. || PDB0_MOD != PDB_PERIOD
  49. || PDB0_IDLY != 1
  50. || PDB0_CH0C1 != 0x0101) {
  51. SIM_SCGC6 |= SIM_SCGC6_PDB;
  52. PDB0_IDLY = 1;
  53. PDB0_MOD = PDB_PERIOD;
  54. PDB0_SC = PDB_CONFIG | PDB_SC_LDOK;
  55. PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG;
  56. PDB0_CH0C1 = 0x0101;
  57. }
  58. dma.TCD->SADDR = dac_buffer;
  59. dma.TCD->SOFF = 2;
  60. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  61. dma.TCD->NBYTES_MLNO = 2;
  62. dma.TCD->SLAST = -sizeof(dac_buffer);
  63. dma.TCD->DADDR = &DAC0_DAT0L;
  64. dma.TCD->DOFF = 0;
  65. dma.TCD->CITER_ELINKNO = sizeof(dac_buffer) / 2;
  66. dma.TCD->DLASTSGA = 0;
  67. dma.TCD->BITER_ELINKNO = sizeof(dac_buffer) / 2;
  68. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  69. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
  70. update_responsibility = update_setup();
  71. dma.enable();
  72. dma.attachInterrupt(isr);
  73. }
  74. void AudioOutputAnalog::analogReference(int ref)
  75. {
  76. // TODO: this should ramp gradually to the new DC level
  77. if (ref == INTERNAL) {
  78. DAC0_C0 &= ~DAC_C0_DACRFS; // 1.2V
  79. } else {
  80. DAC0_C0 |= DAC_C0_DACRFS; // 3.3V
  81. }
  82. }
  83. void AudioOutputAnalog::update(void)
  84. {
  85. audio_block_t *block;
  86. block = receiveReadOnly(0); // input 0
  87. if (block) {
  88. __disable_irq();
  89. if (block_left_1st == NULL) {
  90. block_left_1st = block;
  91. __enable_irq();
  92. } else if (block_left_2nd == NULL) {
  93. block_left_2nd = block;
  94. __enable_irq();
  95. } else {
  96. audio_block_t *tmp = block_left_1st;
  97. block_left_1st = block_left_2nd;
  98. block_left_2nd = block;
  99. __enable_irq();
  100. release(tmp);
  101. }
  102. }
  103. }
  104. // TODO: the DAC has much higher bandwidth than the datasheet says
  105. // can we output a 2X oversampled output, for easier filtering?
  106. void AudioOutputAnalog::isr(void)
  107. {
  108. const int16_t *src, *end;
  109. int16_t *dest;
  110. audio_block_t *block;
  111. uint32_t saddr;
  112. saddr = (uint32_t)(dma.TCD->SADDR);
  113. dma.clearInterrupt();
  114. if (saddr < (uint32_t)dac_buffer + sizeof(dac_buffer) / 2) {
  115. // DMA is transmitting the first half of the buffer
  116. // so we must fill the second half
  117. dest = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  118. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES*2];
  119. } else {
  120. // DMA is transmitting the second half of the buffer
  121. // so we must fill the first half
  122. dest = (int16_t *)dac_buffer;
  123. end = (int16_t *)&dac_buffer[AUDIO_BLOCK_SAMPLES];
  124. }
  125. block = AudioOutputAnalog::block_left_1st;
  126. if (block) {
  127. src = block->data;
  128. do {
  129. // TODO: this should probably dither
  130. *dest++ = ((*src++) + 32768) >> 4;
  131. } while (dest < end);
  132. AudioStream::release(block);
  133. AudioOutputAnalog::block_left_1st = AudioOutputAnalog::block_left_2nd;
  134. AudioOutputAnalog::block_left_2nd = NULL;
  135. } else {
  136. do {
  137. *dest++ = 2048;
  138. } while (dest < end);
  139. }
  140. if (AudioOutputAnalog::update_responsibility) AudioStream::update_all();
  141. }
  142. #elif defined (__MKL26Z64__)
  143. DMAMEM static uint16_t dac_buffer1[AUDIO_BLOCK_SAMPLES];
  144. DMAMEM static uint16_t dac_buffer2[AUDIO_BLOCK_SAMPLES];
  145. audio_block_t * AudioOutputAnalog::block_left_1st = NULL;
  146. audio_block_t * AudioOutputAnalog::block_left_2nd = NULL;
  147. bool AudioOutputAnalog::update_responsibility = false;
  148. DMAChannel AudioOutputAnalog::dma1(false);
  149. void AudioOutputAnalog::begin(void)
  150. {
  151. dma1.begin(true); // Allocate the DMA channels first
  152. //delay(2500);
  153. //Serial.println("AudioOutputAnalog begin");
  154. delay(10);
  155. SIM_SCGC6 |= SIM_SCGC6_DAC0;
  156. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // VDDA (3.3V) ref
  157. // slowly ramp up to DC voltage, approx 1/4 second
  158. for (int16_t i=0; i<2048; i+=8) {
  159. *(int16_t *)&(DAC0_DAT0L) = i;
  160. delay(1);
  161. }
  162. // commandeer FTM1 for timing (PWM on pin 3 & 4 will become 22 kHz)
  163. FTM1_SC = 0;
  164. FTM1_CNT = 0;
  165. FTM1_MOD = (uint32_t)((F_PLL/2) / 44117.64706/*AUDIO_SAMPLE_RATE_EXACT*/ + 0.5);
  166. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_DMA;
  167. dma1.sourceBuffer(dac_buffer1, sizeof(dac_buffer1));
  168. dma1.destination(*(int16_t *)&DAC0_DAT0L);
  169. dma1.interruptAtCompletion();
  170. dma1.disableOnCompletion();
  171. dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_OV);
  172. dma1.attachInterrupt(isr1);
  173. update_responsibility = update_setup();
  174. // Enable DMA transfers on timer
  175. dma1.enable();
  176. }
  177. void AudioOutputAnalog::isr1(void)
  178. {
  179. if(!dma1.complete()) return;
  180. dma1.clearInterrupt();
  181. // Point DMA to the other buffer (which also resets the number of bytes to transfer)
  182. bool finishedFirst = (dma1.sourceAddress() == (void *)&dac_buffer1[AUDIO_BLOCK_SAMPLES]);
  183. if(finishedFirst) {
  184. // Just finished copying the first block, set up the second
  185. dma1.sourceBuffer(dac_buffer2, sizeof(dac_buffer2));
  186. } else {
  187. // Just finished the second buffer, set up the first
  188. dma1.sourceBuffer(dac_buffer1, sizeof(dac_buffer1));
  189. }
  190. // restart DMA on timer calls
  191. dma1.enable();
  192. // Then refill the buffer
  193. int16_t *dest;
  194. const int16_t * end;
  195. if(finishedFirst) {
  196. dest = (int16_t *)dac_buffer1;
  197. end = (int16_t *)&dac_buffer1[AUDIO_BLOCK_SAMPLES];
  198. } else {
  199. dest = (int16_t *)dac_buffer2;
  200. end = (int16_t *)&dac_buffer2[AUDIO_BLOCK_SAMPLES];
  201. }
  202. const int16_t *src;
  203. audio_block_t *block;
  204. block = AudioOutputAnalog::block_left_1st;
  205. if (block) {
  206. src = block->data;
  207. do {
  208. *dest++ = ((*src++) >> 4) + 0x800;
  209. } while (dest < end);
  210. AudioStream::release(block);
  211. AudioOutputAnalog::block_left_1st = AudioOutputAnalog::block_left_2nd;
  212. AudioOutputAnalog::block_left_2nd = NULL;
  213. } else {
  214. do {
  215. *dest++ = 0x800;
  216. } while (dest < end);
  217. }
  218. if (AudioOutputAnalog::update_responsibility) AudioStream::update_all();
  219. }
  220. void AudioOutputAnalog::update(void)
  221. {
  222. audio_block_t *block;
  223. block = receiveReadOnly();
  224. if (block) {
  225. __disable_irq();
  226. if (block_left_1st == NULL) {
  227. block_left_1st = block;
  228. __enable_irq();
  229. } else if (block_left_2nd == NULL) {
  230. block_left_2nd = block;
  231. __enable_irq();
  232. } else {
  233. audio_block_t *tmp = block_left_1st;
  234. block_left_1st = block_left_2nd;
  235. block_left_2nd = block;
  236. __enable_irq();
  237. release(tmp);
  238. }
  239. }
  240. }
  241. #else
  242. void AudioOutputAnalog::begin(void)
  243. {
  244. }
  245. void AudioOutputAnalog::update(void)
  246. {
  247. audio_block_t *block;
  248. block = receiveReadOnly(0); // input 0
  249. if (block) release(block);
  250. }
  251. #endif // defined(__MK20DX256__)