library

Dependents:   USB_CDC_MSD_Hello

Committer:
sherckuith
Date:
Fri Aug 24 02:01:51 2012 +0000
Revision:
0:d5bb9a9c3e24
[mbed] converted /USB_CDC_MSD_Hello/USBDevice

Who changed what in which revision?

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