BlueUSB with USB-> SERIAL (CP210x) GPIO pins working.

Dependencies:   mbed

Committer:
tecnosys
Date:
Fri Apr 23 05:04:28 2010 +0000
Revision:
0:a14eaa2e1445

        

Who changed what in which revision?

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