input_i2s_oct.cpp 7.4KB

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