Android Open Accessory Library化してみました。 バルク転送の使い方が分かっていないため、相変わらず動作は不安定。 I do not understand the usage of the bulk transfer. Please teach someone.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHost.h Source File

USBHost.h

00001 
00002 /*
00003 Copyright (c) 2010 Peter Barrett
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef USBHOST_H
00025 #define USBHOST_H
00026 
00027 //log mode on off
00028 //#define USBHOST_LOG
00029 
00030 
00031 #ifndef u8
00032 typedef unsigned char u8;
00033 typedef unsigned short u16;
00034 typedef unsigned long u32;
00035 
00036 typedef char s8;
00037 typedef short s16;
00038 typedef char s32;
00039 #endif
00040 
00041 #define ENDPOINT_CONTROL 0
00042 #define ENDPOINT_ISOCRONOUS 1
00043 #define ENDPOINT_BULK 2
00044 #define ENDPOINT_INTERRUPT 3
00045 
00046 #define  DESCRIPTOR_TYPE_DEVICE            1
00047 #define  DESCRIPTOR_TYPE_CONFIGURATION    2
00048 #define  DESCRIPTOR_TYPE_STRING            3
00049 #define  DESCRIPTOR_TYPE_INTERFACE        4
00050 #define  DESCRIPTOR_TYPE_ENDPOINT        5
00051 
00052 #define DESCRIPTOR_TYPE_HID         0x21
00053 #define DESCRIPTOR_TYPE_REPORT      0x22
00054 #define DESCRIPTOR_TYPE_PHYSICAL    0x23
00055 #define DESCRIPTOR_TYPE_HUB         0x29
00056 
00057 enum USB_CLASS_CODE
00058 {
00059     CLASS_DEVICE,
00060     CLASS_AUDIO,
00061     CLASS_COMM_AND_CDC_CONTROL,
00062     CLASS_HID,
00063     CLASS_PHYSICAL = 0x05,
00064     CLASS_STILL_IMAGING,
00065     CLASS_PRINTER,
00066     CLASS_MASS_STORAGE,
00067     CLASS_HUB,
00068     CLASS_CDC_DATA,
00069     CLASS_SMART_CARD,
00070     CLASS_CONTENT_SECURITY = 0x0D,
00071     CLASS_VIDEO = 0x0E,
00072     CLASS_DIAGNOSTIC_DEVICE = 0xDC,
00073     CLASS_WIRELESS_CONTROLLER = 0xE0,
00074     CLASS_MISCELLANEOUS = 0xEF,
00075     CLASS_APP_SPECIFIC = 0xFE,
00076     CLASS_VENDOR_SPECIFIC = 0xFF
00077 };
00078 
00079 #define  DEVICE_TO_HOST         0x80
00080 #define  HOST_TO_DEVICE         0x00
00081 #define  REQUEST_TYPE_STANDARD     0x00
00082 #define  REQUEST_TYPE_CLASS     0x20
00083 #define  REQUEST_TYPE_VENDOR     0x40
00084 #define  RECIPIENT_DEVICE       0x00
00085 #define  RECIPIENT_INTERFACE    0x01
00086 #define  RECIPIENT_ENDPOINT        0x02
00087 #define  RECIPIENT_OTHER        0x03
00088 
00089 #define  GET_STATUS                0
00090 #define  CLEAR_FEATURE            1
00091 #define  SET_FEATURE            3
00092 #define  SET_ADDRESS            5
00093 #define  GET_DESCRIPTOR            6
00094 #define  SET_DESCRIPTOR            7
00095 #define  GET_CONFIGURATION        8
00096 #define  SET_CONFIGURATION        9
00097 #define  GET_INTERFACE            10
00098 #define  SET_INTERFACE            11
00099 #define  SYNCH_FRAME            11
00100 
00101 /* HID Request Codes */ 
00102 #define HID_REQUEST_GET_REPORT          0x01 
00103 #define HID_REQUEST_GET_IDLE            0x02 
00104 #define HID_REQUEST_GET_PROTOCOL        0x03 
00105 #define HID_REQUEST_SET_REPORT          0x09 
00106 #define HID_REQUEST_SET_IDLE            0x0A 
00107 #define HID_REQUEST_SET_PROTOCOL        0x0B 
00108  
00109 /* HID Report Types */ 
00110 #define HID_REPORT_INPUT                0x01 
00111 #define HID_REPORT_OUTPUT               0x02 
00112 #define HID_REPORT_FEATURE              0x03 
00113 
00114 
00115 //        -5 is nak
00116 /*
00117 0010 ACK Handshake
00118 1010 NAK Handshake
00119 1110 STALL Handshake
00120 0110 NYET (No Response Yet)
00121 */
00122 
00123 #define IO_PENDING -100
00124 #define ERR_ENDPOINT_NONE_LEFT -101
00125 #define ERR_ENDPOINT_NOT_FOUND -102
00126 #define ERR_DEVICE_NOT_FOUND -103
00127 #define ERR_DEVICE_NONE_LEFT -104
00128 #define ERR_HUB_INIT_FAILED -105
00129 #define ERR_INTERFACE_NOT_FOUND -106
00130 
00131 typedef struct
00132 {
00133     u8    bLength;
00134     u8    bDescriptorType;
00135     u16 bcdUSB;
00136     u8 bDeviceClass;
00137     u8 bDeviceSubClass;
00138     u8 bDeviceProtocol;
00139     u8 bMaxPacketSize;
00140     u16 idVendor;
00141     u16 idProduct;
00142     u16 bcdDevice;    // version
00143     u8 iManufacturer;
00144     u8 iProduct;
00145     u8 iSerialNumber;
00146     u8 bNumConfigurations;
00147 } DeviceDescriptor;    // 16 bytes
00148 
00149 typedef struct
00150 {
00151     u8    bLength;
00152     u8    bDescriptorType;
00153     u16    wTotalLength;
00154     u8    bNumInterfaces;
00155     u8    bConfigurationValue;    // Value to use as an argument to select this configuration
00156     u8    iConfiguration;            // Index of String Descriptor describing this configuration
00157     u8    bmAttributes;            // Bitmap D7 Reserved, set to 1. (USB 1.0 Bus Powered),D6 Self Powered,D5 Remote Wakeup,D4..0 = 0
00158     u8    bMaxPower;                // Maximum Power Consumption in 2mA units
00159 } ConfigurationDescriptor;
00160 
00161 typedef struct
00162 {
00163     u8    bLength;
00164     u8    bDescriptorType;
00165     u8  bInterfaceNumber;
00166     u8    bAlternateSetting;
00167     u8    bNumEndpoints;
00168     u8    bInterfaceClass;
00169     u8    bInterfaceSubClass;
00170     u8    bInterfaceProtocol;
00171     u8    iInterface;                // Index of String Descriptor Describing this interface
00172 } InterfaceDescriptor;
00173 
00174 typedef struct
00175 {
00176     u8    bLength;
00177     u8    bDescriptorType;
00178     u8    bEndpointAddress;    // Bits 0:3 endpoint, Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints)
00179     u8    bmAttributes;        // Bits 0:1 00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt
00180     u16 wMaxPacketSize;
00181     u8    bInterval;            // Interval for polling endpoint data transfers.
00182 } EndpointDescriptor;
00183 
00184 typedef struct {
00185   u8    bLength;
00186   u8    bDescriptorType;
00187   u16   bcdHID;
00188   u8    bCountryCode;
00189   u8    bNumDescriptors;
00190   u8    bDescriptorType2;
00191   u16   wDescriptorLength;
00192 } HIDDescriptor;
00193 
00194 //============================================================================
00195 //============================================================================
00196 
00197 
00198 void USBInit();
00199 void USBLoop();
00200 u8* USBGetBuffer(u32* len);
00201 
00202 //    Optional callback for transfers, called at interrupt time
00203 typedef void (*USBCallback)(int device, int endpoint, int status, u8* data, int len, void* userData);
00204 
00205 //    Transfers
00206 int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback = 0, void* userData = 0);
00207 int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0);
00208 int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0);
00209 
00210 //    Standard Device methods
00211 int GetDescriptor(int device, int descType, int descIndex, u8* data, int length);
00212 int GetString(int device, int index, char* dst, int length);
00213 int SetAddress(int device, int new_addr);
00214 int SetConfiguration(int device, int configNum);
00215 int SetInterface(int device, int ifNum, int altNum);
00216 
00217 //    Implemented to notify app of the arrival of a device
00218 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc);
00219 
00220 #endif