PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AudioStream.cpp 9.3KB

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include <Arduino.h>
  31. #include "AudioStream.h"
  32. #if defined(__MKL26Z64__)
  33. #define MAX_AUDIO_MEMORY 6144
  34. #elif defined(__MK20DX128__)
  35. #define MAX_AUDIO_MEMORY 12288
  36. #elif defined(__MK20DX256__)
  37. #define MAX_AUDIO_MEMORY 49152
  38. #elif defined(__MK64FX512__)
  39. #define MAX_AUDIO_MEMORY 163840
  40. #elif defined(__MK66FX1M0__)
  41. #define MAX_AUDIO_MEMORY 229376
  42. #endif
  43. #define NUM_MASKS (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)
  44. audio_block_t * AudioStream::memory_pool;
  45. uint32_t AudioStream::memory_pool_available_mask[NUM_MASKS];
  46. uint16_t AudioStream::memory_pool_first_mask;
  47. uint16_t AudioStream::cpu_cycles_total = 0;
  48. uint16_t AudioStream::cpu_cycles_total_max = 0;
  49. uint16_t AudioStream::memory_used = 0;
  50. uint16_t AudioStream::memory_used_max = 0;
  51. // Set up the pool of audio data blocks
  52. // placing them all onto the free list
  53. void AudioStream::initialize_memory(audio_block_t *data, unsigned int num)
  54. {
  55. unsigned int i;
  56. unsigned int maxnum = MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2;
  57. //Serial.println("AudioStream initialize_memory");
  58. //delay(10);
  59. if (num > maxnum) num = maxnum;
  60. __disable_irq();
  61. memory_pool = data;
  62. memory_pool_first_mask = 0;
  63. for (i=0; i < NUM_MASKS; i++) {
  64. memory_pool_available_mask[i] = 0;
  65. }
  66. for (i=0; i < num; i++) {
  67. memory_pool_available_mask[i >> 5] |= (1 << (i & 0x1F));
  68. }
  69. for (i=0; i < num; i++) {
  70. data[i].memory_pool_index = i;
  71. }
  72. __enable_irq();
  73. }
  74. // Allocate 1 audio data block. If successful
  75. // the caller is the only owner of this new block
  76. audio_block_t * AudioStream::allocate(void)
  77. {
  78. uint32_t n, index, avail;
  79. uint32_t *p, *end;
  80. audio_block_t *block;
  81. uint32_t used;
  82. p = memory_pool_available_mask;
  83. end = p + NUM_MASKS;
  84. __disable_irq();
  85. index = memory_pool_first_mask;
  86. p += index;
  87. while (1) {
  88. if (p >= end) {
  89. __enable_irq();
  90. //Serial.println("alloc:null");
  91. return NULL;
  92. }
  93. avail = *p;
  94. if (avail) break;
  95. index++;
  96. p++;
  97. }
  98. n = __builtin_clz(avail);
  99. avail &= ~(0x80000000 >> n);
  100. *p = avail;
  101. if (!avail) index++;
  102. memory_pool_first_mask = index;
  103. used = memory_used + 1;
  104. memory_used = used;
  105. __enable_irq();
  106. index = p - memory_pool_available_mask;
  107. block = memory_pool + ((index << 5) + (31 - n));
  108. block->ref_count = 1;
  109. if (used > memory_used_max) memory_used_max = used;
  110. //Serial.print("alloc:");
  111. //Serial.println((uint32_t)block, HEX);
  112. return block;
  113. }
  114. // Release ownership of a data block. If no
  115. // other streams have ownership, the block is
  116. // returned to the free pool
  117. void AudioStream::release(audio_block_t *block)
  118. {
  119. //if (block == NULL) return;
  120. uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
  121. uint32_t index = block->memory_pool_index >> 5;
  122. __disable_irq();
  123. if (block->ref_count > 1) {
  124. block->ref_count--;
  125. } else {
  126. //Serial.print("reles:");
  127. //Serial.println((uint32_t)block, HEX);
  128. memory_pool_available_mask[index] |= mask;
  129. if (index < memory_pool_first_mask) memory_pool_first_mask = index;
  130. memory_used--;
  131. }
  132. __enable_irq();
  133. }
  134. // Transmit an audio data block
  135. // to all streams that connect to an output. The block
  136. // becomes owned by all the recepients, but also is still
  137. // owned by this object. Normally, a block must be released
  138. // by the caller after it's transmitted. This allows the
  139. // caller to transmit to same block to more than 1 output,
  140. // and then release it once after all transmit calls.
  141. void AudioStream::transmit(audio_block_t *block, unsigned char index)
  142. {
  143. for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
  144. if (c->src_index == index) {
  145. if (c->dst.inputQueue[c->dest_index] == NULL) {
  146. c->dst.inputQueue[c->dest_index] = block;
  147. block->ref_count++;
  148. }
  149. }
  150. }
  151. }
  152. // Receive block from an input. The block's data
  153. // may be shared with other streams, so it must not be written
  154. audio_block_t * AudioStream::receiveReadOnly(unsigned int index)
  155. {
  156. audio_block_t *in;
  157. if (index >= num_inputs) return NULL;
  158. in = inputQueue[index];
  159. inputQueue[index] = NULL;
  160. return in;
  161. }
  162. // Receive block from an input. The block will not
  163. // be shared, so its contents may be changed.
  164. audio_block_t * AudioStream::receiveWritable(unsigned int index)
  165. {
  166. audio_block_t *in, *p;
  167. if (index >= num_inputs) return NULL;
  168. in = inputQueue[index];
  169. inputQueue[index] = NULL;
  170. if (in && in->ref_count > 1) {
  171. p = allocate();
  172. if (p) memcpy(p->data, in->data, sizeof(p->data));
  173. in->ref_count--;
  174. in = p;
  175. }
  176. return in;
  177. }
  178. void AudioConnection::connect(void)
  179. {
  180. AudioConnection *p;
  181. if (isConnected) return;
  182. if (dest_index > dst.num_inputs) return;
  183. __disable_irq();
  184. p = src.destination_list;
  185. if (p == NULL) {
  186. src.destination_list = this;
  187. } else {
  188. while (p->next_dest) {
  189. if (&p->src == &this->src && &p->dst == &this->dst
  190. && p->src_index == this->src_index && p->dest_index == this->dest_index) {
  191. //Source and destination already connected through another connection, abort
  192. __enable_irq();
  193. return;
  194. }
  195. p = p->next_dest;
  196. }
  197. p->next_dest = this;
  198. }
  199. this->next_dest = NULL;
  200. src.numConnections++;
  201. src.active = true;
  202. dst.numConnections++;
  203. dst.active = true;
  204. isConnected = true;
  205. __enable_irq();
  206. }
  207. void AudioConnection::disconnect(void)
  208. {
  209. AudioConnection *p;
  210. if (!isConnected) return;
  211. if (dest_index > dst.num_inputs) return;
  212. __disable_irq();
  213. // Remove destination from source list
  214. p = src.destination_list;
  215. if (p == NULL) {
  216. //>>> PAH re-enable the IRQ
  217. __enable_irq();
  218. return;
  219. } else if (p == this) {
  220. if (p->next_dest) {
  221. src.destination_list = next_dest;
  222. } else {
  223. src.destination_list = NULL;
  224. }
  225. } else {
  226. while (p) {
  227. if (p == this) {
  228. if (p->next_dest) {
  229. p = next_dest;
  230. break;
  231. } else {
  232. p = NULL;
  233. break;
  234. }
  235. }
  236. p = p->next_dest;
  237. }
  238. }
  239. //>>> PAH release the audio buffer properly
  240. //Remove possible pending src block from destination
  241. if(dst.inputQueue[dest_index] != NULL) {
  242. AudioStream::release(dst.inputQueue[dest_index]);
  243. // release() re-enables the IRQ. Need it to be disabled a little longer
  244. __disable_irq();
  245. dst.inputQueue[dest_index] = NULL;
  246. }
  247. //Check if the disconnected AudioStream objects should still be active
  248. src.numConnections--;
  249. if (src.numConnections == 0) {
  250. src.active = false;
  251. }
  252. dst.numConnections--;
  253. if (dst.numConnections == 0) {
  254. dst.active = false;
  255. }
  256. isConnected = false;
  257. __enable_irq();
  258. }
  259. // When an object has taken responsibility for calling update_all()
  260. // at each block interval (approx 2.9ms), this variable is set to
  261. // true. Objects that are capable of calling update_all(), typically
  262. // input and output based on interrupts, must check this variable in
  263. // their constructors.
  264. bool AudioStream::update_scheduled = false;
  265. bool AudioStream::update_setup(void)
  266. {
  267. if (update_scheduled) return false;
  268. NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208); // 255 = lowest priority
  269. NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
  270. update_scheduled = true;
  271. return true;
  272. }
  273. void AudioStream::update_stop(void)
  274. {
  275. NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
  276. update_scheduled = false;
  277. }
  278. AudioStream * AudioStream::first_update = NULL;
  279. void software_isr(void) // AudioStream::update_all()
  280. {
  281. AudioStream *p;
  282. #if defined(KINETISK)
  283. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  284. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  285. uint32_t totalcycles = ARM_DWT_CYCCNT;
  286. #elif defined(KINETISL)
  287. uint32_t totalcycles = micros();
  288. #endif
  289. //digitalWriteFast(2, HIGH);
  290. for (p = AudioStream::first_update; p; p = p->next_update) {
  291. if (p->active) {
  292. uint32_t cycles = ARM_DWT_CYCCNT;
  293. p->update();
  294. // TODO: traverse inputQueueArray and release
  295. // any input blocks that weren't consumed?
  296. cycles = (ARM_DWT_CYCCNT - cycles) >> 4;
  297. p->cpu_cycles = cycles;
  298. if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
  299. }
  300. }
  301. //digitalWriteFast(2, LOW);
  302. #if defined(KINETISK)
  303. totalcycles = (ARM_DWT_CYCCNT - totalcycles) >> 4;
  304. #elif defined(KINETISL)
  305. totalcycles = micros() - totalcycles;
  306. #endif
  307. AudioStream::cpu_cycles_total = totalcycles;
  308. if (totalcycles > AudioStream::cpu_cycles_total_max)
  309. AudioStream::cpu_cycles_total_max = totalcycles;
  310. }