PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

input_i2s_hex.cpp 7.2KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, 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_i2s_hex.h"
  28. #include "output_i2s.h"
  29. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_rx_buffer[AUDIO_BLOCK_SAMPLES*3];
  30. audio_block_t * AudioInputI2SHex::block_ch1 = NULL;
  31. audio_block_t * AudioInputI2SHex::block_ch2 = NULL;
  32. audio_block_t * AudioInputI2SHex::block_ch3 = NULL;
  33. audio_block_t * AudioInputI2SHex::block_ch4 = NULL;
  34. audio_block_t * AudioInputI2SHex::block_ch5 = NULL;
  35. audio_block_t * AudioInputI2SHex::block_ch6 = NULL;
  36. uint16_t AudioInputI2SHex::block_offset = 0;
  37. bool AudioInputI2SHex::update_responsibility = false;
  38. DMAChannel AudioInputI2SHex::dma(false);
  39. #if defined(__IMXRT1062__)
  40. void AudioInputI2SHex::begin(void)
  41. {
  42. dma.begin(true); // Allocate the DMA channel first
  43. const int pinoffset = 0; // TODO: make this configurable...
  44. AudioOutputI2S::config_i2s();
  45. I2S1_RCR3 = I2S_RCR3_RCE_3CH << pinoffset;
  46. switch (pinoffset) {
  47. case 0:
  48. CORE_PIN8_CONFIG = 3;
  49. CORE_PIN6_CONFIG = 3;
  50. CORE_PIN9_CONFIG = 3;
  51. IOMUXC_SAI1_RX_DATA0_SELECT_INPUT = 2; // GPIO_B1_00_ALT3, pg 873
  52. IOMUXC_SAI1_RX_DATA1_SELECT_INPUT = 1; // GPIO_B0_10_ALT3, pg 873
  53. IOMUXC_SAI1_RX_DATA2_SELECT_INPUT = 1; // GPIO_B0_11_ALT3, pg 874
  54. break;
  55. case 1:
  56. CORE_PIN6_CONFIG = 3;
  57. CORE_PIN9_CONFIG = 3;
  58. CORE_PIN32_CONFIG = 3;
  59. IOMUXC_SAI1_RX_DATA1_SELECT_INPUT = 1; // GPIO_B0_10_ALT3, pg 873
  60. IOMUXC_SAI1_RX_DATA2_SELECT_INPUT = 1; // GPIO_B0_11_ALT3, pg 874
  61. IOMUXC_SAI1_RX_DATA3_SELECT_INPUT = 1; // GPIO_B0_12_ALT3, pg 875
  62. break;
  63. }
  64. dma.TCD->SADDR = (void *)((uint32_t)&I2S1_RDR0 + 2 + pinoffset * 4);
  65. dma.TCD->SOFF = 4;
  66. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  67. dma.TCD->NBYTES_MLOFFYES = DMA_TCD_NBYTES_SMLOE |
  68. DMA_TCD_NBYTES_MLOFFYES_MLOFF(-12) |
  69. DMA_TCD_NBYTES_MLOFFYES_NBYTES(6);
  70. dma.TCD->SLAST = -12;
  71. dma.TCD->DADDR = i2s_rx_buffer;
  72. dma.TCD->DOFF = 2;
  73. dma.TCD->CITER_ELINKNO = AUDIO_BLOCK_SAMPLES * 2;
  74. dma.TCD->DLASTSGA = -sizeof(i2s_rx_buffer);
  75. dma.TCD->BITER_ELINKNO = AUDIO_BLOCK_SAMPLES * 2;
  76. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  77. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_RX);
  78. //I2S1_RCSR = 0;
  79. //I2S1_RCR3 = I2S_RCR3_RCE_2CH << pinoffset;
  80. I2S1_RCSR = I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
  81. update_responsibility = update_setup();
  82. dma.enable();
  83. dma.attachInterrupt(isr);
  84. }
  85. void AudioInputI2SHex::isr(void)
  86. {
  87. uint32_t daddr, offset;
  88. const int16_t *src;
  89. int16_t *dest1, *dest2, *dest3, *dest4, *dest5, *dest6;
  90. //digitalWriteFast(3, HIGH);
  91. daddr = (uint32_t)(dma.TCD->DADDR);
  92. dma.clearInterrupt();
  93. if (daddr < (uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2) {
  94. // DMA is receiving to the first half of the buffer
  95. // need to remove data from the second half
  96. src = (int16_t *)((uint32_t)i2s_rx_buffer + sizeof(i2s_rx_buffer) / 2);
  97. if (update_responsibility) update_all();
  98. } else {
  99. // DMA is receiving to the second half of the buffer
  100. // need to remove data from the first half
  101. src = (int16_t *)&i2s_rx_buffer[0];
  102. }
  103. if (block_ch1) {
  104. offset = block_offset;
  105. if (offset <= AUDIO_BLOCK_SAMPLES/2) {
  106. arm_dcache_delete((void*)src, sizeof(i2s_rx_buffer) / 2);
  107. block_offset = offset + AUDIO_BLOCK_SAMPLES/2;
  108. dest1 = &(block_ch1->data[offset]);
  109. dest2 = &(block_ch2->data[offset]);
  110. dest3 = &(block_ch3->data[offset]);
  111. dest4 = &(block_ch4->data[offset]);
  112. dest5 = &(block_ch5->data[offset]);
  113. dest6 = &(block_ch6->data[offset]);
  114. for (int i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  115. *dest1++ = *src++;
  116. *dest3++ = *src++;
  117. *dest5++ = *src++;
  118. *dest2++ = *src++;
  119. *dest4++ = *src++;
  120. *dest6++ = *src++;
  121. }
  122. }
  123. }
  124. //digitalWriteFast(3, LOW);
  125. }
  126. void AudioInputI2SHex::update(void)
  127. {
  128. audio_block_t *new1, *new2, *new3, *new4, *new5, *new6;
  129. audio_block_t *out1, *out2, *out3, *out4, *out5, *out6;
  130. // allocate 6 new blocks
  131. new1 = allocate();
  132. new2 = allocate();
  133. new3 = allocate();
  134. new4 = allocate();
  135. new5 = allocate();
  136. new6 = allocate();
  137. // but if any fails, allocate none
  138. if (!new1 || !new2 || !new3 || !new4 || !new5 || !new6) {
  139. if (new1) {
  140. release(new1);
  141. new1 = NULL;
  142. }
  143. if (new2) {
  144. release(new2);
  145. new2 = NULL;
  146. }
  147. if (new3) {
  148. release(new3);
  149. new3 = NULL;
  150. }
  151. if (new4) {
  152. release(new4);
  153. new4 = NULL;
  154. }
  155. if (new5) {
  156. release(new5);
  157. new5 = NULL;
  158. }
  159. if (new6) {
  160. release(new6);
  161. new6 = NULL;
  162. }
  163. }
  164. __disable_irq();
  165. if (block_offset >= AUDIO_BLOCK_SAMPLES) {
  166. // the DMA filled 4 blocks, so grab them and get the
  167. // 4 new blocks to the DMA, as quickly as possible
  168. out1 = block_ch1;
  169. block_ch1 = new1;
  170. out2 = block_ch2;
  171. block_ch2 = new2;
  172. out3 = block_ch3;
  173. block_ch3 = new3;
  174. out4 = block_ch4;
  175. block_ch4 = new4;
  176. out5 = block_ch5;
  177. block_ch5 = new5;
  178. out6 = block_ch6;
  179. block_ch6 = new6;
  180. block_offset = 0;
  181. __enable_irq();
  182. // then transmit the DMA's former blocks
  183. transmit(out1, 0);
  184. release(out1);
  185. transmit(out2, 1);
  186. release(out2);
  187. transmit(out3, 2);
  188. release(out3);
  189. transmit(out4, 3);
  190. release(out4);
  191. transmit(out5, 4);
  192. release(out5);
  193. transmit(out6, 5);
  194. release(out6);
  195. } else if (new1 != NULL) {
  196. // the DMA didn't fill blocks, but we allocated blocks
  197. if (block_ch1 == NULL) {
  198. // the DMA doesn't have any blocks to fill, so
  199. // give it the ones we just allocated
  200. block_ch1 = new1;
  201. block_ch2 = new2;
  202. block_ch3 = new3;
  203. block_ch4 = new4;
  204. block_ch5 = new5;
  205. block_ch6 = new6;
  206. block_offset = 0;
  207. __enable_irq();
  208. } else {
  209. // the DMA already has blocks, doesn't need these
  210. __enable_irq();
  211. release(new1);
  212. release(new2);
  213. release(new3);
  214. release(new4);
  215. release(new5);
  216. release(new6);
  217. }
  218. } else {
  219. // The DMA didn't fill blocks, and we could not allocate
  220. // memory... the system is likely starving for memory!
  221. // Sadly, there's nothing we can do.
  222. __enable_irq();
  223. }
  224. }
  225. #else // not supported
  226. void AudioInputI2SHex::begin(void)
  227. {
  228. }
  229. #endif