![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Code to control a Traxster robot using a Wimote and Android app
Fork of BlueUSB by
BlueUSB/MassStorage.cpp@1:accdaa84fe8d, 2014-04-28 (annotated)
- Committer:
- aswild
- Date:
- Mon Apr 28 16:06:32 2014 +0000
- Revision:
- 1:accdaa84fe8d
- Parent:
- MassStorage.cpp@0:606b230e5b4a
Wiimote controlled Traxster robot using BlueUSB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
peterbarrett1967 | 0:606b230e5b4a | 1 | |
peterbarrett1967 | 0:606b230e5b4a | 2 | /* |
peterbarrett1967 | 0:606b230e5b4a | 3 | Copyright (c) 2010 Peter Barrett |
peterbarrett1967 | 0:606b230e5b4a | 4 | |
peterbarrett1967 | 0:606b230e5b4a | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
peterbarrett1967 | 0:606b230e5b4a | 6 | of this software and associated documentation files (the "Software"), to deal |
peterbarrett1967 | 0:606b230e5b4a | 7 | in the Software without restriction, including without limitation the rights |
peterbarrett1967 | 0:606b230e5b4a | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
peterbarrett1967 | 0:606b230e5b4a | 9 | copies of the Software, and to permit persons to whom the Software is |
peterbarrett1967 | 0:606b230e5b4a | 10 | furnished to do so, subject to the following conditions: |
peterbarrett1967 | 0:606b230e5b4a | 11 | |
peterbarrett1967 | 0:606b230e5b4a | 12 | The above copyright notice and this permission notice shall be included in |
peterbarrett1967 | 0:606b230e5b4a | 13 | all copies or substantial portions of the Software. |
peterbarrett1967 | 0:606b230e5b4a | 14 | |
peterbarrett1967 | 0:606b230e5b4a | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
peterbarrett1967 | 0:606b230e5b4a | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
peterbarrett1967 | 0:606b230e5b4a | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
peterbarrett1967 | 0:606b230e5b4a | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
peterbarrett1967 | 0:606b230e5b4a | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
peterbarrett1967 | 0:606b230e5b4a | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
peterbarrett1967 | 0:606b230e5b4a | 21 | THE SOFTWARE. |
peterbarrett1967 | 0:606b230e5b4a | 22 | */ |
peterbarrett1967 | 0:606b230e5b4a | 23 | |
peterbarrett1967 | 0:606b230e5b4a | 24 | #include "stdlib.h" |
peterbarrett1967 | 0:606b230e5b4a | 25 | #include "stdio.h" |
peterbarrett1967 | 0:606b230e5b4a | 26 | #include "string.h" |
peterbarrett1967 | 0:606b230e5b4a | 27 | |
peterbarrett1967 | 0:606b230e5b4a | 28 | #include "Utils.h" |
peterbarrett1967 | 0:606b230e5b4a | 29 | #include "USBHost.h" |
peterbarrett1967 | 0:606b230e5b4a | 30 | |
peterbarrett1967 | 0:606b230e5b4a | 31 | |
peterbarrett1967 | 0:606b230e5b4a | 32 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize); |
peterbarrett1967 | 0:606b230e5b4a | 33 | int MassStorage_ReadBlock(int device, u32 block, u8* dst); |
peterbarrett1967 | 0:606b230e5b4a | 34 | int MassStorage_WriteBlock(int device, u32 block, const u8* dst); |
peterbarrett1967 | 0:606b230e5b4a | 35 | |
peterbarrett1967 | 0:606b230e5b4a | 36 | |
peterbarrett1967 | 0:606b230e5b4a | 37 | #define ERR_BAD_CSW_SIGNATURE -200 |
peterbarrett1967 | 0:606b230e5b4a | 38 | |
peterbarrett1967 | 0:606b230e5b4a | 39 | #define CBW_SIGNATURE 0x43425355 |
peterbarrett1967 | 0:606b230e5b4a | 40 | #define CSW_SIGNATURE 0x53425355 |
peterbarrett1967 | 0:606b230e5b4a | 41 | |
peterbarrett1967 | 0:606b230e5b4a | 42 | // Command Block |
peterbarrett1967 | 0:606b230e5b4a | 43 | typedef struct |
peterbarrett1967 | 0:606b230e5b4a | 44 | { |
peterbarrett1967 | 0:606b230e5b4a | 45 | u32 Signature; |
peterbarrett1967 | 0:606b230e5b4a | 46 | u32 Tag; |
peterbarrett1967 | 0:606b230e5b4a | 47 | u32 TransferLength; |
peterbarrett1967 | 0:606b230e5b4a | 48 | u8 Flags; |
peterbarrett1967 | 0:606b230e5b4a | 49 | u8 LUN; |
peterbarrett1967 | 0:606b230e5b4a | 50 | u8 CBLength; |
peterbarrett1967 | 0:606b230e5b4a | 51 | u8 CB[16]; // only 6 really |
peterbarrett1967 | 0:606b230e5b4a | 52 | } CBW; |
peterbarrett1967 | 0:606b230e5b4a | 53 | |
peterbarrett1967 | 0:606b230e5b4a | 54 | // Status block |
peterbarrett1967 | 0:606b230e5b4a | 55 | typedef struct |
peterbarrett1967 | 0:606b230e5b4a | 56 | { |
peterbarrett1967 | 0:606b230e5b4a | 57 | u32 Signature; |
peterbarrett1967 | 0:606b230e5b4a | 58 | u32 Tag; |
peterbarrett1967 | 0:606b230e5b4a | 59 | u32 DataResidue; |
peterbarrett1967 | 0:606b230e5b4a | 60 | u8 Status; |
peterbarrett1967 | 0:606b230e5b4a | 61 | } CSW; |
peterbarrett1967 | 0:606b230e5b4a | 62 | |
peterbarrett1967 | 0:606b230e5b4a | 63 | int SCSIRequestSense(int device); |
peterbarrett1967 | 0:606b230e5b4a | 64 | |
peterbarrett1967 | 0:606b230e5b4a | 65 | int DoSCSI(int device, const u8* cmd, int cmdLen, int flags, u8* data, u32 transferLen) |
peterbarrett1967 | 0:606b230e5b4a | 66 | { |
peterbarrett1967 | 0:606b230e5b4a | 67 | CBW cbw; |
peterbarrett1967 | 0:606b230e5b4a | 68 | cbw.Signature = CBW_SIGNATURE; |
peterbarrett1967 | 0:606b230e5b4a | 69 | cbw.Tag = 0; |
peterbarrett1967 | 0:606b230e5b4a | 70 | cbw.TransferLength = transferLen; |
peterbarrett1967 | 0:606b230e5b4a | 71 | cbw.Flags = flags; |
peterbarrett1967 | 0:606b230e5b4a | 72 | cbw.LUN = 0; |
peterbarrett1967 | 0:606b230e5b4a | 73 | cbw.CBLength = cmdLen; |
peterbarrett1967 | 0:606b230e5b4a | 74 | memset(cbw.CB,0,sizeof(cbw.CB)); |
peterbarrett1967 | 0:606b230e5b4a | 75 | memcpy(cbw.CB,cmd,cmdLen); |
peterbarrett1967 | 0:606b230e5b4a | 76 | |
peterbarrett1967 | 0:606b230e5b4a | 77 | int r; |
peterbarrett1967 | 0:606b230e5b4a | 78 | r = USBBulkTransfer(device,0x01,(u8*)&cbw,31); // Send the command |
peterbarrett1967 | 0:606b230e5b4a | 79 | if (r < 0) |
peterbarrett1967 | 0:606b230e5b4a | 80 | return r; |
peterbarrett1967 | 0:606b230e5b4a | 81 | |
peterbarrett1967 | 0:606b230e5b4a | 82 | if (data) |
peterbarrett1967 | 0:606b230e5b4a | 83 | { |
peterbarrett1967 | 0:606b230e5b4a | 84 | r = USBBulkTransfer(device,flags | 1,data,transferLen); |
peterbarrett1967 | 0:606b230e5b4a | 85 | if (r < 0) |
peterbarrett1967 | 0:606b230e5b4a | 86 | return r; |
peterbarrett1967 | 0:606b230e5b4a | 87 | } |
peterbarrett1967 | 0:606b230e5b4a | 88 | |
peterbarrett1967 | 0:606b230e5b4a | 89 | CSW csw; |
peterbarrett1967 | 0:606b230e5b4a | 90 | csw.Signature = 0; |
peterbarrett1967 | 0:606b230e5b4a | 91 | r = USBBulkTransfer(device,0x81,(u8*)&csw,13); |
peterbarrett1967 | 0:606b230e5b4a | 92 | if (r < 0) |
peterbarrett1967 | 0:606b230e5b4a | 93 | return r; |
peterbarrett1967 | 0:606b230e5b4a | 94 | |
peterbarrett1967 | 0:606b230e5b4a | 95 | if (csw.Signature != CSW_SIGNATURE) |
peterbarrett1967 | 0:606b230e5b4a | 96 | return ERR_BAD_CSW_SIGNATURE; |
peterbarrett1967 | 0:606b230e5b4a | 97 | |
peterbarrett1967 | 0:606b230e5b4a | 98 | // ModeSense? |
peterbarrett1967 | 0:606b230e5b4a | 99 | if (csw.Status == 1 && cmd[0] != 3) |
peterbarrett1967 | 0:606b230e5b4a | 100 | return SCSIRequestSense(device); |
peterbarrett1967 | 0:606b230e5b4a | 101 | |
peterbarrett1967 | 0:606b230e5b4a | 102 | return csw.Status; |
peterbarrett1967 | 0:606b230e5b4a | 103 | } |
peterbarrett1967 | 0:606b230e5b4a | 104 | |
peterbarrett1967 | 0:606b230e5b4a | 105 | int SCSITestUnitReady(int device) |
peterbarrett1967 | 0:606b230e5b4a | 106 | { |
peterbarrett1967 | 0:606b230e5b4a | 107 | u8 cmd[6]; |
peterbarrett1967 | 0:606b230e5b4a | 108 | memset(cmd,0,6); |
peterbarrett1967 | 0:606b230e5b4a | 109 | return DoSCSI(device,cmd,6,DEVICE_TO_HOST,0,0); |
peterbarrett1967 | 0:606b230e5b4a | 110 | } |
peterbarrett1967 | 0:606b230e5b4a | 111 | |
peterbarrett1967 | 0:606b230e5b4a | 112 | int SCSIRequestSense(int device) |
peterbarrett1967 | 0:606b230e5b4a | 113 | { |
peterbarrett1967 | 0:606b230e5b4a | 114 | u8 cmd[6] = {0x03,0,0,0,18,0}; |
peterbarrett1967 | 0:606b230e5b4a | 115 | u8 result[18]; |
peterbarrett1967 | 0:606b230e5b4a | 116 | int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,18); |
peterbarrett1967 | 0:606b230e5b4a | 117 | return r; |
peterbarrett1967 | 0:606b230e5b4a | 118 | } |
peterbarrett1967 | 0:606b230e5b4a | 119 | |
peterbarrett1967 | 0:606b230e5b4a | 120 | int SCSIInquiry(int device) |
peterbarrett1967 | 0:606b230e5b4a | 121 | { |
peterbarrett1967 | 0:606b230e5b4a | 122 | u8 cmd[6] = {0x12,0,0,0,36,0}; |
peterbarrett1967 | 0:606b230e5b4a | 123 | u8 result[36+2]; |
peterbarrett1967 | 0:606b230e5b4a | 124 | result[36] = '\n'; |
peterbarrett1967 | 0:606b230e5b4a | 125 | result[37] = 0; |
peterbarrett1967 | 0:606b230e5b4a | 126 | int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,36); |
peterbarrett1967 | 0:606b230e5b4a | 127 | if (r == 0) |
peterbarrett1967 | 0:606b230e5b4a | 128 | printf((const char*)result + 8); |
peterbarrett1967 | 0:606b230e5b4a | 129 | return r; |
peterbarrett1967 | 0:606b230e5b4a | 130 | } |
peterbarrett1967 | 0:606b230e5b4a | 131 | |
peterbarrett1967 | 0:606b230e5b4a | 132 | int SCSIReadCapacity(int device, u32* blockCount, u32* blockSize) |
peterbarrett1967 | 0:606b230e5b4a | 133 | { |
peterbarrett1967 | 0:606b230e5b4a | 134 | u8 cmd[10] = {0x25,0,0,0,8,0,0,0,0,0}; |
peterbarrett1967 | 0:606b230e5b4a | 135 | u8 result[8]; |
peterbarrett1967 | 0:606b230e5b4a | 136 | *blockSize = 0; |
peterbarrett1967 | 0:606b230e5b4a | 137 | *blockCount = 0; |
peterbarrett1967 | 0:606b230e5b4a | 138 | int r = DoSCSI(device,cmd,10,DEVICE_TO_HOST,result,8); |
peterbarrett1967 | 0:606b230e5b4a | 139 | if (r == 0) |
peterbarrett1967 | 0:606b230e5b4a | 140 | { |
peterbarrett1967 | 0:606b230e5b4a | 141 | *blockCount = BE32(result); |
peterbarrett1967 | 0:606b230e5b4a | 142 | *blockSize = BE32(result+4); |
peterbarrett1967 | 0:606b230e5b4a | 143 | } |
peterbarrett1967 | 0:606b230e5b4a | 144 | return r; |
peterbarrett1967 | 0:606b230e5b4a | 145 | } |
peterbarrett1967 | 0:606b230e5b4a | 146 | |
peterbarrett1967 | 0:606b230e5b4a | 147 | int SCSITransfer(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize, int direction) |
peterbarrett1967 | 0:606b230e5b4a | 148 | { |
peterbarrett1967 | 0:606b230e5b4a | 149 | // USB hardware will only do 4k per transfer |
peterbarrett1967 | 0:606b230e5b4a | 150 | while (blockCount*blockSize > 4096) |
peterbarrett1967 | 0:606b230e5b4a | 151 | { |
peterbarrett1967 | 0:606b230e5b4a | 152 | int count = 4096/blockSize; |
peterbarrett1967 | 0:606b230e5b4a | 153 | int r = SCSITransfer(device,blockAddr,count,dst,blockSize,direction); |
peterbarrett1967 | 0:606b230e5b4a | 154 | dst += count*blockSize; |
peterbarrett1967 | 0:606b230e5b4a | 155 | blockAddr += count; |
peterbarrett1967 | 0:606b230e5b4a | 156 | blockCount -= count; |
peterbarrett1967 | 0:606b230e5b4a | 157 | } |
peterbarrett1967 | 0:606b230e5b4a | 158 | |
peterbarrett1967 | 0:606b230e5b4a | 159 | u8 cmd[10]; |
peterbarrett1967 | 0:606b230e5b4a | 160 | memset(cmd,0,10); |
peterbarrett1967 | 0:606b230e5b4a | 161 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
peterbarrett1967 | 0:606b230e5b4a | 162 | BE32(blockAddr,cmd+2); |
peterbarrett1967 | 0:606b230e5b4a | 163 | BE16(blockCount,cmd+7); |
peterbarrett1967 | 0:606b230e5b4a | 164 | return DoSCSI(device,cmd,10,direction,dst,blockSize*blockCount); |
peterbarrett1967 | 0:606b230e5b4a | 165 | } |
peterbarrett1967 | 0:606b230e5b4a | 166 | |
peterbarrett1967 | 0:606b230e5b4a | 167 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize) |
peterbarrett1967 | 0:606b230e5b4a | 168 | { |
peterbarrett1967 | 0:606b230e5b4a | 169 | return SCSIReadCapacity(device,blockCount,blockSize); |
peterbarrett1967 | 0:606b230e5b4a | 170 | } |
peterbarrett1967 | 0:606b230e5b4a | 171 | |
peterbarrett1967 | 0:606b230e5b4a | 172 | int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) |
peterbarrett1967 | 0:606b230e5b4a | 173 | { |
peterbarrett1967 | 0:606b230e5b4a | 174 | return SCSITransfer(device,blockAddr,blockCount,dst,blockSize,DEVICE_TO_HOST); |
peterbarrett1967 | 0:606b230e5b4a | 175 | } |
peterbarrett1967 | 0:606b230e5b4a | 176 | |
peterbarrett1967 | 0:606b230e5b4a | 177 | int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) |
peterbarrett1967 | 0:606b230e5b4a | 178 | { |
peterbarrett1967 | 0:606b230e5b4a | 179 | return SCSITransfer(device,blockAddr,blockCount,dst,blockSize,HOST_TO_DEVICE); |
peterbarrett1967 | 0:606b230e5b4a | 180 | } |