BlackOneとAndroidの連携デモプログラム AndroidAccessoryを改造してBlackOneとAndroidが連携できるようにしました。 サポートしているのは、デモアプリの ”Buttons” B1-SW1, B2-SW2, B3-SW3 ”LED2” RGB-LED のみです。 LCDに表示するイメージをマイクロSDカードに入れてLCDのソケットに挿入しておく必要があります。 イメージは、320X240ドットで”\Image”という名前のフォルダの直下に”10.jpg”という名前で入れてください。

Dependencies:   TextLCD mbed

Committer:
techand
Date:
Fri Dec 23 04:33:33 2011 +0000
Revision:
0:7b556109fd46

        

Who changed what in which revision?

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