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