USBAudio example using a microphone

Dependencies:   USBDevice mbed

Committer:
samux
Date:
Tue Dec 20 10:44:10 2011 +0000
Revision:
5:b49b6a8ca111
Parent:
3:e6a29c83ac52
Child:
6:be128039be16
doesn\'t work but cleaning up...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:539ec61e1fbb 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 0:539ec61e1fbb 2 *
samux 0:539ec61e1fbb 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 0:539ec61e1fbb 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 0:539ec61e1fbb 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 0:539ec61e1fbb 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 0:539ec61e1fbb 7 * Software is furnished to do so, subject to the following conditions:
samux 0:539ec61e1fbb 8 *
samux 0:539ec61e1fbb 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 0:539ec61e1fbb 10 * substantial portions of the Software.
samux 0:539ec61e1fbb 11 *
samux 0:539ec61e1fbb 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 0:539ec61e1fbb 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 0:539ec61e1fbb 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 0:539ec61e1fbb 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 0:539ec61e1fbb 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 0:539ec61e1fbb 17 */
samux 0:539ec61e1fbb 18
samux 0:539ec61e1fbb 19 #ifndef USBAudio_H
samux 0:539ec61e1fbb 20 #define USBAudio_H
samux 0:539ec61e1fbb 21
samux 0:539ec61e1fbb 22 /* These headers are included for child class. */
samux 0:539ec61e1fbb 23 #include "USBEndpoints.h"
samux 0:539ec61e1fbb 24 #include "USBDescriptor.h"
samux 0:539ec61e1fbb 25 #include "USBDevice_Types.h"
samux 0:539ec61e1fbb 26
samux 0:539ec61e1fbb 27 #include "USBDevice.h"
samux 0:539ec61e1fbb 28
samux 0:539ec61e1fbb 29
samux 0:539ec61e1fbb 30 /**
samux 0:539ec61e1fbb 31 * USBAudio example
samux 0:539ec61e1fbb 32 *
samux 0:539ec61e1fbb 33 * #include "mbed.h"
samux 0:539ec61e1fbb 34 * #include "USBAudio.h"
samux 0:539ec61e1fbb 35 *
samux 0:539ec61e1fbb 36 * Serial pc(USBTX, USBRX);
samux 0:539ec61e1fbb 37 *
samux 0:539ec61e1fbb 38 * // frequency: 48 kHz
samux 0:539ec61e1fbb 39 * #define FREQ 48000
samux 0:539ec61e1fbb 40 *
samux 0:539ec61e1fbb 41 * // 1 channel: mono
samux 0:539ec61e1fbb 42 * #define NB_CHA 1
samux 0:539ec61e1fbb 43 *
samux 0:539ec61e1fbb 44 * // 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
samux 0:539ec61e1fbb 45 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1
samux 0:539ec61e1fbb 46 *
samux 0:539ec61e1fbb 47 * // USBAudio
samux 0:539ec61e1fbb 48 * USBAudio audio(FREQ, NB_CHA);
samux 0:539ec61e1fbb 49 *
samux 0:539ec61e1fbb 50 * int main() {
samux 0:539ec61e1fbb 51 * int16_t buf[AUDIO_LENGTH_PACKET/2];
samux 0:539ec61e1fbb 52 *
samux 0:539ec61e1fbb 53 * while (1) {
samux 0:539ec61e1fbb 54 * // read an audio packet
samux 0:539ec61e1fbb 55 * audio.read((uint8_t *)buf);
samux 0:539ec61e1fbb 56 *
samux 0:539ec61e1fbb 57 *
samux 0:539ec61e1fbb 58 * // print packet received
samux 0:539ec61e1fbb 59 * pc.printf("recv: ");
samux 0:539ec61e1fbb 60 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
samux 0:539ec61e1fbb 61 * pc.printf("%d ", buf[i]);
samux 0:539ec61e1fbb 62 * }
samux 0:539ec61e1fbb 63 * pc.printf("\r\n");
samux 0:539ec61e1fbb 64 * }
samux 0:539ec61e1fbb 65 * }
samux 0:539ec61e1fbb 66 * @endcode
samux 0:539ec61e1fbb 67 */
samux 0:539ec61e1fbb 68 class USBAudio: public USBDevice {
samux 0:539ec61e1fbb 69 public:
samux 0:539ec61e1fbb 70
samux 0:539ec61e1fbb 71 /**
samux 0:539ec61e1fbb 72 * Constructor
samux 0:539ec61e1fbb 73 *
samux 0:539ec61e1fbb 74 * @param frequency frequency in Hz (default: 48000)
samux 0:539ec61e1fbb 75 * @param channel_nb channel number (1 or 2) (default: 1)
samux 0:539ec61e1fbb 76 * @param vendor_id Your vendor_id
samux 0:539ec61e1fbb 77 * @param product_id Your product_id
samux 0:539ec61e1fbb 78 * @param product_release Your preoduct_release
samux 0:539ec61e1fbb 79 */
samux 0:539ec61e1fbb 80 USBAudio(uint32_t frequency = 48000, uint8_t channel_nb = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
samux 0:539ec61e1fbb 81
samux 0:539ec61e1fbb 82
samux 0:539ec61e1fbb 83 /**
samux 3:e6a29c83ac52 84 * Write an audio packet
samux 0:539ec61e1fbb 85 *
samux 3:e6a29c83ac52 86 * @param buf audio packet
samux 3:e6a29c83ac52 87 * @returns true if successful
samux 0:539ec61e1fbb 88 */
samux 0:539ec61e1fbb 89 bool write(uint8_t * buf);
samux 0:539ec61e1fbb 90
samux 0:539ec61e1fbb 91
samux 0:539ec61e1fbb 92 protected:
samux 0:539ec61e1fbb 93
samux 0:539ec61e1fbb 94 /*
samux 0:539ec61e1fbb 95 * Called by USBDevice layer. Set configuration of the device.
samux 0:539ec61e1fbb 96 * For instance, you can add all endpoints that you need on this function.
samux 0:539ec61e1fbb 97 *
samux 0:539ec61e1fbb 98 * @param configuration Number of the configuration
samux 0:539ec61e1fbb 99 * @returns true if class handles this request
samux 0:539ec61e1fbb 100 */
samux 0:539ec61e1fbb 101 virtual bool USBCallback_setConfiguration(uint8_t configuration);
samux 0:539ec61e1fbb 102
samux 0:539ec61e1fbb 103 /*
samux 0:539ec61e1fbb 104 * Get string product descriptor
samux 0:539ec61e1fbb 105 *
samux 0:539ec61e1fbb 106 * @returns pointer to the string product descriptor
samux 0:539ec61e1fbb 107 */
samux 0:539ec61e1fbb 108 virtual uint8_t * stringIproductDesc();
samux 0:539ec61e1fbb 109
samux 0:539ec61e1fbb 110 /*
samux 0:539ec61e1fbb 111 * Get string interface descriptor
samux 0:539ec61e1fbb 112 *
samux 0:539ec61e1fbb 113 * @returns pointer to the string interface descriptor
samux 0:539ec61e1fbb 114 */
samux 0:539ec61e1fbb 115 virtual uint8_t * stringIinterfaceDesc();
samux 0:539ec61e1fbb 116
samux 0:539ec61e1fbb 117 /*
samux 0:539ec61e1fbb 118 * Get configuration descriptor
samux 0:539ec61e1fbb 119 *
samux 0:539ec61e1fbb 120 * @returns pointer to the configuration descriptor
samux 0:539ec61e1fbb 121 */
samux 0:539ec61e1fbb 122 virtual uint8_t * configurationDesc();
samux 0:539ec61e1fbb 123
samux 0:539ec61e1fbb 124 /*
samux 0:539ec61e1fbb 125 * Called by USBDevice layer. Set interface/alternate of the device.
samux 0:539ec61e1fbb 126 *
samux 0:539ec61e1fbb 127 * @param interface Number of the interface to be configured
samux 0:539ec61e1fbb 128 * @param alternate Number of the alternate to be configured
samux 0:539ec61e1fbb 129 * @returns true if class handles this request
samux 0:539ec61e1fbb 130 */
samux 0:539ec61e1fbb 131 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
samux 0:539ec61e1fbb 132
samux 0:539ec61e1fbb 133
samux 0:539ec61e1fbb 134 /*
samux 0:539ec61e1fbb 135 * Callback called on each Start of Frame event
samux 0:539ec61e1fbb 136 */
samux 0:539ec61e1fbb 137 virtual void SOF(int frameNumber);
samux 3:e6a29c83ac52 138
samux 3:e6a29c83ac52 139 /*
samux 3:e6a29c83ac52 140 * Callback called when a packet has been sent
samux 3:e6a29c83ac52 141 */
samux 0:539ec61e1fbb 142 virtual bool EP3_IN_callback();
samux 0:539ec61e1fbb 143
samux 0:539ec61e1fbb 144 private:
samux 0:539ec61e1fbb 145
samux 0:539ec61e1fbb 146 // stream available ?
samux 0:539ec61e1fbb 147 volatile bool available;
samux 0:539ec61e1fbb 148
samux 0:539ec61e1fbb 149 // FREQ
samux 0:539ec61e1fbb 150 uint32_t FREQ;
samux 0:539ec61e1fbb 151
samux 0:539ec61e1fbb 152 // size of the maximum packet for the isochronous endpoint
samux 0:539ec61e1fbb 153 uint32_t PACKET_SIZE_ISO;
samux 0:539ec61e1fbb 154
samux 0:539ec61e1fbb 155 // mono, stereo,...
samux 0:539ec61e1fbb 156 uint8_t channel_nb;
samux 0:539ec61e1fbb 157
samux 0:539ec61e1fbb 158 // channel config: master, left, right
samux 0:539ec61e1fbb 159 uint8_t channel_config;
samux 0:539ec61e1fbb 160
samux 0:539ec61e1fbb 161 // mute state
samux 0:539ec61e1fbb 162 uint8_t mute;
samux 0:539ec61e1fbb 163
samux 0:539ec61e1fbb 164 // Volume Current Value
samux 0:539ec61e1fbb 165 uint16_t volCur;
samux 0:539ec61e1fbb 166
samux 0:539ec61e1fbb 167 // Volume Minimum Value
samux 0:539ec61e1fbb 168 uint16_t volMin;
samux 0:539ec61e1fbb 169
samux 0:539ec61e1fbb 170 // Volume Maximum Value
samux 0:539ec61e1fbb 171 uint16_t volMax;
samux 0:539ec61e1fbb 172
samux 0:539ec61e1fbb 173 // Volume Resolution
samux 0:539ec61e1fbb 174 uint16_t volRes;
samux 0:539ec61e1fbb 175
samux 0:539ec61e1fbb 176 // Buffer containing one audio packet
samux 0:539ec61e1fbb 177 uint8_t * buf_stream;
samux 0:539ec61e1fbb 178
samux 0:539ec61e1fbb 179 // callback to update volume
samux 0:539ec61e1fbb 180 FunctionPointer updateVol;
samux 0:539ec61e1fbb 181
samux 0:539ec61e1fbb 182 // boolean showing that the SOF handler has been called. Useful for readNB.
samux 0:539ec61e1fbb 183 volatile bool SOF_handler;
samux 0:539ec61e1fbb 184
samux 5:b49b6a8ca111 185 // Is there an interrupt
samux 0:539ec61e1fbb 186 volatile bool interruptIN;
samux 5:b49b6a8ca111 187
samux 5:b49b6a8ca111 188 // write has been done
samux 5:b49b6a8ca111 189 volatile bool writeIN;
samux 0:539ec61e1fbb 190
samux 0:539ec61e1fbb 191 };
samux 0:539ec61e1fbb 192
samux 0:539ec61e1fbb 193 #endif