Keyboard firmwares for Atmel AVR and Cortex-M
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.

USBAudio.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef USBAudio_H
  19. #define USBAudio_H
  20. /* These headers are included for child class. */
  21. #include "USBEndpoints.h"
  22. #include "USBDescriptor.h"
  23. #include "USBDevice_Types.h"
  24. #include "USBDevice.h"
  25. /**
  26. * USBAudio example
  27. *
  28. * @code
  29. * #include "mbed.h"
  30. * #include "USBAudio.h"
  31. *
  32. * Serial pc(USBTX, USBRX);
  33. *
  34. * // frequency: 48 kHz
  35. * #define FREQ 48000
  36. *
  37. * // 1 channel: mono
  38. * #define NB_CHA 1
  39. *
  40. * // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
  41. * #define AUDIO_LENGTH_PACKET 48 * 2 * 1
  42. *
  43. * // USBAudio
  44. * USBAudio audio(FREQ, NB_CHA);
  45. *
  46. * int main() {
  47. * int16_t buf[AUDIO_LENGTH_PACKET/2];
  48. *
  49. * while (1) {
  50. * // read an audio packet
  51. * audio.read((uint8_t *)buf);
  52. *
  53. *
  54. * // print packet received
  55. * pc.printf("recv: ");
  56. * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
  57. * pc.printf("%d ", buf[i]);
  58. * }
  59. * pc.printf("\r\n");
  60. * }
  61. * }
  62. * @endcode
  63. */
  64. class USBAudio: public USBDevice {
  65. public:
  66. /**
  67. * Constructor
  68. *
  69. * @param frequency_in frequency in Hz (default: 48000)
  70. * @param channel_nb_in channel number (1 or 2) (default: 1)
  71. * @param frequency_out frequency in Hz (default: 8000)
  72. * @param channel_nb_out_in channel number (1 or 2) (default: 1)
  73. * @param vendor_id Your vendor_id
  74. * @param product_id Your product_id
  75. * @param product_release Your preoduct_release
  76. */
  77. USBAudio(uint32_t frequency_in = 48000, uint8_t channel_nb_in = 1, uint32_t frequency_out = 8000, uint8_t channel_nb_out = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
  78. /**
  79. * Get current volume between 0.0 and 1.0
  80. *
  81. * @returns volume
  82. */
  83. float getVolume();
  84. /**
  85. * Read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Blocking
  86. *
  87. * @param buf pointer on a buffer which will be filled with an audio packet
  88. *
  89. * @returns true if successfull
  90. */
  91. bool read(uint8_t * buf);
  92. /**
  93. * Try to read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Non Blocking
  94. *
  95. * @param buf pointer on a buffer which will be filled if an audio packet is available
  96. *
  97. * @returns true if successfull
  98. */
  99. bool readNB(uint8_t * buf);
  100. /**
  101. * Write an audio packet. During a frame, only a single writing (you can't write and read an audio packet during the same frame)can be done using this method.
  102. *
  103. * @param buf pointer on the audio packet which will be sent
  104. * @returns true if successful
  105. */
  106. bool write(uint8_t * buf);
  107. /**
  108. * Write and read an audio packet at the same time (on the same frame)
  109. *
  110. * @param buf_read pointer on a buffer which will be filled with an audio packet
  111. * @param buf_write pointer on the audio packet which will be sent
  112. * @returns true if successful
  113. */
  114. bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
  115. /** attach a handler to update the volume
  116. *
  117. * @param function Function to attach
  118. *
  119. */
  120. void attach(void(*fptr)(void)) {
  121. updateVol.attach(fptr);
  122. }
  123. /** Attach a nonstatic void/void member function to update the volume
  124. *
  125. * @param tptr Object pointer
  126. * @param mptr Member function pointer
  127. *
  128. */
  129. template<typename T>
  130. void attach(T *tptr, void(T::*mptr)(void)) {
  131. updateVol.attach(tptr, mptr);
  132. }
  133. protected:
  134. /*
  135. * Called by USBDevice layer. Set configuration of the device.
  136. * For instance, you can add all endpoints that you need on this function.
  137. *
  138. * @param configuration Number of the configuration
  139. * @returns true if class handles this request
  140. */
  141. virtual bool USBCallback_setConfiguration(uint8_t configuration);
  142. /*
  143. * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
  144. * This is used to handle extensions to standard requests
  145. * and class specific requests
  146. *
  147. * @returns true if class handles this request
  148. */
  149. virtual bool USBCallback_request();
  150. /*
  151. * Get string product descriptor
  152. *
  153. * @returns pointer to the string product descriptor
  154. */
  155. virtual uint8_t * stringIproductDesc();
  156. /*
  157. * Get string interface descriptor
  158. *
  159. * @returns pointer to the string interface descriptor
  160. */
  161. virtual uint8_t * stringIinterfaceDesc();
  162. /*
  163. * Get configuration descriptor
  164. *
  165. * @returns pointer to the configuration descriptor
  166. */
  167. virtual uint8_t * configurationDesc();
  168. /*
  169. * Called by USBDevice layer. Set interface/alternate of the device.
  170. *
  171. * @param interface Number of the interface to be configured
  172. * @param alternate Number of the alternate to be configured
  173. * @returns true if class handles this request
  174. */
  175. virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
  176. /*
  177. * Called by USBDevice on Endpoint0 request completion
  178. * if the 'notify' flag has been set to true. Warning: Called in ISR context
  179. *
  180. * In this case it is used to indicate that a HID report has
  181. * been received from the host on endpoint 0
  182. *
  183. * @param buf buffer received on endpoint 0
  184. * @param length length of this buffer
  185. */
  186. virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
  187. /*
  188. * Callback called on each Start of Frame event
  189. */
  190. virtual void SOF(int frameNumber);
  191. /*
  192. * Callback called when a packet is received
  193. */
  194. virtual bool EPISO_OUT_callback();
  195. /*
  196. * Callback called when a packet has been sent
  197. */
  198. virtual bool EPISO_IN_callback();
  199. private:
  200. // stream available ?
  201. volatile bool available;
  202. // interrupt OUT has been received
  203. volatile bool interruptOUT;
  204. // interrupt IN has been received
  205. volatile bool interruptIN;
  206. // audio packet has been written
  207. volatile bool writeIN;
  208. // FREQ
  209. uint32_t FREQ_OUT;
  210. uint32_t FREQ_IN;
  211. // size of the maximum packet for the isochronous endpoint
  212. uint32_t PACKET_SIZE_ISO_IN;
  213. uint32_t PACKET_SIZE_ISO_OUT;
  214. // mono, stereo,...
  215. uint8_t channel_nb_in;
  216. uint8_t channel_nb_out;
  217. // channel config: master, left, right
  218. uint8_t channel_config_in;
  219. uint8_t channel_config_out;
  220. // mute state
  221. uint8_t mute;
  222. // Volume Current Value
  223. uint16_t volCur;
  224. // Volume Minimum Value
  225. uint16_t volMin;
  226. // Volume Maximum Value
  227. uint16_t volMax;
  228. // Volume Resolution
  229. uint16_t volRes;
  230. // Buffer containing one audio packet (to be read)
  231. volatile uint8_t * buf_stream_in;
  232. // Buffer containing one audio packet (to be written)
  233. volatile uint8_t * buf_stream_out;
  234. // callback to update volume
  235. FunctionPointer updateVol;
  236. // boolean showing that the SOF handler has been called. Useful for readNB.
  237. volatile bool SOF_handler;
  238. volatile float volume;
  239. };
  240. #endif