Brandon Yee / Mbed 2 deprecated Slingshot

Dependencies:   ADXL345 DebounceIn USBDevice mbed

Committer:
Brandon
Date:
Sun Oct 14 18:58:38 2012 +0000
Revision:
0:cf17ea89fd09
lab3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Brandon 0:cf17ea89fd09 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Brandon 0:cf17ea89fd09 2 *
Brandon 0:cf17ea89fd09 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Brandon 0:cf17ea89fd09 4 * and associated documentation files (the "Software"), to deal in the Software without
Brandon 0:cf17ea89fd09 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Brandon 0:cf17ea89fd09 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Brandon 0:cf17ea89fd09 7 * Software is furnished to do so, subject to the following conditions:
Brandon 0:cf17ea89fd09 8 *
Brandon 0:cf17ea89fd09 9 * The above copyright notice and this permission notice shall be included in all copies or
Brandon 0:cf17ea89fd09 10 * substantial portions of the Software.
Brandon 0:cf17ea89fd09 11 *
Brandon 0:cf17ea89fd09 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Brandon 0:cf17ea89fd09 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Brandon 0:cf17ea89fd09 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Brandon 0:cf17ea89fd09 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Brandon 0:cf17ea89fd09 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Brandon 0:cf17ea89fd09 17 */
Brandon 0:cf17ea89fd09 18
Brandon 0:cf17ea89fd09 19 #ifndef USBDEVICE_H
Brandon 0:cf17ea89fd09 20 #define USBDEVICE_H
Brandon 0:cf17ea89fd09 21
Brandon 0:cf17ea89fd09 22 #include "mbed.h"
Brandon 0:cf17ea89fd09 23 #include "USBDevice_Types.h"
Brandon 0:cf17ea89fd09 24 #include "USBHAL.h"
Brandon 0:cf17ea89fd09 25
Brandon 0:cf17ea89fd09 26 class USBDevice: public USBHAL
Brandon 0:cf17ea89fd09 27 {
Brandon 0:cf17ea89fd09 28 public:
Brandon 0:cf17ea89fd09 29 USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
Brandon 0:cf17ea89fd09 30
Brandon 0:cf17ea89fd09 31 /*
Brandon 0:cf17ea89fd09 32 * Check if the device is configured
Brandon 0:cf17ea89fd09 33 *
Brandon 0:cf17ea89fd09 34 * @returns true if configured, false otherwise
Brandon 0:cf17ea89fd09 35 */
Brandon 0:cf17ea89fd09 36 bool configured(void);
Brandon 0:cf17ea89fd09 37
Brandon 0:cf17ea89fd09 38 /*
Brandon 0:cf17ea89fd09 39 * Connect a device
Brandon 0:cf17ea89fd09 40 */
Brandon 0:cf17ea89fd09 41 void connect(void);
Brandon 0:cf17ea89fd09 42
Brandon 0:cf17ea89fd09 43 /*
Brandon 0:cf17ea89fd09 44 * Disconnect a device
Brandon 0:cf17ea89fd09 45 */
Brandon 0:cf17ea89fd09 46 void disconnect(void);
Brandon 0:cf17ea89fd09 47
Brandon 0:cf17ea89fd09 48 /*
Brandon 0:cf17ea89fd09 49 * Add an endpoint
Brandon 0:cf17ea89fd09 50 *
Brandon 0:cf17ea89fd09 51 * @param endpoint endpoint which will be added
Brandon 0:cf17ea89fd09 52 * @param maxPacket Maximum size of a packet which can be sent for this endpoint
Brandon 0:cf17ea89fd09 53 * @returns true if successful, false otherwise
Brandon 0:cf17ea89fd09 54 */
Brandon 0:cf17ea89fd09 55 bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
Brandon 0:cf17ea89fd09 56
Brandon 0:cf17ea89fd09 57 /*
Brandon 0:cf17ea89fd09 58 * Start a reading on a certain endpoint.
Brandon 0:cf17ea89fd09 59 * You can access the result of the reading by USBDevice_read
Brandon 0:cf17ea89fd09 60 *
Brandon 0:cf17ea89fd09 61 * @param endpoint endpoint which will be read
Brandon 0:cf17ea89fd09 62 * @param maxSize the maximum length that can be read
Brandon 0:cf17ea89fd09 63 * @return true if successful
Brandon 0:cf17ea89fd09 64 */
Brandon 0:cf17ea89fd09 65 bool readStart(uint8_t endpoint, uint32_t maxSize);
Brandon 0:cf17ea89fd09 66
Brandon 0:cf17ea89fd09 67 /*
Brandon 0:cf17ea89fd09 68 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
Brandon 0:cf17ea89fd09 69 * must be called.
Brandon 0:cf17ea89fd09 70 *
Brandon 0:cf17ea89fd09 71 * Warning: blocking
Brandon 0:cf17ea89fd09 72 *
Brandon 0:cf17ea89fd09 73 * @param endpoint endpoint which will be read
Brandon 0:cf17ea89fd09 74 * @param buffer buffer will be filled with the data received
Brandon 0:cf17ea89fd09 75 * @param size the number of bytes read will be stored in *size
Brandon 0:cf17ea89fd09 76 * @param maxSize the maximum length that can be read
Brandon 0:cf17ea89fd09 77 * @returns true if successful
Brandon 0:cf17ea89fd09 78 */
Brandon 0:cf17ea89fd09 79 bool readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
Brandon 0:cf17ea89fd09 80
Brandon 0:cf17ea89fd09 81 /*
Brandon 0:cf17ea89fd09 82 * Read a certain endpoint.
Brandon 0:cf17ea89fd09 83 *
Brandon 0:cf17ea89fd09 84 * Warning: non blocking
Brandon 0:cf17ea89fd09 85 *
Brandon 0:cf17ea89fd09 86 * @param endpoint endpoint which will be read
Brandon 0:cf17ea89fd09 87 * @param buffer buffer will be filled with the data received (if data are available)
Brandon 0:cf17ea89fd09 88 * @param size the number of bytes read will be stored in *size
Brandon 0:cf17ea89fd09 89 * @param maxSize the maximum length that can be read
Brandon 0:cf17ea89fd09 90 * @returns true if successful
Brandon 0:cf17ea89fd09 91 */
Brandon 0:cf17ea89fd09 92 bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
Brandon 0:cf17ea89fd09 93
Brandon 0:cf17ea89fd09 94 /*
Brandon 0:cf17ea89fd09 95 * Write a certain endpoint.
Brandon 0:cf17ea89fd09 96 *
Brandon 0:cf17ea89fd09 97 * Warning: blocking
Brandon 0:cf17ea89fd09 98 *
Brandon 0:cf17ea89fd09 99 * @param endpoint endpoint to write
Brandon 0:cf17ea89fd09 100 * @param buffer data contained in buffer will be write
Brandon 0:cf17ea89fd09 101 * @param size the number of bytes to write
Brandon 0:cf17ea89fd09 102 * @param maxSize the maximum length that can be written on this endpoint
Brandon 0:cf17ea89fd09 103 */
Brandon 0:cf17ea89fd09 104 bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
Brandon 0:cf17ea89fd09 105
Brandon 0:cf17ea89fd09 106
Brandon 0:cf17ea89fd09 107 /*
Brandon 0:cf17ea89fd09 108 * Write a certain endpoint.
Brandon 0:cf17ea89fd09 109 *
Brandon 0:cf17ea89fd09 110 * Warning: non blocking
Brandon 0:cf17ea89fd09 111 *
Brandon 0:cf17ea89fd09 112 * @param endpoint endpoint to write
Brandon 0:cf17ea89fd09 113 * @param buffer data contained in buffer will be write
Brandon 0:cf17ea89fd09 114 * @param size the number of bytes to write
Brandon 0:cf17ea89fd09 115 * @param maxSize the maximum length that can be written on this endpoint
Brandon 0:cf17ea89fd09 116 */
Brandon 0:cf17ea89fd09 117 bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
Brandon 0:cf17ea89fd09 118
Brandon 0:cf17ea89fd09 119
Brandon 0:cf17ea89fd09 120 /*
Brandon 0:cf17ea89fd09 121 * Called by USBDevice layer on bus reset. Warning: Called in ISR context
Brandon 0:cf17ea89fd09 122 *
Brandon 0:cf17ea89fd09 123 * May be used to reset state
Brandon 0:cf17ea89fd09 124 */
Brandon 0:cf17ea89fd09 125 virtual void USBCallback_busReset(void) {};
Brandon 0:cf17ea89fd09 126
Brandon 0:cf17ea89fd09 127 /*
Brandon 0:cf17ea89fd09 128 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
Brandon 0:cf17ea89fd09 129 * This is used to handle extensions to standard requests
Brandon 0:cf17ea89fd09 130 * and class specific requests
Brandon 0:cf17ea89fd09 131 *
Brandon 0:cf17ea89fd09 132 * @returns true if class handles this request
Brandon 0:cf17ea89fd09 133 */
Brandon 0:cf17ea89fd09 134 virtual bool USBCallback_request() { return false; };
Brandon 0:cf17ea89fd09 135
Brandon 0:cf17ea89fd09 136 /*
Brandon 0:cf17ea89fd09 137 * Called by USBDevice on Endpoint0 request completion
Brandon 0:cf17ea89fd09 138 * if the 'notify' flag has been set to true. Warning: Called in ISR context
Brandon 0:cf17ea89fd09 139 *
Brandon 0:cf17ea89fd09 140 * In this case it is used to indicate that a HID report has
Brandon 0:cf17ea89fd09 141 * been received from the host on endpoint 0
Brandon 0:cf17ea89fd09 142 *
Brandon 0:cf17ea89fd09 143 * @param buf buffer received on endpoint 0
Brandon 0:cf17ea89fd09 144 * @param length length of this buffer
Brandon 0:cf17ea89fd09 145 */
Brandon 0:cf17ea89fd09 146 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {};
Brandon 0:cf17ea89fd09 147
Brandon 0:cf17ea89fd09 148 /*
Brandon 0:cf17ea89fd09 149 * Called by USBDevice layer. Set configuration of the device.
Brandon 0:cf17ea89fd09 150 * For instance, you can add all endpoints that you need on this function.
Brandon 0:cf17ea89fd09 151 *
Brandon 0:cf17ea89fd09 152 * @param configuration Number of the configuration
Brandon 0:cf17ea89fd09 153 */
Brandon 0:cf17ea89fd09 154 virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };
Brandon 0:cf17ea89fd09 155
Brandon 0:cf17ea89fd09 156 /*
Brandon 0:cf17ea89fd09 157 * Called by USBDevice layer. Set interface/alternate of the device.
Brandon 0:cf17ea89fd09 158 *
Brandon 0:cf17ea89fd09 159 * @param interface Number of the interface to be configured
Brandon 0:cf17ea89fd09 160 * @param alternate Number of the alternate to be configured
Brandon 0:cf17ea89fd09 161 * @returns true if class handles this request
Brandon 0:cf17ea89fd09 162 */
Brandon 0:cf17ea89fd09 163 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
Brandon 0:cf17ea89fd09 164
Brandon 0:cf17ea89fd09 165 /*
Brandon 0:cf17ea89fd09 166 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
Brandon 0:cf17ea89fd09 167 *
Brandon 0:cf17ea89fd09 168 * @returns pointer to the device descriptor
Brandon 0:cf17ea89fd09 169 */
Brandon 0:cf17ea89fd09 170 virtual uint8_t * deviceDesc();
Brandon 0:cf17ea89fd09 171
Brandon 0:cf17ea89fd09 172 /*
Brandon 0:cf17ea89fd09 173 * Get configuration descriptor
Brandon 0:cf17ea89fd09 174 *
Brandon 0:cf17ea89fd09 175 * @returns pointer to the configuration descriptor
Brandon 0:cf17ea89fd09 176 */
Brandon 0:cf17ea89fd09 177 virtual uint8_t * configurationDesc(){return NULL;};
Brandon 0:cf17ea89fd09 178
Brandon 0:cf17ea89fd09 179 /*
Brandon 0:cf17ea89fd09 180 * Get string lang id descriptor
Brandon 0:cf17ea89fd09 181 *
Brandon 0:cf17ea89fd09 182 * @return pointer to the string lang id descriptor
Brandon 0:cf17ea89fd09 183 */
Brandon 0:cf17ea89fd09 184 virtual uint8_t * stringLangidDesc();
Brandon 0:cf17ea89fd09 185
Brandon 0:cf17ea89fd09 186 /*
Brandon 0:cf17ea89fd09 187 * Get string manufacturer descriptor
Brandon 0:cf17ea89fd09 188 *
Brandon 0:cf17ea89fd09 189 * @returns pointer to the string manufacturer descriptor
Brandon 0:cf17ea89fd09 190 */
Brandon 0:cf17ea89fd09 191 virtual uint8_t * stringImanufacturerDesc();
Brandon 0:cf17ea89fd09 192
Brandon 0:cf17ea89fd09 193 /*
Brandon 0:cf17ea89fd09 194 * Get string product descriptor
Brandon 0:cf17ea89fd09 195 *
Brandon 0:cf17ea89fd09 196 * @returns pointer to the string product descriptor
Brandon 0:cf17ea89fd09 197 */
Brandon 0:cf17ea89fd09 198 virtual uint8_t * stringIproductDesc();
Brandon 0:cf17ea89fd09 199
Brandon 0:cf17ea89fd09 200 /*
Brandon 0:cf17ea89fd09 201 * Get string serial descriptor
Brandon 0:cf17ea89fd09 202 *
Brandon 0:cf17ea89fd09 203 * @returns pointer to the string serial descriptor
Brandon 0:cf17ea89fd09 204 */
Brandon 0:cf17ea89fd09 205 virtual uint8_t * stringIserialDesc();
Brandon 0:cf17ea89fd09 206
Brandon 0:cf17ea89fd09 207 /*
Brandon 0:cf17ea89fd09 208 * Get string configuration descriptor
Brandon 0:cf17ea89fd09 209 *
Brandon 0:cf17ea89fd09 210 * @returns pointer to the string configuration descriptor
Brandon 0:cf17ea89fd09 211 */
Brandon 0:cf17ea89fd09 212 virtual uint8_t * stringIConfigurationDesc();
Brandon 0:cf17ea89fd09 213
Brandon 0:cf17ea89fd09 214 /*
Brandon 0:cf17ea89fd09 215 * Get string interface descriptor
Brandon 0:cf17ea89fd09 216 *
Brandon 0:cf17ea89fd09 217 * @returns pointer to the string interface descriptor
Brandon 0:cf17ea89fd09 218 */
Brandon 0:cf17ea89fd09 219 virtual uint8_t * stringIinterfaceDesc();
Brandon 0:cf17ea89fd09 220
Brandon 0:cf17ea89fd09 221 /*
Brandon 0:cf17ea89fd09 222 * Get the length of the report descriptor
Brandon 0:cf17ea89fd09 223 *
Brandon 0:cf17ea89fd09 224 * @returns length of the report descriptor
Brandon 0:cf17ea89fd09 225 */
Brandon 0:cf17ea89fd09 226 virtual uint16_t reportDescLength() { return 0; };
Brandon 0:cf17ea89fd09 227
Brandon 0:cf17ea89fd09 228
Brandon 0:cf17ea89fd09 229
Brandon 0:cf17ea89fd09 230 protected:
Brandon 0:cf17ea89fd09 231 virtual void busReset(void);
Brandon 0:cf17ea89fd09 232 virtual void EP0setupCallback(void);
Brandon 0:cf17ea89fd09 233 virtual void EP0out(void);
Brandon 0:cf17ea89fd09 234 virtual void EP0in(void);
Brandon 0:cf17ea89fd09 235 virtual void connectStateChanged(unsigned int connected);
Brandon 0:cf17ea89fd09 236 virtual void suspendStateChanged(unsigned int suspended);
Brandon 0:cf17ea89fd09 237 uint8_t * findDescriptor(uint8_t descriptorType);
Brandon 0:cf17ea89fd09 238 CONTROL_TRANSFER * getTransferPtr(void);
Brandon 0:cf17ea89fd09 239
Brandon 0:cf17ea89fd09 240 uint16_t VENDOR_ID;
Brandon 0:cf17ea89fd09 241 uint16_t PRODUCT_ID;
Brandon 0:cf17ea89fd09 242 uint16_t PRODUCT_RELEASE;
Brandon 0:cf17ea89fd09 243
Brandon 0:cf17ea89fd09 244 private:
Brandon 0:cf17ea89fd09 245 bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
Brandon 0:cf17ea89fd09 246 bool requestGetDescriptor(void);
Brandon 0:cf17ea89fd09 247 bool controlOut(void);
Brandon 0:cf17ea89fd09 248 bool controlIn(void);
Brandon 0:cf17ea89fd09 249 bool requestSetAddress(void);
Brandon 0:cf17ea89fd09 250 bool requestSetConfiguration(void);
Brandon 0:cf17ea89fd09 251 bool requestSetFeature(void);
Brandon 0:cf17ea89fd09 252 bool requestClearFeature(void);
Brandon 0:cf17ea89fd09 253 bool requestGetStatus(void);
Brandon 0:cf17ea89fd09 254 bool requestSetup(void);
Brandon 0:cf17ea89fd09 255 bool controlSetup(void);
Brandon 0:cf17ea89fd09 256 void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
Brandon 0:cf17ea89fd09 257 bool requestGetConfiguration(void);
Brandon 0:cf17ea89fd09 258 bool requestGetInterface(void);
Brandon 0:cf17ea89fd09 259 bool requestSetInterface(void);
Brandon 0:cf17ea89fd09 260
Brandon 0:cf17ea89fd09 261 CONTROL_TRANSFER transfer;
Brandon 0:cf17ea89fd09 262 USB_DEVICE device;
Brandon 0:cf17ea89fd09 263
Brandon 0:cf17ea89fd09 264 uint16_t currentInterface;
Brandon 0:cf17ea89fd09 265 uint8_t currentAlternate;
Brandon 0:cf17ea89fd09 266 };
Brandon 0:cf17ea89fd09 267
Brandon 0:cf17ea89fd09 268
Brandon 0:cf17ea89fd09 269 #endif