Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBAudio.h Source File

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 = Callback<void()>(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 = Callback<void()>(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 = Callback<void()>(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 = Callback<void()>(tptr, mptr);
00183     }
00184     /** Attach a nonstatic void/void member function to Tx Done
00185      *
00186      * @param tptr Object pointer
00187      * @param mptr Member function pointer
00188      *
00189      */
00190     template<typename T>
00191     void attachTx(T *tptr, void(T::*mptr)(void)) {
00192         txDone = Callback<void()>(tptr, mptr);
00193     }
00194     /** Attach a nonstatic void/void member function to Rx Done
00195      *
00196      * @param tptr Object pointer
00197      * @param mptr Member function pointer
00198      *
00199      */
00200     template<typename T>
00201     void attachRx(T *tptr, void(T::*mptr)(void)) {
00202         rxDone = Callback<void()>(tptr, mptr);
00203     }
00204 
00205     /** Attach a Callback to update the volume
00206      *
00207      * @param cb Callback to attach
00208      *
00209      */
00210     void attach(Callback<void()> &cb) {
00211         updateVol = cb;
00212     }
00213     /** attach a Callback to Tx Done
00214      *
00215      * @param cb Callback to attach
00216      *
00217      */
00218     void attachTx(Callback<void()> &cb) {
00219         txDone = cb;
00220     }
00221     /** attach a Callback to Rx Done
00222      *
00223      * @param cb Callback to attach
00224      *
00225      */
00226     void attachRx(Callback<void()> &cb) {
00227         rxDone = cb;
00228     }
00229 
00230 
00231 protected:
00232 
00233     /*
00234     * Called by USBDevice layer. Set configuration of the device.
00235     * For instance, you can add all endpoints that you need on this function.
00236     *
00237     * @param configuration Number of the configuration
00238     * @returns true if class handles this request
00239     */
00240     virtual bool USBCallback_setConfiguration(uint8_t configuration);
00241 
00242     /*
00243     * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
00244     * This is used to handle extensions to standard requests
00245     * and class specific requests
00246     *
00247     * @returns true if class handles this request
00248     */
00249     virtual bool USBCallback_request();
00250 
00251     /*
00252     * Get string product descriptor
00253     *
00254     * @returns pointer to the string product descriptor
00255     */
00256     virtual const uint8_t * stringIproductDesc();
00257 
00258     /*
00259     * Get string interface descriptor
00260     *
00261     * @returns pointer to the string interface descriptor
00262     */
00263     virtual const uint8_t * stringIinterfaceDesc();
00264 
00265     /*
00266     * Get configuration descriptor
00267     *
00268     * @returns pointer to the configuration descriptor
00269     */
00270     virtual const uint8_t * configurationDesc();
00271 
00272     /*
00273      * Called by USBDevice layer. Set interface/alternate of the device.
00274      *
00275      * @param interface Number of the interface to be configured
00276      * @param alternate Number of the alternate to be configured
00277      * @returns true if class handles this request
00278      */
00279     virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
00280 
00281     /*
00282     * Called by USBDevice on Endpoint0 request completion
00283     * if the 'notify' flag has been set to true. Warning: Called in ISR context
00284     *
00285     * In this case it is used to indicate that a HID report has
00286     * been received from the host on endpoint 0
00287     *
00288     * @param buf buffer received on endpoint 0
00289     * @param length length of this buffer
00290     */
00291     virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
00292 
00293     /*
00294     * Callback called on each Start of Frame event
00295     */
00296     virtual void SOF(int frameNumber);
00297 
00298     /*
00299     * Callback called when a packet is received
00300     */
00301     virtual bool EPISO_OUT_callback();
00302 
00303     /*
00304     * Callback called when a packet has been sent
00305     */
00306     virtual bool EPISO_IN_callback();
00307 
00308 private:
00309 
00310     /*
00311     * Call to rebuild the configuration descriptor
00312     *
00313     * This function should be called on creation or when any
00314     * value that is part of the configuration descriptor
00315     * changes.
00316     * @note This function uses ~200 bytes of stack so
00317     * make sure your stack is big enough for it.
00318     */
00319     void _build_configurationDesc();
00320 
00321     // configuration descriptor
00322     uint8_t configDescriptor[183];
00323 
00324     // stream available ?
00325     volatile bool available;
00326 
00327     // interrupt OUT has been received
00328     volatile bool interruptOUT;
00329 
00330     // interrupt IN has been received
00331     volatile bool interruptIN;
00332 
00333     // audio packet has been written
00334     volatile bool writeIN;
00335 
00336     // FREQ
00337     uint32_t FREQ_OUT;
00338     uint32_t FREQ_IN;
00339 
00340     // size of the maximum packet for the isochronous endpoint
00341     uint32_t PACKET_SIZE_ISO_IN;
00342     uint32_t PACKET_SIZE_ISO_OUT;
00343 
00344     // mono, stereo,...
00345     uint8_t channel_nb_in;
00346     uint8_t channel_nb_out;
00347 
00348     // channel config: master, left, right
00349     uint8_t channel_config_in;
00350     uint8_t channel_config_out;
00351 
00352     // mute state
00353     uint8_t mute;
00354 
00355     // Volume Current Value
00356     uint16_t volCur;
00357 
00358     // Volume Minimum Value
00359     uint16_t volMin;
00360 
00361     // Volume Maximum Value
00362     uint16_t volMax;
00363 
00364     // Volume Resolution
00365     uint16_t volRes;
00366 
00367     // Buffer containing one audio packet (to be read)
00368     volatile uint8_t * buf_stream_in;
00369 
00370     // Buffer containing one audio packet (to be written)
00371     volatile uint8_t * buf_stream_out;
00372 
00373     // callback to update volume
00374     Callback<void()> updateVol;
00375 
00376     // callback transmit Done
00377     Callback<void()> txDone;
00378     // callback transmit Done
00379     Callback<void()> rxDone;
00380 
00381     // boolean showing that the SOF handler has been called. Useful for readNB.
00382     volatile bool SOF_handler;
00383 
00384     volatile float volume;
00385 
00386 };
00387 
00388 #endif