Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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