input_tdm2.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #if defined(__IMXRT1062__)
  27. #include <Arduino.h>
  28. #include "input_tdm2.h"
  29. #include "output_tdm2.h"
  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 * AudioInputTDM2::block_incoming[16] = {
  34. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
  35. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
  36. };
  37. bool AudioInputTDM2::update_responsibility = false;
  38. DMAChannel AudioInputTDM2::dma(false);
  39. void AudioInputTDM2::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. AudioOutputTDM2::config_tdm();
  44. CORE_PIN5_CONFIG = 2; //2:RX_DATA0
  45. IOMUXC_SAI2_RX_DATA0_SELECT_INPUT = 0;
  46. dma.TCD->SADDR = &I2S2_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_SAI2_RX);
  58. update_responsibility = update_setup();
  59. dma.enable();
  60. I2S2_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  61. I2S2_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE;
  62. dma.attachInterrupt(isr);
  63. }
  64. // TODO: needs optimization...
  65. static void memcpy_tdm_rx(uint32_t *dest1, uint32_t *dest2, const uint32_t *src)
  66. {
  67. uint32_t i, in1, in2;
  68. for (i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  69. in1 = *src;
  70. in2 = *(src+8);
  71. src += 16;
  72. *dest1++ = (in1 >> 16) | (in2 & 0xFFFF0000);
  73. *dest2++ = (in1 << 16) | (in2 & 0x0000FFFF);
  74. }
  75. }
  76. void AudioInputTDM2::isr(void)
  77. {
  78. uint32_t daddr;
  79. const uint32_t *src;
  80. unsigned int i;
  81. daddr = (uint32_t)(dma.TCD->DADDR);
  82. dma.clearInterrupt();
  83. if (daddr < (uint32_t)tdm_rx_buffer + sizeof(tdm_rx_buffer) / 2) {
  84. // DMA is receiving to the first half of the buffer
  85. // need to remove data from the second half
  86. src = &tdm_rx_buffer[AUDIO_BLOCK_SAMPLES*8];
  87. } else {
  88. // DMA is receiving to the second half of the buffer
  89. // need to remove data from the first half
  90. src = &tdm_rx_buffer[0];
  91. }
  92. if (block_incoming[0] != nullptr) {
  93. #if IMXRT_CACHE_ENABLED >=1
  94. arm_dcache_delete((void*)src, sizeof(tdm_rx_buffer) / 2);
  95. #endif
  96. for (i=0; i < 16; i += 2) {
  97. uint32_t *dest1 = (uint32_t *)(block_incoming[i]->data);
  98. uint32_t *dest2 = (uint32_t *)(block_incoming[i+1]->data);
  99. memcpy_tdm_rx(dest1, dest2, src);
  100. src++;
  101. }
  102. }
  103. if (update_responsibility) update_all();
  104. }
  105. void AudioInputTDM2::update(void)
  106. {
  107. unsigned int i, j;
  108. audio_block_t *new_block[16];
  109. audio_block_t *out_block[16];
  110. // allocate 16 new blocks. If any fails, allocate none
  111. for (i=0; i < 16; i++) {
  112. new_block[i] = allocate();
  113. if (new_block[i] == nullptr) {
  114. for (j=0; j < i; j++) {
  115. release(new_block[j]);
  116. }
  117. memset(new_block, 0, sizeof(new_block));
  118. break;
  119. }
  120. }
  121. __disable_irq();
  122. memcpy(out_block, block_incoming, sizeof(out_block));
  123. memcpy(block_incoming, new_block, sizeof(block_incoming));
  124. __enable_irq();
  125. if (out_block[0] != nullptr) {
  126. // if we got 1 block, all 16 are filled
  127. for (i=0; i < 16; i++) {
  128. transmit(out_block[i], i);
  129. release(out_block[i]);
  130. }
  131. }
  132. }
  133. #endif