nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 1:55a6170b404f 1 /* mbed USBHost Library
nexpaq 1:55a6170b404f 2 * Copyright (c) 2006-2013 ARM Limited
nexpaq 1:55a6170b404f 3 *
nexpaq 1:55a6170b404f 4 * Licensed under the Apache License, Version 2.0 (the "License");
nexpaq 1:55a6170b404f 5 * you may not use this file except in compliance with the License.
nexpaq 1:55a6170b404f 6 * You may obtain a copy of the License at
nexpaq 1:55a6170b404f 7 *
nexpaq 1:55a6170b404f 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 1:55a6170b404f 9 *
nexpaq 1:55a6170b404f 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 1:55a6170b404f 11 * distributed under the License is distributed on an "AS IS" BASIS,
nexpaq 1:55a6170b404f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 1:55a6170b404f 13 * See the License for the specific language governing permissions and
nexpaq 1:55a6170b404f 14 * limitations under the License.
nexpaq 1:55a6170b404f 15 */
nexpaq 1:55a6170b404f 16
nexpaq 1:55a6170b404f 17 #include "USBHostMSD.h"
nexpaq 1:55a6170b404f 18
nexpaq 1:55a6170b404f 19 #if USBHOST_MSD
nexpaq 1:55a6170b404f 20
nexpaq 1:55a6170b404f 21 #include "dbg.h"
nexpaq 1:55a6170b404f 22
nexpaq 1:55a6170b404f 23 #define CBW_SIGNATURE 0x43425355
nexpaq 1:55a6170b404f 24 #define CSW_SIGNATURE 0x53425355
nexpaq 1:55a6170b404f 25
nexpaq 1:55a6170b404f 26 #define DEVICE_TO_HOST 0x80
nexpaq 1:55a6170b404f 27 #define HOST_TO_DEVICE 0x00
nexpaq 1:55a6170b404f 28
nexpaq 1:55a6170b404f 29 #define GET_MAX_LUN (0xFE)
nexpaq 1:55a6170b404f 30 #define BO_MASS_STORAGE_RESET (0xFF)
nexpaq 1:55a6170b404f 31
nexpaq 1:55a6170b404f 32 USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir)
nexpaq 1:55a6170b404f 33 {
nexpaq 1:55a6170b404f 34 host = USBHost::getHostInst();
nexpaq 1:55a6170b404f 35 init();
nexpaq 1:55a6170b404f 36 }
nexpaq 1:55a6170b404f 37
nexpaq 1:55a6170b404f 38 void USBHostMSD::init() {
nexpaq 1:55a6170b404f 39 dev_connected = false;
nexpaq 1:55a6170b404f 40 dev = NULL;
nexpaq 1:55a6170b404f 41 bulk_in = NULL;
nexpaq 1:55a6170b404f 42 bulk_out = NULL;
nexpaq 1:55a6170b404f 43 dev_connected = false;
nexpaq 1:55a6170b404f 44 blockSize = 0;
nexpaq 1:55a6170b404f 45 blockCount = 0;
nexpaq 1:55a6170b404f 46 msd_intf = -1;
nexpaq 1:55a6170b404f 47 msd_device_found = false;
nexpaq 1:55a6170b404f 48 disk_init = false;
nexpaq 1:55a6170b404f 49 dev_connected = false;
nexpaq 1:55a6170b404f 50 nb_ep = 0;
nexpaq 1:55a6170b404f 51 }
nexpaq 1:55a6170b404f 52
nexpaq 1:55a6170b404f 53
nexpaq 1:55a6170b404f 54 bool USBHostMSD::connected()
nexpaq 1:55a6170b404f 55 {
nexpaq 1:55a6170b404f 56 return dev_connected;
nexpaq 1:55a6170b404f 57 }
nexpaq 1:55a6170b404f 58
nexpaq 1:55a6170b404f 59 bool USBHostMSD::connect()
nexpaq 1:55a6170b404f 60 {
nexpaq 1:55a6170b404f 61
nexpaq 1:55a6170b404f 62 if (dev_connected) {
nexpaq 1:55a6170b404f 63 return true;
nexpaq 1:55a6170b404f 64 }
nexpaq 1:55a6170b404f 65
nexpaq 1:55a6170b404f 66 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
nexpaq 1:55a6170b404f 67 if ((dev = host->getDevice(i)) != NULL) {
nexpaq 1:55a6170b404f 68
nexpaq 1:55a6170b404f 69 USB_DBG("Trying to connect MSD device\r\n");
nexpaq 1:55a6170b404f 70
nexpaq 1:55a6170b404f 71 if(host->enumerate(dev, this))
nexpaq 1:55a6170b404f 72 break;
nexpaq 1:55a6170b404f 73
nexpaq 1:55a6170b404f 74 if (msd_device_found) {
nexpaq 1:55a6170b404f 75 bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN);
nexpaq 1:55a6170b404f 76 bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT);
nexpaq 1:55a6170b404f 77
nexpaq 1:55a6170b404f 78 if (!bulk_in || !bulk_out)
nexpaq 1:55a6170b404f 79 continue;
nexpaq 1:55a6170b404f 80
nexpaq 1:55a6170b404f 81 USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf);
nexpaq 1:55a6170b404f 82 dev->setName("MSD", msd_intf);
nexpaq 1:55a6170b404f 83 host->registerDriver(dev, msd_intf, this, &USBHostMSD::init);
nexpaq 1:55a6170b404f 84
nexpaq 1:55a6170b404f 85 dev_connected = true;
nexpaq 1:55a6170b404f 86 return true;
nexpaq 1:55a6170b404f 87 }
nexpaq 1:55a6170b404f 88 } //if()
nexpaq 1:55a6170b404f 89 } //for()
nexpaq 1:55a6170b404f 90 init();
nexpaq 1:55a6170b404f 91 return false;
nexpaq 1:55a6170b404f 92 }
nexpaq 1:55a6170b404f 93
nexpaq 1:55a6170b404f 94 /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid)
nexpaq 1:55a6170b404f 95 {
nexpaq 1:55a6170b404f 96 // we don't check VID/PID for MSD driver
nexpaq 1:55a6170b404f 97 }
nexpaq 1:55a6170b404f 98
nexpaq 1:55a6170b404f 99 /*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
nexpaq 1:55a6170b404f 100 {
nexpaq 1:55a6170b404f 101 if ((msd_intf == -1) &&
nexpaq 1:55a6170b404f 102 (intf_class == MSD_CLASS) &&
nexpaq 1:55a6170b404f 103 (intf_subclass == 0x06) &&
nexpaq 1:55a6170b404f 104 (intf_protocol == 0x50)) {
nexpaq 1:55a6170b404f 105 msd_intf = intf_nb;
nexpaq 1:55a6170b404f 106 return true;
nexpaq 1:55a6170b404f 107 }
nexpaq 1:55a6170b404f 108 return false;
nexpaq 1:55a6170b404f 109 }
nexpaq 1:55a6170b404f 110
nexpaq 1:55a6170b404f 111 /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
nexpaq 1:55a6170b404f 112 {
nexpaq 1:55a6170b404f 113 if (intf_nb == msd_intf) {
nexpaq 1:55a6170b404f 114 if (type == BULK_ENDPOINT) {
nexpaq 1:55a6170b404f 115 nb_ep++;
nexpaq 1:55a6170b404f 116 if (nb_ep == 2)
nexpaq 1:55a6170b404f 117 msd_device_found = true;
nexpaq 1:55a6170b404f 118 return true;
nexpaq 1:55a6170b404f 119 }
nexpaq 1:55a6170b404f 120 }
nexpaq 1:55a6170b404f 121 return false;
nexpaq 1:55a6170b404f 122 }
nexpaq 1:55a6170b404f 123
nexpaq 1:55a6170b404f 124
nexpaq 1:55a6170b404f 125 int USBHostMSD::testUnitReady() {
nexpaq 1:55a6170b404f 126 USB_DBG("Test unit ready");
nexpaq 1:55a6170b404f 127 return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
nexpaq 1:55a6170b404f 128 }
nexpaq 1:55a6170b404f 129
nexpaq 1:55a6170b404f 130
nexpaq 1:55a6170b404f 131 int USBHostMSD::readCapacity() {
nexpaq 1:55a6170b404f 132 USB_DBG("Read capacity");
nexpaq 1:55a6170b404f 133 uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
nexpaq 1:55a6170b404f 134 uint8_t result[8];
nexpaq 1:55a6170b404f 135 int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8);
nexpaq 1:55a6170b404f 136 if (status == 0) {
nexpaq 1:55a6170b404f 137 blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3];
nexpaq 1:55a6170b404f 138 blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7];
nexpaq 1:55a6170b404f 139 USB_INFO("MSD [dev: %p] - blockCount: %u, blockSize: %d, Capacity: %d\r\n", dev, blockCount, blockSize, blockCount*blockSize);
nexpaq 1:55a6170b404f 140 }
nexpaq 1:55a6170b404f 141 return status;
nexpaq 1:55a6170b404f 142 }
nexpaq 1:55a6170b404f 143
nexpaq 1:55a6170b404f 144
nexpaq 1:55a6170b404f 145 int USBHostMSD::SCSIRequestSense() {
nexpaq 1:55a6170b404f 146 USB_DBG("Request sense");
nexpaq 1:55a6170b404f 147 uint8_t cmd[6] = {0x03,0,0,0,18,0};
nexpaq 1:55a6170b404f 148 uint8_t result[18];
nexpaq 1:55a6170b404f 149 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18);
nexpaq 1:55a6170b404f 150 return status;
nexpaq 1:55a6170b404f 151 }
nexpaq 1:55a6170b404f 152
nexpaq 1:55a6170b404f 153
nexpaq 1:55a6170b404f 154 int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
nexpaq 1:55a6170b404f 155 USB_DBG("Inquiry");
nexpaq 1:55a6170b404f 156 uint8_t evpd = (page_code == 0) ? 0 : 1;
nexpaq 1:55a6170b404f 157 uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0};
nexpaq 1:55a6170b404f 158 uint8_t result[36];
nexpaq 1:55a6170b404f 159 int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36);
nexpaq 1:55a6170b404f 160 if (status == 0) {
nexpaq 1:55a6170b404f 161 char vid_pid[17];
nexpaq 1:55a6170b404f 162 memcpy(vid_pid, &result[8], 8);
nexpaq 1:55a6170b404f 163 vid_pid[8] = 0;
nexpaq 1:55a6170b404f 164 USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid);
nexpaq 1:55a6170b404f 165
nexpaq 1:55a6170b404f 166 memcpy(vid_pid, &result[16], 16);
nexpaq 1:55a6170b404f 167 vid_pid[16] = 0;
nexpaq 1:55a6170b404f 168 USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid);
nexpaq 1:55a6170b404f 169
nexpaq 1:55a6170b404f 170 memcpy(vid_pid, &result[32], 4);
nexpaq 1:55a6170b404f 171 vid_pid[4] = 0;
nexpaq 1:55a6170b404f 172 USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid);
nexpaq 1:55a6170b404f 173 }
nexpaq 1:55a6170b404f 174 return status;
nexpaq 1:55a6170b404f 175 }
nexpaq 1:55a6170b404f 176
nexpaq 1:55a6170b404f 177 int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
nexpaq 1:55a6170b404f 178 // if ep stalled: send clear feature
nexpaq 1:55a6170b404f 179 if (res == USB_TYPE_STALL_ERROR) {
nexpaq 1:55a6170b404f 180 res = host->controlWrite( dev,
nexpaq 1:55a6170b404f 181 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
nexpaq 1:55a6170b404f 182 CLEAR_FEATURE,
nexpaq 1:55a6170b404f 183 0, ep->getAddress(), NULL, 0);
nexpaq 1:55a6170b404f 184 // set state to IDLE if clear feature successful
nexpaq 1:55a6170b404f 185 if (res == USB_TYPE_OK) {
nexpaq 1:55a6170b404f 186 ep->setState(USB_TYPE_IDLE);
nexpaq 1:55a6170b404f 187 }
nexpaq 1:55a6170b404f 188 }
nexpaq 1:55a6170b404f 189
nexpaq 1:55a6170b404f 190 if (res != USB_TYPE_OK)
nexpaq 1:55a6170b404f 191 return -1;
nexpaq 1:55a6170b404f 192
nexpaq 1:55a6170b404f 193 return 0;
nexpaq 1:55a6170b404f 194 }
nexpaq 1:55a6170b404f 195
nexpaq 1:55a6170b404f 196
nexpaq 1:55a6170b404f 197 int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
nexpaq 1:55a6170b404f 198
nexpaq 1:55a6170b404f 199 int res = 0;
nexpaq 1:55a6170b404f 200
nexpaq 1:55a6170b404f 201 cbw.Signature = CBW_SIGNATURE;
nexpaq 1:55a6170b404f 202 cbw.Tag = 0;
nexpaq 1:55a6170b404f 203 cbw.DataLength = transfer_len;
nexpaq 1:55a6170b404f 204 cbw.Flags = flags;
nexpaq 1:55a6170b404f 205 cbw.LUN = 0;
nexpaq 1:55a6170b404f 206 cbw.CBLength = cmd_len;
nexpaq 1:55a6170b404f 207 memset(cbw.CB,0,sizeof(cbw.CB));
nexpaq 1:55a6170b404f 208 if (cmd) {
nexpaq 1:55a6170b404f 209 memcpy(cbw.CB,cmd,cmd_len);
nexpaq 1:55a6170b404f 210 }
nexpaq 1:55a6170b404f 211
nexpaq 1:55a6170b404f 212 // send the cbw
nexpaq 1:55a6170b404f 213 USB_DBG("Send CBW");
nexpaq 1:55a6170b404f 214 res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31);
nexpaq 1:55a6170b404f 215 if (checkResult(res, bulk_out))
nexpaq 1:55a6170b404f 216 return -1;
nexpaq 1:55a6170b404f 217
nexpaq 1:55a6170b404f 218 // data stage if needed
nexpaq 1:55a6170b404f 219 if (data) {
nexpaq 1:55a6170b404f 220 USB_DBG("data stage");
nexpaq 1:55a6170b404f 221 if (flags == HOST_TO_DEVICE) {
nexpaq 1:55a6170b404f 222
nexpaq 1:55a6170b404f 223 res = host->bulkWrite(dev, bulk_out, data, transfer_len);
nexpaq 1:55a6170b404f 224 if (checkResult(res, bulk_out))
nexpaq 1:55a6170b404f 225 return -1;
nexpaq 1:55a6170b404f 226
nexpaq 1:55a6170b404f 227 } else if (flags == DEVICE_TO_HOST) {
nexpaq 1:55a6170b404f 228
nexpaq 1:55a6170b404f 229 res = host->bulkRead(dev, bulk_in, data, transfer_len);
nexpaq 1:55a6170b404f 230 if (checkResult(res, bulk_in))
nexpaq 1:55a6170b404f 231 return -1;
nexpaq 1:55a6170b404f 232 }
nexpaq 1:55a6170b404f 233 }
nexpaq 1:55a6170b404f 234
nexpaq 1:55a6170b404f 235 // status stage
nexpaq 1:55a6170b404f 236 csw.Signature = 0;
nexpaq 1:55a6170b404f 237 USB_DBG("Read CSW");
nexpaq 1:55a6170b404f 238 res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13);
nexpaq 1:55a6170b404f 239 if (checkResult(res, bulk_in))
nexpaq 1:55a6170b404f 240 return -1;
nexpaq 1:55a6170b404f 241
nexpaq 1:55a6170b404f 242 if (csw.Signature != CSW_SIGNATURE) {
nexpaq 1:55a6170b404f 243 return -1;
nexpaq 1:55a6170b404f 244 }
nexpaq 1:55a6170b404f 245
nexpaq 1:55a6170b404f 246 USB_DBG("recv csw: status: %d", csw.Status);
nexpaq 1:55a6170b404f 247
nexpaq 1:55a6170b404f 248 // ModeSense?
nexpaq 1:55a6170b404f 249 if ((csw.Status == 1) && (cmd[0] != 0x03)) {
nexpaq 1:55a6170b404f 250 USB_DBG("request mode sense");
nexpaq 1:55a6170b404f 251 return SCSIRequestSense();
nexpaq 1:55a6170b404f 252 }
nexpaq 1:55a6170b404f 253
nexpaq 1:55a6170b404f 254 // perform reset recovery
nexpaq 1:55a6170b404f 255 if ((csw.Status == 2) && (cmd[0] != 0x03)) {
nexpaq 1:55a6170b404f 256
nexpaq 1:55a6170b404f 257 // send Bulk-Only Mass Storage Reset request
nexpaq 1:55a6170b404f 258 res = host->controlWrite( dev,
nexpaq 1:55a6170b404f 259 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
nexpaq 1:55a6170b404f 260 BO_MASS_STORAGE_RESET,
nexpaq 1:55a6170b404f 261 0, msd_intf, NULL, 0);
nexpaq 1:55a6170b404f 262
nexpaq 1:55a6170b404f 263 // unstall both endpoints
nexpaq 1:55a6170b404f 264 res = host->controlWrite( dev,
nexpaq 1:55a6170b404f 265 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
nexpaq 1:55a6170b404f 266 CLEAR_FEATURE,
nexpaq 1:55a6170b404f 267 0, bulk_in->getAddress(), NULL, 0);
nexpaq 1:55a6170b404f 268
nexpaq 1:55a6170b404f 269 res = host->controlWrite( dev,
nexpaq 1:55a6170b404f 270 USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
nexpaq 1:55a6170b404f 271 CLEAR_FEATURE,
nexpaq 1:55a6170b404f 272 0, bulk_out->getAddress(), NULL, 0);
nexpaq 1:55a6170b404f 273
nexpaq 1:55a6170b404f 274 }
nexpaq 1:55a6170b404f 275
nexpaq 1:55a6170b404f 276 return csw.Status;
nexpaq 1:55a6170b404f 277 }
nexpaq 1:55a6170b404f 278
nexpaq 1:55a6170b404f 279
nexpaq 1:55a6170b404f 280 int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
nexpaq 1:55a6170b404f 281 uint8_t cmd[10];
nexpaq 1:55a6170b404f 282 memset(cmd,0,10);
nexpaq 1:55a6170b404f 283 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
nexpaq 1:55a6170b404f 284
nexpaq 1:55a6170b404f 285 cmd[2] = (block >> 24) & 0xff;
nexpaq 1:55a6170b404f 286 cmd[3] = (block >> 16) & 0xff;
nexpaq 1:55a6170b404f 287 cmd[4] = (block >> 8) & 0xff;
nexpaq 1:55a6170b404f 288 cmd[5] = block & 0xff;
nexpaq 1:55a6170b404f 289
nexpaq 1:55a6170b404f 290 cmd[7] = (nbBlock >> 8) & 0xff;
nexpaq 1:55a6170b404f 291 cmd[8] = nbBlock & 0xff;
nexpaq 1:55a6170b404f 292
nexpaq 1:55a6170b404f 293 return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
nexpaq 1:55a6170b404f 294 }
nexpaq 1:55a6170b404f 295
nexpaq 1:55a6170b404f 296 int USBHostMSD::getMaxLun() {
nexpaq 1:55a6170b404f 297 uint8_t buf[1], res;
nexpaq 1:55a6170b404f 298 res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
nexpaq 1:55a6170b404f 299 0xfe, 0, msd_intf, buf, 1);
nexpaq 1:55a6170b404f 300 USB_DBG("max lun: %d", buf[0]);
nexpaq 1:55a6170b404f 301 return res;
nexpaq 1:55a6170b404f 302 }
nexpaq 1:55a6170b404f 303
nexpaq 1:55a6170b404f 304 int USBHostMSD::disk_initialize() {
nexpaq 1:55a6170b404f 305 USB_DBG("FILESYSTEM: init");
nexpaq 1:55a6170b404f 306 uint16_t i, timeout = 10;
nexpaq 1:55a6170b404f 307
nexpaq 1:55a6170b404f 308 getMaxLun();
nexpaq 1:55a6170b404f 309
nexpaq 1:55a6170b404f 310 for (i = 0; i < timeout; i++) {
nexpaq 1:55a6170b404f 311 Thread::wait(100);
nexpaq 1:55a6170b404f 312 if (!testUnitReady())
nexpaq 1:55a6170b404f 313 break;
nexpaq 1:55a6170b404f 314 }
nexpaq 1:55a6170b404f 315
nexpaq 1:55a6170b404f 316 if (i == timeout) {
nexpaq 1:55a6170b404f 317 disk_init = false;
nexpaq 1:55a6170b404f 318 return -1;
nexpaq 1:55a6170b404f 319 }
nexpaq 1:55a6170b404f 320
nexpaq 1:55a6170b404f 321 inquiry(0, 0);
nexpaq 1:55a6170b404f 322 disk_init = 1;
nexpaq 1:55a6170b404f 323 return readCapacity();
nexpaq 1:55a6170b404f 324 }
nexpaq 1:55a6170b404f 325
nexpaq 1:55a6170b404f 326 int USBHostMSD::disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count) {
nexpaq 1:55a6170b404f 327 USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count);
nexpaq 1:55a6170b404f 328 if (!disk_init) {
nexpaq 1:55a6170b404f 329 disk_initialize();
nexpaq 1:55a6170b404f 330 }
nexpaq 1:55a6170b404f 331 if (!disk_init)
nexpaq 1:55a6170b404f 332 return -1;
nexpaq 1:55a6170b404f 333 for (uint32_t b = block_number; b < block_number + count; b++) {
nexpaq 1:55a6170b404f 334 if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE))
nexpaq 1:55a6170b404f 335 return -1;
nexpaq 1:55a6170b404f 336 buffer += 512;
nexpaq 1:55a6170b404f 337 }
nexpaq 1:55a6170b404f 338 return 0;
nexpaq 1:55a6170b404f 339 }
nexpaq 1:55a6170b404f 340
nexpaq 1:55a6170b404f 341 int USBHostMSD::disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count) {
nexpaq 1:55a6170b404f 342 USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count);
nexpaq 1:55a6170b404f 343 if (!disk_init) {
nexpaq 1:55a6170b404f 344 disk_initialize();
nexpaq 1:55a6170b404f 345 }
nexpaq 1:55a6170b404f 346 if (!disk_init)
nexpaq 1:55a6170b404f 347 return -1;
nexpaq 1:55a6170b404f 348 for (uint32_t b = block_number; b < block_number + count; b++) {
nexpaq 1:55a6170b404f 349 if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST))
nexpaq 1:55a6170b404f 350 return -1;
nexpaq 1:55a6170b404f 351 buffer += 512;
nexpaq 1:55a6170b404f 352 }
nexpaq 1:55a6170b404f 353 return 0;
nexpaq 1:55a6170b404f 354 }
nexpaq 1:55a6170b404f 355
nexpaq 1:55a6170b404f 356 uint32_t USBHostMSD::disk_sectors() {
nexpaq 1:55a6170b404f 357 USB_DBG("FILESYSTEM: sectors");
nexpaq 1:55a6170b404f 358 if (!disk_init) {
nexpaq 1:55a6170b404f 359 disk_initialize();
nexpaq 1:55a6170b404f 360 }
nexpaq 1:55a6170b404f 361 if (!disk_init)
nexpaq 1:55a6170b404f 362 return 0;
nexpaq 1:55a6170b404f 363 return blockCount;
nexpaq 1:55a6170b404f 364 }
nexpaq 1:55a6170b404f 365
nexpaq 1:55a6170b404f 366 #endif