SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

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