Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

input_tdm.cpp 4.6KB

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