Pinscape Controller version 1 fork. This is a fork to allow for ongoing bug fixes to the original controller version, from before the major changes for the expansion board project.

Dependencies:   FastIO FastPWM SimpleDMA mbed

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Mon Feb 15 23:03:55 2016 +0000
Revision:
68:edfecf67a931
Parent:
52:63f0a9b45f0c
Finalize USB library updates to fix compatibility problems introduced in USBHAL_KL25Z overhaul.

Who changed what in which revision?

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