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