BaseJpegDeocde exampe program

Dependencies:   BaseJpegDecode Terminal BaseUsbHost mbed mbed-rtos

Fork of BaseJpegDecode by Norimasa Okamoto

Committer:
va009039
Date:
Wed Dec 05 12:41:25 2012 +0000
Revision:
6:95be1cd2bc14
Parent:
4:7d88de31c55a
update FATFileASystem and BaseJpegDecode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 6:95be1cd2bc14 1 #include "msc.h"
va009039 6:95be1cd2bc14 2 //#define __DEBUG
va009039 6:95be1cd2bc14 3 #include "mydbg.h"
va009039 6:95be1cd2bc14 4 #include "Utils.h"
va009039 6:95be1cd2bc14 5
va009039 6:95be1cd2bc14 6 //#define WRITE_PROTECT
va009039 6:95be1cd2bc14 7
va009039 6:95be1cd2bc14 8 msc::msc(const char* name, int drive): FATFileSystem(name)
va009039 6:95be1cd2bc14 9 {
va009039 6:95be1cd2bc14 10 DBG("drive=%d\n", drive);
va009039 6:95be1cd2bc14 11 m_name = name;
va009039 6:95be1cd2bc14 12 m_drive = drive;
va009039 6:95be1cd2bc14 13 DBG_ASSERT(sizeof(CBW) == 31);
va009039 6:95be1cd2bc14 14 DBG_ASSERT(sizeof(CSW) == 13);
va009039 6:95be1cd2bc14 15 m_numBlocks = 0;
va009039 6:95be1cd2bc14 16 m_BlockSize = 0;
va009039 6:95be1cd2bc14 17 m_lun = 0;
va009039 6:95be1cd2bc14 18 m_interface = 0;
va009039 6:95be1cd2bc14 19 m_pDev = NULL;
va009039 6:95be1cd2bc14 20 m_pEpBulkIn = NULL;
va009039 6:95be1cd2bc14 21 m_pEpBulkOut = NULL;
va009039 6:95be1cd2bc14 22 }
va009039 6:95be1cd2bc14 23
va009039 6:95be1cd2bc14 24 int msc::disk_initialize()
va009039 6:95be1cd2bc14 25 {
va009039 6:95be1cd2bc14 26 DBG("m_BlockSize=%d\n", m_BlockSize);
va009039 6:95be1cd2bc14 27 if (m_BlockSize != 512) {
va009039 6:95be1cd2bc14 28 return 1;
va009039 6:95be1cd2bc14 29 }
va009039 6:95be1cd2bc14 30 return 0;
va009039 6:95be1cd2bc14 31 }
va009039 6:95be1cd2bc14 32
va009039 6:95be1cd2bc14 33 int msc::disk_write(const uint8_t *buffer, uint64_t block_number)
va009039 6:95be1cd2bc14 34 {
va009039 6:95be1cd2bc14 35 DBG("buffer=%p block_number=%d\n", buffer, block_number);
va009039 6:95be1cd2bc14 36 int ret = MS_BulkSend(block_number, 1, (uint8_t*)buffer);
va009039 6:95be1cd2bc14 37 if (ret >= 0) {
va009039 6:95be1cd2bc14 38 return 0;
va009039 6:95be1cd2bc14 39 }
va009039 6:95be1cd2bc14 40 return 1;
va009039 6:95be1cd2bc14 41 }
va009039 6:95be1cd2bc14 42
va009039 6:95be1cd2bc14 43 int msc::disk_read(uint8_t *buffer, uint64_t block_number)
va009039 6:95be1cd2bc14 44 {
va009039 6:95be1cd2bc14 45 DBG("buffer=%p block_number=%d\n", buffer, block_number);
va009039 6:95be1cd2bc14 46 int ret = MS_BulkRecv(block_number, 1, (uint8_t*)buffer);
va009039 6:95be1cd2bc14 47 if (ret >= 0) {
va009039 6:95be1cd2bc14 48 return 0;
va009039 6:95be1cd2bc14 49 }
va009039 6:95be1cd2bc14 50 return 1;
va009039 6:95be1cd2bc14 51 }
va009039 6:95be1cd2bc14 52
va009039 6:95be1cd2bc14 53 int msc::disk_status()
va009039 6:95be1cd2bc14 54 {
va009039 6:95be1cd2bc14 55 DBG("\n");
va009039 6:95be1cd2bc14 56 return 0;
va009039 6:95be1cd2bc14 57 }
va009039 6:95be1cd2bc14 58
va009039 6:95be1cd2bc14 59 int msc::disk_sync()
va009039 6:95be1cd2bc14 60 {
va009039 6:95be1cd2bc14 61 DBG("\n");
va009039 6:95be1cd2bc14 62 return 0;
va009039 6:95be1cd2bc14 63 }
va009039 6:95be1cd2bc14 64
va009039 6:95be1cd2bc14 65 uint64_t msc::disk_sectors()
va009039 6:95be1cd2bc14 66 {
va009039 6:95be1cd2bc14 67 DBG("m_numBlocks=%d\n", m_numBlocks);
va009039 6:95be1cd2bc14 68 return m_numBlocks;
va009039 6:95be1cd2bc14 69 }
va009039 6:95be1cd2bc14 70
va009039 6:95be1cd2bc14 71 int msc::setup(int timeout)
va009039 6:95be1cd2bc14 72 {
va009039 6:95be1cd2bc14 73 for(int i = 0; i < 2; i++) {
va009039 6:95be1cd2bc14 74 m_pDev = m_pHost->getDeviceByClass(0x08, m_drive); // USB Mass Storage Class
va009039 6:95be1cd2bc14 75 if (m_pDev || i > 0) {
va009039 6:95be1cd2bc14 76 break;
va009039 6:95be1cd2bc14 77 }
va009039 6:95be1cd2bc14 78 UsbErr rc = Usb_poll();
va009039 6:95be1cd2bc14 79 if (rc == USBERR_PROCESSING) {
va009039 6:95be1cd2bc14 80 VERBOSE("%p USBERR_PROCESSING\n", this);
va009039 6:95be1cd2bc14 81 return -1;
va009039 6:95be1cd2bc14 82 }
va009039 6:95be1cd2bc14 83 }
va009039 6:95be1cd2bc14 84 DBG("m_pDev=%p\n", m_pDev);
va009039 6:95be1cd2bc14 85 if (m_pDev == NULL) {
va009039 6:95be1cd2bc14 86 VERBOSE("%p MSC DISK(%d) NOT FOUND\n", this, m_drive);
va009039 6:95be1cd2bc14 87 return -1;
va009039 6:95be1cd2bc14 88 }
va009039 6:95be1cd2bc14 89 DBG_ASSERT(m_pDev);
va009039 6:95be1cd2bc14 90
va009039 6:95be1cd2bc14 91 ParseConfiguration();
va009039 6:95be1cd2bc14 92
va009039 6:95be1cd2bc14 93 GetMaxLUN();
va009039 6:95be1cd2bc14 94
va009039 6:95be1cd2bc14 95 int retry = 0;
va009039 6:95be1cd2bc14 96 Timer t;
va009039 6:95be1cd2bc14 97 t.start();
va009039 6:95be1cd2bc14 98 t.reset();
va009039 6:95be1cd2bc14 99 while(t.read_ms() < timeout) {
va009039 6:95be1cd2bc14 100 DBG("retry=%d t=%d\n", retry, t.read_ms());
va009039 6:95be1cd2bc14 101 if (retry > 80) {
va009039 6:95be1cd2bc14 102 return -1;
va009039 6:95be1cd2bc14 103 }
va009039 6:95be1cd2bc14 104 int rc = TestUnitReady();
va009039 6:95be1cd2bc14 105 DBG("TestUnitReady(): %d\n", rc);
va009039 6:95be1cd2bc14 106 if (rc == USBERR_OK) {
va009039 6:95be1cd2bc14 107 DBG("m_CSW.bCSWStatus: %02X\n", m_CSW.bCSWStatus);
va009039 6:95be1cd2bc14 108 if (m_CSW.bCSWStatus == 0x00) {
va009039 6:95be1cd2bc14 109 break;
va009039 6:95be1cd2bc14 110 }
va009039 6:95be1cd2bc14 111 }
va009039 6:95be1cd2bc14 112 GetSenseInfo();
va009039 6:95be1cd2bc14 113 retry++;
va009039 6:95be1cd2bc14 114 wait_ms(50);
va009039 6:95be1cd2bc14 115 }
va009039 6:95be1cd2bc14 116 if (t.read_ms() >= timeout) {
va009039 6:95be1cd2bc14 117 return -1;
va009039 6:95be1cd2bc14 118 }
va009039 6:95be1cd2bc14 119 ReadCapacity();
va009039 6:95be1cd2bc14 120 Inquire();
va009039 6:95be1cd2bc14 121 return 0;
va009039 6:95be1cd2bc14 122 }
va009039 6:95be1cd2bc14 123 void msc::_test()
va009039 6:95be1cd2bc14 124 {
va009039 6:95be1cd2bc14 125 ReadCapacity();
va009039 6:95be1cd2bc14 126
va009039 6:95be1cd2bc14 127 uint8_t buf[512];
va009039 6:95be1cd2bc14 128 for(int block = 0; block < m_numBlocks; block++) {
va009039 6:95be1cd2bc14 129 DBG("block=%d\n", block);
va009039 6:95be1cd2bc14 130 disk_read(buf, block);
va009039 6:95be1cd2bc14 131 }
va009039 6:95be1cd2bc14 132 exit(1);
va009039 6:95be1cd2bc14 133 }
va009039 6:95be1cd2bc14 134
va009039 6:95be1cd2bc14 135 int msc::ParseConfiguration()
va009039 6:95be1cd2bc14 136 {
va009039 6:95be1cd2bc14 137 UsbErr rc;
va009039 6:95be1cd2bc14 138 uint8_t ConfigDesc[9];
va009039 6:95be1cd2bc14 139 int index = 0;
va009039 6:95be1cd2bc14 140 DBG_ASSERT(m_pDev);
va009039 6:95be1cd2bc14 141 rc = m_pDev->GetDescriptor(USB_DESCRIPTOR_TYPE_CONFIGURATION, index, ConfigDesc, sizeof(ConfigDesc));
va009039 6:95be1cd2bc14 142 DBG_ASSERT(rc == USBERR_OK);
va009039 6:95be1cd2bc14 143 DBG_BYTES("ConfigDescriptor 9bytes", ConfigDesc, sizeof(ConfigDesc));
va009039 6:95be1cd2bc14 144 DBG_ASSERT(ConfigDesc[0] == 9);
va009039 6:95be1cd2bc14 145 DBG_ASSERT(ConfigDesc[1] == 0x02);
va009039 6:95be1cd2bc14 146 int wTotalLength = *((uint16_t*)&ConfigDesc[2]);
va009039 6:95be1cd2bc14 147 DBG("TotalLength: %d\n", wTotalLength);
va009039 6:95be1cd2bc14 148 int bConfigValue = ConfigDesc[5];
va009039 6:95be1cd2bc14 149 DBG_ASSERT(bConfigValue == 1);
va009039 6:95be1cd2bc14 150 DBG("ConfigValue: %d\n", bConfigValue);
va009039 6:95be1cd2bc14 151 DBG("MaxPower: %d mA\n", ConfigDesc[8]*2);
va009039 6:95be1cd2bc14 152
va009039 6:95be1cd2bc14 153 uint8_t* buf = new uint8_t[wTotalLength];
va009039 6:95be1cd2bc14 154 DBG_ASSERT(buf);
va009039 6:95be1cd2bc14 155 rc = m_pDev->GetDescriptor(USB_DESCRIPTOR_TYPE_CONFIGURATION, index, buf, wTotalLength);
va009039 6:95be1cd2bc14 156 DBG_ASSERT(rc == USBERR_OK);
va009039 6:95be1cd2bc14 157 DBG_ASSERT(ConfigDesc[1] == 0x02);
va009039 6:95be1cd2bc14 158 for (int pos = 0; pos < wTotalLength; pos += buf[pos]) {
va009039 6:95be1cd2bc14 159 DBG_BYTES("CFG", buf+pos, buf[pos]);
va009039 6:95be1cd2bc14 160 int type = buf[pos+1];
va009039 6:95be1cd2bc14 161 if (USB_DESCRIPTOR_TYPE_INTERFACE == type) { // 0x04
va009039 6:95be1cd2bc14 162 DBG("InterfaceNumber: %d\n", buf[pos+2]);
va009039 6:95be1cd2bc14 163 DBG("AlternateSetting: %d\n", buf[pos+3]);
va009039 6:95be1cd2bc14 164 DBG("NumEndpoint: %d\n", buf[pos+4]);
va009039 6:95be1cd2bc14 165 DBG("InterfaceClass: %02X\n", buf[pos+5]);
va009039 6:95be1cd2bc14 166 DBG("InterfaceSubClass: %02X\n", buf[pos+6]);
va009039 6:95be1cd2bc14 167 DBG("InterfaceProtocol: %02X\n", buf[pos+7]);
va009039 6:95be1cd2bc14 168 DBG_ASSERT(buf[pos+6] == 0x06); // SCSI
va009039 6:95be1cd2bc14 169 DBG_ASSERT(buf[pos+7] == 0x50); // bulk only
va009039 6:95be1cd2bc14 170 }
va009039 6:95be1cd2bc14 171 if (USB_DESCRIPTOR_TYPE_ENDPOINT == type) {
va009039 6:95be1cd2bc14 172 DBG_ASSERT(buf[pos] == 7);
va009039 6:95be1cd2bc14 173 uint8_t att = buf[pos+3];
va009039 6:95be1cd2bc14 174 if (att == 2) { // bulk
va009039 6:95be1cd2bc14 175 uint8_t ep = buf[pos+2];
va009039 6:95be1cd2bc14 176 bool dir = ep & 0x80; // true=IN
va009039 6:95be1cd2bc14 177 uint16_t size = LE16(buf+pos+4);
va009039 6:95be1cd2bc14 178 DBG("EndpointAddress: %02X\n", ep);
va009039 6:95be1cd2bc14 179 DBG("Attribute: %02X\n", att);
va009039 6:95be1cd2bc14 180 DBG("MaxPacketSize: %d\n", size);
va009039 6:95be1cd2bc14 181 UsbEndpoint* pEp = new UsbEndpoint(m_pDev, ep, dir, USB_BULK, size);
va009039 6:95be1cd2bc14 182 DBG_ASSERT(pEp);
va009039 6:95be1cd2bc14 183 if (dir) {
va009039 6:95be1cd2bc14 184 m_pEpBulkIn = pEp;
va009039 6:95be1cd2bc14 185 } else {
va009039 6:95be1cd2bc14 186 m_pEpBulkOut = pEp;
va009039 6:95be1cd2bc14 187 }
va009039 6:95be1cd2bc14 188 }
va009039 6:95be1cd2bc14 189 }
va009039 6:95be1cd2bc14 190 }
va009039 6:95be1cd2bc14 191 delete[] buf;
va009039 6:95be1cd2bc14 192 DBG_ASSERT(m_pEpBulkIn);
va009039 6:95be1cd2bc14 193 DBG_ASSERT(m_pEpBulkOut);
va009039 6:95be1cd2bc14 194 return 0;
va009039 6:95be1cd2bc14 195 }
va009039 6:95be1cd2bc14 196
va009039 6:95be1cd2bc14 197 int msc::BulkOnlyMassStorageReset()
va009039 6:95be1cd2bc14 198 {
va009039 6:95be1cd2bc14 199 DBG_ASSERT(m_pDev);
va009039 6:95be1cd2bc14 200 UsbErr rc = m_pDev->controlReceive(0x21, 0xff, 0x0000, m_interface, NULL, 0);
va009039 6:95be1cd2bc14 201 DBG_ASSERT(rc == USBERR_OK);
va009039 6:95be1cd2bc14 202 return rc;
va009039 6:95be1cd2bc14 203 }
va009039 6:95be1cd2bc14 204
va009039 6:95be1cd2bc14 205 int msc::GetMaxLUN()
va009039 6:95be1cd2bc14 206 {
va009039 6:95be1cd2bc14 207 DBG_ASSERT(m_interface == 0);
va009039 6:95be1cd2bc14 208 uint8_t temp[1];
va009039 6:95be1cd2bc14 209 DBG_ASSERT(m_pDev);
va009039 6:95be1cd2bc14 210 UsbErr rc = m_pDev->controlReceive(0xa1, 0xfe, 0x0000, m_interface, temp, sizeof(temp));
va009039 6:95be1cd2bc14 211 DBG_ASSERT(rc == USBERR_OK);
va009039 6:95be1cd2bc14 212 DBG_BYTES("GetMaxLUN", temp, sizeof(temp));
va009039 6:95be1cd2bc14 213 m_MaxLUN = temp[0];
va009039 6:95be1cd2bc14 214 DBG_ASSERT(m_MaxLUN <= 15);
va009039 6:95be1cd2bc14 215 return rc;
va009039 6:95be1cd2bc14 216 }
va009039 6:95be1cd2bc14 217
va009039 6:95be1cd2bc14 218
va009039 6:95be1cd2bc14 219 int msc::TestUnitReady()
va009039 6:95be1cd2bc14 220 {
va009039 6:95be1cd2bc14 221 const uint8_t cdb[6] = {SCSI_CMD_TEST_UNIT_READY, 0x00, 0x00, 0x00, 0x00, 0x00};
va009039 6:95be1cd2bc14 222 m_CBW.dCBWDataTraansferLength = 0;
va009039 6:95be1cd2bc14 223 m_CBW.bmCBWFlags = 0x00;
va009039 6:95be1cd2bc14 224 CommandTransport(cdb, sizeof(cdb));
va009039 6:95be1cd2bc14 225 StatusTransport();
va009039 6:95be1cd2bc14 226 return 0;
va009039 6:95be1cd2bc14 227 }
va009039 6:95be1cd2bc14 228
va009039 6:95be1cd2bc14 229 int msc::GetSenseInfo()
va009039 6:95be1cd2bc14 230 {
va009039 6:95be1cd2bc14 231 const uint8_t cdb[6] = {SCSI_CMD_REQUEST_SENSE, 0x00, 0x00, 0x00, 18, 0x00};
va009039 6:95be1cd2bc14 232 m_CBW.dCBWDataTraansferLength = 18;
va009039 6:95be1cd2bc14 233 m_CBW.bmCBWFlags = 0x80; // data In
va009039 6:95be1cd2bc14 234 CommandTransport(cdb, sizeof(cdb));
va009039 6:95be1cd2bc14 235
va009039 6:95be1cd2bc14 236 uint8_t buf[18];
va009039 6:95be1cd2bc14 237 _bulkRecv(buf, sizeof(buf));
va009039 6:95be1cd2bc14 238 DBG_HEX(buf, sizeof(buf));
va009039 6:95be1cd2bc14 239
va009039 6:95be1cd2bc14 240 StatusTransport();
va009039 6:95be1cd2bc14 241 DBG_ASSERT(m_CSW.bCSWStatus == 0x00);
va009039 6:95be1cd2bc14 242 return 0;
va009039 6:95be1cd2bc14 243 }
va009039 6:95be1cd2bc14 244
va009039 6:95be1cd2bc14 245 int msc::ReadCapacity()
va009039 6:95be1cd2bc14 246 {
va009039 6:95be1cd2bc14 247 const uint8_t cdb[10] = {SCSI_CMD_READ_CAPACITY, 0x00, 0x00, 0x00, 0x00,
va009039 6:95be1cd2bc14 248 0x00, 0x00, 0x00, 0x00, 0x00};
va009039 6:95be1cd2bc14 249 m_CBW.dCBWDataTraansferLength = 8;
va009039 6:95be1cd2bc14 250 m_CBW.bmCBWFlags = 0x80; // data In
va009039 6:95be1cd2bc14 251 CommandTransport(cdb, sizeof(cdb));
va009039 6:95be1cd2bc14 252
va009039 6:95be1cd2bc14 253 uint8_t buf[8];
va009039 6:95be1cd2bc14 254 int rc = _bulkRecv(buf, sizeof(buf));
va009039 6:95be1cd2bc14 255 DBG_ASSERT(rc >= 0);
va009039 6:95be1cd2bc14 256 DBG_HEX(buf, sizeof(buf));
va009039 6:95be1cd2bc14 257
va009039 6:95be1cd2bc14 258 StatusTransport();
va009039 6:95be1cd2bc14 259 DBG_ASSERT(m_CSW.bCSWStatus == 0x00);
va009039 6:95be1cd2bc14 260
va009039 6:95be1cd2bc14 261 m_numBlocks = BE32(buf);
va009039 6:95be1cd2bc14 262 m_BlockSize = BE32(buf+4);
va009039 6:95be1cd2bc14 263 DBG("m_numBlocks=%d m_BlockSize=%d\n", m_numBlocks, m_BlockSize);
va009039 6:95be1cd2bc14 264 DBG_ASSERT(m_BlockSize == 512);
va009039 6:95be1cd2bc14 265 DBG_ASSERT(m_numBlocks > 0);
va009039 6:95be1cd2bc14 266 return 0;
va009039 6:95be1cd2bc14 267 }
va009039 6:95be1cd2bc14 268
va009039 6:95be1cd2bc14 269 int msc::Inquire()
va009039 6:95be1cd2bc14 270 {
va009039 6:95be1cd2bc14 271 const uint8_t cdb[6] = {SCSI_CMD_INQUIRY, 0x00, 0x00, 0x00, 36, 0x00};
va009039 6:95be1cd2bc14 272 m_CBW.dCBWDataTraansferLength = 36;
va009039 6:95be1cd2bc14 273 m_CBW.bmCBWFlags = 0x80; // data In
va009039 6:95be1cd2bc14 274 CommandTransport(cdb, sizeof(cdb));
va009039 6:95be1cd2bc14 275
va009039 6:95be1cd2bc14 276 uint8_t buf[36];
va009039 6:95be1cd2bc14 277 _bulkRecv(buf, sizeof(buf));
va009039 6:95be1cd2bc14 278 DBG_HEX(buf, sizeof(buf));
va009039 6:95be1cd2bc14 279
va009039 6:95be1cd2bc14 280 StatusTransport();
va009039 6:95be1cd2bc14 281 return 0;
va009039 6:95be1cd2bc14 282 }
va009039 6:95be1cd2bc14 283
va009039 6:95be1cd2bc14 284 int msc::MS_BulkRecv(uint32_t block_number, int num_blocks, uint8_t* user_buffer)
va009039 6:95be1cd2bc14 285 {
va009039 6:95be1cd2bc14 286 DBG_ASSERT(m_BlockSize == 512);
va009039 6:95be1cd2bc14 287 DBG_ASSERT(num_blocks == 1);
va009039 6:95be1cd2bc14 288 DBG_ASSERT(user_buffer);
va009039 6:95be1cd2bc14 289 uint8_t cdb[10] = {SCSI_CMD_READ_10, 0x00, 0x00, 0x00, 0x00,
va009039 6:95be1cd2bc14 290 0x00, 0x00, 0x00, 0x00, 0x00};
va009039 6:95be1cd2bc14 291 BE32(block_number, cdb+2);
va009039 6:95be1cd2bc14 292 BE16(num_blocks, cdb+7);
va009039 6:95be1cd2bc14 293 uint32_t len = m_BlockSize * num_blocks;
va009039 6:95be1cd2bc14 294 DBG_ASSERT(len <= 512);
va009039 6:95be1cd2bc14 295 m_CBW.dCBWDataTraansferLength = len;
va009039 6:95be1cd2bc14 296 m_CBW.bmCBWFlags = 0x80; // data In
va009039 6:95be1cd2bc14 297 CommandTransport(cdb, sizeof(cdb));
va009039 6:95be1cd2bc14 298
va009039 6:95be1cd2bc14 299 int ret = _bulkRecv(user_buffer, len);
va009039 6:95be1cd2bc14 300 //DBG_HEX(user_buffer, len);
va009039 6:95be1cd2bc14 301
va009039 6:95be1cd2bc14 302 StatusTransport();
va009039 6:95be1cd2bc14 303 DBG_ASSERT(m_CSW.bCSWStatus == 0x00);
va009039 6:95be1cd2bc14 304 return ret;
va009039 6:95be1cd2bc14 305 }
va009039 6:95be1cd2bc14 306
va009039 6:95be1cd2bc14 307 int msc::MS_BulkSend(uint32_t block_number, int num_blocks, uint8_t* user_buffer)
va009039 6:95be1cd2bc14 308 {
va009039 6:95be1cd2bc14 309 #ifdef WRITE_PROTECT
va009039 6:95be1cd2bc14 310 return 0;
va009039 6:95be1cd2bc14 311 #else
va009039 6:95be1cd2bc14 312 DBG_ASSERT(num_blocks == 1);
va009039 6:95be1cd2bc14 313 DBG_ASSERT(user_buffer);
va009039 6:95be1cd2bc14 314 uint8_t cdb[10] = {SCSI_CMD_WRITE_10, 0x00, 0x00, 0x00, 0x00,
va009039 6:95be1cd2bc14 315 0x00, 0x00, 0x00, 0x00, 0x00};
va009039 6:95be1cd2bc14 316 BE32(block_number, cdb+2);
va009039 6:95be1cd2bc14 317 BE16(num_blocks, cdb+7);
va009039 6:95be1cd2bc14 318 uint32_t len = m_BlockSize * num_blocks;
va009039 6:95be1cd2bc14 319 DBG_ASSERT(len <= 512);
va009039 6:95be1cd2bc14 320 m_CBW.dCBWDataTraansferLength = len;
va009039 6:95be1cd2bc14 321 m_CBW.bmCBWFlags = 0x00; // data Out
va009039 6:95be1cd2bc14 322 CommandTransport(cdb, sizeof(cdb));
va009039 6:95be1cd2bc14 323
va009039 6:95be1cd2bc14 324 int ret = _bulkSend(user_buffer, len);
va009039 6:95be1cd2bc14 325 //DBG_HEX(user_buffer, len);
va009039 6:95be1cd2bc14 326
va009039 6:95be1cd2bc14 327 StatusTransport();
va009039 6:95be1cd2bc14 328 DBG_ASSERT(m_CSW.bCSWStatus == 0x00);
va009039 6:95be1cd2bc14 329 return ret;
va009039 6:95be1cd2bc14 330 #endif //WRITE_PROTECT
va009039 6:95be1cd2bc14 331 }
va009039 6:95be1cd2bc14 332
va009039 6:95be1cd2bc14 333 int msc::CommandTransport(const uint8_t* cdb, int size)
va009039 6:95be1cd2bc14 334 {
va009039 6:95be1cd2bc14 335 DBG_ASSERT(cdb);
va009039 6:95be1cd2bc14 336 DBG_ASSERT(size >= 6);
va009039 6:95be1cd2bc14 337 DBG_ASSERT(size <= 16);
va009039 6:95be1cd2bc14 338 m_CBW.bCBWLUN = m_lun;
va009039 6:95be1cd2bc14 339 m_CBW.bCBWCBLength = size;
va009039 6:95be1cd2bc14 340 memcpy(m_CBW.CBWCB, cdb, size);
va009039 6:95be1cd2bc14 341
va009039 6:95be1cd2bc14 342 m_CBW.dCBWSignature = 0x43425355;
va009039 6:95be1cd2bc14 343 m_CBW.dCBWTag = m_tag++;
va009039 6:95be1cd2bc14 344 m_CBW.bCBWLUN = 0;
va009039 6:95be1cd2bc14 345 //DBG_HEX((uint8_t*)&m_CBW, sizeof(CBW));
va009039 6:95be1cd2bc14 346 int rc = _bulkSend((uint8_t*)&m_CBW, sizeof(CBW));
va009039 6:95be1cd2bc14 347 return rc;
va009039 6:95be1cd2bc14 348 }
va009039 6:95be1cd2bc14 349
va009039 6:95be1cd2bc14 350 int msc::StatusTransport()
va009039 6:95be1cd2bc14 351 {
va009039 6:95be1cd2bc14 352 DBG_ASSERT(sizeof(CSW) == 13);
va009039 6:95be1cd2bc14 353 int rc = _bulkRecv((uint8_t*)&m_CSW, sizeof(CSW));
va009039 6:95be1cd2bc14 354 //DBG_HEX((uint8_t*)&m_CSW, sizeof(CSW));
va009039 6:95be1cd2bc14 355 DBG_ASSERT(m_CSW.dCSWSignature == 0x53425355);
va009039 6:95be1cd2bc14 356 DBG_ASSERT(m_CSW.dCSWTag == m_CBW.dCBWTag);
va009039 6:95be1cd2bc14 357 DBG_ASSERT(m_CSW.dCSWDataResidue == 0);
va009039 6:95be1cd2bc14 358 return rc;
va009039 6:95be1cd2bc14 359 }
va009039 6:95be1cd2bc14 360
va009039 6:95be1cd2bc14 361 int msc::_bulkRecv(uint8_t* buf, int size)
va009039 6:95be1cd2bc14 362 {
va009039 6:95be1cd2bc14 363 UsbErr rc = m_pEpBulkIn->transfer(buf, size);
va009039 6:95be1cd2bc14 364 DBG_ASSERT(rc == USBERR_PROCESSING);
va009039 6:95be1cd2bc14 365 while(m_pEpBulkIn->status() == USBERR_PROCESSING){
va009039 6:95be1cd2bc14 366 wait_us(1);
va009039 6:95be1cd2bc14 367 }
va009039 6:95be1cd2bc14 368 int ret = m_pEpBulkIn->status();
va009039 6:95be1cd2bc14 369 if (ret >= 0) {
va009039 6:95be1cd2bc14 370 return ret;
va009039 6:95be1cd2bc14 371 }
va009039 6:95be1cd2bc14 372 DBG("buf=%p size=%d ret=%d\n", buf, size, ret);
va009039 6:95be1cd2bc14 373 return ret;
va009039 6:95be1cd2bc14 374 }
va009039 6:95be1cd2bc14 375
va009039 6:95be1cd2bc14 376 int msc::_bulkSend(uint8_t* buf, int size)
va009039 6:95be1cd2bc14 377 {
va009039 6:95be1cd2bc14 378 DBG_ASSERT(m_pEpBulkOut);
va009039 6:95be1cd2bc14 379 UsbErr rc = m_pEpBulkOut->transfer(buf, size);
va009039 6:95be1cd2bc14 380 DBG_ASSERT(rc == USBERR_PROCESSING);
va009039 6:95be1cd2bc14 381 while(m_pEpBulkOut->status() == USBERR_PROCESSING){
va009039 6:95be1cd2bc14 382 wait_us(1);
va009039 6:95be1cd2bc14 383 }
va009039 6:95be1cd2bc14 384 int ret = m_pEpBulkOut->status();
va009039 6:95be1cd2bc14 385 if (ret >= 0) {
va009039 6:95be1cd2bc14 386 return ret;
va009039 6:95be1cd2bc14 387 }
va009039 6:95be1cd2bc14 388 DBG("buf=%p size=%d ret=%d\n", buf, size, ret);
va009039 6:95be1cd2bc14 389 return ret;
va009039 6:95be1cd2bc14 390 }