PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

output_i2s_hex.cpp 9.6KB

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 "output_i2s_hex.h"
  28. #include "output_i2s.h"
  29. #include "memcpy_audio.h"
  30. #if defined(__IMXRT1062__)
  31. #include "utility/imxrt_hw.h"
  32. audio_block_t * AudioOutputI2SHex::block_ch1_1st = NULL;
  33. audio_block_t * AudioOutputI2SHex::block_ch2_1st = NULL;
  34. audio_block_t * AudioOutputI2SHex::block_ch3_1st = NULL;
  35. audio_block_t * AudioOutputI2SHex::block_ch4_1st = NULL;
  36. audio_block_t * AudioOutputI2SHex::block_ch5_1st = NULL;
  37. audio_block_t * AudioOutputI2SHex::block_ch6_1st = NULL;
  38. audio_block_t * AudioOutputI2SHex::block_ch1_2nd = NULL;
  39. audio_block_t * AudioOutputI2SHex::block_ch2_2nd = NULL;
  40. audio_block_t * AudioOutputI2SHex::block_ch3_2nd = NULL;
  41. audio_block_t * AudioOutputI2SHex::block_ch4_2nd = NULL;
  42. audio_block_t * AudioOutputI2SHex::block_ch5_2nd = NULL;
  43. audio_block_t * AudioOutputI2SHex::block_ch6_2nd = NULL;
  44. uint16_t AudioOutputI2SHex::ch1_offset = 0;
  45. uint16_t AudioOutputI2SHex::ch2_offset = 0;
  46. uint16_t AudioOutputI2SHex::ch3_offset = 0;
  47. uint16_t AudioOutputI2SHex::ch4_offset = 0;
  48. uint16_t AudioOutputI2SHex::ch5_offset = 0;
  49. uint16_t AudioOutputI2SHex::ch6_offset = 0;
  50. bool AudioOutputI2SHex::update_responsibility = false;
  51. DMAMEM __attribute__((aligned(32))) static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES*3];
  52. DMAChannel AudioOutputI2SHex::dma(false);
  53. static const uint32_t zerodata[AUDIO_BLOCK_SAMPLES/4] = {0};
  54. void AudioOutputI2SHex::begin(void)
  55. {
  56. dma.begin(true); // Allocate the DMA channel first
  57. block_ch1_1st = NULL;
  58. block_ch2_1st = NULL;
  59. block_ch3_1st = NULL;
  60. block_ch4_1st = NULL;
  61. block_ch5_1st = NULL;
  62. block_ch6_1st = NULL;
  63. const int pinoffset = 0; // TODO: make this configurable...
  64. memset(i2s_tx_buffer, 0, sizeof(i2s_tx_buffer));
  65. AudioOutputI2S::config_i2s();
  66. I2S1_TCR3 = I2S_TCR3_TCE_3CH << pinoffset;
  67. switch (pinoffset) {
  68. case 0:
  69. CORE_PIN7_CONFIG = 3;
  70. CORE_PIN32_CONFIG = 3;
  71. CORE_PIN9_CONFIG = 3;
  72. break;
  73. case 1:
  74. CORE_PIN32_CONFIG = 3;
  75. CORE_PIN9_CONFIG = 3;
  76. CORE_PIN6_CONFIG = 3;
  77. }
  78. dma.TCD->SADDR = i2s_tx_buffer;
  79. dma.TCD->SOFF = 2;
  80. dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(1) | DMA_TCD_ATTR_DSIZE(1);
  81. dma.TCD->NBYTES_MLOFFYES = DMA_TCD_NBYTES_DMLOE |
  82. DMA_TCD_NBYTES_MLOFFYES_MLOFF(-12) |
  83. DMA_TCD_NBYTES_MLOFFYES_NBYTES(6);
  84. dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
  85. dma.TCD->DADDR = (void *)((uint32_t)&I2S1_TDR0 + 2 + pinoffset * 4);
  86. dma.TCD->DOFF = 4;
  87. dma.TCD->CITER_ELINKNO = AUDIO_BLOCK_SAMPLES * 2;
  88. dma.TCD->DLASTSGA = -12;
  89. dma.TCD->BITER_ELINKNO = AUDIO_BLOCK_SAMPLES * 2;
  90. dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
  91. dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
  92. dma.enable();
  93. I2S1_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE;
  94. I2S1_TCSR = I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
  95. I2S1_TCR3 = I2S_TCR3_TCE_3CH << pinoffset;
  96. update_responsibility = update_setup();
  97. dma.attachInterrupt(isr);
  98. }
  99. void AudioOutputI2SHex::isr(void)
  100. {
  101. uint32_t saddr;
  102. const int16_t *src1, *src2, *src3, *src4, *src5, *src6;
  103. const int16_t *zeros = (const int16_t *)zerodata;
  104. int16_t *dest;
  105. saddr = (uint32_t)(dma.TCD->SADDR);
  106. dma.clearInterrupt();
  107. if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
  108. // DMA is transmitting the first half of the buffer
  109. // so we must fill the second half
  110. dest = (int16_t *)((uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2);
  111. if (update_responsibility) update_all();
  112. } else {
  113. dest = (int16_t *)i2s_tx_buffer;
  114. }
  115. src1 = (block_ch1_1st) ? block_ch1_1st->data + ch1_offset : zeros;
  116. src2 = (block_ch2_1st) ? block_ch2_1st->data + ch2_offset : zeros;
  117. src3 = (block_ch3_1st) ? block_ch3_1st->data + ch3_offset : zeros;
  118. src4 = (block_ch4_1st) ? block_ch4_1st->data + ch4_offset : zeros;
  119. src5 = (block_ch5_1st) ? block_ch5_1st->data + ch5_offset : zeros;
  120. src6 = (block_ch6_1st) ? block_ch6_1st->data + ch6_offset : zeros;
  121. #if 0
  122. // TODO: optimized 6 channel copy...
  123. memcpy_tointerleaveQuad(dest, src1, src2, src3, src4);
  124. #else
  125. int16_t *p=dest;
  126. for (int i=0; i < AUDIO_BLOCK_SAMPLES/2; i++) {
  127. *p++ = *src1++;
  128. *p++ = *src3++;
  129. *p++ = *src5++;
  130. *p++ = *src2++;
  131. *p++ = *src4++;
  132. *p++ = *src6++;
  133. }
  134. #endif
  135. arm_dcache_flush_delete(dest, sizeof(i2s_tx_buffer) / 2);
  136. if (block_ch1_1st) {
  137. if (ch1_offset == 0) {
  138. ch1_offset = AUDIO_BLOCK_SAMPLES/2;
  139. } else {
  140. ch1_offset = 0;
  141. release(block_ch1_1st);
  142. block_ch1_1st = block_ch1_2nd;
  143. block_ch1_2nd = NULL;
  144. }
  145. }
  146. if (block_ch2_1st) {
  147. if (ch2_offset == 0) {
  148. ch2_offset = AUDIO_BLOCK_SAMPLES/2;
  149. } else {
  150. ch2_offset = 0;
  151. release(block_ch2_1st);
  152. block_ch2_1st = block_ch2_2nd;
  153. block_ch2_2nd = NULL;
  154. }
  155. }
  156. if (block_ch3_1st) {
  157. if (ch3_offset == 0) {
  158. ch3_offset = AUDIO_BLOCK_SAMPLES/2;
  159. } else {
  160. ch3_offset = 0;
  161. release(block_ch3_1st);
  162. block_ch3_1st = block_ch3_2nd;
  163. block_ch3_2nd = NULL;
  164. }
  165. }
  166. if (block_ch4_1st) {
  167. if (ch4_offset == 0) {
  168. ch4_offset = AUDIO_BLOCK_SAMPLES/2;
  169. } else {
  170. ch4_offset = 0;
  171. release(block_ch4_1st);
  172. block_ch4_1st = block_ch4_2nd;
  173. block_ch4_2nd = NULL;
  174. }
  175. }
  176. if (block_ch5_1st) {
  177. if (ch5_offset == 0) {
  178. ch5_offset = AUDIO_BLOCK_SAMPLES/2;
  179. } else {
  180. ch5_offset = 0;
  181. release(block_ch5_1st);
  182. block_ch5_1st = block_ch5_2nd;
  183. block_ch5_2nd = NULL;
  184. }
  185. }
  186. if (block_ch6_1st) {
  187. if (ch6_offset == 0) {
  188. ch6_offset = AUDIO_BLOCK_SAMPLES/2;
  189. } else {
  190. ch6_offset = 0;
  191. release(block_ch6_1st);
  192. block_ch6_1st = block_ch6_2nd;
  193. block_ch6_2nd = NULL;
  194. }
  195. }
  196. }
  197. void AudioOutputI2SHex::update(void)
  198. {
  199. audio_block_t *block, *tmp;
  200. block = receiveReadOnly(0); // channel 1
  201. if (block) {
  202. __disable_irq();
  203. if (block_ch1_1st == NULL) {
  204. block_ch1_1st = block;
  205. ch1_offset = 0;
  206. __enable_irq();
  207. } else if (block_ch1_2nd == NULL) {
  208. block_ch1_2nd = block;
  209. __enable_irq();
  210. } else {
  211. tmp = block_ch1_1st;
  212. block_ch1_1st = block_ch1_2nd;
  213. block_ch1_2nd = block;
  214. ch1_offset = 0;
  215. __enable_irq();
  216. release(tmp);
  217. }
  218. }
  219. block = receiveReadOnly(1); // channel 2
  220. if (block) {
  221. __disable_irq();
  222. if (block_ch2_1st == NULL) {
  223. block_ch2_1st = block;
  224. ch2_offset = 0;
  225. __enable_irq();
  226. } else if (block_ch2_2nd == NULL) {
  227. block_ch2_2nd = block;
  228. __enable_irq();
  229. } else {
  230. tmp = block_ch2_1st;
  231. block_ch2_1st = block_ch2_2nd;
  232. block_ch2_2nd = block;
  233. ch2_offset = 0;
  234. __enable_irq();
  235. release(tmp);
  236. }
  237. }
  238. block = receiveReadOnly(2); // channel 3
  239. if (block) {
  240. __disable_irq();
  241. if (block_ch3_1st == NULL) {
  242. block_ch3_1st = block;
  243. ch3_offset = 0;
  244. __enable_irq();
  245. } else if (block_ch3_2nd == NULL) {
  246. block_ch3_2nd = block;
  247. __enable_irq();
  248. } else {
  249. tmp = block_ch3_1st;
  250. block_ch3_1st = block_ch3_2nd;
  251. block_ch3_2nd = block;
  252. ch3_offset = 0;
  253. __enable_irq();
  254. release(tmp);
  255. }
  256. }
  257. block = receiveReadOnly(3); // channel 4
  258. if (block) {
  259. __disable_irq();
  260. if (block_ch4_1st == NULL) {
  261. block_ch4_1st = block;
  262. ch4_offset = 0;
  263. __enable_irq();
  264. } else if (block_ch4_2nd == NULL) {
  265. block_ch4_2nd = block;
  266. __enable_irq();
  267. } else {
  268. tmp = block_ch4_1st;
  269. block_ch4_1st = block_ch4_2nd;
  270. block_ch4_2nd = block;
  271. ch4_offset = 0;
  272. __enable_irq();
  273. release(tmp);
  274. }
  275. }
  276. block = receiveReadOnly(4); // channel 5
  277. if (block) {
  278. __disable_irq();
  279. if (block_ch5_1st == NULL) {
  280. block_ch5_1st = block;
  281. ch5_offset = 0;
  282. __enable_irq();
  283. } else if (block_ch5_2nd == NULL) {
  284. block_ch5_2nd = block;
  285. __enable_irq();
  286. } else {
  287. tmp = block_ch5_1st;
  288. block_ch5_1st = block_ch5_2nd;
  289. block_ch5_2nd = block;
  290. ch5_offset = 0;
  291. __enable_irq();
  292. release(tmp);
  293. }
  294. }
  295. block = receiveReadOnly(5); // channel 6
  296. if (block) {
  297. __disable_irq();
  298. if (block_ch6_1st == NULL) {
  299. block_ch6_1st = block;
  300. ch6_offset = 0;
  301. __enable_irq();
  302. } else if (block_ch6_2nd == NULL) {
  303. block_ch6_2nd = block;
  304. __enable_irq();
  305. } else {
  306. tmp = block_ch6_1st;
  307. block_ch6_1st = block_ch6_2nd;
  308. block_ch6_2nd = block;
  309. ch6_offset = 0;
  310. __enable_irq();
  311. release(tmp);
  312. }
  313. }
  314. }
  315. #else // not supported
  316. void AudioOutputI2SHex::begin(void)
  317. {
  318. }
  319. void AudioOutputI2SHex::update(void)
  320. {
  321. audio_block_t *block;
  322. block = receiveReadOnly(0);
  323. if (block) release(block);
  324. block = receiveReadOnly(1);
  325. if (block) release(block);
  326. block = receiveReadOnly(2);
  327. if (block) release(block);
  328. block = receiveReadOnly(3);
  329. if (block) release(block);
  330. block = receiveReadOnly(4);
  331. if (block) release(block);
  332. block = receiveReadOnly(5);
  333. if (block) release(block);
  334. }
  335. #endif