Peter Barrett氏のBlueUSBにMIDI USB HOST機能を加えたサンプルプログラムです。KORG nanoKEYなどのUSB MIDIストリームをシリアルMIDI(Serial TX p9)にブリッジします。動作確認はKORG nanoKEY、AKAI LPK-25、EDIROL PC-50のみです。

Dependencies:   mbed

Committer:
radiojunkbox
Date:
Fri May 11 10:05:40 2012 +0000
Revision:
0:79620c558b0c
Rev. 1.0

Who changed what in which revision?

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