No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

input_tdm.cpp 5.5KB

hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
hace 7 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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(__IMXRT1052__) || 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. #else
  64. CORE_PIN7_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. I2S1_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE;
  82. dma.attachInterrupt(isr);
  83. #endif
  84. }
  85. // TODO: needs optimization...
  86. static void memcpy_tdm_rx(uint32_t *dest1, uint32_t *dest2, const uint32_t *src)
  87. {
  88. uint32_t i, in1, in2;
  89. for (i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  90. in1 = *src;
  91. in2 = *(src+8);
  92. src += 16;
  93. *dest1++ = (in1 >> 16) | (in2 & 0xFFFF0000);
  94. *dest2++ = (in1 << 16) | (in2 & 0x0000FFFF);
  95. }
  96. }
  97. void AudioInputTDM::isr(void)
  98. {
  99. uint32_t daddr;
  100. const uint32_t *src;
  101. unsigned int i;
  102. daddr = (uint32_t)(dma.TCD->DADDR);
  103. dma.clearInterrupt();
  104. if (daddr < (uint32_t)tdm_rx_buffer + sizeof(tdm_rx_buffer) / 2) {
  105. // DMA is receiving to the first half of the buffer
  106. // need to remove data from the second half
  107. src = &tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*8];
  108. } else {
  109. // DMA is receiving to the second half of the buffer
  110. // need to remove data from the first half
  111. src = &tdm_rx_buffer[0];
  112. }
  113. if (block_incoming[0] != nullptr) {
  114. #if IMXRT_CACHE_ENABLED >=1
  115. arm_dcache_delete((void*)src, sizeof(tdm_rx_buffer) / 2);
  116. #endif
  117. for (i=0; i < 16; i += 2) {
  118. uint32_t *dest1 = (uint32_t *)(block_incoming[i]->data);
  119. uint32_t *dest2 = (uint32_t *)(block_incoming[i+1]->data);
  120. memcpy_tdm_rx(dest1, dest2, src);
  121. src++;
  122. }
  123. }
  124. if (update_responsibility) update_all();
  125. }
  126. void AudioInputTDM::update(void)
  127. {
  128. unsigned int i, j;
  129. audio_block_t *new_block[16];
  130. audio_block_t *out_block[16];
  131. // allocate 16 new blocks. If any fails, allocate none
  132. for (i=0; i < 16; i++) {
  133. new_block[i] = allocate();
  134. if (new_block[i] == nullptr) {
  135. for (j=0; j < i; j++) {
  136. release(new_block[j]);
  137. }
  138. memset(new_block, 0, sizeof(new_block));
  139. break;
  140. }
  141. }
  142. __disable_irq();
  143. memcpy(out_block, block_incoming, sizeof(out_block));
  144. memcpy(block_incoming, new_block, sizeof(block_incoming));
  145. __enable_irq();
  146. if (out_block[0] != nullptr) {
  147. // if we got 1 block, all 16 are filled
  148. for (i=0; i < 16; i++) {
  149. transmit(out_block[i], i);
  150. release(out_block[i]);
  151. }
  152. }
  153. }
  154. #endif