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.
Fork of USBDevice by
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 "USBDevice/USBDevice/USBEndpoints.h" 00024 #include "USBDevice/USBDevice/USBDescriptor.h" 00025 #include "USBDevice/USBDevice/USBDevice_Types.h" 00026 00027 #include "USBDevice/USBDevice/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 { 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 * 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. 00112 * 00113 * @param buf pointer on the audio packet which will be sent 00114 * @returns true if successful 00115 */ 00116 bool write( uint8_t *buf ); 00117 00118 /** 00119 * Write and read an audio packet at the same time (on the same frame) 00120 * 00121 * @param buf_read pointer on a buffer which will be filled with an audio packet 00122 * @param buf_write pointer on the audio packet which will be sent 00123 * @returns true if successful 00124 */ 00125 bool readWrite( uint8_t *buf_read, uint8_t *buf_write ); 00126 00127 00128 /** attach a handler to update the volume 00129 * 00130 * @param function Function to attach 00131 * 00132 */ 00133 void attach( void( *fptr )( void ) ) 00134 { 00135 updateVol.attach( fptr ); 00136 } 00137 00138 /** Attach a nonstatic void/void member function to update the volume 00139 * 00140 * @param tptr Object pointer 00141 * @param mptr Member function pointer 00142 * 00143 */ 00144 template<typename T> 00145 void attach( T *tptr, void( T::*mptr )( void ) ) 00146 { 00147 updateVol.attach( tptr, mptr ); 00148 } 00149 00150 00151 protected: 00152 00153 /* 00154 * Called by USBDevice layer. Set configuration of the device. 00155 * For instance, you can add all endpoints that you need on this function. 00156 * 00157 * @param configuration Number of the configuration 00158 * @returns true if class handles this request 00159 */ 00160 virtual bool USBCallback_setConfiguration( uint8_t configuration ); 00161 00162 /* 00163 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context 00164 * This is used to handle extensions to standard requests 00165 * and class specific requests 00166 * 00167 * @returns true if class handles this request 00168 */ 00169 virtual bool USBCallback_request(); 00170 00171 /* 00172 * Get string product descriptor 00173 * 00174 * @returns pointer to the string product descriptor 00175 */ 00176 virtual uint8_t *stringIproductDesc(); 00177 00178 /* 00179 * Get string interface descriptor 00180 * 00181 * @returns pointer to the string interface descriptor 00182 */ 00183 virtual uint8_t *stringIinterfaceDesc(); 00184 00185 /* 00186 * Get configuration descriptor 00187 * 00188 * @returns pointer to the configuration descriptor 00189 */ 00190 virtual uint8_t *configurationDesc(); 00191 00192 /* 00193 * Called by USBDevice layer. Set interface/alternate of the device. 00194 * 00195 * @param interface Number of the interface to be configured 00196 * @param alternate Number of the alternate to be configured 00197 * @returns true if class handles this request 00198 */ 00199 virtual bool USBCallback_setInterface( uint16_t interface, uint8_t alternate ); 00200 00201 /* 00202 * Called by USBDevice on Endpoint0 request completion 00203 * if the 'notify' flag has been set to true. Warning: Called in ISR context 00204 * 00205 * In this case it is used to indicate that a HID report has 00206 * been received from the host on endpoint 0 00207 * 00208 * @param buf buffer received on endpoint 0 00209 * @param length length of this buffer 00210 */ 00211 virtual void USBCallback_requestCompleted( uint8_t *buf, uint32_t length ); 00212 00213 /* 00214 * Callback called on each Start of Frame event 00215 */ 00216 virtual void SOF( int frameNumber ); 00217 00218 /* 00219 * Callback called when a packet is received 00220 */ 00221 virtual bool EP3_OUT_callback(); 00222 00223 /* 00224 * Callback called when a packet has been sent 00225 */ 00226 virtual bool EP3_IN_callback(); 00227 00228 private: 00229 00230 // stream available ? 00231 volatile bool available; 00232 00233 // interrupt OUT has been received 00234 volatile bool interruptOUT; 00235 00236 // interrupt IN has been received 00237 volatile bool interruptIN; 00238 00239 // audio packet has been written 00240 volatile bool writeIN; 00241 00242 // FREQ 00243 uint32_t FREQ_OUT; 00244 uint32_t FREQ_IN; 00245 00246 // size of the maximum packet for the isochronous endpoint 00247 uint32_t PACKET_SIZE_ISO_IN; 00248 uint32_t PACKET_SIZE_ISO_OUT; 00249 00250 // mono, stereo,... 00251 uint8_t channel_nb_in; 00252 uint8_t channel_nb_out; 00253 00254 // channel config: master, left, right 00255 uint8_t channel_config_in; 00256 uint8_t channel_config_out; 00257 00258 // mute state 00259 uint8_t mute; 00260 00261 // Volume Current Value 00262 uint16_t volCur; 00263 00264 // Volume Minimum Value 00265 uint16_t volMin; 00266 00267 // Volume Maximum Value 00268 uint16_t volMax; 00269 00270 // Volume Resolution 00271 uint16_t volRes; 00272 00273 // Buffer containing one audio packet (to be read) 00274 volatile uint8_t *buf_stream_in; 00275 00276 // Buffer containing one audio packet (to be written) 00277 volatile uint8_t *buf_stream_out; 00278 00279 // callback to update volume 00280 FunctionPointer updateVol; 00281 00282 // boolean showing that the SOF handler has been called. Useful for readNB. 00283 volatile bool SOF_handler; 00284 00285 volatile float volume; 00286 00287 }; 00288 00289 #endif 00290
Generated on Sat Jul 16 2022 03:17:41 by
1.7.2
