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.

313 lines
8.2KB

  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 (isConnected) return;
  159. if (dest_index > dst.num_inputs) return;
  160. __disable_irq();
  161. p = src.destination_list;
  162. if (p == NULL) {
  163. src.destination_list = this;
  164. } else {
  165. while (p->next_dest) {
  166. if (&p->src == &this->src && &p->dst == &this->dst
  167. && p->src_index == this->src_index && p->dest_index == this->dest_index) {
  168. //Source and destination already connected through another connection, abort
  169. __enable_irq();
  170. return;
  171. }
  172. p = p->next_dest;
  173. }
  174. p->next_dest = this;
  175. }
  176. this->next_dest = NULL;
  177. src.numConnections++;
  178. src.active = true;
  179. dst.numConnections++;
  180. dst.active = true;
  181. isConnected = true;
  182. __enable_irq();
  183. }
  184. void AudioConnection::disconnect(void)
  185. {
  186. AudioConnection *p;
  187. if (!isConnected) return;
  188. if (dest_index > dst.num_inputs) return;
  189. __disable_irq();
  190. // Remove destination from source list
  191. p = src.destination_list;
  192. if (p == NULL) {
  193. return;
  194. } else if (p == this) {
  195. if (p->next_dest) {
  196. src.destination_list = next_dest;
  197. } else {
  198. src.destination_list = NULL;
  199. }
  200. } else {
  201. while (p) {
  202. if (p == this) {
  203. if (p->next_dest) {
  204. p = next_dest;
  205. break;
  206. } else {
  207. p = NULL;
  208. break;
  209. }
  210. }
  211. p = p->next_dest;
  212. }
  213. }
  214. //Remove possible pending src block from destination
  215. dst.inputQueue[dest_index] = NULL;
  216. //Check if the disconnected AudioStream objects should still be active
  217. src.numConnections--;
  218. if (src.numConnections == 0) {
  219. src.active = false;
  220. }
  221. dst.numConnections--;
  222. if (dst.numConnections == 0) {
  223. dst.active = false;
  224. }
  225. isConnected = false;
  226. __enable_irq();
  227. }
  228. // When an object has taken responsibility for calling update_all()
  229. // at each block interval (approx 2.9ms), this variable is set to
  230. // true. Objects that are capable of calling update_all(), typically
  231. // input and output based on interrupts, must check this variable in
  232. // their constructors.
  233. bool AudioStream::update_scheduled = false;
  234. bool AudioStream::update_setup(void)
  235. {
  236. if (update_scheduled) return false;
  237. NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208); // 255 = lowest priority
  238. NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
  239. update_scheduled = true;
  240. return true;
  241. }
  242. void AudioStream::update_stop(void)
  243. {
  244. NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
  245. update_scheduled = false;
  246. }
  247. AudioStream * AudioStream::first_update = NULL;
  248. void software_isr(void) // AudioStream::update_all()
  249. {
  250. AudioStream *p;
  251. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  252. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  253. uint32_t totalcycles = ARM_DWT_CYCCNT;
  254. //digitalWriteFast(2, HIGH);
  255. for (p = AudioStream::first_update; p; p = p->next_update) {
  256. if (p->active) {
  257. uint32_t cycles = ARM_DWT_CYCCNT;
  258. p->update();
  259. // TODO: traverse inputQueueArray and release
  260. // any input blocks that weren't consumed?
  261. cycles = (ARM_DWT_CYCCNT - cycles) >> 4;
  262. p->cpu_cycles = cycles;
  263. if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
  264. }
  265. }
  266. //digitalWriteFast(2, LOW);
  267. totalcycles = (ARM_DWT_CYCCNT - totalcycles) >> 4;;
  268. AudioStream::cpu_cycles_total = totalcycles;
  269. if (totalcycles > AudioStream::cpu_cycles_total_max)
  270. AudioStream::cpu_cycles_total_max = totalcycles;
  271. }