You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MassStorageDriver.cpp 19KB

4 jaren geleden
3 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /* MSC Teensy36 USB Host Mass Storage library
  2. * Copyright (c) 2017-2019 Warren Watson.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. //MassStorageDriver.cpp
  25. #include <Arduino.h>
  26. #include "USBHost_t36.h" // Read this header first for key info
  27. #define print USBHost::print_
  28. #define println USBHost::println_
  29. // Uncomment this to display function usage and sequencing.
  30. #define DBGprint 0
  31. // Big Endian/Little Endian
  32. #define swap32(x) ((x >> 24) & 0xff) | \
  33. ((x << 8) & 0xff0000) | \
  34. ((x >> 8) & 0xff00) | \
  35. ((x << 24) & 0xff000000)
  36. void msController::init()
  37. {
  38. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  39. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  40. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  41. driver_ready_for_device(this);
  42. }
  43. bool msController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  44. {
  45. println("msController claim this=", (uint32_t)this, HEX);
  46. // only claim at interface level
  47. if (type != 1) return false;
  48. if (len < 9+7+7) return false; // Interface descriptor + 2 endpoint decriptors
  49. print_hexbytes(descriptors, len);
  50. uint32_t numendpoint = descriptors[4];
  51. if (numendpoint < 1) return false;
  52. if (descriptors[5] != 8) return false; // bInterfaceClass, 8 = MASS Storage class
  53. if (descriptors[6] != 6) return false; // bInterfaceSubClass, 6 = SCSI transparent command set (SCSI Standards)
  54. if (descriptors[7] != 80) return false; // bInterfaceProtocol, 80 = BULK-ONLY TRANSPORT
  55. bInterfaceNumber = descriptors[2];
  56. uint8_t desc_index = 9;
  57. uint8_t in_index = 0xff, out_index = 0xff;
  58. println("numendpoint=", numendpoint, HEX);
  59. while (numendpoint--) {
  60. if ((descriptors[desc_index] != 7) || (descriptors[desc_index+1] != 5)) return false; // not an end point
  61. if (descriptors[desc_index+3] == 2) { // Bulk end point
  62. if (descriptors[desc_index+2] & 0x80)
  63. in_index = desc_index;
  64. else
  65. out_index = desc_index;
  66. }
  67. desc_index += 7; // point to next one...
  68. }
  69. if ((in_index == 0xff) || (out_index == 0xff)) return false; // did not find end point
  70. endpointIn = descriptors[in_index+2]; // bulk-in descriptor 1 81h
  71. endpointOut = descriptors[out_index+2]; // bulk-out descriptor 2 02h
  72. println("endpointIn=", endpointIn, HEX);
  73. println("endpointOut=", endpointOut, HEX);
  74. uint32_t sizeIn = descriptors[in_index+4] | (descriptors[in_index+5] << 8);
  75. println("packet size in (msController) = ", sizeIn);
  76. uint32_t sizeOut = descriptors[out_index+4] | (descriptors[out_index+5] << 8);
  77. println("packet size out (msController) = ", sizeOut);
  78. packetSizeIn = sizeIn;
  79. packetSizeOut = sizeOut;
  80. uint32_t intervalIn = descriptors[in_index+6];
  81. uint32_t intervalOut = descriptors[out_index+6];
  82. println("polling intervalIn = ", intervalIn);
  83. println("polling intervalOut = ", intervalOut);
  84. datapipeIn = new_Pipe(dev, 2, endpointIn, 1, packetSizeIn, intervalIn);
  85. datapipeOut = new_Pipe(dev, 2, endpointOut, 0, packetSizeOut, intervalOut);
  86. datapipeIn->callback_function = callbackIn;
  87. datapipeOut->callback_function = callbackOut;
  88. idVendor = dev->idVendor;
  89. idProduct = dev->idProduct;
  90. hubNumber = dev->hub_address;
  91. deviceAddress = dev->address;
  92. hubPort = dev->hub_port; // Used for device ID with multiple drives.
  93. msOutCompleted = false;
  94. msInCompleted = false;
  95. msControlCompleted = false;
  96. deviceAvailable = true;
  97. msDriveInfo.initialized = false;
  98. msDriveInfo.connected = true;
  99. #ifdef DBGprint
  100. print(" connected = ");
  101. println(msDriveInfo.connected);
  102. print(" initialized = ");
  103. println(msDriveInfo.initialized);
  104. #endif
  105. return true;
  106. }
  107. void msController::disconnect()
  108. {
  109. deviceAvailable = false;
  110. println("Device Disconnected...");
  111. msDriveInfo.connected = false;
  112. msDriveInfo.initialized = false;
  113. memset(&msDriveInfo, 0, sizeof(msDriveInfo_t));
  114. #ifdef DBGprint
  115. print(" connected ");
  116. println(msDriveInfo.connected);
  117. print(" initialized ");
  118. println(msDriveInfo.initialized);
  119. #endif
  120. }
  121. void msController::control(const Transfer_t *transfer)
  122. {
  123. println("control CallbackIn (msController)");
  124. print_hexbytes(report, 8);
  125. msControlCompleted = true;
  126. }
  127. void msController::callbackIn(const Transfer_t *transfer)
  128. {
  129. println("msController CallbackIn (static)");
  130. if (transfer->driver) {
  131. print("transfer->qtd.token = ");
  132. println(transfer->qtd.token & 255);
  133. ((msController *)(transfer->driver))->new_dataIn(transfer);
  134. }
  135. }
  136. void msController::callbackOut(const Transfer_t *transfer)
  137. {
  138. println("msController CallbackOut (static)");
  139. if (transfer->driver) {
  140. print("transfer->qtd.token = ");
  141. println(transfer->qtd.token & 255);
  142. ((msController *)(transfer->driver))->new_dataOut(transfer);
  143. }
  144. }
  145. void msController::new_dataOut(const Transfer_t *transfer)
  146. {
  147. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  148. println("msController dataOut (static)", len, DEC);
  149. print_hexbytes((uint8_t*)transfer->buffer, (len < 32)? len : 32 );
  150. msOutCompleted = true; // Last out transaction is completed.
  151. }
  152. void msController::new_dataIn(const Transfer_t *transfer)
  153. {
  154. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  155. println("msController dataIn (static): ", len, DEC);
  156. print_hexbytes((uint8_t*)transfer->buffer, (len < 32)? len : 32 );
  157. msInCompleted = true; // Last in transaction is completed.
  158. }
  159. // Initialize Mass Storage Device
  160. uint8_t msController::mscInit(void) {
  161. #ifdef DBGprint
  162. println("mscIint()");
  163. #endif
  164. uint8_t msResult = MS_CBW_PASS;
  165. CBWTag = 0;
  166. uint32_t start = millis();
  167. // Check if device is connected.
  168. do {
  169. if((millis() - start) >= MSC_CONNECT_TIMEOUT) {
  170. return MS_NO_MEDIA_ERR; // Not connected Error.
  171. }
  172. yield();
  173. } while(!available());
  174. msReset();
  175. // delay(500); // Not needed any more.
  176. maxLUN = msGetMaxLun();
  177. // msResult = msReportLUNs(&maxLUN);
  178. //println("maxLUN = ");
  179. //println(maxLUN);
  180. // delay(150);
  181. //-------------------------------------------------------
  182. msResult = msStartStopUnit(1);
  183. msResult = WaitMediaReady();
  184. if(msResult)
  185. return msResult;
  186. // Retrieve drive information.
  187. msDriveInfo.initialized = true;
  188. msDriveInfo.hubNumber = getHubNumber(); // Which HUB.
  189. msDriveInfo.hubPort = getHubPort(); // Which HUB port.
  190. msDriveInfo.deviceAddress = getDeviceAddress(); // Device addreess.
  191. msDriveInfo.idVendor = getIDVendor(); // USB Vendor ID.
  192. msDriveInfo.idProduct = getIDProduct(); // USB Product ID.
  193. msResult = msDeviceInquiry(&msInquiry); // Config Info.
  194. if(msResult)
  195. return msResult;
  196. msResult = msReadDeviceCapacity(&msCapacity); // Size Info.
  197. if(msResult)
  198. return msResult;
  199. memcpy(&msDriveInfo.inquiry, &msInquiry, sizeof(msInquiryResponse_t));
  200. memcpy(&msDriveInfo.capacity, &msCapacity, sizeof(msSCSICapacity_t));
  201. return msResult;
  202. }
  203. //---------------------------------------------------------------------------
  204. // Perform Mass Storage Reset
  205. void msController::msReset(void) {
  206. #ifdef DBGprint
  207. println("msReset()");
  208. #endif
  209. mk_setup(setup, 0x21, 0xff, 0, bInterfaceNumber, 0);
  210. queue_Control_Transfer(device, &setup, NULL, this);
  211. while (!msControlCompleted) yield();
  212. msControlCompleted = false;
  213. }
  214. //---------------------------------------------------------------------------
  215. // Get MAX LUN
  216. uint8_t msController::msGetMaxLun(void) {
  217. #ifdef DBGprint
  218. println("msGetMaxLun()");
  219. #endif
  220. report[0] = 0;
  221. mk_setup(setup, 0xa1, 0xfe, 0, bInterfaceNumber, 1);
  222. queue_Control_Transfer(device, &setup, report, this);
  223. while (!msControlCompleted) yield();
  224. msControlCompleted = false;
  225. maxLUN = report[0];
  226. return maxLUN;
  227. }
  228. uint8_t msController::WaitMediaReady() {
  229. uint8_t msResult;
  230. uint32_t start = millis();
  231. #ifdef DBGprint
  232. println("WaitMediaReady()");
  233. #endif
  234. do {
  235. if((millis() - start) >= MEDIA_READY_TIMEOUT) {
  236. return MS_UNIT_NOT_READY; // Not Ready Error.
  237. }
  238. msResult = msTestReady();
  239. yield();
  240. } while(msResult == 1);
  241. return msResult;
  242. }
  243. // Check if drive is connected and Initialized.
  244. uint8_t msController::checkConnectedInitialized(void) {
  245. uint8_t msResult = MS_CBW_PASS;
  246. #ifdef DBGprint
  247. print("checkConnectedInitialized()");
  248. #endif
  249. if(!msDriveInfo.connected) {
  250. return MS_NO_MEDIA_ERR;
  251. }
  252. if(!msDriveInfo.initialized) {
  253. msResult = mscInit();
  254. if(msResult != MS_CBW_PASS) return MS_UNIT_NOT_READY; // Not Initialized
  255. }
  256. return MS_CBW_PASS;
  257. }
  258. //---------------------------------------------------------------------------
  259. // Send SCSI Command
  260. // Do a complete 3 stage transfer.
  261. uint8_t msController::msDoCommand(msCommandBlockWrapper_t *CBW, void *buffer)
  262. {
  263. uint8_t CSWResult = 0;
  264. mscTransferComplete = false;
  265. #ifdef DBGprint
  266. println("msDoCommand()");
  267. #endif
  268. if(CBWTag == 0xFFFFFFFF) CBWTag = 1;
  269. queue_Data_Transfer(datapipeOut, CBW, sizeof(msCommandBlockWrapper_t), this); // Command stage.
  270. while(!msOutCompleted) yield();
  271. msOutCompleted = false;
  272. if((CBW->Flags == CMD_DIR_DATA_IN)) { // Data stage from device.
  273. queue_Data_Transfer(datapipeIn, buffer, CBW->TransferLength, this);
  274. while(!msInCompleted) yield();
  275. msInCompleted = false;
  276. } else { // Data stage to device.
  277. queue_Data_Transfer(datapipeOut, buffer, CBW->TransferLength, this);
  278. while(!msOutCompleted) yield();
  279. msOutCompleted = false;
  280. }
  281. CSWResult = msGetCSW(); // Status stage.
  282. // All stages of this transfer have completed.
  283. //Check for special cases.
  284. //If test for unit ready command is given then
  285. // return the CSW status byte.
  286. //Bit 0 == 1 == not ready else
  287. //Bit 0 == 0 == ready.
  288. //And the Start/Stop Unit command as well.
  289. if((CBW->CommandData[0] == CMD_TEST_UNIT_READY) ||
  290. (CBW->CommandData[0] == CMD_START_STOP_UNIT))
  291. return CSWResult;
  292. else // Process possible SCSI errors.
  293. return msProcessError(CSWResult);
  294. }
  295. //---------------------------------------------------------------------------
  296. // Get Command Status Wrapper
  297. uint8_t msController::msGetCSW(void) {
  298. #ifdef DBGprint
  299. println("msGetCSW()");
  300. #endif
  301. msCommandStatusWrapper_t StatusBlockWrapper = (msCommandStatusWrapper_t)
  302. {
  303. .Signature = CSW_SIGNATURE,
  304. .Tag = 0,
  305. .DataResidue = 0, // TODO: Proccess this if received.
  306. .Status = 0
  307. };
  308. queue_Data_Transfer(datapipeIn, &StatusBlockWrapper, sizeof(StatusBlockWrapper), this);
  309. while(!msInCompleted) yield();
  310. msInCompleted = false;
  311. mscTransferComplete = true;
  312. if(StatusBlockWrapper.Signature != CSW_SIGNATURE) return msProcessError(MS_CSW_SIG_ERROR); // Signature error
  313. if(StatusBlockWrapper.Tag != CBWTag) return msProcessError(MS_CSW_TAG_ERROR); // Tag mismatch error
  314. return StatusBlockWrapper.Status;
  315. }
  316. //---------------------------------------------------------------------------
  317. // Test Unit Ready
  318. uint8_t msController::msTestReady() {
  319. #ifdef DBGprint
  320. println("msTestReady()");
  321. #endif
  322. msCommandBlockWrapper_t CommandBlockWrapper = (msCommandBlockWrapper_t)
  323. {
  324. .Signature = CBW_SIGNATURE,
  325. .Tag = ++CBWTag,
  326. .TransferLength = 0,
  327. .Flags = CMD_DIR_DATA_IN,
  328. .LUN = currentLUN,
  329. .CommandLength = 6,
  330. .CommandData = {CMD_TEST_UNIT_READY, 0x00, 0x00, 0x00, 0x00, 0x00}
  331. };
  332. queue_Data_Transfer(datapipeOut, &CommandBlockWrapper, sizeof(CommandBlockWrapper), this);
  333. while(!msOutCompleted) yield();
  334. msOutCompleted = false;
  335. return msGetCSW();
  336. }
  337. //---------------------------------------------------------------------------
  338. // Start/Stop unit
  339. uint8_t msController::msStartStopUnit(uint8_t mode) {
  340. #ifdef DBGprint
  341. println("msStartStopUnit()");
  342. #endif
  343. msCommandBlockWrapper_t CommandBlockWrapper = (msCommandBlockWrapper_t)
  344. {
  345. .Signature = CBW_SIGNATURE,
  346. .Tag = ++CBWTag,
  347. .TransferLength = 0,
  348. .Flags = CMD_DIR_DATA_IN,
  349. .LUN = currentLUN,
  350. .CommandLength = 6,
  351. .CommandData = {CMD_START_STOP_UNIT, 0x01, 0x00, 0x00, mode, 0x00}
  352. };
  353. queue_Data_Transfer(datapipeOut, &CommandBlockWrapper, sizeof(CommandBlockWrapper), this);
  354. while(!msOutCompleted) yield();
  355. msOutCompleted = false;
  356. return msGetCSW();
  357. }
  358. //---------------------------------------------------------------------------
  359. // Read Mass Storage Device Capacity (Number of Blocks and Block Size)
  360. uint8_t msController::msReadDeviceCapacity(msSCSICapacity_t * const Capacity) {
  361. #ifdef DBGprint
  362. println("msReadDeviceCapacity()");
  363. #endif
  364. uint8_t result = 0;
  365. msCommandBlockWrapper_t CommandBlockWrapper = (msCommandBlockWrapper_t)
  366. {
  367. .Signature = CBW_SIGNATURE,
  368. .Tag = ++CBWTag,
  369. .TransferLength = sizeof(msSCSICapacity_t),
  370. .Flags = CMD_DIR_DATA_IN,
  371. .LUN = currentLUN,
  372. .CommandLength = 10,
  373. .CommandData = {CMD_RD_CAPACITY_10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  374. };
  375. result = msDoCommand(&CommandBlockWrapper, Capacity);
  376. Capacity->Blocks = swap32(Capacity->Blocks);
  377. Capacity->BlockSize = swap32(Capacity->BlockSize);
  378. return result;
  379. }
  380. //---------------------------------------------------------------------------
  381. // Do Mass Storage Device Inquiry
  382. uint8_t msController::msDeviceInquiry(msInquiryResponse_t * const Inquiry)
  383. {
  384. #ifdef DBGprint
  385. println("msDeviceInquiry()");
  386. #endif
  387. msCommandBlockWrapper_t CommandBlockWrapper = (msCommandBlockWrapper_t)
  388. {
  389. .Signature = CBW_SIGNATURE,
  390. .Tag = ++CBWTag,
  391. .TransferLength = sizeof(msInquiryResponse_t),
  392. .Flags = CMD_DIR_DATA_IN,
  393. .LUN = currentLUN,
  394. .CommandLength = 6,
  395. .CommandData = {CMD_INQUIRY,0x00,0x00,0x00,sizeof(msInquiryResponse_t),0x00}
  396. };
  397. return msDoCommand(&CommandBlockWrapper, Inquiry);
  398. }
  399. //---------------------------------------------------------------------------
  400. // Request Sense Data
  401. uint8_t msController::msRequestSense(msRequestSenseResponse_t * const Sense)
  402. {
  403. #ifdef DBGprint
  404. println("msRequestSense()");
  405. #endif
  406. msCommandBlockWrapper_t CommandBlockWrapper = (msCommandBlockWrapper_t)
  407. {
  408. .Signature = CBW_SIGNATURE,
  409. .Tag = ++CBWTag,
  410. .TransferLength = sizeof(msRequestSenseResponse_t),
  411. .Flags = CMD_DIR_DATA_IN,
  412. .LUN = currentLUN,
  413. .CommandLength = 6,
  414. .CommandData = {CMD_REQUEST_SENSE, 0x00, 0x00, 0x00, sizeof(msRequestSenseResponse_t), 0x00}
  415. };
  416. return msDoCommand(&CommandBlockWrapper, Sense);
  417. }
  418. //---------------------------------------------------------------------------
  419. // Report LUNs
  420. uint8_t msController::msReportLUNs(uint8_t *Buffer)
  421. {
  422. #ifdef DBGprint
  423. println("msReportLuns()");
  424. #endif
  425. msCommandBlockWrapper_t CommandBlockWrapper = (msCommandBlockWrapper_t)
  426. {
  427. .Signature = CBW_SIGNATURE,
  428. .Tag = ++CBWTag,
  429. .TransferLength = MAXLUNS,
  430. .Flags = CMD_DIR_DATA_IN,
  431. .LUN = currentLUN,
  432. .CommandLength = 12,
  433. .CommandData = {CMD_REPORT_LUNS, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, MAXLUNS, 0x00, 0x00}
  434. };
  435. return msDoCommand(&CommandBlockWrapper, Buffer);
  436. }
  437. //---------------------------------------------------------------------------
  438. // Read Sectors (Multi Sector Capable)
  439. uint8_t msController::msReadBlocks(
  440. const uint32_t BlockAddress,
  441. const uint16_t Blocks,
  442. const uint16_t BlockSize,
  443. void * sectorBuffer)
  444. {
  445. #ifdef DBGprint
  446. println("msReadBlocks()");
  447. #endif
  448. uint8_t BlockHi = (Blocks >> 8) & 0xFF;
  449. uint8_t BlockLo = Blocks & 0xFF;
  450. msCommandBlockWrapper_t CommandBlockWrapper = (msCommandBlockWrapper_t)
  451. {
  452. .Signature = CBW_SIGNATURE,
  453. .Tag = ++CBWTag,
  454. .TransferLength = (uint32_t)(Blocks * BlockSize),
  455. .Flags = CMD_DIR_DATA_IN,
  456. .LUN = currentLUN,
  457. .CommandLength = 10,
  458. .CommandData = {CMD_RD_10, 0x00,
  459. (uint8_t)(BlockAddress >> 24),
  460. (uint8_t)(BlockAddress >> 16),
  461. (uint8_t)(BlockAddress >> 8),
  462. (uint8_t)(BlockAddress & 0xFF),
  463. 0x00, BlockHi, BlockLo, 0x00}
  464. };
  465. return msDoCommand(&CommandBlockWrapper, sectorBuffer);
  466. }
  467. //---------------------------------------------------------------------------
  468. // Write Sectors (Multi Sector Capable)
  469. uint8_t msController::msWriteBlocks(
  470. const uint32_t BlockAddress,
  471. const uint16_t Blocks,
  472. const uint16_t BlockSize,
  473. const void * sectorBuffer)
  474. {
  475. #ifdef DBGprint
  476. println("msWriteBlocks()");
  477. #endif
  478. uint8_t BlockHi = (Blocks >> 8) & 0xFF;
  479. uint8_t BlockLo = Blocks & 0xFF;
  480. msCommandBlockWrapper_t CommandBlockWrapper = (msCommandBlockWrapper_t)
  481. {
  482. .Signature = CBW_SIGNATURE,
  483. .Tag = ++CBWTag,
  484. .TransferLength = (uint32_t)(Blocks * BlockSize),
  485. .Flags = CMD_DIR_DATA_OUT,
  486. .LUN = currentLUN,
  487. .CommandLength = 10,
  488. .CommandData = {CMD_WR_10, 0x00,
  489. (uint8_t)(BlockAddress >> 24),
  490. (uint8_t)(BlockAddress >> 16),
  491. (uint8_t)(BlockAddress >> 8),
  492. (uint8_t)(BlockAddress & 0xFF),
  493. 0x00, BlockHi, BlockLo, 0x00}
  494. };
  495. return msDoCommand(&CommandBlockWrapper, (void *)sectorBuffer);
  496. }
  497. // Proccess Possible SCSI errors
  498. uint8_t msController::msProcessError(uint8_t msStatus) {
  499. #ifdef DBGprint
  500. println("msProcessError()");
  501. #endif
  502. uint8_t msResult = 0;
  503. switch(msStatus) {
  504. case MS_CBW_PASS:
  505. return MS_CBW_PASS;
  506. break;
  507. case MS_CBW_PHASE_ERROR:
  508. print("SCSI Phase Error: ");
  509. println(msStatus);
  510. return MS_SCSI_ERROR;
  511. break;
  512. case MS_CSW_TAG_ERROR:
  513. print("CSW Tag Error: ");
  514. println(MS_CSW_TAG_ERROR);
  515. return MS_CSW_TAG_ERROR;
  516. break;
  517. case MS_CSW_SIG_ERROR:
  518. print("CSW Signature Error: ");
  519. println(MS_CSW_SIG_ERROR);
  520. return MS_CSW_SIG_ERROR;
  521. break;
  522. case MS_CBW_FAIL:
  523. if(msResult = msRequestSense(&msSense)) {
  524. print("Failed to get sense codes. Returned code: ");
  525. println(msResult);
  526. }
  527. return MS_CBW_FAIL;
  528. break;
  529. default:
  530. print("SCSI Error: ");
  531. println(msStatus);
  532. return msStatus;
  533. }
  534. }