3/5

Dependencies:   mbed

Committer:
yuki0701
Date:
Tue Mar 05 04:30:35 2019 +0000
Revision:
1:530908de68c6
Parent:
0:a56be39653d0
a

Who changed what in which revision?

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