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