Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

input_tdm.cpp 5.5KB

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