A USB/360deg Rotational device, communicating via blue-tooth, test code

Dependencies:   FatFileSystem mbed

Committer:
lolpcc
Date:
Fri Dec 21 11:37:38 2012 +0000
Revision:
0:87da38093be9
Used to test the Lazy Susan at work, hence the A2D code in Utils.cpp

Who changed what in which revision?

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