Twitter client, with TextLCD, Ethernet Connection, and USB Host for keyboard.

Dependencies:   EthernetNetIf TextLCD mbed

Committer:
blmarket
Date:
Thu Apr 21 05:54:26 2011 +0000
Revision:
0:70571fd24107
Initial Release

Who changed what in which revision?

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