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