USB device stack

Dependents:   blinky_max32630fthr FTHR_USB_serial FTHR_OLED HSP_RPC_GUI_3_0_1 ... more

Fork of USBDevice by mbed official

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