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

