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 /*
radiojunkbox 0:79620c558b0c 25 May 11 2012 RadioJunkBox : added MIDI USB support
radiojunkbox 0:79620c558b0c 26 */
radiojunkbox 0:79620c558b0c 27
radiojunkbox 0:79620c558b0c 28 #include "mbed.h"
radiojunkbox 0:79620c558b0c 29 #include "USBHost.h"
radiojunkbox 0:79620c558b0c 30 #include "Utils.h"
radiojunkbox 0:79620c558b0c 31
radiojunkbox 0:79620c558b0c 32 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
radiojunkbox 0:79620c558b0c 33 #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1)
radiojunkbox 0:79620c558b0c 34 #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2)
radiojunkbox 0:79620c558b0c 35
radiojunkbox 0:79620c558b0c 36 u8 auto_mouse[4]; // buttons,dx,dy,scroll
radiojunkbox 0:79620c558b0c 37 u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6
radiojunkbox 0:79620c558b0c 38 u8 auto_joystick[4]; // x,y,buttons,throttle
radiojunkbox 0:79620c558b0c 39
radiojunkbox 0:79620c558b0c 40 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
radiojunkbox 0:79620c558b0c 41 {
radiojunkbox 0:79620c558b0c 42 int evt = (int)userData;
radiojunkbox 0:79620c558b0c 43 switch (evt)
radiojunkbox 0:79620c558b0c 44 {
radiojunkbox 0:79620c558b0c 45 case AUTO_KEYBOARD:
radiojunkbox 0:79620c558b0c 46 printf("AUTO_KEYBOARD ");
radiojunkbox 0:79620c558b0c 47 break;
radiojunkbox 0:79620c558b0c 48 case AUTO_MOUSE:
radiojunkbox 0:79620c558b0c 49 printf("AUTO_MOUSE ");
radiojunkbox 0:79620c558b0c 50 break;
radiojunkbox 0:79620c558b0c 51 default:
radiojunkbox 0:79620c558b0c 52 printf("HUH ");
radiojunkbox 0:79620c558b0c 53 }
radiojunkbox 0:79620c558b0c 54 printfBytes("data",data,len);
radiojunkbox 0:79620c558b0c 55 USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData);
radiojunkbox 0:79620c558b0c 56 }
radiojunkbox 0:79620c558b0c 57
radiojunkbox 0:79620c558b0c 58 // Establish transfers for interrupt events
radiojunkbox 0:79620c558b0c 59 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed)
radiojunkbox 0:79620c558b0c 60 {
radiojunkbox 0:79620c558b0c 61 if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80))
radiojunkbox 0:79620c558b0c 62 return;
radiojunkbox 0:79620c558b0c 63
radiojunkbox 0:79620c558b0c 64 // Make automatic interrupt enpoints for known devices
radiojunkbox 0:79620c558b0c 65 u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
radiojunkbox 0:79620c558b0c 66 u8* dst = 0;
radiojunkbox 0:79620c558b0c 67 int len;
radiojunkbox 0:79620c558b0c 68 switch (evt)
radiojunkbox 0:79620c558b0c 69 {
radiojunkbox 0:79620c558b0c 70 case AUTO_MOUSE:
radiojunkbox 0:79620c558b0c 71 dst = auto_mouse;
radiojunkbox 0:79620c558b0c 72 len = sizeof(auto_mouse);
radiojunkbox 0:79620c558b0c 73 break;
radiojunkbox 0:79620c558b0c 74 case AUTO_KEYBOARD:
radiojunkbox 0:79620c558b0c 75 dst = auto_keyboard;
radiojunkbox 0:79620c558b0c 76 len = sizeof(auto_keyboard);
radiojunkbox 0:79620c558b0c 77 break;
radiojunkbox 0:79620c558b0c 78 default:
radiojunkbox 0:79620c558b0c 79 printf("Interrupt endpoint %02X %08X\n",ed->bEndpointAddress,evt);
radiojunkbox 0:79620c558b0c 80 break;
radiojunkbox 0:79620c558b0c 81 }
radiojunkbox 0:79620c558b0c 82 if (dst)
radiojunkbox 0:79620c558b0c 83 {
radiojunkbox 0:79620c558b0c 84 printf("Auto Event for %02X %08X\n",ed->bEndpointAddress,evt);
radiojunkbox 0:79620c558b0c 85 USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt);
radiojunkbox 0:79620c558b0c 86 }
radiojunkbox 0:79620c558b0c 87 }
radiojunkbox 0:79620c558b0c 88
radiojunkbox 0:79620c558b0c 89 void PrintString(int device, int i)
radiojunkbox 0:79620c558b0c 90 {
radiojunkbox 0:79620c558b0c 91 u8 buffer[256];
radiojunkbox 0:79620c558b0c 92 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255);
radiojunkbox 0:79620c558b0c 93 if (le < 0)
radiojunkbox 0:79620c558b0c 94 return;
radiojunkbox 0:79620c558b0c 95 char* dst = (char*)buffer;
radiojunkbox 0:79620c558b0c 96 for (int j = 2; j < le; j += 2)
radiojunkbox 0:79620c558b0c 97 *dst++ = buffer[j];
radiojunkbox 0:79620c558b0c 98 *dst = 0;
radiojunkbox 0:79620c558b0c 99 printf("%d:%s\n",i,(const char*)buffer);
radiojunkbox 0:79620c558b0c 100 }
radiojunkbox 0:79620c558b0c 101
radiojunkbox 0:79620c558b0c 102 // Walk descriptors and create endpoints for a given device
radiojunkbox 0:79620c558b0c 103 int StartAutoEvent(int device, int configuration, int interfaceNumber)
radiojunkbox 0:79620c558b0c 104 {
radiojunkbox 0:79620c558b0c 105 u8 buffer[255];
radiojunkbox 0:79620c558b0c 106 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
radiojunkbox 0:79620c558b0c 107 if (err < 0)
radiojunkbox 0:79620c558b0c 108 return err;
radiojunkbox 0:79620c558b0c 109
radiojunkbox 0:79620c558b0c 110 int len = buffer[2] | (buffer[3] << 8);
radiojunkbox 0:79620c558b0c 111 u8* d = buffer;
radiojunkbox 0:79620c558b0c 112 u8* end = d + len;
radiojunkbox 0:79620c558b0c 113 while (d < end)
radiojunkbox 0:79620c558b0c 114 {
radiojunkbox 0:79620c558b0c 115 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
radiojunkbox 0:79620c558b0c 116 {
radiojunkbox 0:79620c558b0c 117 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
radiojunkbox 0:79620c558b0c 118 if (id->bInterfaceNumber == interfaceNumber)
radiojunkbox 0:79620c558b0c 119 {
radiojunkbox 0:79620c558b0c 120 d += d[0];
radiojunkbox 0:79620c558b0c 121 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
radiojunkbox 0:79620c558b0c 122 {
radiojunkbox 0:79620c558b0c 123 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
radiojunkbox 0:79620c558b0c 124 AddAutoEvent(device,id,(EndpointDescriptor*)d);
radiojunkbox 0:79620c558b0c 125 d += d[0];
radiojunkbox 0:79620c558b0c 126 }
radiojunkbox 0:79620c558b0c 127 }
radiojunkbox 0:79620c558b0c 128 }
radiojunkbox 0:79620c558b0c 129 d += d[0];
radiojunkbox 0:79620c558b0c 130 }
radiojunkbox 0:79620c558b0c 131 return 0;
radiojunkbox 0:79620c558b0c 132 }
radiojunkbox 0:79620c558b0c 133
radiojunkbox 0:79620c558b0c 134 // Implemented in main.cpp
radiojunkbox 0:79620c558b0c 135 int OnDiskInsert(int device);
radiojunkbox 0:79620c558b0c 136
radiojunkbox 0:79620c558b0c 137 // Implemented in TestShell.cpp
radiojunkbox 0:79620c558b0c 138 int OnBluetoothInsert(int device);
radiojunkbox 0:79620c558b0c 139
radiojunkbox 0:79620c558b0c 140 // Added by RadioJunkBox ------------------------------------
radiojunkbox 0:79620c558b0c 141 // Implemented in MidiUSB.cpp
radiojunkbox 0:79620c558b0c 142 int OnMidiInsert(int device);
radiojunkbox 0:79620c558b0c 143 // -----------------------------------------------------------
radiojunkbox 0:79620c558b0c 144
radiojunkbox 0:79620c558b0c 145 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
radiojunkbox 0:79620c558b0c 146 {
radiojunkbox 0:79620c558b0c 147 printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
radiojunkbox 0:79620c558b0c 148 char s[128];
radiojunkbox 0:79620c558b0c 149 for (int i = 1; i < 3; i++)
radiojunkbox 0:79620c558b0c 150 {
radiojunkbox 0:79620c558b0c 151 if (GetString(device,i,s,sizeof(s)) < 0)
radiojunkbox 0:79620c558b0c 152 break;
radiojunkbox 0:79620c558b0c 153 printf("%d: %s\n",i,s);
radiojunkbox 0:79620c558b0c 154 }
radiojunkbox 0:79620c558b0c 155
radiojunkbox 0:79620c558b0c 156 switch (interfaceDesc->bInterfaceClass)
radiojunkbox 0:79620c558b0c 157 {
radiojunkbox 0:79620c558b0c 158 case CLASS_MASS_STORAGE:
radiojunkbox 0:79620c558b0c 159 if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50)
radiojunkbox 0:79620c558b0c 160 OnDiskInsert(device); // it's SCSI!
radiojunkbox 0:79620c558b0c 161 break;
radiojunkbox 0:79620c558b0c 162 case CLASS_WIRELESS_CONTROLLER:
radiojunkbox 0:79620c558b0c 163 if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
radiojunkbox 0:79620c558b0c 164 OnBluetoothInsert(device); // it's bluetooth!
radiojunkbox 0:79620c558b0c 165 break;
radiojunkbox 0:79620c558b0c 166 // Added by RadioJunkBox ------------------------------------
radiojunkbox 0:79620c558b0c 167 case CLASS_AUDIO:
radiojunkbox 0:79620c558b0c 168 if (interfaceDesc->bInterfaceSubClass == 0x03 && interfaceDesc->bInterfaceProtocol == 0x00)
radiojunkbox 0:79620c558b0c 169 OnMidiInsert(device); // it's MIDI!
radiojunkbox 0:79620c558b0c 170 break;
radiojunkbox 0:79620c558b0c 171 case CLASS_VENDOR_SPECIFIC:
radiojunkbox 0:79620c558b0c 172 if (interfaceDesc->bInterfaceSubClass == 0x03 && interfaceDesc->bInterfaceProtocol == 0x00)
radiojunkbox 0:79620c558b0c 173 OnMidiInsert(device); // it's MIDI! (EDIROL PC-50)
radiojunkbox 0:79620c558b0c 174 break;
radiojunkbox 0:79620c558b0c 175 // ----------------------------------------------------------
radiojunkbox 0:79620c558b0c 176 default:
radiojunkbox 0:79620c558b0c 177 StartAutoEvent(device,1,0);
radiojunkbox 0:79620c558b0c 178 break;
radiojunkbox 0:79620c558b0c 179 }
radiojunkbox 0:79620c558b0c 180 }