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