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.

338 satır
8.9KB

  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(__IMXRT1062__)
  33. #define MAX_AUDIO_MEMORY 229376
  34. #endif
  35. #define NUM_MASKS (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)
  36. audio_block_t * AudioStream::memory_pool;
  37. uint32_t AudioStream::memory_pool_available_mask[NUM_MASKS];
  38. uint16_t AudioStream::memory_pool_first_mask;
  39. uint16_t AudioStream::cpu_cycles_total = 0;
  40. uint16_t AudioStream::cpu_cycles_total_max = 0;
  41. uint16_t AudioStream::memory_used = 0;
  42. uint16_t AudioStream::memory_used_max = 0;
  43. void software_isr(void);
  44. // Set up the pool of audio data blocks
  45. // placing them all onto the free list
  46. FLASHMEM void AudioStream::initialize_memory(audio_block_t *data, unsigned int num)
  47. {
  48. unsigned int i;
  49. unsigned int maxnum = MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2;
  50. //Serial.println("AudioStream initialize_memory");
  51. //delay(10);
  52. if (num > maxnum) num = maxnum;
  53. __disable_irq();
  54. memory_pool = data;
  55. memory_pool_first_mask = 0;
  56. for (i=0; i < NUM_MASKS; i++) {
  57. memory_pool_available_mask[i] = 0;
  58. }
  59. for (i=0; i < num; i++) {
  60. memory_pool_available_mask[i >> 5] |= (1 << (i & 0x1F));
  61. }
  62. for (i=0; i < num; i++) {
  63. data[i].memory_pool_index = i;
  64. }
  65. __enable_irq();
  66. }
  67. // Allocate 1 audio data block. If successful
  68. // the caller is the only owner of this new block
  69. audio_block_t * AudioStream::allocate(void)
  70. {
  71. uint32_t n, index, avail;
  72. uint32_t *p, *end;
  73. audio_block_t *block;
  74. uint32_t used;
  75. p = memory_pool_available_mask;
  76. end = p + NUM_MASKS;
  77. __disable_irq();
  78. index = memory_pool_first_mask;
  79. p += index;
  80. while (1) {
  81. if (p >= end) {
  82. __enable_irq();
  83. //Serial.println("alloc:null");
  84. return NULL;
  85. }
  86. avail = *p;
  87. if (avail) break;
  88. index++;
  89. p++;
  90. }
  91. n = __builtin_clz(avail);
  92. avail &= ~(0x80000000 >> n);
  93. *p = avail;
  94. if (!avail) index++;
  95. memory_pool_first_mask = index;
  96. used = memory_used + 1;
  97. memory_used = used;
  98. __enable_irq();
  99. index = p - memory_pool_available_mask;
  100. block = memory_pool + ((index << 5) + (31 - n));
  101. block->ref_count = 1;
  102. if (used > memory_used_max) memory_used_max = used;
  103. //Serial.print("alloc:");
  104. //Serial.println((uint32_t)block, HEX);
  105. return block;
  106. }
  107. // Release ownership of a data block. If no
  108. // other streams have ownership, the block is
  109. // returned to the free pool
  110. void AudioStream::release(audio_block_t *block)
  111. {
  112. //if (block == NULL) return;
  113. uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
  114. uint32_t index = block->memory_pool_index >> 5;
  115. __disable_irq();
  116. if (block->ref_count > 1) {
  117. block->ref_count--;
  118. } else {
  119. //Serial.print("reles:");
  120. //Serial.println((uint32_t)block, HEX);
  121. memory_pool_available_mask[index] |= mask;
  122. if (index < memory_pool_first_mask) memory_pool_first_mask = index;
  123. memory_used--;
  124. }
  125. __enable_irq();
  126. }
  127. // Transmit an audio data block
  128. // to all streams that connect to an output. The block
  129. // becomes owned by all the recepients, but also is still
  130. // owned by this object. Normally, a block must be released
  131. // by the caller after it's transmitted. This allows the
  132. // caller to transmit to same block to more than 1 output,
  133. // and then release it once after all transmit calls.
  134. void AudioStream::transmit(audio_block_t *block, unsigned char index)
  135. {
  136. for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
  137. if (c->src_index == index) {
  138. if (c->dst.inputQueue[c->dest_index] == NULL) {
  139. c->dst.inputQueue[c->dest_index] = block;
  140. block->ref_count++;
  141. }
  142. }
  143. }
  144. }
  145. // Receive block from an input. The block's data
  146. // may be shared with other streams, so it must not be written
  147. audio_block_t * AudioStream::receiveReadOnly(unsigned int index)
  148. {
  149. audio_block_t *in;
  150. if (index >= num_inputs) return NULL;
  151. in = inputQueue[index];
  152. inputQueue[index] = NULL;
  153. return in;
  154. }
  155. // Receive block from an input. The block will not
  156. // be shared, so its contents may be changed.
  157. audio_block_t * AudioStream::receiveWritable(unsigned int index)
  158. {
  159. audio_block_t *in, *p;
  160. if (index >= num_inputs) return NULL;
  161. in = inputQueue[index];
  162. inputQueue[index] = NULL;
  163. if (in && in->ref_count > 1) {
  164. p = allocate();
  165. if (p) memcpy(p->data, in->data, sizeof(p->data));
  166. in->ref_count--;
  167. in = p;
  168. }
  169. return in;
  170. }
  171. void AudioConnection::connect(void)
  172. {
  173. AudioConnection *p;
  174. if (isConnected) return;
  175. if (dest_index > dst.num_inputs) return;
  176. __disable_irq();
  177. p = src.destination_list;
  178. if (p == NULL) {
  179. src.destination_list = this;
  180. } else {
  181. while (p->next_dest) {
  182. if (&p->src == &this->src && &p->dst == &this->dst
  183. && p->src_index == this->src_index && p->dest_index == this->dest_index) {
  184. //Source and destination already connected through another connection, abort
  185. __enable_irq();
  186. return;
  187. }
  188. p = p->next_dest;
  189. }
  190. p->next_dest = this;
  191. }
  192. this->next_dest = NULL;
  193. src.numConnections++;
  194. src.active = true;
  195. dst.numConnections++;
  196. dst.active = true;
  197. isConnected = true;
  198. __enable_irq();
  199. }
  200. void AudioConnection::disconnect(void)
  201. {
  202. AudioConnection *p;
  203. if (!isConnected) return;
  204. if (dest_index > dst.num_inputs) return;
  205. __disable_irq();
  206. // Remove destination from source list
  207. p = src.destination_list;
  208. if (p == NULL) {
  209. //>>> PAH re-enable the IRQ
  210. __enable_irq();
  211. return;
  212. } else if (p == this) {
  213. if (p->next_dest) {
  214. src.destination_list = next_dest;
  215. } else {
  216. src.destination_list = NULL;
  217. }
  218. } else {
  219. while (p) {
  220. if (p == this) {
  221. if (p->next_dest) {
  222. p = next_dest;
  223. break;
  224. } else {
  225. p = NULL;
  226. break;
  227. }
  228. }
  229. p = p->next_dest;
  230. }
  231. }
  232. //>>> PAH release the audio buffer properly
  233. //Remove possible pending src block from destination
  234. if(dst.inputQueue[dest_index] != NULL) {
  235. AudioStream::release(dst.inputQueue[dest_index]);
  236. // release() re-enables the IRQ. Need it to be disabled a little longer
  237. __disable_irq();
  238. dst.inputQueue[dest_index] = NULL;
  239. }
  240. //Check if the disconnected AudioStream objects should still be active
  241. src.numConnections--;
  242. if (src.numConnections == 0) {
  243. src.active = false;
  244. }
  245. dst.numConnections--;
  246. if (dst.numConnections == 0) {
  247. dst.active = false;
  248. }
  249. isConnected = false;
  250. __enable_irq();
  251. }
  252. // When an object has taken responsibility for calling update_all()
  253. // at each block interval (approx 2.9ms), this variable is set to
  254. // true. Objects that are capable of calling update_all(), typically
  255. // input and output based on interrupts, must check this variable in
  256. // their constructors.
  257. bool AudioStream::update_scheduled = false;
  258. bool AudioStream::update_setup(void)
  259. {
  260. if (update_scheduled) return false;
  261. attachInterruptVector(IRQ_SOFTWARE, software_isr);
  262. NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208); // 255 = lowest priority
  263. NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
  264. update_scheduled = true;
  265. return true;
  266. }
  267. void AudioStream::update_stop(void)
  268. {
  269. NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
  270. update_scheduled = false;
  271. }
  272. AudioStream * AudioStream::first_update = NULL;
  273. void software_isr(void) // AudioStream::update_all()
  274. {
  275. AudioStream *p;
  276. uint32_t totalcycles = ARM_DWT_CYCCNT;
  277. //digitalWriteFast(2, HIGH);
  278. for (p = AudioStream::first_update; p; p = p->next_update) {
  279. if (p->active) {
  280. uint32_t cycles = ARM_DWT_CYCCNT;
  281. p->update();
  282. // TODO: traverse inputQueueArray and release
  283. // any input blocks that weren't consumed?
  284. cycles = (ARM_DWT_CYCCNT - cycles) >> 6;
  285. p->cpu_cycles = cycles;
  286. if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
  287. }
  288. }
  289. //digitalWriteFast(2, LOW);
  290. totalcycles = (ARM_DWT_CYCCNT - totalcycles) >> 6;
  291. AudioStream::cpu_cycles_total = totalcycles;
  292. if (totalcycles > AudioStream::cpu_cycles_total_max)
  293. AudioStream::cpu_cycles_total_max = totalcycles;
  294. asm("DSB");
  295. }