LPC11U35 ADC Tick & USBSerial

Dependencies:   mbed

Dependents:   SmallDoseMeter_SingleCH_AE_lpc11u35_V1_00

Committer:
H_Tsunemoto
Date:
Mon Feb 19 08:51:33 2018 +0000
Revision:
0:871ab6846b18
test

Who changed what in which revision?

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