ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

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     /**
00127      * Write packet in endpoint fifo. assuming tx fifo is empty
00128      * @param buf pointer on the audio packet which will be sent
00129      */
00130     void writeSync(uint8_t *buf);
00131 
00132     /**
00133     * Write and read an audio packet at the same time (on the same frame)
00134     *
00135     * @param buf_read pointer on a buffer which will be filled with an audio packet
00136     * @param buf_write pointer on the audio packet which will be sent
00137     * @returns true if successful
00138     */
00139     bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
00140 
00141 
00142     /** attach a handler to update the volume
00143      *
00144      * @param function Function to attach
00145      *
00146      */
00147     void attach(void(*fptr)(void)) {
00148         updateVol.attach(fptr);
00149     }
00150     /** attach a handler to Tx Done
00151      *
00152      * @param function Function to attach
00153      *
00154      */
00155     void attachTx(void(*fptr)(void)) {
00156         txDone.attach(fptr);
00157     }
00158     /** attach a handler to Rx Done
00159      *
00160      * @param function Function to attach
00161      *
00162      */
00163     void attachRx(void(*fptr)(void)) {
00164         rxDone.attach(fptr);
00165     }
00166 
00167     /** Attach a nonstatic void/void member function to update the volume
00168      *
00169      * @param tptr Object pointer
00170      * @param mptr Member function pointer
00171      *
00172      */
00173     template<typename T>
00174     void attach(T *tptr, void(T::*mptr)(void)) {
00175         updateVol.attach(tptr, mptr);
00176     }
00177     template<typename T>
00178     void attachTx(T *tptr, void(T::*mptr)(void)) {
00179         txDone.attach(tptr, mptr);
00180     }
00181     template<typename T>
00182     void attachRx(T *tptr, void(T::*mptr)(void)) {
00183         rxDone.attach(tptr, mptr);
00184     }
00185 
00186 
00187 protected:
00188 
00189     /*
00190     * Called by USBDevice layer. Set configuration of the device.
00191     * For instance, you can add all endpoints that you need on this function.
00192     *
00193     * @param configuration Number of the configuration
00194     * @returns true if class handles this request
00195     */
00196     virtual bool USBCallback_setConfiguration(uint8_t configuration);
00197 
00198     /*
00199     * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
00200     * This is used to handle extensions to standard requests
00201     * and class specific requests
00202     *
00203     * @returns true if class handles this request
00204     */
00205     virtual bool USBCallback_request();
00206 
00207     /*
00208     * Get string product descriptor
00209     *
00210     * @returns pointer to the string product descriptor
00211     */
00212     virtual uint8_t * stringIproductDesc();
00213 
00214     /*
00215     * Get string interface descriptor
00216     *
00217     * @returns pointer to the string interface descriptor
00218     */
00219     virtual uint8_t * stringIinterfaceDesc();
00220 
00221     /*
00222     * Get configuration descriptor
00223     *
00224     * @returns pointer to the configuration descriptor
00225     */
00226     virtual uint8_t * configurationDesc();
00227 
00228     /*
00229      * Called by USBDevice layer. Set interface/alternate of the device.
00230      *
00231      * @param interface Number of the interface to be configured
00232      * @param alternate Number of the alternate to be configured
00233      * @returns true if class handles this request
00234      */
00235     virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
00236 
00237     /*
00238     * Called by USBDevice on Endpoint0 request completion
00239     * if the 'notify' flag has been set to true. Warning: Called in ISR context
00240     *
00241     * In this case it is used to indicate that a HID report has
00242     * been received from the host on endpoint 0
00243     *
00244     * @param buf buffer received on endpoint 0
00245     * @param length length of this buffer
00246     */
00247     virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
00248 
00249     /*
00250     * Callback called on each Start of Frame event
00251     */
00252     virtual void SOF(int frameNumber);
00253 
00254     /*
00255     * Callback called when a packet is received
00256     */
00257     virtual bool EPISO_OUT_callback();
00258 
00259     /*
00260     * Callback called when a packet has been sent
00261     */
00262     virtual bool EPISO_IN_callback();
00263 
00264 private:
00265 
00266     // stream available ?
00267     volatile bool available;
00268 
00269     // interrupt OUT has been received
00270     volatile bool interruptOUT;
00271 
00272     // interrupt IN has been received
00273     volatile bool interruptIN;
00274 
00275     // audio packet has been written
00276     volatile bool writeIN;
00277 
00278     // FREQ
00279     uint32_t FREQ_OUT;
00280     uint32_t FREQ_IN;
00281 
00282     // size of the maximum packet for the isochronous endpoint
00283     uint32_t PACKET_SIZE_ISO_IN;
00284     uint32_t PACKET_SIZE_ISO_OUT;
00285 
00286     // mono, stereo,...
00287     uint8_t channel_nb_in;
00288     uint8_t channel_nb_out;
00289 
00290     // channel config: master, left, right
00291     uint8_t channel_config_in;
00292     uint8_t channel_config_out;
00293 
00294     // mute state
00295     uint8_t mute;
00296 
00297     // Volume Current Value
00298     uint16_t volCur;
00299 
00300     // Volume Minimum Value
00301     uint16_t volMin;
00302 
00303     // Volume Maximum Value
00304     uint16_t volMax;
00305 
00306     // Volume Resolution
00307     uint16_t volRes;
00308 
00309     // Buffer containing one audio packet (to be read)
00310     volatile uint8_t * buf_stream_in;
00311 
00312     // Buffer containing one audio packet (to be written)
00313     volatile uint8_t * buf_stream_out;
00314 
00315     // callback to update volume
00316     Callback<void()> updateVol;
00317 
00318     // callback transmit Done
00319     Callback<void()> txDone;
00320     // callback transmit Done
00321     Callback<void()> rxDone;
00322 
00323     // boolean showing that the SOF handler has been called. Useful for readNB.
00324     volatile bool SOF_handler;
00325 
00326     volatile float volume;
00327 
00328 };
00329 
00330 #endif