Teensy 4.1 core updated for C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

193 rindas
6.7KB

  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. #ifndef AudioStream_h
  31. #define AudioStream_h
  32. #ifndef __ASSEMBLER__
  33. #include <stdio.h> // for NULL
  34. #include <string.h> // for memcpy
  35. #include "kinetis.h"
  36. #endif
  37. // AUDIO_BLOCK_SAMPLES determines how many samples the audio library processes
  38. // per update. It may be reduced to achieve lower latency response to events,
  39. // at the expense of higher interrupt and DMA setup overhead.
  40. //
  41. // Less than 32 may not work with some input & output objects. Multiples of 16
  42. // should be used, since some synthesis objects generate 16 samples per loop.
  43. //
  44. // Some parts of the audio library may have hard-coded dependency on 128 samples.
  45. // Please report these on the forum with reproducible test cases.
  46. #ifndef AUDIO_BLOCK_SAMPLES
  47. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  48. #define AUDIO_BLOCK_SAMPLES 128
  49. #elif defined(__MKL26Z64__)
  50. #define AUDIO_BLOCK_SAMPLES 64
  51. #endif
  52. #endif
  53. #ifndef AUDIO_SAMPLE_RATE_EXACT
  54. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  55. #define AUDIO_SAMPLE_RATE_EXACT 44117.64706 // 48 MHz / 1088, or 96 MHz * 2 / 17 / 256
  56. #elif defined(__MKL26Z64__)
  57. //#define AUDIO_SAMPLE_RATE_EXACT 22058.82353 // 48 MHz / 2176, or 96 MHz * 1 / 17 / 256
  58. #define AUDIO_SAMPLE_RATE_EXACT 44117.64706
  59. #endif
  60. #endif
  61. #define AUDIO_SAMPLE_RATE AUDIO_SAMPLE_RATE_EXACT
  62. #ifndef __ASSEMBLER__
  63. class AudioStream;
  64. class AudioConnection;
  65. typedef struct audio_block_struct {
  66. uint8_t ref_count;
  67. uint8_t reserved1;
  68. uint16_t memory_pool_index;
  69. int16_t data[AUDIO_BLOCK_SAMPLES];
  70. } audio_block_t;
  71. class AudioConnection
  72. {
  73. public:
  74. AudioConnection(AudioStream &source, AudioStream &destination) :
  75. src(source), dst(destination), src_index(0), dest_index(0),
  76. next_dest(NULL)
  77. { isConnected = false;
  78. connect(); }
  79. AudioConnection(AudioStream &source, unsigned char sourceOutput,
  80. AudioStream &destination, unsigned char destinationInput) :
  81. src(source), dst(destination),
  82. src_index(sourceOutput), dest_index(destinationInput),
  83. next_dest(NULL)
  84. { isConnected = false;
  85. connect(); }
  86. friend class AudioStream;
  87. ~AudioConnection() {
  88. disconnect();
  89. }
  90. void disconnect(void);
  91. void connect(void);
  92. protected:
  93. AudioStream &src;
  94. AudioStream &dst;
  95. unsigned char src_index;
  96. unsigned char dest_index;
  97. AudioConnection *next_dest;
  98. bool isConnected;
  99. };
  100. #define AudioMemory(num) ({ \
  101. static DMAMEM audio_block_t data[num]; \
  102. AudioStream::initialize_memory(data, num); \
  103. })
  104. #if defined(KINETISK)
  105. #define CYCLE_COUNTER_APPROX_PERCENT(n) (((n) + (F_CPU / 32 / AUDIO_SAMPLE_RATE * AUDIO_BLOCK_SAMPLES / 100)) / (F_CPU / 16 / AUDIO_SAMPLE_RATE * AUDIO_BLOCK_SAMPLES / 100))
  106. #elif defined(KINETISL)
  107. #define CYCLE_COUNTER_APPROX_PERCENT(n) ((n) * (int)(AUDIO_SAMPLE_RATE) + (int)(AUDIO_SAMPLE_RATE/2)) / (AUDIO_BLOCK_SAMPLES * 10000)
  108. #endif
  109. #define AudioProcessorUsage() (CYCLE_COUNTER_APPROX_PERCENT(AudioStream::cpu_cycles_total))
  110. #define AudioProcessorUsageMax() (CYCLE_COUNTER_APPROX_PERCENT(AudioStream::cpu_cycles_total_max))
  111. #define AudioProcessorUsageMaxReset() (AudioStream::cpu_cycles_total_max = AudioStream::cpu_cycles_total)
  112. #define AudioMemoryUsage() (AudioStream::memory_used)
  113. #define AudioMemoryUsageMax() (AudioStream::memory_used_max)
  114. #define AudioMemoryUsageMaxReset() (AudioStream::memory_used_max = AudioStream::memory_used)
  115. class AudioStream
  116. {
  117. public:
  118. AudioStream(unsigned char ninput, audio_block_t **iqueue) :
  119. num_inputs(ninput), inputQueue(iqueue) {
  120. active = false;
  121. destination_list = NULL;
  122. for (int i=0; i < num_inputs; i++) {
  123. inputQueue[i] = NULL;
  124. }
  125. // add to a simple list, for update_all
  126. // TODO: replace with a proper data flow analysis in update_all
  127. if (first_update == NULL) {
  128. first_update = this;
  129. } else {
  130. AudioStream *p;
  131. for (p=first_update; p->next_update; p = p->next_update) ;
  132. p->next_update = this;
  133. }
  134. next_update = NULL;
  135. cpu_cycles = 0;
  136. cpu_cycles_max = 0;
  137. numConnections = 0;
  138. }
  139. static void initialize_memory(audio_block_t *data, unsigned int num);
  140. int processorUsage(void) { return CYCLE_COUNTER_APPROX_PERCENT(cpu_cycles); }
  141. int processorUsageMax(void) { return CYCLE_COUNTER_APPROX_PERCENT(cpu_cycles_max); }
  142. void processorUsageMaxReset(void) { cpu_cycles_max = cpu_cycles; }
  143. bool isActive(void) { return active; }
  144. uint16_t cpu_cycles;
  145. uint16_t cpu_cycles_max;
  146. static uint16_t cpu_cycles_total;
  147. static uint16_t cpu_cycles_total_max;
  148. static uint16_t memory_used;
  149. static uint16_t memory_used_max;
  150. protected:
  151. bool active;
  152. unsigned char num_inputs;
  153. static audio_block_t * allocate(void);
  154. static void release(audio_block_t * block);
  155. void transmit(audio_block_t *block, unsigned char index = 0);
  156. audio_block_t * receiveReadOnly(unsigned int index = 0);
  157. audio_block_t * receiveWritable(unsigned int index = 0);
  158. static bool update_setup(void);
  159. static void update_stop(void);
  160. static void update_all(void) { NVIC_SET_PENDING(IRQ_SOFTWARE); }
  161. friend void software_isr(void);
  162. friend class AudioConnection;
  163. uint8_t numConnections;
  164. private:
  165. AudioConnection *destination_list;
  166. audio_block_t **inputQueue;
  167. static bool update_scheduled;
  168. virtual void update(void) = 0;
  169. static AudioStream *first_update; // for update_all
  170. AudioStream *next_update; // for update_all
  171. static audio_block_t *memory_pool;
  172. static uint32_t memory_pool_available_mask[];
  173. static uint16_t memory_pool_first_mask;
  174. };
  175. #endif
  176. #endif