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.

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