This is a for debugging \\\\\\\"BLUE USB\\\\\\\". You can connect with HCI mode. How to connect White Wizard Board TANK *White Wizard Board - Motor Driver Board * p 21 - IN_R1 * p 22 - IN_R2 * p 23 - IN_L2 * p 24 - IN_L1

Dependencies:   mbed

Committer:
halfpitch
Date:
Wed Aug 31 11:10:18 2011 +0000
Revision:
1:c56059923036
Parent:
0:a6476c138e84
Rev.B

Who changed what in which revision?

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