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.

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