Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 #include "Callback.h" 00029 00030 /** 00031 * USBAudio example 00032 * 00033 * @code 00034 * #include "mbed.h" 00035 * #include "USBAudio.h" 00036 * 00037 * Serial pc(USBTX, USBRX); 00038 * 00039 * // frequency: 48 kHz 00040 * #define FREQ 48000 00041 * 00042 * // 1 channel: mono 00043 * #define NB_CHA 1 00044 * 00045 * // 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 00046 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1 00047 * 00048 * // USBAudio 00049 * USBAudio audio(FREQ, NB_CHA); 00050 * 00051 * int main() { 00052 * int16_t buf[AUDIO_LENGTH_PACKET/2]; 00053 * 00054 * while (1) { 00055 * // read an audio packet 00056 * audio.read((uint8_t *)buf); 00057 * 00058 * 00059 * // print packet received 00060 * pc.printf("recv: "); 00061 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) { 00062 * pc.printf("%d ", buf[i]); 00063 * } 00064 * pc.printf("\r\n"); 00065 * } 00066 * } 00067 * @endcode 00068 */ 00069 class USBAudio: public USBDevice { 00070 public: 00071 00072 /** 00073 * Constructor 00074 * 00075 * @param frequency_in frequency in Hz (default: 48000) 00076 * @param channel_nb_in channel number (1 or 2) (default: 1) 00077 * @param frequency_out frequency in Hz (default: 8000) 00078 * @param channel_nb_out_in channel number (1 or 2) (default: 1) 00079 * @param vendor_id Your vendor_id 00080 * @param product_id Your product_id 00081 * @param product_release Your preoduct_release 00082 */ 00083 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); 00084 00085 /** 00086 * Get current volume between 0.0 and 1.0 00087 * 00088 * @returns volume 00089 */ 00090 float getVolume(); 00091 00092 /** 00093 * 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 00094 * 00095 * @param buf pointer on a buffer which will be filled with an audio packet 00096 * 00097 * @returns true if successfull 00098 */ 00099 bool read(uint8_t * buf); 00100 00101 /** 00102 * 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 00103 * 00104 * @param buf pointer on a buffer which will be filled if an audio packet is available 00105 * 00106 * @returns true if successfull 00107 */ 00108 bool readNB(uint8_t * buf); 00109 00110 /** 00111 * read last received packet if some. 00112 * @param buf pointer on a buffer which will be filled if an audio packet is available 00113 * 00114 * @returns the packet length 00115 */ 00116 uint32_t readSync(uint8_t *buf); 00117 00118 /** 00119 * 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. 00120 * 00121 * @param buf pointer on the audio packet which will be sent 00122 * @returns true if successful 00123 */ 00124 bool write(uint8_t * buf); 00125 00126 /** Audio Jitter value*/ 00127 enum AudioSampleCorrectType { 00128 RemoveOneSample = -1, 00129 NoCorrection = 0, 00130 AddOneSample = 1 00131 }; 00132 /** 00133 * Write packet in endpoint fifo. assuming tx fifo is empty 00134 * @param buf pointer on the audio packet which will be sent 00135 * @param jitter_nb : AudioSampleCorrecttype 00136 **/ 00137 void writeSync(uint8_t *buf, AudioSampleCorrectType jitter_nb = NoCorrection ); 00138 00139 /** 00140 * Write and read an audio packet at the same time (on the same frame) 00141 * 00142 * @param buf_read pointer on a buffer which will be filled with an audio packet 00143 * @param buf_write pointer on the audio packet which will be sent 00144 * @returns true if successful 00145 */ 00146 bool readWrite(uint8_t * buf_read, uint8_t * buf_write); 00147 00148 00149 /** attach a handler to update the volume 00150 * 00151 * @param function Function to attach 00152 * 00153 */ 00154 void attach(void(*fptr)(void)) { 00155 updateVol.attach(fptr); 00156 } 00157 /** attach a handler to Tx Done 00158 * 00159 * @param function Function to attach 00160 * 00161 */ 00162 void attachTx(void(*fptr)(void)) { 00163 txDone.attach(fptr); 00164 } 00165 /** attach a handler to Rx Done 00166 * 00167 * @param function Function to attach 00168 * 00169 */ 00170 void attachRx(void(*fptr)(void)) { 00171 rxDone.attach(fptr); 00172 } 00173 00174 /** Attach a nonstatic void/void member function to update the volume 00175 * 00176 * @param tptr Object pointer 00177 * @param mptr Member function pointer 00178 * 00179 */ 00180 template<typename T> 00181 void attach(T *tptr, void(T::*mptr)(void)) { 00182 updateVol.attach(tptr, mptr); 00183 } 00184 template<typename T> 00185 void attachTx(T *tptr, void(T::*mptr)(void)) { 00186 txDone.attach(tptr, mptr); 00187 } 00188 template<typename T> 00189 void attachRx(T *tptr, void(T::*mptr)(void)) { 00190 rxDone.attach(tptr, mptr); 00191 } 00192 00193 00194 protected: 00195 00196 /* 00197 * Called by USBDevice layer. Set configuration of the device. 00198 * For instance, you can add all endpoints that you need on this function. 00199 * 00200 * @param configuration Number of the configuration 00201 * @returns true if class handles this request 00202 */ 00203 virtual bool USBCallback_setConfiguration(uint8_t configuration); 00204 00205 /* 00206 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context 00207 * This is used to handle extensions to standard requests 00208 * and class specific requests 00209 * 00210 * @returns true if class handles this request 00211 */ 00212 virtual bool USBCallback_request(); 00213 00214 /* 00215 * Get string product descriptor 00216 * 00217 * @returns pointer to the string product descriptor 00218 */ 00219 virtual uint8_t * stringIproductDesc(); 00220 00221 /* 00222 * Get string interface descriptor 00223 * 00224 * @returns pointer to the string interface descriptor 00225 */ 00226 virtual uint8_t * stringIinterfaceDesc(); 00227 00228 /* 00229 * Get configuration descriptor 00230 * 00231 * @returns pointer to the configuration descriptor 00232 */ 00233 virtual uint8_t * configurationDesc(); 00234 00235 /* 00236 * Called by USBDevice layer. Set interface/alternate of the device. 00237 * 00238 * @param interface Number of the interface to be configured 00239 * @param alternate Number of the alternate to be configured 00240 * @returns true if class handles this request 00241 */ 00242 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate); 00243 00244 /* 00245 * Called by USBDevice on Endpoint0 request completion 00246 * if the 'notify' flag has been set to true. Warning: Called in ISR context 00247 * 00248 * In this case it is used to indicate that a HID report has 00249 * been received from the host on endpoint 0 00250 * 00251 * @param buf buffer received on endpoint 0 00252 * @param length length of this buffer 00253 */ 00254 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length); 00255 00256 /* 00257 * Callback called on each Start of Frame event 00258 */ 00259 virtual void SOF(int frameNumber); 00260 00261 /* 00262 * Callback called when a packet is received 00263 */ 00264 virtual bool EPISO_OUT_callback(); 00265 00266 /* 00267 * Callback called when a packet has been sent 00268 */ 00269 virtual bool EPISO_IN_callback(); 00270 00271 private: 00272 00273 // stream available ? 00274 volatile bool available; 00275 00276 // interrupt OUT has been received 00277 volatile bool interruptOUT; 00278 00279 // interrupt IN has been received 00280 volatile bool interruptIN; 00281 00282 // audio packet has been written 00283 volatile bool writeIN; 00284 00285 // FREQ 00286 uint32_t FREQ_OUT; 00287 uint32_t FREQ_IN; 00288 00289 // size of the maximum packet for the isochronous endpoint 00290 uint32_t PACKET_SIZE_ISO_IN; 00291 uint32_t PACKET_SIZE_ISO_OUT; 00292 00293 // mono, stereo,... 00294 uint8_t channel_nb_in; 00295 uint8_t channel_nb_out; 00296 00297 // channel config: master, left, right 00298 uint8_t channel_config_in; 00299 uint8_t channel_config_out; 00300 00301 // mute state 00302 uint8_t mute; 00303 00304 // Volume Current Value 00305 uint16_t volCur; 00306 00307 // Volume Minimum Value 00308 uint16_t volMin; 00309 00310 // Volume Maximum Value 00311 uint16_t volMax; 00312 00313 // Volume Resolution 00314 uint16_t volRes; 00315 00316 // Buffer containing one audio packet (to be read) 00317 volatile uint8_t * buf_stream_in; 00318 00319 // Buffer containing one audio packet (to be written) 00320 volatile uint8_t * buf_stream_out; 00321 00322 // callback to update volume 00323 Callback<void()> updateVol; 00324 00325 // callback transmit Done 00326 Callback<void()> txDone; 00327 // callback transmit Done 00328 Callback<void()> rxDone; 00329 00330 // boolean showing that the SOF handler has been called. Useful for readNB. 00331 volatile bool SOF_handler; 00332 00333 volatile float volume; 00334 00335 }; 00336 00337 #endif
Generated on Thu Jul 14 2022 14:36:24 by
