Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

146 lines
4.5KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2017, 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 "input_tdm.h"
  27. #include "output_tdm.h"
  28. #if defined(KINETISK)
  29. DMAMEM static uint32_t tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*16];
  30. audio_block_t * AudioInputTDM::block_incoming[16] = {
  31. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  32. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  33. };
  34. bool AudioInputTDM::update_responsibility = false;
  35. DMAChannel AudioInputTDM::dma(false);
  36. void AudioInputTDM::begin(void)
  37. {
  38. dma.begin(true); // Allocate the DMA channel first
  39. // TODO: should we set & clear the I2S_RCSR_SR bit here?
  40. AudioOutputTDM::config_tdm();
  41. CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
  42. dma.TCD->SADDR = &I2S0_RDR0;
  43. dma.TCD->SOFF = 0;
  44. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
  45. dma.TCD->NBYTES_MLNO = 4;
  46. dma.TCD->SLAST = 0;
  47. dma.TCD->DADDR = tdm_rx_buffer;
  48. dma.TCD->DOFF = 4;
  49. dma.TCD->CITER_ELINKNO = sizeof(tdm_rx_buffer) / 4;
  50. dma.TCD->DLASTSGA = -sizeof(tdm_rx_buffer);
  51. dma.TCD->BITER_ELINKNO = sizeof(tdm_rx_buffer) / 4;
  52. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  53. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_I2S0_RX);
  54. update_responsibility = update_setup();
  55. dma.enable();
  56. I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  57. I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX
  58. dma.attachInterrupt(isr);
  59. }
  60. // TODO: needs optimization...
  61. static void memcpy_tdm_rx(uint32_t *dest1, uint32_t *dest2, const uint32_t *src)
  62. {
  63. uint32_t i, in1, in2;
  64. for (i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  65. in1 = *src;
  66. in2 = *(src+8);
  67. src += 16;
  68. *dest1++ = (in1 >> 16) | (in2 & 0xFFFF0000);
  69. *dest2++ = (in1 << 16) | (in2 & 0x0000FFFF);
  70. }
  71. }
  72. void AudioInputTDM::isr(void)
  73. {
  74. uint32_t daddr;
  75. const uint32_t *src;
  76. unsigned int i;
  77. //digitalWriteFast(3, HIGH);
  78. daddr = (uint32_t)(dma.TCD->DADDR);
  79. dma.clearInterrupt();
  80. if (daddr < (uint32_t)tdm_rx_buffer + sizeof(tdm_rx_buffer) / 2) {
  81. // DMA is receiving to the first half of the buffer
  82. // need to remove data from the second half
  83. src = &tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*8];
  84. } else {
  85. // DMA is receiving to the second half of the buffer
  86. // need to remove data from the first half
  87. src = &tdm_rx_buffer[0];
  88. }
  89. if (block_incoming[0] != NULL) {
  90. for (i=0; i < 16; i += 2) {
  91. uint32_t *dest1 = (uint32_t *)(block_incoming[i]->data);
  92. uint32_t *dest2 = (uint32_t *)(block_incoming[i+1]->data);
  93. memcpy_tdm_rx(dest1, dest2, src);
  94. src++;
  95. }
  96. }
  97. if (update_responsibility) update_all();
  98. //digitalWriteFast(3, LOW);
  99. }
  100. void AudioInputTDM::update(void)
  101. {
  102. unsigned int i, j;
  103. audio_block_t *new_block[16];
  104. audio_block_t *out_block[16];
  105. // allocate 16 new blocks. If any fails, allocate none
  106. for (i=0; i < 16; i++) {
  107. new_block[i] = allocate();
  108. if (new_block[i] == NULL) {
  109. for (j=0; j < i; j++) {
  110. release(new_block[j]);
  111. }
  112. memset(new_block, 0, sizeof(new_block));
  113. break;
  114. }
  115. }
  116. __disable_irq();
  117. memcpy(out_block, block_incoming, sizeof(out_block));
  118. memcpy(block_incoming, new_block, sizeof(block_incoming));
  119. __enable_irq();
  120. if (out_block[0] != NULL) {
  121. // if we got 1 block, all 16 are filled
  122. for (i=0; i < 16; i++) {
  123. transmit(out_block[i], i);
  124. release(out_block[i]);
  125. }
  126. }
  127. }
  128. #endif // KINETISK