Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

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