Test Ver

Dependencies:   mbed FatFileSystem

Committer:
jksoft
Date:
Sat Nov 17 13:22:00 2012 +0000
Revision:
0:269589d8d2c2
Test Program

Who changed what in which revision?

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