Norimasa Okamoto / Mbed 2 deprecated USBMSD_LPC_HelloWorld

Dependencies:   SWD USBDevice mbed BaseDAP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USB_HID.cpp Source File

USB_HID.cpp

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 #include "stdint.h"
00020 #include "USB_HID.h"
00021 
00022 USB_HID::USB_HID(USBDevice* device, uint8_t output_report_length, uint8_t input_report_length) : _device(device)
00023 {
00024     output_length = output_report_length;
00025     input_length = input_report_length;
00026 }
00027 
00028 bool USB_HID::send(HID_REPORT *report)
00029 {
00030     return _device->write(HID_EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
00031 }
00032 
00033 bool USB_HID::sendNB(HID_REPORT *report)
00034 {
00035     return _device->writeNB(HID_EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
00036 }
00037 
00038 bool USB_HID::read(HID_REPORT *report)
00039 {
00040     uint32_t bytesRead = 0;
00041     bool result;
00042     result = _device->readEP(HID_EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
00043     if(!_device->readStart(HID_EPINT_OUT, MAX_HID_REPORT_SIZE))
00044         return false;
00045     report->length = bytesRead;
00046     return result;
00047 }
00048 
00049 bool USB_HID::readNB(HID_REPORT *report)
00050 {
00051     uint32_t bytesRead = 0;
00052     bool result;
00053     result = _device->readEP_NB(HID_EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
00054     report->length = bytesRead;
00055     if(!_device->readStart(HID_EPINT_OUT, MAX_HID_REPORT_SIZE))
00056         return false;
00057     return result;
00058 }
00059 
00060 /* virtual */ uint8_t * USB_HID::reportDesc() {
00061     static uint8_t reportDescriptor[] = {
00062         0x06, 0x00, 0xff,
00063         0x09, 0x01,         // usage
00064         0xA1, 0x01,         // Collection 0x01
00065         0x15, 0x00,         // logical minimum = 0
00066         0x26, 0xFF, 0x00,   // logical maximum = 255
00067         0x75, 0x08,         // report size = 8 bits
00068         0x95, 0x40,         // report count
00069         0x09, 0x01,         // usage
00070         0x81, 0x02,         // Input (array)
00071         0x95, 0x40,         // report count
00072         0x09, 0x01,         // usage
00073         0x91, 0x02,         // Output (array)
00074         0x95, 0x01,         // report count
00075         0x09, 0x01,         // usage
00076         0xb1, 0x02,
00077         0xC0                // end collection
00078     };
00079     reportLength = sizeof(reportDescriptor);
00080     return reportDescriptor;
00081 }
00082 
00083 /* virtual */ uint16_t USB_HID::reportDescLength() {
00084     reportDesc();
00085     return reportLength;
00086 }
00087 
00088 bool USB_HID::Request_callback(CONTROL_TRANSFER* transfer, uint8_t* hidDescriptor)
00089 {
00090     // Process additional standard requests
00091     if (transfer->setup.bmRequestType.Type == STANDARD_TYPE) {
00092         switch (transfer->setup.bRequest) {
00093             case GET_DESCRIPTOR:
00094                 switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) {
00095                     case REPORT_DESCRIPTOR:
00096                         if ((reportDesc() != NULL) && (reportDescLength() != 0)) {
00097                             transfer->remaining = reportDescLength();
00098                             transfer->ptr = reportDesc();
00099                             transfer->direction = DEVICE_TO_HOST;
00100                             return true;
00101                         }
00102                         break;
00103                     case HID_DESCRIPTOR:
00104                         if (hidDescriptor != NULL) {
00105                             transfer->remaining = HID_DESCRIPTOR_LENGTH;
00106                             transfer->ptr = hidDescriptor;
00107                             transfer->direction = DEVICE_TO_HOST;
00108                             return true;
00109                         }
00110                         break;
00111                     default:
00112                         break;
00113                 }
00114                 break;
00115             default:
00116                 break;
00117         }
00118     }
00119 
00120     if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
00121         switch (transfer->setup.bRequest) {
00122              case SET_REPORT:
00123                 // First byte will be used for report ID
00124                 outputReport.data[0] = transfer->setup.wValue & 0xff;
00125                 outputReport.length = transfer->setup.wLength + 1;
00126 
00127                 transfer->remaining = sizeof(outputReport.data) - 1;
00128                 transfer->ptr = &outputReport.data[1];
00129                 transfer->direction = HOST_TO_DEVICE;
00130                 transfer->notify = true;
00131                 return true;
00132         }
00133     }
00134     return false;
00135 }