Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
USBDevice/USBHID/.svn/text-base/RawHID.cpp.svn-base@0:505207de8566, 2011-11-29 (annotated)
- Committer:
- znuh
- Date:
- Tue Nov 29 21:26:20 2011 +0000
- Revision:
- 0:505207de8566
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| znuh | 0:505207de8566 | 1 | /* USBKeyboard.c */ |
| znuh | 0:505207de8566 | 2 | /* USB device example: Standard keyboard */ |
| znuh | 0:505207de8566 | 3 | /* Copyright (c) 2011 ARM Limited. All rights reserved. */ |
| znuh | 0:505207de8566 | 4 | |
| znuh | 0:505207de8566 | 5 | #include "stdint.h" |
| znuh | 0:505207de8566 | 6 | |
| znuh | 0:505207de8566 | 7 | #include "RawHID.h" |
| znuh | 0:505207de8566 | 8 | |
| znuh | 0:505207de8566 | 9 | /* |
| znuh | 0:505207de8566 | 10 | * Descriptors |
| znuh | 0:505207de8566 | 11 | */ |
| znuh | 0:505207de8566 | 12 | |
| znuh | 0:505207de8566 | 13 | #define RAWHID_USAGE_PAGE 0xFFAB // recommended: 0xFF00 to 0xFFFF |
| znuh | 0:505207de8566 | 14 | #define RAWHID_USAGE 0x0200 // recommended: 0x0100 to 0xFFFF |
| znuh | 0:505207de8566 | 15 | |
| znuh | 0:505207de8566 | 16 | uint8_t * RawHID::ReportDesc() { |
| znuh | 0:505207de8566 | 17 | static uint8_t reportDescriptor[] = { |
| znuh | 0:505207de8566 | 18 | 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), |
| znuh | 0:505207de8566 | 19 | 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), |
| znuh | 0:505207de8566 | 20 | 0xA1, 0x01, // Collection 0x01 |
| znuh | 0:505207de8566 | 21 | 0x75, 0x08, // report size = 8 bits |
| znuh | 0:505207de8566 | 22 | 0x15, 0x00, // logical minimum = 0 |
| znuh | 0:505207de8566 | 23 | 0x26, 0xFF, 0x00, // logical maximum = 255 |
| znuh | 0:505207de8566 | 24 | 0x95, 64, // report count |
| znuh | 0:505207de8566 | 25 | 0x09, 0x01, // usage |
| znuh | 0:505207de8566 | 26 | 0x81, 0x02, // Input (array) |
| znuh | 0:505207de8566 | 27 | 0x95, 64, // report count |
| znuh | 0:505207de8566 | 28 | 0x09, 0x02, // usage |
| znuh | 0:505207de8566 | 29 | 0x91, 0x02, // Output (array) |
| znuh | 0:505207de8566 | 30 | 0xC0 // end collection |
| znuh | 0:505207de8566 | 31 | |
| znuh | 0:505207de8566 | 32 | }; |
| znuh | 0:505207de8566 | 33 | reportLength = sizeof(reportDescriptor); |
| znuh | 0:505207de8566 | 34 | return reportDescriptor; |
| znuh | 0:505207de8566 | 35 | } |
| znuh | 0:505207de8566 | 36 | |
| znuh | 0:505207de8566 | 37 | |
| znuh | 0:505207de8566 | 38 | #define DEFAULT_CONFIGURATION (1) |
| znuh | 0:505207de8566 | 39 | #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ |
| znuh | 0:505207de8566 | 40 | + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ |
| znuh | 0:505207de8566 | 41 | + (1 * HID_DESCRIPTOR_LENGTH) \ |
| znuh | 0:505207de8566 | 42 | + (2 * ENDPOINT_DESCRIPTOR_LENGTH)) |
| znuh | 0:505207de8566 | 43 | |
| znuh | 0:505207de8566 | 44 | uint8_t * RawHID::ConfigurationDesc() { |
| znuh | 0:505207de8566 | 45 | static uint8_t configurationDescriptor[] = { |
| znuh | 0:505207de8566 | 46 | CONFIGURATION_DESCRIPTOR_LENGTH,/* bLength */ |
| znuh | 0:505207de8566 | 47 | CONFIGURATION_DESCRIPTOR, /* bDescriptorType */ |
| znuh | 0:505207de8566 | 48 | LSB(TOTAL_DESCRIPTOR_LENGTH), /* wTotalLength (LSB) */ |
| znuh | 0:505207de8566 | 49 | MSB(TOTAL_DESCRIPTOR_LENGTH), /* wTotalLength (MSB) */ |
| znuh | 0:505207de8566 | 50 | 0x01, /* bNumInterfaces */ |
| znuh | 0:505207de8566 | 51 | DEFAULT_CONFIGURATION, /* bConfigurationValue */ |
| znuh | 0:505207de8566 | 52 | 0x00, /* iConfiguration */ |
| znuh | 0:505207de8566 | 53 | C_RESERVED | C_SELF_POWERED, /* bmAttributes */ |
| znuh | 0:505207de8566 | 54 | C_POWER(0), /* bMaxPower */ |
| znuh | 0:505207de8566 | 55 | |
| znuh | 0:505207de8566 | 56 | INTERFACE_DESCRIPTOR_LENGTH, /* bLength */ |
| znuh | 0:505207de8566 | 57 | INTERFACE_DESCRIPTOR, /* bDescriptorType */ |
| znuh | 0:505207de8566 | 58 | 0x00, /* bInterfaceNumber */ |
| znuh | 0:505207de8566 | 59 | 0x00, /* bAlternateSetting */ |
| znuh | 0:505207de8566 | 60 | 0x02, /* bNumEndpoints */ |
| znuh | 0:505207de8566 | 61 | HID_CLASS, /* bInterfaceClass */ |
| znuh | 0:505207de8566 | 62 | HID_SUBCLASS_NONE, /* bInterfaceSubClass */ |
| znuh | 0:505207de8566 | 63 | HID_PROTOCOL_NONE, /* bInterfaceProtocol */ |
| znuh | 0:505207de8566 | 64 | 0x00, /* iInterface */ |
| znuh | 0:505207de8566 | 65 | |
| znuh | 0:505207de8566 | 66 | HID_DESCRIPTOR_LENGTH, /* bLength */ |
| znuh | 0:505207de8566 | 67 | HID_DESCRIPTOR, /* bDescriptorType */ |
| znuh | 0:505207de8566 | 68 | LSB(HID_VERSION_1_11), /* bcdHID (LSB) */ |
| znuh | 0:505207de8566 | 69 | MSB(HID_VERSION_1_11), /* bcdHID (MSB) */ |
| znuh | 0:505207de8566 | 70 | 0x00, /* bCountryCode */ |
| znuh | 0:505207de8566 | 71 | 0x01, /* bNumDescriptors */ |
| znuh | 0:505207de8566 | 72 | REPORT_DESCRIPTOR, /* bDescriptorType */ |
| znuh | 0:505207de8566 | 73 | LSB(this->ReportDescLength()), /* wDescriptorLength (LSB) */ |
| znuh | 0:505207de8566 | 74 | MSB(this->ReportDescLength()), /* wDescriptorLength (MSB) */ |
| znuh | 0:505207de8566 | 75 | |
| znuh | 0:505207de8566 | 76 | ENDPOINT_DESCRIPTOR_LENGTH, /* bLength */ |
| znuh | 0:505207de8566 | 77 | ENDPOINT_DESCRIPTOR, /* bDescriptorType */ |
| znuh | 0:505207de8566 | 78 | PHY_TO_DESC(EPINT_IN), /* bEndpointAddress */ |
| znuh | 0:505207de8566 | 79 | E_INTERRUPT, /* bmAttributes */ |
| znuh | 0:505207de8566 | 80 | LSB(MAX_PACKET_SIZE_EPINT), /* wMaxPacketSize (LSB) */ |
| znuh | 0:505207de8566 | 81 | MSB(MAX_PACKET_SIZE_EPINT), /* wMaxPacketSize (MSB) */ |
| znuh | 0:505207de8566 | 82 | 10, /* bInterval (milliseconds) */ |
| znuh | 0:505207de8566 | 83 | |
| znuh | 0:505207de8566 | 84 | ENDPOINT_DESCRIPTOR_LENGTH, /* bLength */ |
| znuh | 0:505207de8566 | 85 | ENDPOINT_DESCRIPTOR, /* bDescriptorType */ |
| znuh | 0:505207de8566 | 86 | PHY_TO_DESC(EPINT_OUT), /* bEndpointAddress */ |
| znuh | 0:505207de8566 | 87 | E_INTERRUPT, /* bmAttributes */ |
| znuh | 0:505207de8566 | 88 | LSB(MAX_PACKET_SIZE_EPINT), /* wMaxPacketSize (LSB) */ |
| znuh | 0:505207de8566 | 89 | MSB(MAX_PACKET_SIZE_EPINT), /* wMaxPacketSize (MSB) */ |
| znuh | 0:505207de8566 | 90 | 10, /* bInterval (milliseconds) */ |
| znuh | 0:505207de8566 | 91 | }; |
| znuh | 0:505207de8566 | 92 | return configurationDescriptor; |
| znuh | 0:505207de8566 | 93 | } |
| znuh | 0:505207de8566 | 94 | |
| znuh | 0:505207de8566 | 95 | bool RawHID::send(uint8_t * buf, uint8_t length) |
| znuh | 0:505207de8566 | 96 | { |
| znuh | 0:505207de8566 | 97 | HID_REPORT report; |
| znuh | 0:505207de8566 | 98 | for(int i = 0; i < length; i++) |
| znuh | 0:505207de8566 | 99 | report.data[i] = buf[i]; |
| znuh | 0:505207de8566 | 100 | for(int i = length; i < MAX_PACKET_SIZE_EPINT; i++) |
| znuh | 0:505207de8566 | 101 | report.data[i] = 0; |
| znuh | 0:505207de8566 | 102 | report.length = MAX_PACKET_SIZE_EPINT; |
| znuh | 0:505207de8566 | 103 | return USBHID_send(EPINT_IN, &report); |
| znuh | 0:505207de8566 | 104 | |
| znuh | 0:505207de8566 | 105 | } |
| znuh | 0:505207de8566 | 106 | |
| znuh | 0:505207de8566 | 107 | bool RawHID::USBCallback_setConfiguration(uint8_t configuration) { |
| znuh | 0:505207de8566 | 108 | /* Called in ISR context */ |
| znuh | 0:505207de8566 | 109 | |
| znuh | 0:505207de8566 | 110 | /* Set configuration. Return false if the */ |
| znuh | 0:505207de8566 | 111 | /* configuration is not supported. */ |
| znuh | 0:505207de8566 | 112 | if (configuration != DEFAULT_CONFIGURATION) { |
| znuh | 0:505207de8566 | 113 | return false; |
| znuh | 0:505207de8566 | 114 | } |
| znuh | 0:505207de8566 | 115 | |
| znuh | 0:505207de8566 | 116 | /* Configure endpoints > 0 */ |
| znuh | 0:505207de8566 | 117 | USBDevice_addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT); |
| znuh | 0:505207de8566 | 118 | USBDevice_addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT); |
| znuh | 0:505207de8566 | 119 | return true; |
| znuh | 0:505207de8566 | 120 | } |
| znuh | 0:505207de8566 | 121 | |
| znuh | 0:505207de8566 | 122 | bool RawHID::recv(HID_REPORT& report) |
| znuh | 0:505207de8566 | 123 | { |
| znuh | 0:505207de8566 | 124 | return USBHID_read(EPINT_OUT, &report); |
| znuh | 0:505207de8566 | 125 | } |
| znuh | 0:505207de8566 | 126 | |
| znuh | 0:505207de8566 | 127 | bool RawHID::recvNB(HID_REPORT& report) |
| znuh | 0:505207de8566 | 128 | { |
| znuh | 0:505207de8566 | 129 | return USBHID_readNB(EPINT_OUT, &report); |
| znuh | 0:505207de8566 | 130 | } |