Quick hack to make NSX-39 (Poke-Miku) USB MIDI device to speak "mbed" from mbed which acts as an USB host.
Dependencies: FatFileSystem mbed
Fork of MIDI_BlueUSB by
Description of the project
This is quick hack to control Poke-miku (NSX-39) from mbed. The mbed acts as an USB host and controls USB MIDI device NSX-39. It speaks "mbed" if you send "s¥n" from virtual USB serial (connected to PC or Mac) or push SW connected to p21. It plays MIDI file "test.mid" on local file-system if you push SW connected to p22. You can find files that I have tested at the bottom. The standard MIDI file support is still preliminary. See TestShell.cpp for the hack. This program is derived from MIDI_BlueUSB (http://mbed.org/users/radiojunkbox/code/MIDI_BlueUSB/) by Radio Junk Box.
ポケミク(NSX-39)を無改造のままmbedから鳴らせるようにしてみました。mbedがUSB hostになって、USB MIDIデバイスのポケミクを鳴らします。mbedのバーチャルシリアル(USBシリアル)にPCからs\nを送るか、p21につないだスイッチを押すとmbedとしゃべります。p22につないだスイッチを押すと、ローカルファイルシステム(.binと同じ場所)に保存した test.mid を再生します。試したファイルは下にある test1.mid と test2.mid です。MIDIファイルのサポートはまだまだ完全とはいえません。
tested MIDI files
Video: Poke-miku speaks `mbed'
MassStorage.cpp@4:cd0d8ce967d8, 2014-04-29 (annotated)
- Committer:
- non
- Date:
- Tue Apr 29 06:01:52 2014 +0000
- Revision:
- 4:cd0d8ce967d8
- Parent:
- 0:79620c558b0c
Fix issues when it plays a MIDI file which has multi tracks.
Who changed what in which revision?
User | Revision | Line number | New 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 | #include "stdlib.h" |
radiojunkbox | 0:79620c558b0c | 25 | #include "stdio.h" |
radiojunkbox | 0:79620c558b0c | 26 | #include "string.h" |
radiojunkbox | 0:79620c558b0c | 27 | |
radiojunkbox | 0:79620c558b0c | 28 | #include "Utils.h" |
radiojunkbox | 0:79620c558b0c | 29 | #include "USBHost.h" |
radiojunkbox | 0:79620c558b0c | 30 | |
radiojunkbox | 0:79620c558b0c | 31 | |
radiojunkbox | 0:79620c558b0c | 32 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize); |
radiojunkbox | 0:79620c558b0c | 33 | int MassStorage_ReadBlock(int device, u32 block, u8* dst); |
radiojunkbox | 0:79620c558b0c | 34 | int MassStorage_WriteBlock(int device, u32 block, const u8* dst); |
radiojunkbox | 0:79620c558b0c | 35 | |
radiojunkbox | 0:79620c558b0c | 36 | |
radiojunkbox | 0:79620c558b0c | 37 | #define ERR_BAD_CSW_SIGNATURE -200 |
radiojunkbox | 0:79620c558b0c | 38 | |
radiojunkbox | 0:79620c558b0c | 39 | #define CBW_SIGNATURE 0x43425355 |
radiojunkbox | 0:79620c558b0c | 40 | #define CSW_SIGNATURE 0x53425355 |
radiojunkbox | 0:79620c558b0c | 41 | |
radiojunkbox | 0:79620c558b0c | 42 | // Command Block |
radiojunkbox | 0:79620c558b0c | 43 | typedef struct |
radiojunkbox | 0:79620c558b0c | 44 | { |
radiojunkbox | 0:79620c558b0c | 45 | u32 Signature; |
radiojunkbox | 0:79620c558b0c | 46 | u32 Tag; |
radiojunkbox | 0:79620c558b0c | 47 | u32 TransferLength; |
radiojunkbox | 0:79620c558b0c | 48 | u8 Flags; |
radiojunkbox | 0:79620c558b0c | 49 | u8 LUN; |
radiojunkbox | 0:79620c558b0c | 50 | u8 CBLength; |
radiojunkbox | 0:79620c558b0c | 51 | u8 CB[16]; // only 6 really |
radiojunkbox | 0:79620c558b0c | 52 | } CBW; |
radiojunkbox | 0:79620c558b0c | 53 | |
radiojunkbox | 0:79620c558b0c | 54 | // Status block |
radiojunkbox | 0:79620c558b0c | 55 | typedef struct |
radiojunkbox | 0:79620c558b0c | 56 | { |
radiojunkbox | 0:79620c558b0c | 57 | u32 Signature; |
radiojunkbox | 0:79620c558b0c | 58 | u32 Tag; |
radiojunkbox | 0:79620c558b0c | 59 | u32 DataResidue; |
radiojunkbox | 0:79620c558b0c | 60 | u8 Status; |
radiojunkbox | 0:79620c558b0c | 61 | } CSW; |
radiojunkbox | 0:79620c558b0c | 62 | |
radiojunkbox | 0:79620c558b0c | 63 | int SCSIRequestSense(int device); |
radiojunkbox | 0:79620c558b0c | 64 | |
radiojunkbox | 0:79620c558b0c | 65 | int DoSCSI(int device, const u8* cmd, int cmdLen, int flags, u8* data, u32 transferLen) |
radiojunkbox | 0:79620c558b0c | 66 | { |
radiojunkbox | 0:79620c558b0c | 67 | CBW cbw; |
radiojunkbox | 0:79620c558b0c | 68 | cbw.Signature = CBW_SIGNATURE; |
radiojunkbox | 0:79620c558b0c | 69 | cbw.Tag = 0; |
radiojunkbox | 0:79620c558b0c | 70 | cbw.TransferLength = transferLen; |
radiojunkbox | 0:79620c558b0c | 71 | cbw.Flags = flags; |
radiojunkbox | 0:79620c558b0c | 72 | cbw.LUN = 0; |
radiojunkbox | 0:79620c558b0c | 73 | cbw.CBLength = cmdLen; |
radiojunkbox | 0:79620c558b0c | 74 | memset(cbw.CB,0,sizeof(cbw.CB)); |
radiojunkbox | 0:79620c558b0c | 75 | memcpy(cbw.CB,cmd,cmdLen); |
radiojunkbox | 0:79620c558b0c | 76 | |
radiojunkbox | 0:79620c558b0c | 77 | int r; |
radiojunkbox | 0:79620c558b0c | 78 | r = USBBulkTransfer(device,0x01,(u8*)&cbw,31); // Send the command |
radiojunkbox | 0:79620c558b0c | 79 | if (r < 0) |
radiojunkbox | 0:79620c558b0c | 80 | return r; |
radiojunkbox | 0:79620c558b0c | 81 | |
radiojunkbox | 0:79620c558b0c | 82 | if (data) |
radiojunkbox | 0:79620c558b0c | 83 | { |
radiojunkbox | 0:79620c558b0c | 84 | r = USBBulkTransfer(device,flags | 1,data,transferLen); |
radiojunkbox | 0:79620c558b0c | 85 | if (r < 0) |
radiojunkbox | 0:79620c558b0c | 86 | return r; |
radiojunkbox | 0:79620c558b0c | 87 | } |
radiojunkbox | 0:79620c558b0c | 88 | |
radiojunkbox | 0:79620c558b0c | 89 | CSW csw; |
radiojunkbox | 0:79620c558b0c | 90 | csw.Signature = 0; |
radiojunkbox | 0:79620c558b0c | 91 | r = USBBulkTransfer(device,0x81,(u8*)&csw,13); |
radiojunkbox | 0:79620c558b0c | 92 | if (r < 0) |
radiojunkbox | 0:79620c558b0c | 93 | return r; |
radiojunkbox | 0:79620c558b0c | 94 | |
radiojunkbox | 0:79620c558b0c | 95 | if (csw.Signature != CSW_SIGNATURE) |
radiojunkbox | 0:79620c558b0c | 96 | return ERR_BAD_CSW_SIGNATURE; |
radiojunkbox | 0:79620c558b0c | 97 | |
radiojunkbox | 0:79620c558b0c | 98 | // ModeSense? |
radiojunkbox | 0:79620c558b0c | 99 | if (csw.Status == 1 && cmd[0] != 3) |
radiojunkbox | 0:79620c558b0c | 100 | return SCSIRequestSense(device); |
radiojunkbox | 0:79620c558b0c | 101 | |
radiojunkbox | 0:79620c558b0c | 102 | return csw.Status; |
radiojunkbox | 0:79620c558b0c | 103 | } |
radiojunkbox | 0:79620c558b0c | 104 | |
radiojunkbox | 0:79620c558b0c | 105 | int SCSITestUnitReady(int device) |
radiojunkbox | 0:79620c558b0c | 106 | { |
radiojunkbox | 0:79620c558b0c | 107 | u8 cmd[6]; |
radiojunkbox | 0:79620c558b0c | 108 | memset(cmd,0,6); |
radiojunkbox | 0:79620c558b0c | 109 | return DoSCSI(device,cmd,6,DEVICE_TO_HOST,0,0); |
radiojunkbox | 0:79620c558b0c | 110 | } |
radiojunkbox | 0:79620c558b0c | 111 | |
radiojunkbox | 0:79620c558b0c | 112 | int SCSIRequestSense(int device) |
radiojunkbox | 0:79620c558b0c | 113 | { |
radiojunkbox | 0:79620c558b0c | 114 | u8 cmd[6] = {0x03,0,0,0,18,0}; |
radiojunkbox | 0:79620c558b0c | 115 | u8 result[18]; |
radiojunkbox | 0:79620c558b0c | 116 | int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,18); |
radiojunkbox | 0:79620c558b0c | 117 | return r; |
radiojunkbox | 0:79620c558b0c | 118 | } |
radiojunkbox | 0:79620c558b0c | 119 | |
radiojunkbox | 0:79620c558b0c | 120 | int SCSIInquiry(int device) |
radiojunkbox | 0:79620c558b0c | 121 | { |
radiojunkbox | 0:79620c558b0c | 122 | u8 cmd[6] = {0x12,0,0,0,36,0}; |
radiojunkbox | 0:79620c558b0c | 123 | u8 result[36+2]; |
radiojunkbox | 0:79620c558b0c | 124 | result[36] = '\n'; |
radiojunkbox | 0:79620c558b0c | 125 | result[37] = 0; |
radiojunkbox | 0:79620c558b0c | 126 | int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,36); |
radiojunkbox | 0:79620c558b0c | 127 | if (r == 0) |
radiojunkbox | 0:79620c558b0c | 128 | printf((const char*)result + 8); |
radiojunkbox | 0:79620c558b0c | 129 | return r; |
radiojunkbox | 0:79620c558b0c | 130 | } |
radiojunkbox | 0:79620c558b0c | 131 | |
radiojunkbox | 0:79620c558b0c | 132 | int SCSIReadCapacity(int device, u32* blockCount, u32* blockSize) |
radiojunkbox | 0:79620c558b0c | 133 | { |
radiojunkbox | 0:79620c558b0c | 134 | u8 cmd[10] = {0x25,0,0,0,8,0,0,0,0,0}; |
radiojunkbox | 0:79620c558b0c | 135 | u8 result[8]; |
radiojunkbox | 0:79620c558b0c | 136 | *blockSize = 0; |
radiojunkbox | 0:79620c558b0c | 137 | *blockCount = 0; |
radiojunkbox | 0:79620c558b0c | 138 | int r = DoSCSI(device,cmd,10,DEVICE_TO_HOST,result,8); |
radiojunkbox | 0:79620c558b0c | 139 | if (r == 0) |
radiojunkbox | 0:79620c558b0c | 140 | { |
radiojunkbox | 0:79620c558b0c | 141 | *blockCount = BE32(result); |
radiojunkbox | 0:79620c558b0c | 142 | *blockSize = BE32(result+4); |
radiojunkbox | 0:79620c558b0c | 143 | } |
radiojunkbox | 0:79620c558b0c | 144 | return r; |
radiojunkbox | 0:79620c558b0c | 145 | } |
radiojunkbox | 0:79620c558b0c | 146 | |
radiojunkbox | 0:79620c558b0c | 147 | int SCSITransfer(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize, int direction) |
radiojunkbox | 0:79620c558b0c | 148 | { |
radiojunkbox | 0:79620c558b0c | 149 | // USB hardware will only do 4k per transfer |
radiojunkbox | 0:79620c558b0c | 150 | while (blockCount*blockSize > 4096) |
radiojunkbox | 0:79620c558b0c | 151 | { |
radiojunkbox | 0:79620c558b0c | 152 | int count = 4096/blockSize; |
radiojunkbox | 0:79620c558b0c | 153 | int r = SCSITransfer(device,blockAddr,count,dst,blockSize,direction); |
radiojunkbox | 0:79620c558b0c | 154 | dst += count*blockSize; |
radiojunkbox | 0:79620c558b0c | 155 | blockAddr += count; |
radiojunkbox | 0:79620c558b0c | 156 | blockCount -= count; |
radiojunkbox | 0:79620c558b0c | 157 | } |
radiojunkbox | 0:79620c558b0c | 158 | |
radiojunkbox | 0:79620c558b0c | 159 | u8 cmd[10]; |
radiojunkbox | 0:79620c558b0c | 160 | memset(cmd,0,10); |
radiojunkbox | 0:79620c558b0c | 161 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
radiojunkbox | 0:79620c558b0c | 162 | BE32(blockAddr,cmd+2); |
radiojunkbox | 0:79620c558b0c | 163 | BE16(blockCount,cmd+7); |
radiojunkbox | 0:79620c558b0c | 164 | return DoSCSI(device,cmd,10,direction,dst,blockSize*blockCount); |
radiojunkbox | 0:79620c558b0c | 165 | } |
radiojunkbox | 0:79620c558b0c | 166 | |
radiojunkbox | 0:79620c558b0c | 167 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize) |
radiojunkbox | 0:79620c558b0c | 168 | { |
radiojunkbox | 0:79620c558b0c | 169 | return SCSIReadCapacity(device,blockCount,blockSize); |
radiojunkbox | 0:79620c558b0c | 170 | } |
radiojunkbox | 0:79620c558b0c | 171 | |
radiojunkbox | 0:79620c558b0c | 172 | int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) |
radiojunkbox | 0:79620c558b0c | 173 | { |
radiojunkbox | 0:79620c558b0c | 174 | return SCSITransfer(device,blockAddr,blockCount,dst,blockSize,DEVICE_TO_HOST); |
radiojunkbox | 0:79620c558b0c | 175 | } |
radiojunkbox | 0:79620c558b0c | 176 | |
radiojunkbox | 0:79620c558b0c | 177 | int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) |
radiojunkbox | 0:79620c558b0c | 178 | { |
radiojunkbox | 0:79620c558b0c | 179 | return SCSITransfer(device,blockAddr,blockCount,dst,blockSize,HOST_TO_DEVICE); |
radiojunkbox | 0:79620c558b0c | 180 | } |