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