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