Twitter client, with TextLCD, Ethernet Connection, and USB Host for keyboard.

Dependencies:   EthernetNetIf TextLCD mbed

Committer:
blmarket
Date:
Thu Apr 21 05:54:26 2011 +0000
Revision:
0:70571fd24107
Initial Release

Who changed what in which revision?

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