Teensy 4.1 core updated for 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.

преди 11 години
преди 7 години
преди 11 години
преди 7 години
преди 11 години
преди 7 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <string.h> // for memcpy
  31. #include "AudioStream.h"
  32. #include <Arduino.h>
  33. #if defined(__MKL26Z64__)
  34. #define MAX_AUDIO_MEMORY 6144
  35. #elif defined(__MK20DX128__)
  36. #define MAX_AUDIO_MEMORY 12288
  37. #elif defined(__MK20DX256__)
  38. #define MAX_AUDIO_MEMORY 49152
  39. #elif defined(__MK64FX512__)
  40. #define MAX_AUDIO_MEMORY 163840
  41. #elif defined(__MK66FX1M0__)
  42. #define MAX_AUDIO_MEMORY 229376
  43. #endif
  44. #define NUM_MASKS (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)
  45. audio_block_t * AudioStream::memory_pool;
  46. uint32_t AudioStream::memory_pool_available_mask[NUM_MASKS];
  47. uint16_t AudioStream::memory_pool_first_mask;
  48. uint16_t AudioStream::cpu_cycles_total = 0;
  49. uint16_t AudioStream::cpu_cycles_total_max = 0;
  50. uint16_t AudioStream::memory_used = 0;
  51. uint16_t AudioStream::memory_used_max = 0;
  52. // Set up the pool of audio data blocks
  53. // placing them all onto the free list
  54. void AudioStream::initialize_memory(audio_block_t *data, unsigned int num)
  55. {
  56. unsigned int i;
  57. unsigned int maxnum = MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2;
  58. //Serial.println("AudioStream initialize_memory");
  59. //delay(10);
  60. if (num > maxnum) num = maxnum;
  61. __disable_irq();
  62. memory_pool = data;
  63. memory_pool_first_mask = 0;
  64. for (i=0; i < NUM_MASKS; i++) {
  65. memory_pool_available_mask[i] = 0;
  66. }
  67. for (i=0; i < num; i++) {
  68. memory_pool_available_mask[i >> 5] |= (1 << (i & 0x1F));
  69. }
  70. for (i=0; i < num; i++) {
  71. data[i].memory_pool_index = i;
  72. }
  73. __enable_irq();
  74. }
  75. // Allocate 1 audio data block. If successful
  76. // the caller is the only owner of this new block
  77. audio_block_t * AudioStream::allocate(void)
  78. {
  79. uint32_t n, index, avail;
  80. uint32_t *p, *end;
  81. audio_block_t *block;
  82. uint8_t used;
  83. p = memory_pool_available_mask;
  84. end = p + NUM_MASKS;
  85. __disable_irq();
  86. index = memory_pool_first_mask;
  87. p += index;
  88. while (1) {
  89. if (p >= end) {
  90. __enable_irq();
  91. //Serial.println("alloc:null");
  92. return NULL;
  93. }
  94. avail = *p;
  95. if (avail) break;
  96. index++;
  97. p++;
  98. }
  99. n = __builtin_clz(avail);
  100. avail &= ~(0x80000000 >> n);
  101. *p = avail;
  102. if (!avail) index++;
  103. memory_pool_first_mask = index;
  104. used = memory_used + 1;
  105. memory_used = used;
  106. __enable_irq();
  107. index = p - memory_pool_available_mask;
  108. block = memory_pool + ((index << 5) + (31 - n));
  109. block->ref_count = 1;
  110. if (used > memory_used_max) memory_used_max = used;
  111. //Serial.print("alloc:");
  112. //Serial.println((uint32_t)block, HEX);
  113. return block;
  114. }
  115. // Release ownership of a data block. If no
  116. // other streams have ownership, the block is
  117. // returned to the free pool
  118. void AudioStream::release(audio_block_t *block)
  119. {
  120. //if (block == NULL) return;
  121. uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
  122. uint32_t index = block->memory_pool_index >> 5;
  123. __disable_irq();
  124. if (block->ref_count > 1) {
  125. block->ref_count--;
  126. } else {
  127. //Serial.print("reles:");
  128. //Serial.println((uint32_t)block, HEX);
  129. memory_pool_available_mask[index] |= mask;
  130. if (index < memory_pool_first_mask) memory_pool_first_mask = index;
  131. memory_used--;
  132. }
  133. __enable_irq();
  134. }
  135. // Transmit an audio data block
  136. // to all streams that connect to an output. The block
  137. // becomes owned by all the recepients, but also is still
  138. // owned by this object. Normally, a block must be released
  139. // by the caller after it's transmitted. This allows the
  140. // caller to transmit to same block to more than 1 output,
  141. // and then release it once after all transmit calls.
  142. void AudioStream::transmit(audio_block_t *block, unsigned char index)
  143. {
  144. for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
  145. if (c->src_index == index) {
  146. if (c->dst.inputQueue[c->dest_index] == NULL) {
  147. c->dst.inputQueue[c->dest_index] = block;
  148. block->ref_count++;
  149. }
  150. }
  151. }
  152. }
  153. // Receive block from an input. The block's data
  154. // may be shared with other streams, so it must not be written
  155. audio_block_t * AudioStream::receiveReadOnly(unsigned int index)
  156. {
  157. audio_block_t *in;
  158. if (index >= num_inputs) return NULL;
  159. in = inputQueue[index];
  160. inputQueue[index] = NULL;
  161. return in;
  162. }
  163. // Receive block from an input. The block will not
  164. // be shared, so its contents may be changed.
  165. audio_block_t * AudioStream::receiveWritable(unsigned int index)
  166. {
  167. audio_block_t *in, *p;
  168. if (index >= num_inputs) return NULL;
  169. in = inputQueue[index];
  170. inputQueue[index] = NULL;
  171. if (in && in->ref_count > 1) {
  172. p = allocate();
  173. if (p) memcpy(p->data, in->data, sizeof(p->data));
  174. in->ref_count--;
  175. in = p;
  176. }
  177. return in;
  178. }
  179. void AudioConnection::connect(void)
  180. {
  181. AudioConnection *p;
  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) p = p->next_dest;
  189. p->next_dest = this;
  190. }
  191. src.active = true;
  192. dst.active = true;
  193. __enable_irq();
  194. }
  195. // When an object has taken responsibility for calling update_all()
  196. // at each block interval (approx 2.9ms), this variable is set to
  197. // true. Objects that are capable of calling update_all(), typically
  198. // input and output based on interrupts, must check this variable in
  199. // their constructors.
  200. bool AudioStream::update_scheduled = false;
  201. bool AudioStream::update_setup(void)
  202. {
  203. if (update_scheduled) return false;
  204. NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208); // 255 = lowest priority
  205. NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
  206. update_scheduled = true;
  207. return true;
  208. }
  209. void AudioStream::update_stop(void)
  210. {
  211. NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
  212. update_scheduled = false;
  213. }
  214. AudioStream * AudioStream::first_update = NULL;
  215. void software_isr(void) // AudioStream::update_all()
  216. {
  217. AudioStream *p;
  218. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  219. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  220. uint32_t totalcycles = ARM_DWT_CYCCNT;
  221. //digitalWriteFast(2, HIGH);
  222. for (p = AudioStream::first_update; p; p = p->next_update) {
  223. if (p->active) {
  224. uint32_t cycles = ARM_DWT_CYCCNT;
  225. p->update();
  226. // TODO: traverse inputQueueArray and release
  227. // any input blocks that weren't consumed?
  228. cycles = (ARM_DWT_CYCCNT - cycles) >> 4;
  229. p->cpu_cycles = cycles;
  230. if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
  231. }
  232. }
  233. //digitalWriteFast(2, LOW);
  234. totalcycles = (ARM_DWT_CYCCNT - totalcycles) >> 4;;
  235. AudioStream::cpu_cycles_total = totalcycles;
  236. if (totalcycles > AudioStream::cpu_cycles_total_max)
  237. AudioStream::cpu_cycles_total_max = totalcycles;
  238. }