BlueUSB with USB-> SERIAL (CP210x) GPIO pins working.

Dependencies:   mbed

Committer:
tecnosys
Date:
Fri Apr 23 05:04:28 2010 +0000
Revision:
0:a14eaa2e1445

        

Who changed what in which revision?

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