USBDevice stack
Dependents: erg USBSerial_HelloWorld Slingshot SuperScanner ... more
USBAudio.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef USBAudio_H 00020 #define USBAudio_H 00021 00022 /* These headers are included for child class. */ 00023 #include "USBEndpoints.h" 00024 #include "USBDescriptor.h" 00025 #include "USBDevice_Types.h" 00026 00027 #include "USBDevice.h" 00028 00029 00030 /** 00031 * USBAudio example 00032 * 00033 * #include "mbed.h" 00034 * #include "USBAudio.h" 00035 * 00036 * Serial pc(USBTX, USBRX); 00037 * 00038 * // frequency: 48 kHz 00039 * #define FREQ 48000 00040 * 00041 * // 1 channel: mono 00042 * #define NB_CHA 1 00043 * 00044 * // 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 00045 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1 00046 * 00047 * // USBAudio 00048 * USBAudio audio(FREQ, NB_CHA); 00049 * 00050 * int main() { 00051 * int16_t buf[AUDIO_LENGTH_PACKET/2]; 00052 * 00053 * while (1) { 00054 * // read an audio packet 00055 * audio.read((uint8_t *)buf); 00056 * 00057 * 00058 * // print packet received 00059 * pc.printf("recv: "); 00060 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) { 00061 * pc.printf("%d ", buf[i]); 00062 * } 00063 * pc.printf("\r\n"); 00064 * } 00065 * } 00066 * @endcode 00067 */ 00068 class USBAudio: public USBDevice { 00069 public: 00070 00071 /** 00072 * Constructor 00073 * 00074 * @param frequency_in frequency in Hz (default: 48000) 00075 * @param channel_nb_in channel number (1 or 2) (default: 1) 00076 * @param frequency_out frequency in Hz (default: 8000) 00077 * @param channel_nb_out_in channel number (1 or 2) (default: 1) 00078 * @param vendor_id Your vendor_id 00079 * @param product_id Your product_id 00080 * @param product_release Your preoduct_release 00081 */ 00082 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); 00083 00084 /** 00085 * Get current volume between 0.0 and 1.0 00086 * 00087 * @returns volume 00088 */ 00089 float getVolume(); 00090 00091 /** 00092 * 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 00093 * 00094 * @param buf pointer on a buffer which will be filled with an audio packet 00095 * 00096 * @returns true if successfull 00097 */ 00098 bool read(uint8_t * buf); 00099 00100 /** 00101 * 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 00102 * 00103 * @param buf pointer on a buffer which will be filled if an audio packet is available 00104 * 00105 * @returns true if successfull 00106 */ 00107 bool readNB(uint8_t * buf); 00108 00109 /** 00110 * 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. 00111 * 00112 * @param buf pointer on the audio packet which will be sent 00113 * @returns true if successful 00114 */ 00115 bool write(uint8_t * buf); 00116 00117 /** 00118 * Write and read an audio packet at the same time (on the same frame) 00119 * 00120 * @param buf_read pointer on a buffer which will be filled with an audio packet 00121 * @param buf_write pointer on the audio packet which will be sent 00122 * @returns true if successful 00123 */ 00124 bool readWrite(uint8_t * buf_read, uint8_t * buf_write); 00125 00126 00127 /** attach a handler to update the volume 00128 * 00129 * @param function Function to attach 00130 * 00131 */ 00132 void attach(void(*fptr)(void)) { 00133 updateVol.attach(fptr); 00134 } 00135 00136 /** Attach a nonstatic void/void member function to update the volume 00137 * 00138 * @param tptr Object pointer 00139 * @param mptr Member function pointer 00140 * 00141 */ 00142 template<typename T> 00143 void attach(T *tptr, void(T::*mptr)(void)) { 00144 updateVol.attach(tptr, mptr); 00145 } 00146 00147 00148 protected: 00149 00150 /* 00151 * Called by USBDevice layer. Set configuration of the device. 00152 * For instance, you can add all endpoints that you need on this function. 00153 * 00154 * @param configuration Number of the configuration 00155 * @returns true if class handles this request 00156 */ 00157 virtual bool USBCallback_setConfiguration(uint8_t configuration); 00158 00159 /* 00160 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context 00161 * This is used to handle extensions to standard requests 00162 * and class specific requests 00163 * 00164 * @returns true if class handles this request 00165 */ 00166 virtual bool USBCallback_request(); 00167 00168 /* 00169 * Get string product descriptor 00170 * 00171 * @returns pointer to the string product descriptor 00172 */ 00173 virtual uint8_t * stringIproductDesc(); 00174 00175 /* 00176 * Get string interface descriptor 00177 * 00178 * @returns pointer to the string interface descriptor 00179 */ 00180 virtual uint8_t * stringIinterfaceDesc(); 00181 00182 /* 00183 * Get configuration descriptor 00184 * 00185 * @returns pointer to the configuration descriptor 00186 */ 00187 virtual uint8_t * configurationDesc(); 00188 00189 /* 00190 * Called by USBDevice layer. Set interface/alternate of the device. 00191 * 00192 * @param interface Number of the interface to be configured 00193 * @param alternate Number of the alternate to be configured 00194 * @returns true if class handles this request 00195 */ 00196 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate); 00197 00198 /* 00199 * Called by USBDevice on Endpoint0 request completion 00200 * if the 'notify' flag has been set to true. Warning: Called in ISR context 00201 * 00202 * In this case it is used to indicate that a HID report has 00203 * been received from the host on endpoint 0 00204 * 00205 * @param buf buffer received on endpoint 0 00206 * @param length length of this buffer 00207 */ 00208 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length); 00209 00210 /* 00211 * Callback called on each Start of Frame event 00212 */ 00213 virtual void SOF(int frameNumber); 00214 00215 /* 00216 * Callback called when a packet is received 00217 */ 00218 virtual bool EP3_OUT_callback(); 00219 00220 /* 00221 * Callback called when a packet has been sent 00222 */ 00223 virtual bool EP3_IN_callback(); 00224 00225 private: 00226 00227 // stream available ? 00228 volatile bool available; 00229 00230 // interrupt OUT has been received 00231 volatile bool interruptOUT; 00232 00233 // interrupt IN has been received 00234 volatile bool interruptIN; 00235 00236 // audio packet has been written 00237 volatile bool writeIN; 00238 00239 // FREQ 00240 uint32_t FREQ_OUT; 00241 uint32_t FREQ_IN; 00242 00243 // size of the maximum packet for the isochronous endpoint 00244 uint32_t PACKET_SIZE_ISO_IN; 00245 uint32_t PACKET_SIZE_ISO_OUT; 00246 00247 // mono, stereo,... 00248 uint8_t channel_nb_in; 00249 uint8_t channel_nb_out; 00250 00251 // channel config: master, left, right 00252 uint8_t channel_config_in; 00253 uint8_t channel_config_out; 00254 00255 // mute state 00256 uint8_t mute; 00257 00258 // Volume Current Value 00259 uint16_t volCur; 00260 00261 // Volume Minimum Value 00262 uint16_t volMin; 00263 00264 // Volume Maximum Value 00265 uint16_t volMax; 00266 00267 // Volume Resolution 00268 uint16_t volRes; 00269 00270 // Buffer containing one audio packet (to be read) 00271 volatile uint8_t * buf_stream_in; 00272 00273 // Buffer containing one audio packet (to be written) 00274 volatile uint8_t * buf_stream_out; 00275 00276 // callback to update volume 00277 FunctionPointer updateVol; 00278 00279 // boolean showing that the SOF handler has been called. Useful for readNB. 00280 volatile bool SOF_handler; 00281 00282 volatile float volume; 00283 00284 }; 00285 00286 #endif
Generated on Tue Jul 12 2022 14:44:02 by 1.7.2