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.

301 lines
8.6KB

  1. #include "output_i2s.h"
  2. // MCLK needs to be 48e6 / 1088 * 256 = 11.29411765 MHz -> 44.117647 kHz sample rate
  3. // Possible to create using fractional divider for all USB-compatible Kinetis:
  4. // MCLK = 16e6 * 12 / 17
  5. // MCLK = 24e6 * 8 / 17
  6. // MCLK = 48e6 * 4 / 17
  7. // MCLK = 72e6 * 8 / 51
  8. // MCLK = 96e6 * 2 / 17
  9. // MCLK = 120e6 * 8 / 85
  10. // TODO: instigate using I2S0_MCR to select the crystal directly instead of the system
  11. // clock, which has audio band jitter from the PLL
  12. audio_block_t * AudioOutputI2S::block_left_1st = NULL;
  13. audio_block_t * AudioOutputI2S::block_right_1st = NULL;
  14. audio_block_t * AudioOutputI2S::block_left_2nd = NULL;
  15. audio_block_t * AudioOutputI2S::block_right_2nd = NULL;
  16. uint16_t AudioOutputI2S::block_left_offset = 0;
  17. uint16_t AudioOutputI2S::block_right_offset = 0;
  18. bool AudioOutputI2S::update_responsibility = false;
  19. DMAMEM static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  20. void AudioOutputI2S::begin(void)
  21. {
  22. //pinMode(2, OUTPUT);
  23. block_left_1st = NULL;
  24. block_right_1st = NULL;
  25. config_i2s();
  26. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  27. DMA_CR = 0;
  28. DMA_TCD0_SADDR = i2s_tx_buffer;
  29. DMA_TCD0_SOFF = 2;
  30. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  31. DMA_TCD0_NBYTES_MLNO = 2;
  32. DMA_TCD0_SLAST = -sizeof(i2s_tx_buffer);
  33. DMA_TCD0_DADDR = &I2S0_TDR0;
  34. DMA_TCD0_DOFF = 0;
  35. DMA_TCD0_CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  36. DMA_TCD0_DLASTSGA = 0;
  37. DMA_TCD0_BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  38. DMA_TCD0_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  39. DMAMUX0_CHCFG0 = DMAMUX_DISABLE;
  40. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_I2S0_TX | DMAMUX_ENABLE;
  41. update_responsibility = update_setup();
  42. DMA_SERQ = 0;
  43. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  44. NVIC_ENABLE_IRQ(IRQ_DMA_CH0);
  45. }
  46. void dma_ch0_isr(void)
  47. {
  48. const int16_t *src, *end;
  49. int16_t *dest;
  50. audio_block_t *block;
  51. uint32_t saddr, offset;
  52. saddr = (uint32_t)DMA_TCD0_SADDR;
  53. DMA_CINT = 0;
  54. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  55. // DMA is transmitting the first half of the buffer
  56. // so we must fill the second half
  57. dest = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  58. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
  59. if (AudioOutputI2S::update_responsibility) AudioStream::update_all();
  60. } else {
  61. // DMA is transmitting the second half of the buffer
  62. // so we must fill the first half
  63. dest = (int16_t *)i2s_tx_buffer;
  64. end = (int16_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES/2];
  65. }
  66. // TODO: these copy routines could be merged and optimized, maybe in assembly?
  67. block = AudioOutputI2S::block_left_1st;
  68. if (block) {
  69. offset = AudioOutputI2S::block_left_offset;
  70. src = &block->data[offset];
  71. do {
  72. *dest = *src++;
  73. dest += 2;
  74. } while (dest < end);
  75. offset += AUDIO_BLOCK_SAMPLES/2;
  76. if (offset < AUDIO_BLOCK_SAMPLES) {
  77. AudioOutputI2S::block_left_offset = offset;
  78. } else {
  79. AudioOutputI2S::block_left_offset = 0;
  80. AudioStream::release(block);
  81. AudioOutputI2S::block_left_1st = AudioOutputI2S::block_left_2nd;
  82. AudioOutputI2S::block_left_2nd = NULL;
  83. }
  84. } else {
  85. do {
  86. *dest = 0;
  87. dest += 2;
  88. } while (dest < end);
  89. }
  90. dest -= AUDIO_BLOCK_SAMPLES - 1;
  91. block = AudioOutputI2S::block_right_1st;
  92. if (block) {
  93. offset = AudioOutputI2S::block_right_offset;
  94. src = &block->data[offset];
  95. do {
  96. *dest = *src++;
  97. dest += 2;
  98. } while (dest < end);
  99. offset += AUDIO_BLOCK_SAMPLES/2;
  100. if (offset < AUDIO_BLOCK_SAMPLES) {
  101. AudioOutputI2S::block_right_offset = offset;
  102. } else {
  103. AudioOutputI2S::block_right_offset = 0;
  104. AudioStream::release(block);
  105. AudioOutputI2S::block_right_1st = AudioOutputI2S::block_right_2nd;
  106. AudioOutputI2S::block_right_2nd = NULL;
  107. }
  108. } else {
  109. do {
  110. *dest = 0;
  111. dest += 2;
  112. } while (dest < end);
  113. }
  114. }
  115. void AudioOutputI2S::update(void)
  116. {
  117. // null audio device: discard all incoming data
  118. //if (!active) return;
  119. //audio_block_t *block = receiveReadOnly();
  120. //if (block) release(block);
  121. audio_block_t *block;
  122. block = receiveReadOnly(0); // input 0 = left channel
  123. if (block) {
  124. __disable_irq();
  125. if (block_left_1st == NULL) {
  126. block_left_1st = block;
  127. block_left_offset = 0;
  128. __enable_irq();
  129. } else if (block_left_2nd == NULL) {
  130. block_left_2nd = block;
  131. __enable_irq();
  132. } else {
  133. audio_block_t *tmp = block_left_1st;
  134. block_left_1st = block_left_2nd;
  135. block_left_2nd = block;
  136. block_left_offset = 0;
  137. __enable_irq();
  138. release(tmp);
  139. }
  140. }
  141. block = receiveReadOnly(1); // input 1 = right channel
  142. if (block) {
  143. __disable_irq();
  144. if (block_right_1st == NULL) {
  145. block_right_1st = block;
  146. block_right_offset = 0;
  147. __enable_irq();
  148. } else if (block_right_2nd == NULL) {
  149. block_right_2nd = block;
  150. __enable_irq();
  151. } else {
  152. audio_block_t *tmp = block_right_1st;
  153. block_right_1st = block_right_2nd;
  154. block_right_2nd = block;
  155. block_right_offset = 0;
  156. __enable_irq();
  157. release(tmp);
  158. }
  159. }
  160. }
  161. void AudioOutputI2S::config_i2s(void)
  162. {
  163. SIM_SCGC6 |= SIM_SCGC6_I2S;
  164. SIM_SCGC7 |= SIM_SCGC7_DMA;
  165. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  166. // if either transmitter or receiver is enabled, do nothing
  167. if (I2S0_TCSR & I2S_TCSR_TE) return;
  168. if (I2S0_RCSR & I2S_RCSR_RE) return;
  169. // enable MCLK output
  170. I2S0_MCR = I2S_MCR_MICS(3) | I2S_MCR_MOE;
  171. I2S0_MDR = I2S_MDR_FRACT(1) | I2S_MDR_DIVIDE(16);
  172. // configure transmitter
  173. I2S0_TMR = 0;
  174. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  175. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1)
  176. | I2S_TCR2_BCD | I2S_TCR2_DIV(3);
  177. I2S0_TCR3 = I2S_TCR3_TCE;
  178. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  179. | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
  180. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  181. // configure receiver (sync'd to transmitter clocks)
  182. I2S0_RMR = 0;
  183. I2S0_RCR1 = I2S_RCR1_RFW(1);
  184. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP | I2S_RCR2_MSEL(1)
  185. | I2S_RCR2_BCD | I2S_RCR2_DIV(3);
  186. I2S0_RCR3 = I2S_RCR3_RCE;
  187. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  188. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  189. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  190. // configure pin mux for 3 clock signals
  191. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  192. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  193. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  194. }
  195. /******************************************************************/
  196. void AudioOutputI2Sslave::begin(void)
  197. {
  198. //pinMode(2, OUTPUT);
  199. block_left_1st = NULL;
  200. block_right_1st = NULL;
  201. AudioOutputI2Sslave::config_i2s();
  202. CORE_PIN22_CONFIG = PORT_PCR_MUX(6); // pin 22, PTC1, I2S0_TXD0
  203. DMA_CR = 0;
  204. DMA_TCD0_SADDR = i2s_tx_buffer;
  205. DMA_TCD0_SOFF = 2;
  206. DMA_TCD0_ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  207. DMA_TCD0_NBYTES_MLNO = 2;
  208. DMA_TCD0_SLAST = -sizeof(i2s_tx_buffer);
  209. DMA_TCD0_DADDR = &I2S0_TDR0;
  210. DMA_TCD0_DOFF = 0;
  211. DMA_TCD0_CITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  212. DMA_TCD0_DLASTSGA = 0;
  213. DMA_TCD0_BITER_ELINKNO = sizeof(i2s_tx_buffer) / 2;
  214. DMA_TCD0_CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  215. DMAMUX0_CHCFG0 = DMAMUX_DISABLE;
  216. DMAMUX0_CHCFG0 = DMAMUX_SOURCE_I2S0_TX | DMAMUX_ENABLE;
  217. update_responsibility = update_setup();
  218. DMA_SERQ = 0;
  219. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE | I2S_TCSR_FR;
  220. NVIC_ENABLE_IRQ(IRQ_DMA_CH0);
  221. }
  222. void AudioOutputI2Sslave::config_i2s(void)
  223. {
  224. SIM_SCGC6 |= SIM_SCGC6_I2S;
  225. SIM_SCGC7 |= SIM_SCGC7_DMA;
  226. SIM_SCGC6 |= SIM_SCGC6_DMAMUX;
  227. // if either transmitter or receiver is enabled, do nothing
  228. if (I2S0_TCSR & I2S_TCSR_TE) return;
  229. if (I2S0_RCSR & I2S_RCSR_RE) return;
  230. // Select input clock 0
  231. // Configure to input the bit-clock from pin, bypasses the MCLK divider
  232. I2S0_MCR = I2S_MCR_MICS(0);
  233. I2S0_MDR = 0;
  234. // configure transmitter
  235. I2S0_TMR = 0;
  236. I2S0_TCR1 = I2S_TCR1_TFW(1); // watermark at half fifo size
  237. I2S0_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP;
  238. I2S0_TCR3 = I2S_TCR3_TCE;
  239. I2S0_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(15) | I2S_TCR4_MF
  240. | I2S_TCR4_FSE | I2S_TCR4_FSP;
  241. I2S0_TCR5 = I2S_TCR5_WNW(15) | I2S_TCR5_W0W(15) | I2S_TCR5_FBT(15);
  242. // configure receiver (sync'd to transmitter clocks)
  243. I2S0_RMR = 0;
  244. I2S0_RCR1 = I2S_RCR1_RFW(1);
  245. I2S0_RCR2 = I2S_RCR2_SYNC(1) | I2S_TCR2_BCP;
  246. I2S0_RCR3 = I2S_RCR3_RCE;
  247. I2S0_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(15) | I2S_RCR4_MF
  248. | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
  249. I2S0_RCR5 = I2S_RCR5_WNW(15) | I2S_RCR5_W0W(15) | I2S_RCR5_FBT(15);
  250. // configure pin mux for 3 clock signals
  251. CORE_PIN23_CONFIG = PORT_PCR_MUX(6); // pin 23, PTC2, I2S0_TX_FS (LRCLK)
  252. CORE_PIN9_CONFIG = PORT_PCR_MUX(6); // pin 9, PTC3, I2S0_TX_BCLK
  253. CORE_PIN11_CONFIG = PORT_PCR_MUX(6); // pin 11, PTC6, I2S0_MCLK
  254. }