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.

преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef AudioStream_h
  31. #define AudioStream_h
  32. #include <stdio.h> // for NULL
  33. #include <string.h> // for memcpy
  34. #include "kinetis.h"
  35. #if defined(KINETISK)
  36. #define AUDIO_BLOCK_SAMPLES 128
  37. #define AUDIO_SAMPLE_RATE 44117.64706
  38. #define AUDIO_SAMPLE_RATE_EXACT 44117.64706 // 48 MHz / 1088, or 96 MHz * 2 / 17 / 256
  39. #elif defined(KINETISL)
  40. #define AUDIO_BLOCK_SAMPLES 64
  41. #define AUDIO_SAMPLE_RATE 22058.82353
  42. #define AUDIO_SAMPLE_RATE_EXACT 22058.82353 // 48 MHz / 2176, or 96 MHz * 1 / 17 / 256
  43. #endif
  44. class AudioStream;
  45. class AudioConnection;
  46. typedef struct audio_block_struct {
  47. unsigned char ref_count;
  48. unsigned char memory_pool_index;
  49. unsigned char reserved1;
  50. unsigned char reserved2;
  51. int16_t data[AUDIO_BLOCK_SAMPLES];
  52. } audio_block_t;
  53. class AudioConnection
  54. {
  55. public:
  56. AudioConnection(AudioStream &source, AudioStream &destination) :
  57. src(source), dst(destination), src_index(0), dest_index(0),
  58. next_dest(NULL)
  59. { connect(); }
  60. AudioConnection(AudioStream &source, unsigned char sourceOutput,
  61. AudioStream &destination, unsigned char destinationInput) :
  62. src(source), dst(destination),
  63. src_index(sourceOutput), dest_index(destinationInput),
  64. next_dest(NULL)
  65. { connect(); }
  66. friend class AudioStream;
  67. protected:
  68. void connect(void);
  69. AudioStream &src;
  70. AudioStream &dst;
  71. unsigned char src_index;
  72. unsigned char dest_index;
  73. AudioConnection *next_dest;
  74. };
  75. #define AudioMemory(num) ({ \
  76. static DMAMEM audio_block_t data[num]; \
  77. AudioStream::initialize_memory(data, num); \
  78. })
  79. #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))
  80. #define AudioProcessorUsage() (CYCLE_COUNTER_APPROX_PERCENT(AudioStream::cpu_cycles_total))
  81. #define AudioProcessorUsageMax() (CYCLE_COUNTER_APPROX_PERCENT(AudioStream::cpu_cycles_total_max))
  82. #define AudioProcessorUsageMaxReset() (AudioStream::cpu_cycles_total_max = AudioStream::cpu_cycles_total)
  83. #define AudioMemoryUsage() (AudioStream::memory_used)
  84. #define AudioMemoryUsageMax() (AudioStream::memory_used_max)
  85. #define AudioMemoryUsageMaxReset() (AudioStream::memory_used_max = AudioStream::memory_used)
  86. class AudioStream
  87. {
  88. public:
  89. AudioStream(unsigned char ninput, audio_block_t **iqueue) :
  90. num_inputs(ninput), inputQueue(iqueue) {
  91. active = false;
  92. destination_list = NULL;
  93. for (int i=0; i < num_inputs; i++) {
  94. inputQueue[i] = NULL;
  95. }
  96. // add to a simple list, for update_all
  97. // TODO: replace with a proper data flow analysis in update_all
  98. if (first_update == NULL) {
  99. first_update = this;
  100. } else {
  101. AudioStream *p;
  102. for (p=first_update; p->next_update; p = p->next_update) ;
  103. p->next_update = this;
  104. }
  105. next_update = NULL;
  106. cpu_cycles = 0;
  107. cpu_cycles_max = 0;
  108. }
  109. static void initialize_memory(audio_block_t *data, unsigned int num);
  110. int processorUsage(void) { return CYCLE_COUNTER_APPROX_PERCENT(cpu_cycles); }
  111. int processorUsageMax(void) { return CYCLE_COUNTER_APPROX_PERCENT(cpu_cycles_max); }
  112. void processorUsageMaxReset(void) { cpu_cycles_max = cpu_cycles; }
  113. uint16_t cpu_cycles;
  114. uint16_t cpu_cycles_max;
  115. static uint16_t cpu_cycles_total;
  116. static uint16_t cpu_cycles_total_max;
  117. static uint8_t memory_used;
  118. static uint8_t memory_used_max;
  119. protected:
  120. bool active;
  121. unsigned char num_inputs;
  122. static audio_block_t * allocate(void);
  123. static void release(audio_block_t * block);
  124. void transmit(audio_block_t *block, unsigned char index = 0);
  125. audio_block_t * receiveReadOnly(unsigned int index = 0);
  126. audio_block_t * receiveWritable(unsigned int index = 0);
  127. static bool update_setup(void);
  128. static void update_stop(void);
  129. static void update_all(void) { NVIC_SET_PENDING(IRQ_SOFTWARE); }
  130. friend void software_isr(void);
  131. friend class AudioConnection;
  132. private:
  133. AudioConnection *destination_list;
  134. audio_block_t **inputQueue;
  135. static bool update_scheduled;
  136. virtual void update(void) = 0;
  137. static AudioStream *first_update; // for update_all
  138. AudioStream *next_update; // for update_all
  139. static audio_block_t *memory_pool;
  140. static uint32_t memory_pool_available_mask[6];
  141. };
  142. #endif