Maxim nexpaq / nexpaq_dev
Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
nexpaq 0:6c56fb4bc5f0 2 *
nexpaq 0:6c56fb4bc5f0 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
nexpaq 0:6c56fb4bc5f0 4 * and associated documentation files (the "Software"), to deal in the Software without
nexpaq 0:6c56fb4bc5f0 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
nexpaq 0:6c56fb4bc5f0 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
nexpaq 0:6c56fb4bc5f0 7 * Software is furnished to do so, subject to the following conditions:
nexpaq 0:6c56fb4bc5f0 8 *
nexpaq 0:6c56fb4bc5f0 9 * The above copyright notice and this permission notice shall be included in all copies or
nexpaq 0:6c56fb4bc5f0 10 * substantial portions of the Software.
nexpaq 0:6c56fb4bc5f0 11 *
nexpaq 0:6c56fb4bc5f0 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
nexpaq 0:6c56fb4bc5f0 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
nexpaq 0:6c56fb4bc5f0 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
nexpaq 0:6c56fb4bc5f0 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
nexpaq 0:6c56fb4bc5f0 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nexpaq 0:6c56fb4bc5f0 17 */
nexpaq 0:6c56fb4bc5f0 18
nexpaq 0:6c56fb4bc5f0 19 #ifndef USBAudio_H
nexpaq 0:6c56fb4bc5f0 20 #define USBAudio_H
nexpaq 0:6c56fb4bc5f0 21
nexpaq 0:6c56fb4bc5f0 22 /* These headers are included for child class. */
nexpaq 0:6c56fb4bc5f0 23 #include "USBEndpoints.h"
nexpaq 0:6c56fb4bc5f0 24 #include "USBDescriptor.h"
nexpaq 0:6c56fb4bc5f0 25 #include "USBDevice_Types.h"
nexpaq 0:6c56fb4bc5f0 26
nexpaq 0:6c56fb4bc5f0 27 #include "USBDevice.h"
nexpaq 0:6c56fb4bc5f0 28 #include "Callback.h"
nexpaq 0:6c56fb4bc5f0 29
nexpaq 0:6c56fb4bc5f0 30 /**
nexpaq 0:6c56fb4bc5f0 31 * USBAudio example
nexpaq 0:6c56fb4bc5f0 32 *
nexpaq 0:6c56fb4bc5f0 33 * @code
nexpaq 0:6c56fb4bc5f0 34 * #include "mbed.h"
nexpaq 0:6c56fb4bc5f0 35 * #include "USBAudio.h"
nexpaq 0:6c56fb4bc5f0 36 *
nexpaq 0:6c56fb4bc5f0 37 * Serial pc(USBTX, USBRX);
nexpaq 0:6c56fb4bc5f0 38 *
nexpaq 0:6c56fb4bc5f0 39 * // frequency: 48 kHz
nexpaq 0:6c56fb4bc5f0 40 * #define FREQ 48000
nexpaq 0:6c56fb4bc5f0 41 *
nexpaq 0:6c56fb4bc5f0 42 * // 1 channel: mono
nexpaq 0:6c56fb4bc5f0 43 * #define NB_CHA 1
nexpaq 0:6c56fb4bc5f0 44 *
nexpaq 0:6c56fb4bc5f0 45 * // 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
nexpaq 0:6c56fb4bc5f0 46 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1
nexpaq 0:6c56fb4bc5f0 47 *
nexpaq 0:6c56fb4bc5f0 48 * // USBAudio
nexpaq 0:6c56fb4bc5f0 49 * USBAudio audio(FREQ, NB_CHA);
nexpaq 0:6c56fb4bc5f0 50 *
nexpaq 0:6c56fb4bc5f0 51 * int main() {
nexpaq 0:6c56fb4bc5f0 52 * int16_t buf[AUDIO_LENGTH_PACKET/2];
nexpaq 0:6c56fb4bc5f0 53 *
nexpaq 0:6c56fb4bc5f0 54 * while (1) {
nexpaq 0:6c56fb4bc5f0 55 * // read an audio packet
nexpaq 0:6c56fb4bc5f0 56 * audio.read((uint8_t *)buf);
nexpaq 0:6c56fb4bc5f0 57 *
nexpaq 0:6c56fb4bc5f0 58 *
nexpaq 0:6c56fb4bc5f0 59 * // print packet received
nexpaq 0:6c56fb4bc5f0 60 * pc.printf("recv: ");
nexpaq 0:6c56fb4bc5f0 61 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
nexpaq 0:6c56fb4bc5f0 62 * pc.printf("%d ", buf[i]);
nexpaq 0:6c56fb4bc5f0 63 * }
nexpaq 0:6c56fb4bc5f0 64 * pc.printf("\r\n");
nexpaq 0:6c56fb4bc5f0 65 * }
nexpaq 0:6c56fb4bc5f0 66 * }
nexpaq 0:6c56fb4bc5f0 67 * @endcode
nexpaq 0:6c56fb4bc5f0 68 */
nexpaq 0:6c56fb4bc5f0 69 class USBAudio: public USBDevice {
nexpaq 0:6c56fb4bc5f0 70 public:
nexpaq 0:6c56fb4bc5f0 71
nexpaq 0:6c56fb4bc5f0 72 /**
nexpaq 0:6c56fb4bc5f0 73 * Constructor
nexpaq 0:6c56fb4bc5f0 74 *
nexpaq 0:6c56fb4bc5f0 75 * @param frequency_in frequency in Hz (default: 48000)
nexpaq 0:6c56fb4bc5f0 76 * @param channel_nb_in channel number (1 or 2) (default: 1)
nexpaq 0:6c56fb4bc5f0 77 * @param frequency_out frequency in Hz (default: 8000)
nexpaq 0:6c56fb4bc5f0 78 * @param channel_nb_out_in channel number (1 or 2) (default: 1)
nexpaq 0:6c56fb4bc5f0 79 * @param vendor_id Your vendor_id
nexpaq 0:6c56fb4bc5f0 80 * @param product_id Your product_id
nexpaq 0:6c56fb4bc5f0 81 * @param product_release Your preoduct_release
nexpaq 0:6c56fb4bc5f0 82 */
nexpaq 0:6c56fb4bc5f0 83 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);
nexpaq 0:6c56fb4bc5f0 84
nexpaq 0:6c56fb4bc5f0 85 /**
nexpaq 0:6c56fb4bc5f0 86 * Get current volume between 0.0 and 1.0
nexpaq 0:6c56fb4bc5f0 87 *
nexpaq 0:6c56fb4bc5f0 88 * @returns volume
nexpaq 0:6c56fb4bc5f0 89 */
nexpaq 0:6c56fb4bc5f0 90 float getVolume();
nexpaq 0:6c56fb4bc5f0 91
nexpaq 0:6c56fb4bc5f0 92 /**
nexpaq 0:6c56fb4bc5f0 93 * 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
nexpaq 0:6c56fb4bc5f0 94 *
nexpaq 0:6c56fb4bc5f0 95 * @param buf pointer on a buffer which will be filled with an audio packet
nexpaq 0:6c56fb4bc5f0 96 *
nexpaq 0:6c56fb4bc5f0 97 * @returns true if successfull
nexpaq 0:6c56fb4bc5f0 98 */
nexpaq 0:6c56fb4bc5f0 99 bool read(uint8_t * buf);
nexpaq 0:6c56fb4bc5f0 100
nexpaq 0:6c56fb4bc5f0 101 /**
nexpaq 0:6c56fb4bc5f0 102 * 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
nexpaq 0:6c56fb4bc5f0 103 *
nexpaq 0:6c56fb4bc5f0 104 * @param buf pointer on a buffer which will be filled if an audio packet is available
nexpaq 0:6c56fb4bc5f0 105 *
nexpaq 0:6c56fb4bc5f0 106 * @returns true if successfull
nexpaq 0:6c56fb4bc5f0 107 */
nexpaq 0:6c56fb4bc5f0 108 bool readNB(uint8_t * buf);
nexpaq 0:6c56fb4bc5f0 109
nexpaq 0:6c56fb4bc5f0 110 /**
nexpaq 0:6c56fb4bc5f0 111 * 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.
nexpaq 0:6c56fb4bc5f0 112 *
nexpaq 0:6c56fb4bc5f0 113 * @param buf pointer on the audio packet which will be sent
nexpaq 0:6c56fb4bc5f0 114 * @returns true if successful
nexpaq 0:6c56fb4bc5f0 115 */
nexpaq 0:6c56fb4bc5f0 116 bool write(uint8_t * buf);
nexpaq 0:6c56fb4bc5f0 117
nexpaq 0:6c56fb4bc5f0 118 /**
nexpaq 0:6c56fb4bc5f0 119 * Write and read an audio packet at the same time (on the same frame)
nexpaq 0:6c56fb4bc5f0 120 *
nexpaq 0:6c56fb4bc5f0 121 * @param buf_read pointer on a buffer which will be filled with an audio packet
nexpaq 0:6c56fb4bc5f0 122 * @param buf_write pointer on the audio packet which will be sent
nexpaq 0:6c56fb4bc5f0 123 * @returns true if successful
nexpaq 0:6c56fb4bc5f0 124 */
nexpaq 0:6c56fb4bc5f0 125 bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
nexpaq 0:6c56fb4bc5f0 126
nexpaq 0:6c56fb4bc5f0 127
nexpaq 0:6c56fb4bc5f0 128 /** attach a handler to update the volume
nexpaq 0:6c56fb4bc5f0 129 *
nexpaq 0:6c56fb4bc5f0 130 * @param function Function to attach
nexpaq 0:6c56fb4bc5f0 131 *
nexpaq 0:6c56fb4bc5f0 132 */
nexpaq 0:6c56fb4bc5f0 133 void attach(void(*fptr)(void)) {
nexpaq 0:6c56fb4bc5f0 134 updateVol.attach(fptr);
nexpaq 0:6c56fb4bc5f0 135 }
nexpaq 0:6c56fb4bc5f0 136
nexpaq 0:6c56fb4bc5f0 137 /** Attach a nonstatic void/void member function to update the volume
nexpaq 0:6c56fb4bc5f0 138 *
nexpaq 0:6c56fb4bc5f0 139 * @param tptr Object pointer
nexpaq 0:6c56fb4bc5f0 140 * @param mptr Member function pointer
nexpaq 0:6c56fb4bc5f0 141 *
nexpaq 0:6c56fb4bc5f0 142 */
nexpaq 0:6c56fb4bc5f0 143 template<typename T>
nexpaq 0:6c56fb4bc5f0 144 void attach(T *tptr, void(T::*mptr)(void)) {
nexpaq 0:6c56fb4bc5f0 145 updateVol.attach(tptr, mptr);
nexpaq 0:6c56fb4bc5f0 146 }
nexpaq 0:6c56fb4bc5f0 147
nexpaq 0:6c56fb4bc5f0 148
nexpaq 0:6c56fb4bc5f0 149 protected:
nexpaq 0:6c56fb4bc5f0 150
nexpaq 0:6c56fb4bc5f0 151 /*
nexpaq 0:6c56fb4bc5f0 152 * Called by USBDevice layer. Set configuration of the device.
nexpaq 0:6c56fb4bc5f0 153 * For instance, you can add all endpoints that you need on this function.
nexpaq 0:6c56fb4bc5f0 154 *
nexpaq 0:6c56fb4bc5f0 155 * @param configuration Number of the configuration
nexpaq 0:6c56fb4bc5f0 156 * @returns true if class handles this request
nexpaq 0:6c56fb4bc5f0 157 */
nexpaq 0:6c56fb4bc5f0 158 virtual bool USBCallback_setConfiguration(uint8_t configuration);
nexpaq 0:6c56fb4bc5f0 159
nexpaq 0:6c56fb4bc5f0 160 /*
nexpaq 0:6c56fb4bc5f0 161 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
nexpaq 0:6c56fb4bc5f0 162 * This is used to handle extensions to standard requests
nexpaq 0:6c56fb4bc5f0 163 * and class specific requests
nexpaq 0:6c56fb4bc5f0 164 *
nexpaq 0:6c56fb4bc5f0 165 * @returns true if class handles this request
nexpaq 0:6c56fb4bc5f0 166 */
nexpaq 0:6c56fb4bc5f0 167 virtual bool USBCallback_request();
nexpaq 0:6c56fb4bc5f0 168
nexpaq 0:6c56fb4bc5f0 169 /*
nexpaq 0:6c56fb4bc5f0 170 * Get string product descriptor
nexpaq 0:6c56fb4bc5f0 171 *
nexpaq 0:6c56fb4bc5f0 172 * @returns pointer to the string product descriptor
nexpaq 0:6c56fb4bc5f0 173 */
nexpaq 0:6c56fb4bc5f0 174 virtual uint8_t * stringIproductDesc();
nexpaq 0:6c56fb4bc5f0 175
nexpaq 0:6c56fb4bc5f0 176 /*
nexpaq 0:6c56fb4bc5f0 177 * Get string interface descriptor
nexpaq 0:6c56fb4bc5f0 178 *
nexpaq 0:6c56fb4bc5f0 179 * @returns pointer to the string interface descriptor
nexpaq 0:6c56fb4bc5f0 180 */
nexpaq 0:6c56fb4bc5f0 181 virtual uint8_t * stringIinterfaceDesc();
nexpaq 0:6c56fb4bc5f0 182
nexpaq 0:6c56fb4bc5f0 183 /*
nexpaq 0:6c56fb4bc5f0 184 * Get configuration descriptor
nexpaq 0:6c56fb4bc5f0 185 *
nexpaq 0:6c56fb4bc5f0 186 * @returns pointer to the configuration descriptor
nexpaq 0:6c56fb4bc5f0 187 */
nexpaq 0:6c56fb4bc5f0 188 virtual uint8_t * configurationDesc();
nexpaq 0:6c56fb4bc5f0 189
nexpaq 0:6c56fb4bc5f0 190 /*
nexpaq 0:6c56fb4bc5f0 191 * Called by USBDevice layer. Set interface/alternate of the device.
nexpaq 0:6c56fb4bc5f0 192 *
nexpaq 0:6c56fb4bc5f0 193 * @param interface Number of the interface to be configured
nexpaq 0:6c56fb4bc5f0 194 * @param alternate Number of the alternate to be configured
nexpaq 0:6c56fb4bc5f0 195 * @returns true if class handles this request
nexpaq 0:6c56fb4bc5f0 196 */
nexpaq 0:6c56fb4bc5f0 197 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
nexpaq 0:6c56fb4bc5f0 198
nexpaq 0:6c56fb4bc5f0 199 /*
nexpaq 0:6c56fb4bc5f0 200 * Called by USBDevice on Endpoint0 request completion
nexpaq 0:6c56fb4bc5f0 201 * if the 'notify' flag has been set to true. Warning: Called in ISR context
nexpaq 0:6c56fb4bc5f0 202 *
nexpaq 0:6c56fb4bc5f0 203 * In this case it is used to indicate that a HID report has
nexpaq 0:6c56fb4bc5f0 204 * been received from the host on endpoint 0
nexpaq 0:6c56fb4bc5f0 205 *
nexpaq 0:6c56fb4bc5f0 206 * @param buf buffer received on endpoint 0
nexpaq 0:6c56fb4bc5f0 207 * @param length length of this buffer
nexpaq 0:6c56fb4bc5f0 208 */
nexpaq 0:6c56fb4bc5f0 209 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
nexpaq 0:6c56fb4bc5f0 210
nexpaq 0:6c56fb4bc5f0 211 /*
nexpaq 0:6c56fb4bc5f0 212 * Callback called on each Start of Frame event
nexpaq 0:6c56fb4bc5f0 213 */
nexpaq 0:6c56fb4bc5f0 214 virtual void SOF(int frameNumber);
nexpaq 0:6c56fb4bc5f0 215
nexpaq 0:6c56fb4bc5f0 216 /*
nexpaq 0:6c56fb4bc5f0 217 * Callback called when a packet is received
nexpaq 0:6c56fb4bc5f0 218 */
nexpaq 0:6c56fb4bc5f0 219 virtual bool EPISO_OUT_callback();
nexpaq 0:6c56fb4bc5f0 220
nexpaq 0:6c56fb4bc5f0 221 /*
nexpaq 0:6c56fb4bc5f0 222 * Callback called when a packet has been sent
nexpaq 0:6c56fb4bc5f0 223 */
nexpaq 0:6c56fb4bc5f0 224 virtual bool EPISO_IN_callback();
nexpaq 0:6c56fb4bc5f0 225
nexpaq 0:6c56fb4bc5f0 226 private:
nexpaq 0:6c56fb4bc5f0 227
nexpaq 0:6c56fb4bc5f0 228 // stream available ?
nexpaq 0:6c56fb4bc5f0 229 volatile bool available;
nexpaq 0:6c56fb4bc5f0 230
nexpaq 0:6c56fb4bc5f0 231 // interrupt OUT has been received
nexpaq 0:6c56fb4bc5f0 232 volatile bool interruptOUT;
nexpaq 0:6c56fb4bc5f0 233
nexpaq 0:6c56fb4bc5f0 234 // interrupt IN has been received
nexpaq 0:6c56fb4bc5f0 235 volatile bool interruptIN;
nexpaq 0:6c56fb4bc5f0 236
nexpaq 0:6c56fb4bc5f0 237 // audio packet has been written
nexpaq 0:6c56fb4bc5f0 238 volatile bool writeIN;
nexpaq 0:6c56fb4bc5f0 239
nexpaq 0:6c56fb4bc5f0 240 // FREQ
nexpaq 0:6c56fb4bc5f0 241 uint32_t FREQ_OUT;
nexpaq 0:6c56fb4bc5f0 242 uint32_t FREQ_IN;
nexpaq 0:6c56fb4bc5f0 243
nexpaq 0:6c56fb4bc5f0 244 // size of the maximum packet for the isochronous endpoint
nexpaq 0:6c56fb4bc5f0 245 uint32_t PACKET_SIZE_ISO_IN;
nexpaq 0:6c56fb4bc5f0 246 uint32_t PACKET_SIZE_ISO_OUT;
nexpaq 0:6c56fb4bc5f0 247
nexpaq 0:6c56fb4bc5f0 248 // mono, stereo,...
nexpaq 0:6c56fb4bc5f0 249 uint8_t channel_nb_in;
nexpaq 0:6c56fb4bc5f0 250 uint8_t channel_nb_out;
nexpaq 0:6c56fb4bc5f0 251
nexpaq 0:6c56fb4bc5f0 252 // channel config: master, left, right
nexpaq 0:6c56fb4bc5f0 253 uint8_t channel_config_in;
nexpaq 0:6c56fb4bc5f0 254 uint8_t channel_config_out;
nexpaq 0:6c56fb4bc5f0 255
nexpaq 0:6c56fb4bc5f0 256 // mute state
nexpaq 0:6c56fb4bc5f0 257 uint8_t mute;
nexpaq 0:6c56fb4bc5f0 258
nexpaq 0:6c56fb4bc5f0 259 // Volume Current Value
nexpaq 0:6c56fb4bc5f0 260 uint16_t volCur;
nexpaq 0:6c56fb4bc5f0 261
nexpaq 0:6c56fb4bc5f0 262 // Volume Minimum Value
nexpaq 0:6c56fb4bc5f0 263 uint16_t volMin;
nexpaq 0:6c56fb4bc5f0 264
nexpaq 0:6c56fb4bc5f0 265 // Volume Maximum Value
nexpaq 0:6c56fb4bc5f0 266 uint16_t volMax;
nexpaq 0:6c56fb4bc5f0 267
nexpaq 0:6c56fb4bc5f0 268 // Volume Resolution
nexpaq 0:6c56fb4bc5f0 269 uint16_t volRes;
nexpaq 0:6c56fb4bc5f0 270
nexpaq 0:6c56fb4bc5f0 271 // Buffer containing one audio packet (to be read)
nexpaq 0:6c56fb4bc5f0 272 volatile uint8_t * buf_stream_in;
nexpaq 0:6c56fb4bc5f0 273
nexpaq 0:6c56fb4bc5f0 274 // Buffer containing one audio packet (to be written)
nexpaq 0:6c56fb4bc5f0 275 volatile uint8_t * buf_stream_out;
nexpaq 0:6c56fb4bc5f0 276
nexpaq 0:6c56fb4bc5f0 277 // callback to update volume
nexpaq 0:6c56fb4bc5f0 278 Callback<void()> updateVol;
nexpaq 0:6c56fb4bc5f0 279
nexpaq 0:6c56fb4bc5f0 280 // boolean showing that the SOF handler has been called. Useful for readNB.
nexpaq 0:6c56fb4bc5f0 281 volatile bool SOF_handler;
nexpaq 0:6c56fb4bc5f0 282
nexpaq 0:6c56fb4bc5f0 283 volatile float volume;
nexpaq 0:6c56fb4bc5f0 284
nexpaq 0:6c56fb4bc5f0 285 };
nexpaq 0:6c56fb4bc5f0 286
nexpaq 0:6c56fb4bc5f0 287 #endif