Android Open Accessory Library化してみました。 バルク転送の使い方が分かっていないため、相変わらず動作は不安定。 I do not understand the usage of the bulk transfer. Please teach someone.
USBHost.h@0:4fcfc05943ff, 2011-06-03 (annotated)
- Committer:
- abe00makoto
- Date:
- Fri Jun 03 17:43:15 2011 +0000
- Revision:
- 0:4fcfc05943ff
Android Open Accessory Library+ demokit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abe00makoto | 0:4fcfc05943ff | 1 | |
abe00makoto | 0:4fcfc05943ff | 2 | /* |
abe00makoto | 0:4fcfc05943ff | 3 | Copyright (c) 2010 Peter Barrett |
abe00makoto | 0:4fcfc05943ff | 4 | |
abe00makoto | 0:4fcfc05943ff | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
abe00makoto | 0:4fcfc05943ff | 6 | of this software and associated documentation files (the "Software"), to deal |
abe00makoto | 0:4fcfc05943ff | 7 | in the Software without restriction, including without limitation the rights |
abe00makoto | 0:4fcfc05943ff | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
abe00makoto | 0:4fcfc05943ff | 9 | copies of the Software, and to permit persons to whom the Software is |
abe00makoto | 0:4fcfc05943ff | 10 | furnished to do so, subject to the following conditions: |
abe00makoto | 0:4fcfc05943ff | 11 | |
abe00makoto | 0:4fcfc05943ff | 12 | The above copyright notice and this permission notice shall be included in |
abe00makoto | 0:4fcfc05943ff | 13 | all copies or substantial portions of the Software. |
abe00makoto | 0:4fcfc05943ff | 14 | |
abe00makoto | 0:4fcfc05943ff | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
abe00makoto | 0:4fcfc05943ff | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
abe00makoto | 0:4fcfc05943ff | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
abe00makoto | 0:4fcfc05943ff | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
abe00makoto | 0:4fcfc05943ff | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
abe00makoto | 0:4fcfc05943ff | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
abe00makoto | 0:4fcfc05943ff | 21 | THE SOFTWARE. |
abe00makoto | 0:4fcfc05943ff | 22 | */ |
abe00makoto | 0:4fcfc05943ff | 23 | |
abe00makoto | 0:4fcfc05943ff | 24 | #ifndef USBHOST_H |
abe00makoto | 0:4fcfc05943ff | 25 | #define USBHOST_H |
abe00makoto | 0:4fcfc05943ff | 26 | |
abe00makoto | 0:4fcfc05943ff | 27 | //log mode on off |
abe00makoto | 0:4fcfc05943ff | 28 | //#define USBHOST_LOG |
abe00makoto | 0:4fcfc05943ff | 29 | |
abe00makoto | 0:4fcfc05943ff | 30 | |
abe00makoto | 0:4fcfc05943ff | 31 | #ifndef u8 |
abe00makoto | 0:4fcfc05943ff | 32 | typedef unsigned char u8; |
abe00makoto | 0:4fcfc05943ff | 33 | typedef unsigned short u16; |
abe00makoto | 0:4fcfc05943ff | 34 | typedef unsigned long u32; |
abe00makoto | 0:4fcfc05943ff | 35 | |
abe00makoto | 0:4fcfc05943ff | 36 | typedef char s8; |
abe00makoto | 0:4fcfc05943ff | 37 | typedef short s16; |
abe00makoto | 0:4fcfc05943ff | 38 | typedef char s32; |
abe00makoto | 0:4fcfc05943ff | 39 | #endif |
abe00makoto | 0:4fcfc05943ff | 40 | |
abe00makoto | 0:4fcfc05943ff | 41 | #define ENDPOINT_CONTROL 0 |
abe00makoto | 0:4fcfc05943ff | 42 | #define ENDPOINT_ISOCRONOUS 1 |
abe00makoto | 0:4fcfc05943ff | 43 | #define ENDPOINT_BULK 2 |
abe00makoto | 0:4fcfc05943ff | 44 | #define ENDPOINT_INTERRUPT 3 |
abe00makoto | 0:4fcfc05943ff | 45 | |
abe00makoto | 0:4fcfc05943ff | 46 | #define DESCRIPTOR_TYPE_DEVICE 1 |
abe00makoto | 0:4fcfc05943ff | 47 | #define DESCRIPTOR_TYPE_CONFIGURATION 2 |
abe00makoto | 0:4fcfc05943ff | 48 | #define DESCRIPTOR_TYPE_STRING 3 |
abe00makoto | 0:4fcfc05943ff | 49 | #define DESCRIPTOR_TYPE_INTERFACE 4 |
abe00makoto | 0:4fcfc05943ff | 50 | #define DESCRIPTOR_TYPE_ENDPOINT 5 |
abe00makoto | 0:4fcfc05943ff | 51 | |
abe00makoto | 0:4fcfc05943ff | 52 | #define DESCRIPTOR_TYPE_HID 0x21 |
abe00makoto | 0:4fcfc05943ff | 53 | #define DESCRIPTOR_TYPE_REPORT 0x22 |
abe00makoto | 0:4fcfc05943ff | 54 | #define DESCRIPTOR_TYPE_PHYSICAL 0x23 |
abe00makoto | 0:4fcfc05943ff | 55 | #define DESCRIPTOR_TYPE_HUB 0x29 |
abe00makoto | 0:4fcfc05943ff | 56 | |
abe00makoto | 0:4fcfc05943ff | 57 | enum USB_CLASS_CODE |
abe00makoto | 0:4fcfc05943ff | 58 | { |
abe00makoto | 0:4fcfc05943ff | 59 | CLASS_DEVICE, |
abe00makoto | 0:4fcfc05943ff | 60 | CLASS_AUDIO, |
abe00makoto | 0:4fcfc05943ff | 61 | CLASS_COMM_AND_CDC_CONTROL, |
abe00makoto | 0:4fcfc05943ff | 62 | CLASS_HID, |
abe00makoto | 0:4fcfc05943ff | 63 | CLASS_PHYSICAL = 0x05, |
abe00makoto | 0:4fcfc05943ff | 64 | CLASS_STILL_IMAGING, |
abe00makoto | 0:4fcfc05943ff | 65 | CLASS_PRINTER, |
abe00makoto | 0:4fcfc05943ff | 66 | CLASS_MASS_STORAGE, |
abe00makoto | 0:4fcfc05943ff | 67 | CLASS_HUB, |
abe00makoto | 0:4fcfc05943ff | 68 | CLASS_CDC_DATA, |
abe00makoto | 0:4fcfc05943ff | 69 | CLASS_SMART_CARD, |
abe00makoto | 0:4fcfc05943ff | 70 | CLASS_CONTENT_SECURITY = 0x0D, |
abe00makoto | 0:4fcfc05943ff | 71 | CLASS_VIDEO = 0x0E, |
abe00makoto | 0:4fcfc05943ff | 72 | CLASS_DIAGNOSTIC_DEVICE = 0xDC, |
abe00makoto | 0:4fcfc05943ff | 73 | CLASS_WIRELESS_CONTROLLER = 0xE0, |
abe00makoto | 0:4fcfc05943ff | 74 | CLASS_MISCELLANEOUS = 0xEF, |
abe00makoto | 0:4fcfc05943ff | 75 | CLASS_APP_SPECIFIC = 0xFE, |
abe00makoto | 0:4fcfc05943ff | 76 | CLASS_VENDOR_SPECIFIC = 0xFF |
abe00makoto | 0:4fcfc05943ff | 77 | }; |
abe00makoto | 0:4fcfc05943ff | 78 | |
abe00makoto | 0:4fcfc05943ff | 79 | #define DEVICE_TO_HOST 0x80 |
abe00makoto | 0:4fcfc05943ff | 80 | #define HOST_TO_DEVICE 0x00 |
abe00makoto | 0:4fcfc05943ff | 81 | #define REQUEST_TYPE_STANDARD 0x00 |
abe00makoto | 0:4fcfc05943ff | 82 | #define REQUEST_TYPE_CLASS 0x20 |
abe00makoto | 0:4fcfc05943ff | 83 | #define REQUEST_TYPE_VENDOR 0x40 |
abe00makoto | 0:4fcfc05943ff | 84 | #define RECIPIENT_DEVICE 0x00 |
abe00makoto | 0:4fcfc05943ff | 85 | #define RECIPIENT_INTERFACE 0x01 |
abe00makoto | 0:4fcfc05943ff | 86 | #define RECIPIENT_ENDPOINT 0x02 |
abe00makoto | 0:4fcfc05943ff | 87 | #define RECIPIENT_OTHER 0x03 |
abe00makoto | 0:4fcfc05943ff | 88 | |
abe00makoto | 0:4fcfc05943ff | 89 | #define GET_STATUS 0 |
abe00makoto | 0:4fcfc05943ff | 90 | #define CLEAR_FEATURE 1 |
abe00makoto | 0:4fcfc05943ff | 91 | #define SET_FEATURE 3 |
abe00makoto | 0:4fcfc05943ff | 92 | #define SET_ADDRESS 5 |
abe00makoto | 0:4fcfc05943ff | 93 | #define GET_DESCRIPTOR 6 |
abe00makoto | 0:4fcfc05943ff | 94 | #define SET_DESCRIPTOR 7 |
abe00makoto | 0:4fcfc05943ff | 95 | #define GET_CONFIGURATION 8 |
abe00makoto | 0:4fcfc05943ff | 96 | #define SET_CONFIGURATION 9 |
abe00makoto | 0:4fcfc05943ff | 97 | #define GET_INTERFACE 10 |
abe00makoto | 0:4fcfc05943ff | 98 | #define SET_INTERFACE 11 |
abe00makoto | 0:4fcfc05943ff | 99 | #define SYNCH_FRAME 11 |
abe00makoto | 0:4fcfc05943ff | 100 | |
abe00makoto | 0:4fcfc05943ff | 101 | /* HID Request Codes */ |
abe00makoto | 0:4fcfc05943ff | 102 | #define HID_REQUEST_GET_REPORT 0x01 |
abe00makoto | 0:4fcfc05943ff | 103 | #define HID_REQUEST_GET_IDLE 0x02 |
abe00makoto | 0:4fcfc05943ff | 104 | #define HID_REQUEST_GET_PROTOCOL 0x03 |
abe00makoto | 0:4fcfc05943ff | 105 | #define HID_REQUEST_SET_REPORT 0x09 |
abe00makoto | 0:4fcfc05943ff | 106 | #define HID_REQUEST_SET_IDLE 0x0A |
abe00makoto | 0:4fcfc05943ff | 107 | #define HID_REQUEST_SET_PROTOCOL 0x0B |
abe00makoto | 0:4fcfc05943ff | 108 | |
abe00makoto | 0:4fcfc05943ff | 109 | /* HID Report Types */ |
abe00makoto | 0:4fcfc05943ff | 110 | #define HID_REPORT_INPUT 0x01 |
abe00makoto | 0:4fcfc05943ff | 111 | #define HID_REPORT_OUTPUT 0x02 |
abe00makoto | 0:4fcfc05943ff | 112 | #define HID_REPORT_FEATURE 0x03 |
abe00makoto | 0:4fcfc05943ff | 113 | |
abe00makoto | 0:4fcfc05943ff | 114 | |
abe00makoto | 0:4fcfc05943ff | 115 | // -5 is nak |
abe00makoto | 0:4fcfc05943ff | 116 | /* |
abe00makoto | 0:4fcfc05943ff | 117 | 0010 ACK Handshake |
abe00makoto | 0:4fcfc05943ff | 118 | 1010 NAK Handshake |
abe00makoto | 0:4fcfc05943ff | 119 | 1110 STALL Handshake |
abe00makoto | 0:4fcfc05943ff | 120 | 0110 NYET (No Response Yet) |
abe00makoto | 0:4fcfc05943ff | 121 | */ |
abe00makoto | 0:4fcfc05943ff | 122 | |
abe00makoto | 0:4fcfc05943ff | 123 | #define IO_PENDING -100 |
abe00makoto | 0:4fcfc05943ff | 124 | #define ERR_ENDPOINT_NONE_LEFT -101 |
abe00makoto | 0:4fcfc05943ff | 125 | #define ERR_ENDPOINT_NOT_FOUND -102 |
abe00makoto | 0:4fcfc05943ff | 126 | #define ERR_DEVICE_NOT_FOUND -103 |
abe00makoto | 0:4fcfc05943ff | 127 | #define ERR_DEVICE_NONE_LEFT -104 |
abe00makoto | 0:4fcfc05943ff | 128 | #define ERR_HUB_INIT_FAILED -105 |
abe00makoto | 0:4fcfc05943ff | 129 | #define ERR_INTERFACE_NOT_FOUND -106 |
abe00makoto | 0:4fcfc05943ff | 130 | |
abe00makoto | 0:4fcfc05943ff | 131 | typedef struct |
abe00makoto | 0:4fcfc05943ff | 132 | { |
abe00makoto | 0:4fcfc05943ff | 133 | u8 bLength; |
abe00makoto | 0:4fcfc05943ff | 134 | u8 bDescriptorType; |
abe00makoto | 0:4fcfc05943ff | 135 | u16 bcdUSB; |
abe00makoto | 0:4fcfc05943ff | 136 | u8 bDeviceClass; |
abe00makoto | 0:4fcfc05943ff | 137 | u8 bDeviceSubClass; |
abe00makoto | 0:4fcfc05943ff | 138 | u8 bDeviceProtocol; |
abe00makoto | 0:4fcfc05943ff | 139 | u8 bMaxPacketSize; |
abe00makoto | 0:4fcfc05943ff | 140 | u16 idVendor; |
abe00makoto | 0:4fcfc05943ff | 141 | u16 idProduct; |
abe00makoto | 0:4fcfc05943ff | 142 | u16 bcdDevice; // version |
abe00makoto | 0:4fcfc05943ff | 143 | u8 iManufacturer; |
abe00makoto | 0:4fcfc05943ff | 144 | u8 iProduct; |
abe00makoto | 0:4fcfc05943ff | 145 | u8 iSerialNumber; |
abe00makoto | 0:4fcfc05943ff | 146 | u8 bNumConfigurations; |
abe00makoto | 0:4fcfc05943ff | 147 | } DeviceDescriptor; // 16 bytes |
abe00makoto | 0:4fcfc05943ff | 148 | |
abe00makoto | 0:4fcfc05943ff | 149 | typedef struct |
abe00makoto | 0:4fcfc05943ff | 150 | { |
abe00makoto | 0:4fcfc05943ff | 151 | u8 bLength; |
abe00makoto | 0:4fcfc05943ff | 152 | u8 bDescriptorType; |
abe00makoto | 0:4fcfc05943ff | 153 | u16 wTotalLength; |
abe00makoto | 0:4fcfc05943ff | 154 | u8 bNumInterfaces; |
abe00makoto | 0:4fcfc05943ff | 155 | u8 bConfigurationValue; // Value to use as an argument to select this configuration |
abe00makoto | 0:4fcfc05943ff | 156 | u8 iConfiguration; // Index of String Descriptor describing this configuration |
abe00makoto | 0:4fcfc05943ff | 157 | u8 bmAttributes; // Bitmap D7 Reserved, set to 1. (USB 1.0 Bus Powered),D6 Self Powered,D5 Remote Wakeup,D4..0 = 0 |
abe00makoto | 0:4fcfc05943ff | 158 | u8 bMaxPower; // Maximum Power Consumption in 2mA units |
abe00makoto | 0:4fcfc05943ff | 159 | } ConfigurationDescriptor; |
abe00makoto | 0:4fcfc05943ff | 160 | |
abe00makoto | 0:4fcfc05943ff | 161 | typedef struct |
abe00makoto | 0:4fcfc05943ff | 162 | { |
abe00makoto | 0:4fcfc05943ff | 163 | u8 bLength; |
abe00makoto | 0:4fcfc05943ff | 164 | u8 bDescriptorType; |
abe00makoto | 0:4fcfc05943ff | 165 | u8 bInterfaceNumber; |
abe00makoto | 0:4fcfc05943ff | 166 | u8 bAlternateSetting; |
abe00makoto | 0:4fcfc05943ff | 167 | u8 bNumEndpoints; |
abe00makoto | 0:4fcfc05943ff | 168 | u8 bInterfaceClass; |
abe00makoto | 0:4fcfc05943ff | 169 | u8 bInterfaceSubClass; |
abe00makoto | 0:4fcfc05943ff | 170 | u8 bInterfaceProtocol; |
abe00makoto | 0:4fcfc05943ff | 171 | u8 iInterface; // Index of String Descriptor Describing this interface |
abe00makoto | 0:4fcfc05943ff | 172 | } InterfaceDescriptor; |
abe00makoto | 0:4fcfc05943ff | 173 | |
abe00makoto | 0:4fcfc05943ff | 174 | typedef struct |
abe00makoto | 0:4fcfc05943ff | 175 | { |
abe00makoto | 0:4fcfc05943ff | 176 | u8 bLength; |
abe00makoto | 0:4fcfc05943ff | 177 | u8 bDescriptorType; |
abe00makoto | 0:4fcfc05943ff | 178 | u8 bEndpointAddress; // Bits 0:3 endpoint, Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints) |
abe00makoto | 0:4fcfc05943ff | 179 | u8 bmAttributes; // Bits 0:1 00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt |
abe00makoto | 0:4fcfc05943ff | 180 | u16 wMaxPacketSize; |
abe00makoto | 0:4fcfc05943ff | 181 | u8 bInterval; // Interval for polling endpoint data transfers. |
abe00makoto | 0:4fcfc05943ff | 182 | } EndpointDescriptor; |
abe00makoto | 0:4fcfc05943ff | 183 | |
abe00makoto | 0:4fcfc05943ff | 184 | typedef struct { |
abe00makoto | 0:4fcfc05943ff | 185 | u8 bLength; |
abe00makoto | 0:4fcfc05943ff | 186 | u8 bDescriptorType; |
abe00makoto | 0:4fcfc05943ff | 187 | u16 bcdHID; |
abe00makoto | 0:4fcfc05943ff | 188 | u8 bCountryCode; |
abe00makoto | 0:4fcfc05943ff | 189 | u8 bNumDescriptors; |
abe00makoto | 0:4fcfc05943ff | 190 | u8 bDescriptorType2; |
abe00makoto | 0:4fcfc05943ff | 191 | u16 wDescriptorLength; |
abe00makoto | 0:4fcfc05943ff | 192 | } HIDDescriptor; |
abe00makoto | 0:4fcfc05943ff | 193 | |
abe00makoto | 0:4fcfc05943ff | 194 | //============================================================================ |
abe00makoto | 0:4fcfc05943ff | 195 | //============================================================================ |
abe00makoto | 0:4fcfc05943ff | 196 | |
abe00makoto | 0:4fcfc05943ff | 197 | |
abe00makoto | 0:4fcfc05943ff | 198 | void USBInit(); |
abe00makoto | 0:4fcfc05943ff | 199 | void USBLoop(); |
abe00makoto | 0:4fcfc05943ff | 200 | u8* USBGetBuffer(u32* len); |
abe00makoto | 0:4fcfc05943ff | 201 | |
abe00makoto | 0:4fcfc05943ff | 202 | // Optional callback for transfers, called at interrupt time |
abe00makoto | 0:4fcfc05943ff | 203 | typedef void (*USBCallback)(int device, int endpoint, int status, u8* data, int len, void* userData); |
abe00makoto | 0:4fcfc05943ff | 204 | |
abe00makoto | 0:4fcfc05943ff | 205 | // Transfers |
abe00makoto | 0:4fcfc05943ff | 206 | int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
abe00makoto | 0:4fcfc05943ff | 207 | int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
abe00makoto | 0:4fcfc05943ff | 208 | int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
abe00makoto | 0:4fcfc05943ff | 209 | |
abe00makoto | 0:4fcfc05943ff | 210 | // Standard Device methods |
abe00makoto | 0:4fcfc05943ff | 211 | int GetDescriptor(int device, int descType, int descIndex, u8* data, int length); |
abe00makoto | 0:4fcfc05943ff | 212 | int GetString(int device, int index, char* dst, int length); |
abe00makoto | 0:4fcfc05943ff | 213 | int SetAddress(int device, int new_addr); |
abe00makoto | 0:4fcfc05943ff | 214 | int SetConfiguration(int device, int configNum); |
abe00makoto | 0:4fcfc05943ff | 215 | int SetInterface(int device, int ifNum, int altNum); |
abe00makoto | 0:4fcfc05943ff | 216 | |
abe00makoto | 0:4fcfc05943ff | 217 | // Implemented to notify app of the arrival of a device |
abe00makoto | 0:4fcfc05943ff | 218 | void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc); |
abe00makoto | 0:4fcfc05943ff | 219 | |
abe00makoto | 0:4fcfc05943ff | 220 | #endif |