USBDevice

Dependents:   QEI_X1_LCD_test3 macnica_test

Committer:
toucyy
Date:
Thu Apr 18 07:49:37 2013 +0000
Revision:
0:2d8d0b73e1ff
[mbed] converted /QEI_HelloWorld/USBDevice

Who changed what in which revision?

UserRevisionLine numberNew contents of line
toucyy 0:2d8d0b73e1ff 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
toucyy 0:2d8d0b73e1ff 2 *
toucyy 0:2d8d0b73e1ff 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
toucyy 0:2d8d0b73e1ff 4 * and associated documentation files (the "Software"), to deal in the Software without
toucyy 0:2d8d0b73e1ff 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
toucyy 0:2d8d0b73e1ff 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
toucyy 0:2d8d0b73e1ff 7 * Software is furnished to do so, subject to the following conditions:
toucyy 0:2d8d0b73e1ff 8 *
toucyy 0:2d8d0b73e1ff 9 * The above copyright notice and this permission notice shall be included in all copies or
toucyy 0:2d8d0b73e1ff 10 * substantial portions of the Software.
toucyy 0:2d8d0b73e1ff 11 *
toucyy 0:2d8d0b73e1ff 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
toucyy 0:2d8d0b73e1ff 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
toucyy 0:2d8d0b73e1ff 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
toucyy 0:2d8d0b73e1ff 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
toucyy 0:2d8d0b73e1ff 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
toucyy 0:2d8d0b73e1ff 17 */
toucyy 0:2d8d0b73e1ff 18
toucyy 0:2d8d0b73e1ff 19 #ifndef USB_HID_H
toucyy 0:2d8d0b73e1ff 20 #define USB_HID_H
toucyy 0:2d8d0b73e1ff 21
toucyy 0:2d8d0b73e1ff 22 /* These headers are included for child class. */
toucyy 0:2d8d0b73e1ff 23 #include "USBEndpoints.h"
toucyy 0:2d8d0b73e1ff 24 #include "USBDescriptor.h"
toucyy 0:2d8d0b73e1ff 25 #include "USBDevice_Types.h"
toucyy 0:2d8d0b73e1ff 26
toucyy 0:2d8d0b73e1ff 27 #include "USBHID_Types.h"
toucyy 0:2d8d0b73e1ff 28 #include "USBDevice.h"
toucyy 0:2d8d0b73e1ff 29
toucyy 0:2d8d0b73e1ff 30
toucyy 0:2d8d0b73e1ff 31 /**
toucyy 0:2d8d0b73e1ff 32 * USBHID example
toucyy 0:2d8d0b73e1ff 33 * @code
toucyy 0:2d8d0b73e1ff 34 * #include "mbed.h"
toucyy 0:2d8d0b73e1ff 35 * #include "USBHID.h"
toucyy 0:2d8d0b73e1ff 36 *
toucyy 0:2d8d0b73e1ff 37 * USBHID hid;
toucyy 0:2d8d0b73e1ff 38 * HID_REPORT recv;
toucyy 0:2d8d0b73e1ff 39 * BusOut leds(LED1,LED2,LED3,LED4);
toucyy 0:2d8d0b73e1ff 40 *
toucyy 0:2d8d0b73e1ff 41 * int main(void) {
toucyy 0:2d8d0b73e1ff 42 * while (1) {
toucyy 0:2d8d0b73e1ff 43 * hid.read(&recv);
toucyy 0:2d8d0b73e1ff 44 * leds = recv.data[0];
toucyy 0:2d8d0b73e1ff 45 * }
toucyy 0:2d8d0b73e1ff 46 * }
toucyy 0:2d8d0b73e1ff 47 * @endcode
toucyy 0:2d8d0b73e1ff 48 */
toucyy 0:2d8d0b73e1ff 49
toucyy 0:2d8d0b73e1ff 50 class USBHID: public USBDevice {
toucyy 0:2d8d0b73e1ff 51 public:
toucyy 0:2d8d0b73e1ff 52
toucyy 0:2d8d0b73e1ff 53 /**
toucyy 0:2d8d0b73e1ff 54 * Constructor
toucyy 0:2d8d0b73e1ff 55 *
toucyy 0:2d8d0b73e1ff 56 * @param output_report_length Maximum length of a sent report (up to 64 bytes) (default: 64 bytes)
toucyy 0:2d8d0b73e1ff 57 * @param input_report_length Maximum length of a received report (up to 64 bytes) (default: 64 bytes)
toucyy 0:2d8d0b73e1ff 58 * @param vendor_id Your vendor_id
toucyy 0:2d8d0b73e1ff 59 * @param product_id Your product_id
toucyy 0:2d8d0b73e1ff 60 * @param product_release Your preoduct_release
toucyy 0:2d8d0b73e1ff 61 * @param connect Connect the device
toucyy 0:2d8d0b73e1ff 62 */
toucyy 0:2d8d0b73e1ff 63 USBHID(uint8_t output_report_length = 64, uint8_t input_report_length = 64, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0006, uint16_t product_release = 0x0001, bool connect = true);
toucyy 0:2d8d0b73e1ff 64
toucyy 0:2d8d0b73e1ff 65
toucyy 0:2d8d0b73e1ff 66 /**
toucyy 0:2d8d0b73e1ff 67 * Send a Report. warning: blocking
toucyy 0:2d8d0b73e1ff 68 *
toucyy 0:2d8d0b73e1ff 69 * @param report Report which will be sent (a report is defined by all data and the length)
toucyy 0:2d8d0b73e1ff 70 * @returns true if successful
toucyy 0:2d8d0b73e1ff 71 */
toucyy 0:2d8d0b73e1ff 72 bool send(HID_REPORT *report);
toucyy 0:2d8d0b73e1ff 73
toucyy 0:2d8d0b73e1ff 74
toucyy 0:2d8d0b73e1ff 75 /**
toucyy 0:2d8d0b73e1ff 76 * Send a Report. warning: non blocking
toucyy 0:2d8d0b73e1ff 77 *
toucyy 0:2d8d0b73e1ff 78 * @param report Report which will be sent (a report is defined by all data and the length)
toucyy 0:2d8d0b73e1ff 79 * @returns true if successful
toucyy 0:2d8d0b73e1ff 80 */
toucyy 0:2d8d0b73e1ff 81 bool sendNB(HID_REPORT *report);
toucyy 0:2d8d0b73e1ff 82
toucyy 0:2d8d0b73e1ff 83 /**
toucyy 0:2d8d0b73e1ff 84 * Read a report: blocking
toucyy 0:2d8d0b73e1ff 85 *
toucyy 0:2d8d0b73e1ff 86 * @param report pointer to the report to fill
toucyy 0:2d8d0b73e1ff 87 * @returns true if successful
toucyy 0:2d8d0b73e1ff 88 */
toucyy 0:2d8d0b73e1ff 89 bool read(HID_REPORT * report);
toucyy 0:2d8d0b73e1ff 90
toucyy 0:2d8d0b73e1ff 91 /**
toucyy 0:2d8d0b73e1ff 92 * Read a report: non blocking
toucyy 0:2d8d0b73e1ff 93 *
toucyy 0:2d8d0b73e1ff 94 * @param report pointer to the report to fill
toucyy 0:2d8d0b73e1ff 95 * @returns true if successful
toucyy 0:2d8d0b73e1ff 96 */
toucyy 0:2d8d0b73e1ff 97 bool readNB(HID_REPORT * report);
toucyy 0:2d8d0b73e1ff 98
toucyy 0:2d8d0b73e1ff 99 protected:
toucyy 0:2d8d0b73e1ff 100 uint16_t reportLength;
toucyy 0:2d8d0b73e1ff 101
toucyy 0:2d8d0b73e1ff 102 /*
toucyy 0:2d8d0b73e1ff 103 * Get the Report descriptor
toucyy 0:2d8d0b73e1ff 104 *
toucyy 0:2d8d0b73e1ff 105 * @returns pointer to the report descriptor
toucyy 0:2d8d0b73e1ff 106 */
toucyy 0:2d8d0b73e1ff 107 virtual uint8_t * reportDesc();
toucyy 0:2d8d0b73e1ff 108
toucyy 0:2d8d0b73e1ff 109 /*
toucyy 0:2d8d0b73e1ff 110 * Get the length of the report descriptor
toucyy 0:2d8d0b73e1ff 111 *
toucyy 0:2d8d0b73e1ff 112 * @returns the length of the report descriptor
toucyy 0:2d8d0b73e1ff 113 */
toucyy 0:2d8d0b73e1ff 114 virtual uint16_t reportDescLength();
toucyy 0:2d8d0b73e1ff 115
toucyy 0:2d8d0b73e1ff 116 /*
toucyy 0:2d8d0b73e1ff 117 * Get string product descriptor
toucyy 0:2d8d0b73e1ff 118 *
toucyy 0:2d8d0b73e1ff 119 * @returns pointer to the string product descriptor
toucyy 0:2d8d0b73e1ff 120 */
toucyy 0:2d8d0b73e1ff 121 virtual uint8_t * stringIproductDesc();
toucyy 0:2d8d0b73e1ff 122
toucyy 0:2d8d0b73e1ff 123 /*
toucyy 0:2d8d0b73e1ff 124 * Get string interface descriptor
toucyy 0:2d8d0b73e1ff 125 *
toucyy 0:2d8d0b73e1ff 126 * @returns pointer to the string interface descriptor
toucyy 0:2d8d0b73e1ff 127 */
toucyy 0:2d8d0b73e1ff 128 virtual uint8_t * stringIinterfaceDesc();
toucyy 0:2d8d0b73e1ff 129
toucyy 0:2d8d0b73e1ff 130 /*
toucyy 0:2d8d0b73e1ff 131 * Get configuration descriptor
toucyy 0:2d8d0b73e1ff 132 *
toucyy 0:2d8d0b73e1ff 133 * @returns pointer to the configuration descriptor
toucyy 0:2d8d0b73e1ff 134 */
toucyy 0:2d8d0b73e1ff 135 virtual uint8_t * configurationDesc();
toucyy 0:2d8d0b73e1ff 136
toucyy 0:2d8d0b73e1ff 137
toucyy 0:2d8d0b73e1ff 138 /*
toucyy 0:2d8d0b73e1ff 139 * HID Report received by SET_REPORT request. Warning: Called in ISR context
toucyy 0:2d8d0b73e1ff 140 * First byte of data will be the report ID
toucyy 0:2d8d0b73e1ff 141 *
toucyy 0:2d8d0b73e1ff 142 * @param report Data and length received
toucyy 0:2d8d0b73e1ff 143 */
toucyy 0:2d8d0b73e1ff 144 virtual void HID_callbackSetReport(HID_REPORT *report){};
toucyy 0:2d8d0b73e1ff 145
toucyy 0:2d8d0b73e1ff 146
toucyy 0:2d8d0b73e1ff 147 /*
toucyy 0:2d8d0b73e1ff 148 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
toucyy 0:2d8d0b73e1ff 149 * This is used to handle extensions to standard requests
toucyy 0:2d8d0b73e1ff 150 * and class specific requests
toucyy 0:2d8d0b73e1ff 151 *
toucyy 0:2d8d0b73e1ff 152 * @returns true if class handles this request
toucyy 0:2d8d0b73e1ff 153 */
toucyy 0:2d8d0b73e1ff 154 virtual bool USBCallback_request();
toucyy 0:2d8d0b73e1ff 155
toucyy 0:2d8d0b73e1ff 156
toucyy 0:2d8d0b73e1ff 157 /*
toucyy 0:2d8d0b73e1ff 158 * Called by USBDevice layer. Set configuration of the device.
toucyy 0:2d8d0b73e1ff 159 * For instance, you can add all endpoints that you need on this function.
toucyy 0:2d8d0b73e1ff 160 *
toucyy 0:2d8d0b73e1ff 161 * @param configuration Number of the configuration
toucyy 0:2d8d0b73e1ff 162 * @returns true if class handles this request
toucyy 0:2d8d0b73e1ff 163 */
toucyy 0:2d8d0b73e1ff 164 virtual bool USBCallback_setConfiguration(uint8_t configuration);
toucyy 0:2d8d0b73e1ff 165
toucyy 0:2d8d0b73e1ff 166 private:
toucyy 0:2d8d0b73e1ff 167 HID_REPORT outputReport;
toucyy 0:2d8d0b73e1ff 168 uint8_t output_length;
toucyy 0:2d8d0b73e1ff 169 uint8_t input_length;
toucyy 0:2d8d0b73e1ff 170 };
toucyy 0:2d8d0b73e1ff 171
toucyy 0:2d8d0b73e1ff 172 #endif