Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

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