takayuki ikai / Mbed 2 deprecated BlueWii

Dependencies:   mbed

Committer:
ikaii
Date:
Mon Dec 06 17:02:12 2010 +0000
Revision:
0:010465683d59

        

Who changed what in which revision?

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