KL46Z-lab2

Dependencies:   SLCD- mbed TSI MMA8451Q USBDevice MAG3110

Committer:
Lokkus
Date:
Thu Feb 21 07:41:00 2019 +0000
Revision:
0:29277ae50860
KL46Z-lab2

Who changed what in which revision?

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