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