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