Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

230 lines
7.5KB

  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. // Frank B
  27. #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  28. #include <Arduino.h>
  29. #include "output_mqs.h"
  30. #include "memcpy_audio.h"
  31. #include "utility/imxrt_hw.h"
  32. audio_block_t * AudioOutputMQS::block_left_1st = NULL;
  33. audio_block_t * AudioOutputMQS::block_right_1st = NULL;
  34. audio_block_t * AudioOutputMQS::block_left_2nd = NULL;
  35. audio_block_t * AudioOutputMQS::block_right_2nd = NULL;
  36. uint16_t AudioOutputMQS::block_left_offset = 0;
  37. uint16_t AudioOutputMQS::block_right_offset = 0;
  38. bool AudioOutputMQS::update_responsibility = false;
  39. static uint32_t I2S3_tx_buffer[AUDIO_BLOCK_SAMPLES];
  40. DMAChannel AudioOutputMQS::dma(false);
  41. void AudioOutputMQS::begin(void)
  42. {
  43. dma.begin(true); // Allocate the DMA channel first
  44. block_left_1st = NULL;
  45. block_right_1st = NULL;
  46. config_i2s();
  47. CORE_PIN10_CONFIG = 2;//B0_00 MQS_RIGHT
  48. CORE_PIN12_CONFIG = 2;//B0_01 MQS_LEFT
  49. dma.TCD->SADDR = I2S3_tx_buffer;
  50. dma.TCD->SOFF = 2;
  51. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  52. dma.TCD->NBYTES_MLNO = 2;
  53. dma.TCD->SLAST = -sizeof(I2S3_tx_buffer);
  54. dma.TCD->DOFF = 0;
  55. dma.TCD->CITER_ELINKNO = sizeof(I2S3_tx_buffer) / 2;
  56. dma.TCD->DLASTSGA = 0;
  57. dma.TCD->BITER_ELINKNO = sizeof(I2S3_tx_buffer) / 2;
  58. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  59. dma.TCD->DADDR = (void *)((uint32_t)&I2S3_TDR0 + 0);
  60. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI3_TX);
  61. I2S3_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  62. update_responsibility = update_setup();
  63. dma.attachInterrupt(isr);
  64. dma.enable();
  65. }
  66. void AudioOutputMQS::isr(void)
  67. {
  68. int16_t *dest;
  69. audio_block_t *blockL, *blockR;
  70. uint32_t saddr, offsetL, offsetR;
  71. saddr = (uint32_t)(dma.TCD->SADDR);
  72. dma.clearInterrupt();
  73. if (saddr < (uint32_t)I2S3_tx_buffer + sizeof(I2S3_tx_buffer) / 2) {
  74. // DMA is transmitting the first half of the buffer
  75. // so we must fill the second half
  76. dest = (int16_t *)&I2S3_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  77. if (AudioOutputMQS::update_responsibility) AudioStream::update_all();
  78. } else {
  79. // DMA is transmitting the second half of the buffer
  80. // so we must fill the first half
  81. dest = (int16_t *)I2S3_tx_buffer;
  82. }
  83. blockL = AudioOutputMQS::block_left_1st;
  84. blockR = AudioOutputMQS::block_right_1st;
  85. offsetL = AudioOutputMQS::block_left_offset;
  86. offsetR = AudioOutputMQS::block_right_offset;
  87. if (blockL && blockR) {
  88. memcpy_tointerleaveLR(dest, blockL->data + offsetL, blockR->data + offsetR);
  89. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  90. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  91. } else if (blockL) {
  92. memcpy_tointerleaveL(dest, blockL->data + offsetL);
  93. offsetL += AUDIO_BLOCK_SAMPLES / 2;
  94. } else if (blockR) {
  95. memcpy_tointerleaveR(dest, blockR->data + offsetR);
  96. offsetR += AUDIO_BLOCK_SAMPLES / 2;
  97. } else {
  98. memset(dest,0,AUDIO_BLOCK_SAMPLES * 2);
  99. return;
  100. }
  101. if (offsetL < AUDIO_BLOCK_SAMPLES) {
  102. AudioOutputMQS::block_left_offset = offsetL;
  103. } else {
  104. AudioOutputMQS::block_left_offset = 0;
  105. AudioStream::release(blockL);
  106. AudioOutputMQS::block_left_1st = AudioOutputMQS::block_left_2nd;
  107. AudioOutputMQS::block_left_2nd = NULL;
  108. }
  109. if (offsetR < AUDIO_BLOCK_SAMPLES) {
  110. AudioOutputMQS::block_right_offset = offsetR;
  111. } else {
  112. AudioOutputMQS::block_right_offset = 0;
  113. AudioStream::release(blockR);
  114. AudioOutputMQS::block_right_1st = AudioOutputMQS::block_right_2nd;
  115. AudioOutputMQS::block_right_2nd = NULL;
  116. }
  117. }
  118. void AudioOutputMQS::update(void)
  119. {
  120. // null audio device: discard all incoming data
  121. //if (!active) return;
  122. //audio_block_t *block = receiveReadOnly();
  123. //if (block) release(block);
  124. //digitalWriteFast(13, LOW);
  125. audio_block_t *block;
  126. block = receiveReadOnly(0); // input 0 = left channel
  127. if (block) {
  128. __disable_irq();
  129. if (block_left_1st == NULL) {
  130. block_left_1st = block;
  131. block_left_offset = 0;
  132. __enable_irq();
  133. } else if (block_left_2nd == NULL) {
  134. block_left_2nd = block;
  135. __enable_irq();
  136. } else {
  137. audio_block_t *tmp = block_left_1st;
  138. block_left_1st = block_left_2nd;
  139. block_left_2nd = block;
  140. block_left_offset = 0;
  141. __enable_irq();
  142. release(tmp);
  143. }
  144. }
  145. block = receiveReadOnly(1); // input 1 = right channel
  146. if (block) {
  147. __disable_irq();
  148. if (block_right_1st == NULL) {
  149. block_right_1st = block;
  150. block_right_offset = 0;
  151. __enable_irq();
  152. } else if (block_right_2nd == NULL) {
  153. block_right_2nd = block;
  154. __enable_irq();
  155. } else {
  156. audio_block_t *tmp = block_right_1st;
  157. block_right_1st = block_right_2nd;
  158. block_right_2nd = block;
  159. block_right_offset = 0;
  160. __enable_irq();
  161. release(tmp);
  162. }
  163. }
  164. }
  165. void AudioOutputMQS::config_i2s(void)
  166. {
  167. CCM_CCGR5 |= CCM_CCGR5_SAI3(CCM_CCGR_ON);
  168. CCM_CCGR0 |= CCM_CCGR0_MQS_HMCLK(CCM_CCGR_ON);
  169. //PLL:
  170. //TODO: Check if frequencies are correct!
  171. int fs = AUDIO_SAMPLE_RATE_EXACT;
  172. int oversample = 64*8;
  173. // PLL between 27*24 = 648MHz und 54*24=1296MHz
  174. int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
  175. int n2 = 1 + (24000000 * 27) / (fs * oversample * n1);
  176. double C = ((double)fs * oversample * n1 * n2) / 24000000;
  177. int c0 = C;
  178. int c2 = 10000;
  179. int c1 = C * c2 - (c0 * c2);
  180. set_audioClock(c0, c1, c2);
  181. CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI3_CLK_SEL_MASK))
  182. | CCM_CSCMR1_SAI3_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4,
  183. CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI3_CLK_PRED_MASK | CCM_CS1CDR_SAI3_CLK_PODF_MASK))
  184. | CCM_CS1CDR_SAI3_CLK_PRED(n1-1)
  185. | CCM_CS1CDR_SAI3_CLK_PODF(n2-1);
  186. IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI3_MCLK3_SEL_MASK))
  187. | (IOMUXC_GPR_GPR1_SAI3_MCLK_DIR | IOMUXC_GPR_GPR1_SAI3_MCLK3_SEL(0)); //Select MCLK
  188. IOMUXC_GPR_GPR2 = (IOMUXC_GPR_GPR2 & ~(IOMUXC_GPR_GPR2_MQS_OVERSAMPLE | IOMUXC_GPR_GPR2_MQS_CLK_DIV_MASK))
  189. | IOMUXC_GPR_GPR2_MQS_EN | IOMUXC_GPR_GPR2_MQS_OVERSAMPLE | IOMUXC_GPR_GPR2_MQS_CLK_DIV(0);
  190. if (I2S3_TCSR & I2S_TCSR_TE) return;
  191. I2S3_TMR = 0;
  192. // I2S3_TCSR = (1<<25); //Reset
  193. I2S3_TCR1 = I2S_TCR1_RFW(1);
  194. I2S3_TCR2 = I2S_TCR2_SYNC(0) /*| I2S_TCR2_BCP*/ // sync=0; tx is async;
  195. | (I2S_TCR2_BCD | I2S_TCR2_DIV((7)) | I2S_TCR2_MSEL(1));
  196. I2S3_TCR3 = I2S_TCR3_TCE;
  197. I2S3_TCR4 = I2S_TCR4_FRSZ((2-1)) | I2S_TCR4_SYWD((16-1)) | I2S_TCR4_MF | I2S_TCR4_FSD /*| I2S_TCR4_FSE*/ /* | I2S_TCR4_FSP */;
  198. I2S3_TCR5 = I2S_TCR5_WNW((16-1)) | I2S_TCR5_W0W((16-1)) | I2S_TCR5_FBT((16-1));
  199. }
  200. #endif //defined(__IMXRT1062__)