KL46Z-lab2

Dependencies:   SLCD- mbed TSI MMA8451Q USBDevice MAG3110

Committer:
Lokkus
Date:
Thu Feb 21 07:41:00 2019 +0000
Revision:
0:29277ae50860
KL46Z-lab2

Who changed what in which revision?

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