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.

167 lines
4.3KB

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