USB device stack, with KL25Z fixes for USB 3.0 hosts and sleep/resume interrupt handling

Dependents:   frdm_Slider_Keyboard idd_hw2_figlax_PanType idd_hw2_appachu_finger_chording idd_hw3_AngieWangAntonioDeLimaFernandesDanielLim_BladeSymphony ... more

Fork of USBDevice by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHID.h Source File

USBHID.h

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #ifndef USB_HID_H
00020 #define USB_HID_H
00021 
00022 /* These headers are included for child class. */
00023 #include "USBEndpoints.h"
00024 #include "USBDescriptor.h"
00025 #include "USBDevice_Types.h"
00026 
00027 #include "USBHID_Types.h"
00028 #include "USBDevice.h"
00029 
00030 
00031 /**
00032  * USBHID example
00033  * @code
00034  * #include "mbed.h"
00035  * #include "USBHID.h"
00036  *
00037  * USBHID hid;
00038  * HID_REPORT recv;
00039  * BusOut leds(LED1,LED2,LED3,LED4);
00040  *
00041  * int main(void) {
00042  *    while (1) {
00043  *        hid.read(&recv);
00044  *        leds = recv.data[0];
00045  *    }
00046  * }
00047  * @endcode
00048  */
00049 
00050 class USBHID: public USBDevice {
00051 public:
00052 
00053     /**
00054     * Constructor
00055     *
00056     * @param output_report_length Maximum length of a sent report (up to 64 bytes) (default: 64 bytes)
00057     * @param input_report_length Maximum length of a received report (up to 64 bytes) (default: 64 bytes)
00058     * @param vendor_id Your vendor_id
00059     * @param product_id Your product_id
00060     * @param product_release Your preoduct_release
00061     * @param connect Connect the device
00062     */
00063     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);
00064 
00065 
00066     /**
00067     * Send a Report. warning: blocking
00068     *
00069     * @param report Report which will be sent (a report is defined by all data and the length)
00070     * @returns true if successful
00071     */
00072     bool send(HID_REPORT *report);
00073 
00074 
00075     /**
00076     * Send a Report. warning: non blocking
00077     *
00078     * @param report Report which will be sent (a report is defined by all data and the length)
00079     * @returns true if successful
00080     */
00081     bool sendNB(HID_REPORT *report);
00082     
00083     /**
00084     * Send a Report with timeout.  Blocks until the report has been sent or the timeout
00085     * expires, whichever comes first.
00086     *
00087     * @param report Report to be sent
00088     * @param timeout_ms Timeout in milliseconds
00089     * @returns true if successful
00090     */
00091     bool sendTO(HID_REPORT *report, int timeout_ms);
00092 
00093     /**
00094     * Read a report: blocking
00095     *
00096     * @param report pointer to the report to fill
00097     * @returns true if successful
00098     */
00099     bool read(HID_REPORT * report);
00100 
00101     /**
00102     * Read a report: non blocking
00103     *
00104     * @param report pointer to the report to fill
00105     * @returns true if successful
00106     */
00107     bool readNB(HID_REPORT * report);
00108 
00109 protected:
00110     /*
00111     * Get the Report descriptor
00112     *
00113     * @param idx interface index
00114     * @param len [out] report descriptor length in bytes
00115     * @returns pointer to the report descriptor
00116     */
00117     virtual const uint8_t *reportDesc(int idx, uint16_t &len);
00118 
00119     /*
00120     * Get the length of the report descriptor
00121     *
00122     * @param idx interface index
00123     * @returns the length of the report descriptor
00124     */
00125     uint16_t reportDescLength(int idx)
00126     {
00127         uint16_t len;
00128         (void)reportDesc(idx, len);
00129         return len;
00130     }
00131 
00132     /*
00133     * Get string product descriptor
00134     *
00135     * @returns pointer to the string product descriptor
00136     */
00137     virtual const uint8_t *stringIproductDesc();
00138 
00139     /*
00140     * Get string interface descriptor
00141     *
00142     * @returns pointer to the string interface descriptor
00143     */
00144     virtual const uint8_t *stringIinterfaceDesc();
00145 
00146     /*
00147     * Get configuration descriptor
00148     *
00149     * @returns pointer to the configuration descriptor
00150     */
00151     virtual const uint8_t *configurationDesc();
00152     
00153     /*
00154     * Set the idle time on the given interface.  The idle time is the time between
00155     * INTERRUPT IN reports sent from the device to the host in the absence of any
00156     * updates in values.  E.g., for a keyboard, this is the time between reports
00157     * when there's no new key up/down activity to report.  An infinite idle time
00158     * means that reports are sent only when new activity occurs.
00159     *
00160     * @param ifc Interface index (this specifies which interface is affected, in
00161     * cases where the device has multiple interfaces)
00162     *
00163     * @param reportID Report ID (specifies which report type is affected, in cases
00164     * where the device has multiple report types)
00165     *
00166     * @param t Idle time in 4ms units, with the special case that 0 means infinity.
00167     * The maximum value is 255 units (1.02s).
00168     */
00169     virtual void setIdleTime(int ifc, int reportId, int t) { }
00170     
00171     /*
00172     * Get the idle time on the given interface.  Returns the idle time information
00173     * previously set with setIdleTime().
00174     *
00175     * @param ifc Interface index (specifies which interface is being queried, in
00176     * cases where the device has multiple interfaces)
00177     *
00178     * @param reportID Report ID (specifies which report type is being queried, in
00179     * cases where the device has multiple report types)
00180     *
00181     * @return The idle time currently set on the interface, in 4ms units.  0 means
00182     * infinity (so reports will only be sent when there's new data)
00183     */
00184     virtual uint8_t getIdleTime(int ifc, int reportId) { return 0; }
00185 
00186     /*
00187     * HID Report received by SET_REPORT request. Warning: Called in ISR context
00188     * First byte of data will be the report ID
00189     *
00190     * @param report Data and length received
00191     */
00192     virtual void HID_callbackSetReport(HID_REPORT *report){};
00193 
00194 
00195     /*
00196     * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
00197     * This is used to handle extensions to standard requests
00198     * and class specific requests
00199     *
00200     * @returns true if class handles this request
00201     */
00202     virtual bool USBCallback_request();
00203 
00204 
00205     /*
00206     * Called by USBDevice layer. Set configuration of the device.
00207     * For instance, you can add all endpoints that you need on this function.
00208     *
00209     * @param configuration Number of the configuration
00210     * @returns true if class handles this request
00211     */
00212     virtual bool USBCallback_setConfiguration(uint8_t configuration);
00213 
00214 private:
00215     HID_REPORT outputReport;
00216     uint8_t output_length;
00217     uint8_t input_length;
00218     uint8_t idleData;
00219 };
00220 
00221 #endif