主導機 mbed用のプログラムです

Dependencies:   mbed

Fork of PS3_BlueUSB_user_ver2 by 2017  部内対抗4班 ROBOSTEP

Committer:
yuto17320508
Date:
Wed Aug 23 03:31:34 2017 +0000
Revision:
18:2579c275ef57
Parent:
0:736c76a75def
a

Who changed what in which revision?

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