Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

11 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 must be released
  115. // by the caller after it's transmitted. This allows the
  116. // caller to transmit to same block to more than 1 output,
  117. // and then release it once after all transmit calls.
  118. void AudioStream::transmit(audio_block_t *block, unsigned char index)
  119. {
  120. for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
  121. if (c->src_index == index) {
  122. if (c->dst.inputQueue[c->dest_index] == NULL) {
  123. c->dst.inputQueue[c->dest_index] = block;
  124. block->ref_count++;
  125. }
  126. }
  127. }
  128. }
  129. // Receive block from an input. The block's data
  130. // may be shared with other streams, so it must not be written
  131. audio_block_t * AudioStream::receiveReadOnly(unsigned int index)
  132. {
  133. audio_block_t *in;
  134. if (index >= num_inputs) return NULL;
  135. in = inputQueue[index];
  136. inputQueue[index] = NULL;
  137. return in;
  138. }
  139. // Receive block from an input. The block will not
  140. // be shared, so its contents may be changed.
  141. audio_block_t * AudioStream::receiveWritable(unsigned int index)
  142. {
  143. audio_block_t *in, *p;
  144. if (index >= num_inputs) return NULL;
  145. in = inputQueue[index];
  146. inputQueue[index] = NULL;
  147. if (in && in->ref_count > 1) {
  148. p = allocate();
  149. if (p) memcpy(p->data, in->data, sizeof(p->data));
  150. in->ref_count--;
  151. in = p;
  152. }
  153. return in;
  154. }
  155. void AudioConnection::connect(void)
  156. {
  157. AudioConnection *p;
  158. if (dest_index > dst.num_inputs) return;
  159. __disable_irq();
  160. p = src.destination_list;
  161. if (p == NULL) {
  162. src.destination_list = this;
  163. } else {
  164. while (p->next_dest) p = p->next_dest;
  165. p->next_dest = this;
  166. }
  167. src.active = true;
  168. dst.active = true;
  169. __enable_irq();
  170. }
  171. // When an object has taken responsibility for calling update_all()
  172. // at each block interval (approx 2.9ms), this variable is set to
  173. // true. Objects that are capable of calling update_all(), typically
  174. // input and output based on interrupts, must check this variable in
  175. // their constructors.
  176. bool AudioStream::update_scheduled = false;
  177. bool AudioStream::update_setup(void)
  178. {
  179. if (update_scheduled) return false;
  180. NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208); // 255 = lowest priority
  181. NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
  182. update_scheduled = true;
  183. return true;
  184. }
  185. void AudioStream::update_stop(void)
  186. {
  187. NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
  188. update_scheduled = false;
  189. }
  190. AudioStream * AudioStream::first_update = NULL;
  191. void software_isr(void) // AudioStream::update_all()
  192. {
  193. AudioStream *p;
  194. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  195. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  196. uint32_t totalcycles = ARM_DWT_CYCCNT;
  197. //digitalWriteFast(2, HIGH);
  198. for (p = AudioStream::first_update; p; p = p->next_update) {
  199. if (p->active) {
  200. uint32_t cycles = ARM_DWT_CYCCNT;
  201. p->update();
  202. // TODO: traverse inputQueueArray and release
  203. // any input blocks that weren't consumed?
  204. cycles = (ARM_DWT_CYCCNT - cycles) >> 4;
  205. p->cpu_cycles = cycles;
  206. if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
  207. }
  208. }
  209. //digitalWriteFast(2, LOW);
  210. totalcycles = (ARM_DWT_CYCCNT - totalcycles) >> 4;;
  211. AudioStream::cpu_cycles_total = totalcycles;
  212. if (totalcycles > AudioStream::cpu_cycles_total_max)
  213. AudioStream::cpu_cycles_total_max = totalcycles;
  214. }