Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

190 linhas
5.6KB

  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_pwm.h"
  27. audio_block_t * AudioOutputPWM::block_1st = NULL;
  28. audio_block_t * AudioOutputPWM::block_2nd = NULL;
  29. uint32_t AudioOutputPWM::block_offset = 0;
  30. bool AudioOutputPWM::update_responsibility = false;
  31. uint8_t AudioOutputPWM::interrupt_count = 0;
  32. DMAMEM uint32_t pwm_dma_buffer[AUDIO_BLOCK_SAMPLES*2];
  33. DMAChannel AudioOutputPWM::dma;
  34. void AudioOutputPWM::begin(void)
  35. {
  36. dma.begin(true); // Allocate the DMA channel first
  37. //Serial.println("AudioPwmOutput constructor");
  38. block_1st = NULL;
  39. FTM1_SC = 0;
  40. FTM1_CNT = 0;
  41. FTM1_MOD = 543;
  42. FTM1_C0SC = 0x69; // send DMA request on match
  43. FTM1_C1SC = 0x28;
  44. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
  45. CORE_PIN3_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  46. CORE_PIN4_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  47. FTM1_C0V = 120; // range 120 to 375
  48. FTM1_C1V = 0; // range 0 to 255
  49. for (int i=0; i<256; i+=2) {
  50. pwm_dma_buffer[i] = 120; // zero must not be used
  51. pwm_dma_buffer[i+1] = 0;
  52. }
  53. dma.TCD->SADDR = pwm_dma_buffer;
  54. dma.TCD->SOFF = 4;
  55. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2)
  56. | DMA_TCD_ATTR_DSIZE(2) | DMA_TCD_ATTR_DMOD(4);
  57. dma.TCD->NBYTES_MLNO = 8;
  58. dma.TCD->SLAST = -sizeof(pwm_dma_buffer);
  59. dma.TCD->DADDR = &FTM1_C0V;
  60. dma.TCD->DOFF = 8;
  61. dma.TCD->CITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  62. dma.TCD->DLASTSGA = 0;
  63. dma.TCD->BITER_ELINKNO = sizeof(pwm_dma_buffer) / 8;
  64. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  65. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_FTM1_CH0);
  66. dma.enable();
  67. update_responsibility = update_setup();
  68. dma.attachInterrupt(isr);
  69. }
  70. void AudioOutputPWM::update(void)
  71. {
  72. audio_block_t *block;
  73. block = receiveReadOnly();
  74. if (!block) return;
  75. __disable_irq();
  76. if (block_1st == NULL) {
  77. block_1st = block;
  78. block_offset = 0;
  79. __enable_irq();
  80. } else if (block_2nd == NULL) {
  81. block_2nd = block;
  82. __enable_irq();
  83. } else {
  84. audio_block_t *tmp = block_1st;
  85. block_1st = block_2nd;
  86. block_2nd = block;
  87. block_offset = 0;
  88. __enable_irq();
  89. release(tmp);
  90. }
  91. }
  92. void AudioOutputPWM::isr(void)
  93. {
  94. int16_t *src;
  95. uint32_t *dest;
  96. audio_block_t *block;
  97. uint32_t saddr, offset;
  98. saddr = (uint32_t)(dma.TCD->SADDR);
  99. dma.clearInterrupt();
  100. if (saddr < (uint32_t)pwm_dma_buffer + sizeof(pwm_dma_buffer) / 2) {
  101. // DMA is transmitting the first half of the buffer
  102. // so we must fill the second half
  103. dest = &pwm_dma_buffer[AUDIO_BLOCK_SAMPLES];
  104. } else {
  105. // DMA is transmitting the second half of the buffer
  106. // so we must fill the first half
  107. dest = pwm_dma_buffer;
  108. }
  109. block = AudioOutputPWM::block_1st;
  110. offset = AudioOutputPWM::block_offset;
  111. if (block) {
  112. src = &block->data[offset];
  113. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  114. uint16_t sample = *src++ + 0x8000;
  115. uint32_t msb = ((sample >> 8) & 255) + 120;
  116. uint32_t lsb = sample & 255;
  117. *dest++ = msb;
  118. *dest++ = lsb;
  119. *dest++ = msb;
  120. *dest++ = lsb;
  121. }
  122. offset += AUDIO_BLOCK_SAMPLES/4;
  123. if (offset < AUDIO_BLOCK_SAMPLES) {
  124. AudioOutputPWM::block_offset = offset;
  125. } else {
  126. AudioOutputPWM::block_offset = 0;
  127. AudioStream::release(block);
  128. AudioOutputPWM::block_1st = AudioOutputPWM::block_2nd;
  129. AudioOutputPWM::block_2nd = NULL;
  130. }
  131. } else {
  132. // fill with silence when no data available
  133. for (int i=0; i < AUDIO_BLOCK_SAMPLES/4; i++) {
  134. *dest++ = 248;
  135. *dest++ = 0;
  136. *dest++ = 248;
  137. *dest++ = 0;
  138. }
  139. }
  140. if (AudioOutputPWM::update_responsibility) {
  141. if (++AudioOutputPWM::interrupt_count >= 4) {
  142. AudioOutputPWM::interrupt_count = 0;
  143. AudioStream::update_all();
  144. }
  145. }
  146. }
  147. // DMA target is: (registers require 32 bit writes)
  148. // 40039010 Channel 0 Value (FTM1_C0V)
  149. // 40039018 Channel 1 Value (FTM1_C1V)
  150. // TCD:
  151. // source address = buffer address
  152. // source offset = 4 bytes
  153. // attr = no src mod, ssize = 32 bit, dest mod = 16 bytes (4), dsize = 32 bit
  154. // minor loop byte count = 8
  155. // source last adjust = -sizeof(buffer)
  156. // dest address = FTM1_C0V
  157. // dest address offset = 8
  158. // citer = sizeof(buffer) / 8 (no minor loop linking)
  159. // dest last adjust = 0 (dest modulo keeps it ready for more)
  160. // control:
  161. // throttling = 0
  162. // major link to same channel
  163. // done = 0
  164. // active = 0
  165. // majorlink = 1
  166. // scatter/gather = 0
  167. // disable request = 0
  168. // inthalf = 1
  169. // intmajor = 1
  170. // start = 0
  171. // biter = sizeof(buffer) / 8 (no minor loop linking)