USB composite device example program, drag-and-drop flash writer.

Dependencies:   SWD USBDevice mbed BaseDAP

Committer:
va009039
Date:
Sat Sep 28 03:21:14 2013 +0000
Revision:
1:ea8e179320d7
add USBMSD_Drop class. add CDC(Virtual COM) and HID(for example CMSIS-DAP), but KL25Z not work.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 1:ea8e179320d7 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
va009039 1:ea8e179320d7 2 *
va009039 1:ea8e179320d7 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
va009039 1:ea8e179320d7 4 * and associated documentation files (the "Software"), to deal in the Software without
va009039 1:ea8e179320d7 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
va009039 1:ea8e179320d7 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
va009039 1:ea8e179320d7 7 * Software is furnished to do so, subject to the following conditions:
va009039 1:ea8e179320d7 8 *
va009039 1:ea8e179320d7 9 * The above copyright notice and this permission notice shall be included in all copies or
va009039 1:ea8e179320d7 10 * substantial portions of the Software.
va009039 1:ea8e179320d7 11 *
va009039 1:ea8e179320d7 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
va009039 1:ea8e179320d7 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
va009039 1:ea8e179320d7 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
va009039 1:ea8e179320d7 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
va009039 1:ea8e179320d7 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
va009039 1:ea8e179320d7 17 */
va009039 1:ea8e179320d7 18
va009039 1:ea8e179320d7 19 #include "stdint.h"
va009039 1:ea8e179320d7 20 #include "USB_HID.h"
va009039 1:ea8e179320d7 21
va009039 1:ea8e179320d7 22 USB_HID::USB_HID(USBDevice* device, uint8_t output_report_length, uint8_t input_report_length) : _device(device)
va009039 1:ea8e179320d7 23 {
va009039 1:ea8e179320d7 24 output_length = output_report_length;
va009039 1:ea8e179320d7 25 input_length = input_report_length;
va009039 1:ea8e179320d7 26 }
va009039 1:ea8e179320d7 27
va009039 1:ea8e179320d7 28 bool USB_HID::send(HID_REPORT *report)
va009039 1:ea8e179320d7 29 {
va009039 1:ea8e179320d7 30 return _device->write(HID_EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
va009039 1:ea8e179320d7 31 }
va009039 1:ea8e179320d7 32
va009039 1:ea8e179320d7 33 bool USB_HID::sendNB(HID_REPORT *report)
va009039 1:ea8e179320d7 34 {
va009039 1:ea8e179320d7 35 return _device->writeNB(HID_EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
va009039 1:ea8e179320d7 36 }
va009039 1:ea8e179320d7 37
va009039 1:ea8e179320d7 38 bool USB_HID::read(HID_REPORT *report)
va009039 1:ea8e179320d7 39 {
va009039 1:ea8e179320d7 40 uint32_t bytesRead = 0;
va009039 1:ea8e179320d7 41 bool result;
va009039 1:ea8e179320d7 42 result = _device->readEP(HID_EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
va009039 1:ea8e179320d7 43 if(!_device->readStart(HID_EPINT_OUT, MAX_HID_REPORT_SIZE))
va009039 1:ea8e179320d7 44 return false;
va009039 1:ea8e179320d7 45 report->length = bytesRead;
va009039 1:ea8e179320d7 46 return result;
va009039 1:ea8e179320d7 47 }
va009039 1:ea8e179320d7 48
va009039 1:ea8e179320d7 49 bool USB_HID::readNB(HID_REPORT *report)
va009039 1:ea8e179320d7 50 {
va009039 1:ea8e179320d7 51 uint32_t bytesRead = 0;
va009039 1:ea8e179320d7 52 bool result;
va009039 1:ea8e179320d7 53 result = _device->readEP_NB(HID_EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
va009039 1:ea8e179320d7 54 report->length = bytesRead;
va009039 1:ea8e179320d7 55 if(!_device->readStart(HID_EPINT_OUT, MAX_HID_REPORT_SIZE))
va009039 1:ea8e179320d7 56 return false;
va009039 1:ea8e179320d7 57 return result;
va009039 1:ea8e179320d7 58 }
va009039 1:ea8e179320d7 59
va009039 1:ea8e179320d7 60 /* virtual */ uint8_t * USB_HID::reportDesc() {
va009039 1:ea8e179320d7 61 static uint8_t reportDescriptor[] = {
va009039 1:ea8e179320d7 62 0x06, 0x00, 0xff,
va009039 1:ea8e179320d7 63 0x09, 0x01, // usage
va009039 1:ea8e179320d7 64 0xA1, 0x01, // Collection 0x01
va009039 1:ea8e179320d7 65 0x15, 0x00, // logical minimum = 0
va009039 1:ea8e179320d7 66 0x26, 0xFF, 0x00, // logical maximum = 255
va009039 1:ea8e179320d7 67 0x75, 0x08, // report size = 8 bits
va009039 1:ea8e179320d7 68 0x95, 0x40, // report count
va009039 1:ea8e179320d7 69 0x09, 0x01, // usage
va009039 1:ea8e179320d7 70 0x81, 0x02, // Input (array)
va009039 1:ea8e179320d7 71 0x95, 0x40, // report count
va009039 1:ea8e179320d7 72 0x09, 0x01, // usage
va009039 1:ea8e179320d7 73 0x91, 0x02, // Output (array)
va009039 1:ea8e179320d7 74 0x95, 0x01, // report count
va009039 1:ea8e179320d7 75 0x09, 0x01, // usage
va009039 1:ea8e179320d7 76 0xb1, 0x02,
va009039 1:ea8e179320d7 77 0xC0 // end collection
va009039 1:ea8e179320d7 78 };
va009039 1:ea8e179320d7 79 reportLength = sizeof(reportDescriptor);
va009039 1:ea8e179320d7 80 return reportDescriptor;
va009039 1:ea8e179320d7 81 }
va009039 1:ea8e179320d7 82
va009039 1:ea8e179320d7 83 /* virtual */ uint16_t USB_HID::reportDescLength() {
va009039 1:ea8e179320d7 84 reportDesc();
va009039 1:ea8e179320d7 85 return reportLength;
va009039 1:ea8e179320d7 86 }
va009039 1:ea8e179320d7 87
va009039 1:ea8e179320d7 88 bool USB_HID::Request_callback(CONTROL_TRANSFER* transfer, uint8_t* hidDescriptor)
va009039 1:ea8e179320d7 89 {
va009039 1:ea8e179320d7 90 // Process additional standard requests
va009039 1:ea8e179320d7 91 if (transfer->setup.bmRequestType.Type == STANDARD_TYPE) {
va009039 1:ea8e179320d7 92 switch (transfer->setup.bRequest) {
va009039 1:ea8e179320d7 93 case GET_DESCRIPTOR:
va009039 1:ea8e179320d7 94 switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) {
va009039 1:ea8e179320d7 95 case REPORT_DESCRIPTOR:
va009039 1:ea8e179320d7 96 if ((reportDesc() != NULL) && (reportDescLength() != 0)) {
va009039 1:ea8e179320d7 97 transfer->remaining = reportDescLength();
va009039 1:ea8e179320d7 98 transfer->ptr = reportDesc();
va009039 1:ea8e179320d7 99 transfer->direction = DEVICE_TO_HOST;
va009039 1:ea8e179320d7 100 return true;
va009039 1:ea8e179320d7 101 }
va009039 1:ea8e179320d7 102 break;
va009039 1:ea8e179320d7 103 case HID_DESCRIPTOR:
va009039 1:ea8e179320d7 104 if (hidDescriptor != NULL) {
va009039 1:ea8e179320d7 105 transfer->remaining = HID_DESCRIPTOR_LENGTH;
va009039 1:ea8e179320d7 106 transfer->ptr = hidDescriptor;
va009039 1:ea8e179320d7 107 transfer->direction = DEVICE_TO_HOST;
va009039 1:ea8e179320d7 108 return true;
va009039 1:ea8e179320d7 109 }
va009039 1:ea8e179320d7 110 break;
va009039 1:ea8e179320d7 111 default:
va009039 1:ea8e179320d7 112 break;
va009039 1:ea8e179320d7 113 }
va009039 1:ea8e179320d7 114 break;
va009039 1:ea8e179320d7 115 default:
va009039 1:ea8e179320d7 116 break;
va009039 1:ea8e179320d7 117 }
va009039 1:ea8e179320d7 118 }
va009039 1:ea8e179320d7 119
va009039 1:ea8e179320d7 120 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
va009039 1:ea8e179320d7 121 switch (transfer->setup.bRequest) {
va009039 1:ea8e179320d7 122 case SET_REPORT:
va009039 1:ea8e179320d7 123 // First byte will be used for report ID
va009039 1:ea8e179320d7 124 outputReport.data[0] = transfer->setup.wValue & 0xff;
va009039 1:ea8e179320d7 125 outputReport.length = transfer->setup.wLength + 1;
va009039 1:ea8e179320d7 126
va009039 1:ea8e179320d7 127 transfer->remaining = sizeof(outputReport.data) - 1;
va009039 1:ea8e179320d7 128 transfer->ptr = &outputReport.data[1];
va009039 1:ea8e179320d7 129 transfer->direction = HOST_TO_DEVICE;
va009039 1:ea8e179320d7 130 transfer->notify = true;
va009039 1:ea8e179320d7 131 return true;
va009039 1:ea8e179320d7 132 }
va009039 1:ea8e179320d7 133 }
va009039 1:ea8e179320d7 134 return false;
va009039 1:ea8e179320d7 135 }