Simons Wii controlled m3pi program

Dependencies:   mbed m3pi ID12RFIDIRQ

Committer:
chris
Date:
Wed May 18 14:50:46 2011 +0000
Revision:
0:0ab65a1aef12
Added some printing to the m3pi screen

Who changed what in which revision?

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