您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

190 行
5.7KB

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