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