BlueSCSI.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /*
  2. * BlueSCSI
  3. * Copyright (c) 2021 Eric Helgeson, Androda
  4. *
  5. * This file is free software: you may copy, redistribute and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation, either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This file is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see https://github.com/erichelgeson/bluescsi.
  17. *
  18. * This file incorporates work covered by the following copyright and
  19. * permission notice:
  20. *
  21. * Copyright (c) 2019 komatsu
  22. *
  23. * Permission to use, copy, modify, and/or distribute this software
  24. * for any purpose with or without fee is hereby granted, provided
  25. * that the above copyright notice and this permission notice appear
  26. * in all copies.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  29. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  31. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  32. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  33. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  34. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  35. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  36. */
  37. #include <Arduino.h> // For Platform.IO
  38. #include <SdFat.h>
  39. #include <setjmp.h>
  40. #include "scsi_cmds.h"
  41. #include "scsi_sense.h"
  42. #include "scsi_status.h"
  43. #ifdef USE_STM32_DMA
  44. #warning "warning USE_STM32_DMA"
  45. #endif
  46. #define DEBUG 0 // 0:No debug information output
  47. // 1: Debug information output to USB Serial
  48. // 2: Debug information output to LOG.txt (slow)
  49. // SCSI config
  50. #define NUM_SCSIID 7 // Maximum number of supported SCSI-IDs (The minimum is 0)
  51. #define NUM_SCSILUN 2 // Maximum number of LUNs supported (The minimum is 0)
  52. #define READ_PARITY_CHECK 0 // Perform read parity check (unverified)
  53. // HDD format
  54. #define MAX_BLOCKSIZE 2048 // Maximum BLOCK size
  55. // SDFAT
  56. SdFs SD;
  57. #if DEBUG == 1
  58. #define LOG(XX) Serial.print(XX)
  59. #define LOGHEX(XX) Serial.print(XX, HEX)
  60. #define LOGN(XX) Serial.println(XX)
  61. #define LOGHEXN(XX) Serial.println(XX, HEX)
  62. #elif DEBUG == 2
  63. #define LOG(XX) LOG_FILE.print(XX); LOG_FILE.sync();
  64. #define LOGHEX(XX) LOG_FILE.print(XX, HEX); LOG_FILE.sync();
  65. #define LOGN(XX) LOG_FILE.println(XX); LOG_FILE.sync();
  66. #define LOGHEXN(XX) LOG_FILE.println(XX, HEX); LOG_FILE.sync();
  67. #else
  68. #define LOG(XX) //Serial.print(XX)
  69. #define LOGHEX(XX) //Serial.print(XX, HEX)
  70. #define LOGN(XX) //Serial.println(XX)
  71. #define LOGHEXN(XX) //Serial.println(XX, HEX)
  72. #endif
  73. #define active 1
  74. #define inactive 0
  75. #define high 0
  76. #define low 1
  77. #define isHigh(XX) ((XX) == high)
  78. #define isLow(XX) ((XX) != high)
  79. #define gpio_mode(pin,val) gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val);
  80. #define gpio_write(pin,val) gpio_write_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val)
  81. #define gpio_read(pin) gpio_read_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit)
  82. //#define DB0 PB8 // SCSI:DB0
  83. //#define DB1 PB9 // SCSI:DB1
  84. //#define DB2 PB10 // SCSI:DB2
  85. //#define DB3 PB11 // SCSI:DB3
  86. //#define DB4 PB12 // SCSI:DB4
  87. //#define DB5 PB13 // SCSI:DB5
  88. //#define DB6 PB14 // SCSI:DB6
  89. //#define DB7 PB15 // SCSI:DB7
  90. //#define DBP PB0 // SCSI:DBP
  91. #define ATN PA8 // SCSI:ATN
  92. #define BSY PA9 // SCSI:BSY
  93. #define ACK PA10 // SCSI:ACK
  94. #define RST PA15 // SCSI:RST
  95. #define MSG PB3 // SCSI:MSG
  96. #define SEL PB4 // SCSI:SEL
  97. #define CD PB5 // SCSI:C/D
  98. #define REQ PB6 // SCSI:REQ
  99. #define IO PB7 // SCSI:I/O
  100. #define LED2 PA0 // External LED
  101. #define SD_CS PA4 // SDCARD:CS
  102. #define LED PC13 // LED
  103. // Image Set Selector
  104. #ifdef XCVR
  105. #define IMAGE_SELECT1 PC14
  106. #define IMAGE_SELECT2 PC15
  107. #else
  108. #define IMAGE_SELECT1 PA1
  109. #define IMAGE_SELECT2 PB1
  110. #endif
  111. // GPIO register port
  112. #define PAREG GPIOA->regs
  113. #define PBREG GPIOB->regs
  114. // LED control
  115. #define LED_ON() gpio_write(LED, high); gpio_write(LED2, low);
  116. #define LED_OFF() gpio_write(LED, low); gpio_write(LED2, high);
  117. // Virtual pin (Arduio compatibility is slow, so make it MCU-dependent)
  118. #define PA(BIT) (BIT)
  119. #define PB(BIT) (BIT+16)
  120. // Virtual pin decoding
  121. #define GPIOREG(VPIN) ((VPIN)>=16?PBREG:PAREG)
  122. #define BITMASK(VPIN) (1<<((VPIN)&15))
  123. #define vATN PA(8) // SCSI:ATN
  124. #define vBSY PA(9) // SCSI:BSY
  125. #define vACK PA(10) // SCSI:ACK
  126. #define vRST PA(15) // SCSI:RST
  127. #define vMSG PB(3) // SCSI:MSG
  128. #define vSEL PB(4) // SCSI:SEL
  129. #define vCD PB(5) // SCSI:C/D
  130. #define vREQ PB(6) // SCSI:REQ
  131. #define vIO PB(7) // SCSI:I/O
  132. #define vSD_CS PA(4) // SDCARD:CS
  133. // SCSI output pin control: opendrain active LOW (direct pin drive)
  134. #define SCSI_OUT(VPIN,ACTIVE) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN)<<((ACTIVE)?16:0); }
  135. // SCSI input pin check (inactive=0,avtive=1)
  136. #define SCSI_IN(VPIN) ((~GPIOREG(VPIN)->IDR>>(VPIN&15))&1)
  137. // SCSI phase change as single write to port B
  138. #define SCSIPHASEMASK(MSGACTIVE, CDACTIVE, IOACTIVE) ((BITMASK(vMSG)<<((MSGACTIVE)?16:0)) | (BITMASK(vCD)<<((CDACTIVE)?16:0)) | (BITMASK(vIO)<<((IOACTIVE)?16:0)))
  139. #define SCSI_PHASE_DATAOUT SCSIPHASEMASK(inactive, inactive, inactive)
  140. #define SCSI_PHASE_DATAIN SCSIPHASEMASK(inactive, inactive, active)
  141. #define SCSI_PHASE_COMMAND SCSIPHASEMASK(inactive, active, inactive)
  142. #define SCSI_PHASE_STATUS SCSIPHASEMASK(inactive, active, active)
  143. #define SCSI_PHASE_MESSAGEOUT SCSIPHASEMASK(active, active, inactive)
  144. #define SCSI_PHASE_MESSAGEIN SCSIPHASEMASK(active, active, active)
  145. #define SCSI_PHASE_CHANGE(MASK) { PBREG->BSRR = (MASK); }
  146. #ifdef XCVR
  147. #define TR_TARGET PA1 // Target Transceiver Control Pin
  148. #define TR_DBP PA2 // Data Pins Transceiver Control Pin
  149. #define TR_INITIATOR PA3 // Initiator Transciever Control Pin
  150. #define vTR_TARGET PA(1) // Target Transceiver Control Pin
  151. #define vTR_DBP PA(2) // Data Pins Transceiver Control Pin
  152. #define vTR_INITIATOR PA(3) // Initiator Transciever Control Pin
  153. #define TR_INPUT 1
  154. #define TR_OUTPUT 0
  155. // Transceiver control definitions
  156. #define TRANSCEIVER_IO_SET(VPIN,TR_INPUT) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN) << ((TR_INPUT) ? 16 : 0); }
  157. // Turn on the output only for BSY
  158. #define SCSI_BSY_ACTIVE() { gpio_mode(BSY, GPIO_OUTPUT_PP); SCSI_OUT(vBSY, active) }
  159. #define SCSI_TARGET_ACTIVE() { gpio_mode(REQ, GPIO_OUTPUT_PP); gpio_mode(MSG, GPIO_OUTPUT_PP); gpio_mode(CD, GPIO_OUTPUT_PP); gpio_mode(IO, GPIO_OUTPUT_PP); gpio_mode(BSY, GPIO_OUTPUT_PP); TRANSCEIVER_IO_SET(vTR_TARGET,TR_OUTPUT);}
  160. // BSY,REQ,MSG,CD,IO Turn off output, BSY is the last input
  161. #define SCSI_TARGET_INACTIVE() { pinMode(REQ, INPUT); pinMode(MSG, INPUT); pinMode(CD, INPUT); pinMode(IO, INPUT); pinMode(BSY, INPUT); TRANSCEIVER_IO_SET(vTR_TARGET,TR_INPUT); }
  162. #define DB_MODE_OUT 1 // push-pull mode
  163. #define DB_MODE_IN 4 // floating inputs
  164. #else
  165. // GPIO mode
  166. // IN , FLOAT : 4
  167. // IN , PU/PD : 8
  168. // OUT, PUSH/PULL : 3
  169. // OUT, OD : 7
  170. #define DB_MODE_OUT 3
  171. //#define DB_MODE_OUT 7
  172. #define DB_MODE_IN 8
  173. // Turn on the output only for BSY
  174. #define SCSI_BSY_ACTIVE() { gpio_mode(BSY, GPIO_OUTPUT_OD); SCSI_OUT(vBSY, active) }
  175. // BSY,REQ,MSG,CD,IO Turn on the output (no change required for OD)
  176. #define SCSI_TARGET_ACTIVE() { if (DB_MODE_OUT != 7) gpio_mode(REQ, GPIO_OUTPUT_PP);}
  177. // BSY,REQ,MSG,CD,IO Turn off output, BSY is the last input
  178. #define SCSI_TARGET_INACTIVE() { if (DB_MODE_OUT == 7) SCSI_OUT(vREQ,inactive) else { if (DB_MODE_IN == 8) gpio_mode(REQ, GPIO_INPUT_PU) else gpio_mode(REQ, GPIO_INPUT_FLOATING)} SCSI_PHASE_CHANGE(SCSI_PHASE_DATAOUT); gpio_mode(BSY, GPIO_INPUT_PU); }
  179. #endif
  180. // Put DB and DP in output mode
  181. #define SCSI_DB_OUTPUT() { PBREG->CRL=(PBREG->CRL &0xfffffff0)|DB_MODE_OUT; PBREG->CRH = 0x11111111*DB_MODE_OUT; }
  182. // Put DB and DP in input mode
  183. #define SCSI_DB_INPUT() { PBREG->CRL=(PBREG->CRL &0xfffffff0)|DB_MODE_IN ; PBREG->CRH = 0x11111111*DB_MODE_IN; }
  184. // HDDiamge file
  185. #define HDIMG_ID_POS 2 // Position to embed ID number
  186. #define HDIMG_LUN_POS 3 // Position to embed LUN numbers
  187. #define HDIMG_BLK_POS 5 // Position to embed block size numbers
  188. #define MAX_FILE_PATH 32 // Maximum file name length
  189. // HDD image
  190. typedef struct hddimg_struct
  191. {
  192. FsFile m_file; // File object
  193. uint64_t m_fileSize; // File size
  194. size_t m_blocksize; // SCSI BLOCK size
  195. }HDDIMG;
  196. HDDIMG img[NUM_SCSIID][NUM_SCSILUN]; // Maximum number
  197. uint8_t m_senseKey = 0; // Sense key
  198. uint16_t m_addition_sense = 0; // Additional sense information
  199. volatile bool m_isBusReset = false; // Bus reset
  200. volatile bool m_resetJmp = false; // Call longjmp on reset
  201. jmp_buf m_resetJmpBuf;
  202. byte scsi_id_mask; // Mask list of responding SCSI IDs
  203. byte m_id; // Currently responding SCSI-ID
  204. byte m_lun; // Logical unit number currently responding
  205. byte m_sts; // Status byte
  206. byte m_msg; // Message bytes
  207. HDDIMG *m_img; // HDD image for current SCSI-ID, LUN
  208. byte m_buf[MAX_BLOCKSIZE]; // General purpose buffer
  209. byte m_msb[256]; // Command storage bytes
  210. /*
  211. * Data byte to BSRR register setting value and parity table
  212. */
  213. // Parity bit generation
  214. #define PTY(V) (1^((V)^((V)>>1)^((V)>>2)^((V)>>3)^((V)>>4)^((V)>>5)^((V)>>6)^((V)>>7))&1)
  215. // Data byte to BSRR register setting value conversion table
  216. // BSRR[31:24] = DB[7:0]
  217. // BSRR[ 16] = PTY(DB)
  218. // BSRR[15: 8] = ~DB[7:0]
  219. // BSRR[ 0] = ~PTY(DB)
  220. // Set DBP, set REQ = inactive
  221. #define DBP(D) ((((((uint32_t)(D)<<8)|PTY(D))*0x00010001)^0x0000ff01)|BITMASK(vREQ))
  222. // BSRR register control value that simultaneously performs DB set, DP set, and REQ = H (inactrive)
  223. uint32_t db_bsrr[256];
  224. // Parity bit acquisition
  225. #define PARITY(DB) (db_bsrr[DB]&1)
  226. // Macro cleaning
  227. #undef DBP32
  228. #undef DBP8
  229. //#undef DBP
  230. //#undef PTY
  231. // Log File
  232. #define VERSION "1.1-SNAPSHOT-20220407"
  233. #define LOG_FILENAME "LOG.txt"
  234. FsFile LOG_FILE;
  235. // SCSI Drive Vendor information
  236. byte SCSI_INFO_BUF[36] = {
  237. 0x00, //device type
  238. 0x00, //RMB = 0
  239. 0x01, //ISO, ECMA, ANSI version
  240. 0x01, //Response data format
  241. 35 - 4, //Additional data length
  242. 0, 0, //Reserve
  243. 0x00, //Support function
  244. 'Q', 'U', 'A', 'N', 'T', 'U', 'M', ' ', // vendor 8
  245. 'F', 'I', 'R', 'E', 'B', 'A', 'L', 'L', '1', ' ', ' ',' ', ' ', ' ', ' ', ' ', // product 16
  246. '1', '.', '0', ' ' // version 4
  247. };
  248. void onFalseInit(void);
  249. void noSDCardFound(void);
  250. void onBusReset(void);
  251. void initFileLog(int);
  252. void finalizeFileLog(void);
  253. void findDriveImages(FsFile root);
  254. /*
  255. * IO read.
  256. */
  257. inline byte readIO(void)
  258. {
  259. // Port input data register
  260. uint32_t ret = GPIOB->regs->IDR;
  261. byte bret = (byte)(~(ret>>8));
  262. #if READ_PARITY_CHECK
  263. if((db_bsrr[bret]^ret)&1)
  264. m_sts |= 0x01; // parity error
  265. #endif
  266. return bret;
  267. }
  268. // If config file exists, read the first three lines and copy the contents.
  269. // File must be well formed or you will get junk in the SCSI Vendor fields.
  270. void readSCSIDeviceConfig() {
  271. FsFile config_file = SD.open("scsi-config.txt", O_RDONLY);
  272. if (!config_file.isOpen()) {
  273. return;
  274. }
  275. char vendor[9];
  276. memset(vendor, 0, sizeof(vendor));
  277. config_file.readBytes(vendor, sizeof(vendor));
  278. LOG_FILE.print("SCSI VENDOR: ");
  279. LOG_FILE.println(vendor);
  280. memcpy(&(SCSI_INFO_BUF[8]), vendor, 8);
  281. char product[17];
  282. memset(product, 0, sizeof(product));
  283. config_file.readBytes(product, sizeof(product));
  284. LOG_FILE.print("SCSI PRODUCT: ");
  285. LOG_FILE.println(product);
  286. memcpy(&(SCSI_INFO_BUF[16]), product, 16);
  287. char version[5];
  288. memset(version, 0, sizeof(version));
  289. config_file.readBytes(version, sizeof(version));
  290. LOG_FILE.print("SCSI VERSION: ");
  291. LOG_FILE.println(version);
  292. memcpy(&(SCSI_INFO_BUF[32]), version, 4);
  293. config_file.close();
  294. }
  295. // read SD information and print to logfile
  296. void readSDCardInfo()
  297. {
  298. cid_t sd_cid;
  299. if(SD.card()->readCID(&sd_cid))
  300. {
  301. LOG_FILE.print("Sd MID:");
  302. LOG_FILE.print(sd_cid.mid, 16);
  303. LOG_FILE.print(" OID:");
  304. LOG_FILE.print(sd_cid.oid[0]);
  305. LOG_FILE.println(sd_cid.oid[1]);
  306. LOG_FILE.print("Sd Name:");
  307. LOG_FILE.print(sd_cid.pnm[0]);
  308. LOG_FILE.print(sd_cid.pnm[1]);
  309. LOG_FILE.print(sd_cid.pnm[2]);
  310. LOG_FILE.print(sd_cid.pnm[3]);
  311. LOG_FILE.println(sd_cid.pnm[4]);
  312. LOG_FILE.print("Sd Date:");
  313. LOG_FILE.print(sd_cid.mdt_month);
  314. LOG_FILE.print("/20"); // CID year is 2000 + high/low
  315. LOG_FILE.print(sd_cid.mdt_year_high);
  316. LOG_FILE.println(sd_cid.mdt_year_low);
  317. LOG_FILE.print("Sd Serial:");
  318. LOG_FILE.println(sd_cid.psn);
  319. LOG_FILE.sync();
  320. }
  321. }
  322. /*
  323. * Open HDD image file
  324. */
  325. bool hddimageOpen(HDDIMG *h, FsFile file,int id,int lun,int blocksize)
  326. {
  327. h->m_fileSize = 0;
  328. h->m_blocksize = blocksize;
  329. h->m_file = file;
  330. if(h->m_file.isOpen())
  331. {
  332. h->m_fileSize = h->m_file.size();
  333. if(h->m_fileSize>0)
  334. {
  335. // check blocksize dummy file
  336. LOG_FILE.print(" / ");
  337. LOG_FILE.print(h->m_fileSize);
  338. LOG_FILE.print("bytes / ");
  339. LOG_FILE.print(h->m_fileSize / 1024);
  340. LOG_FILE.print("KiB / ");
  341. LOG_FILE.print(h->m_fileSize / 1024 / 1024);
  342. LOG_FILE.println("MiB");
  343. return true; // File opened
  344. }
  345. else
  346. {
  347. LOG_FILE.println(" - file is 0 bytes, can not use.");
  348. h->m_file.close();
  349. h->m_fileSize = h->m_blocksize = 0; // no file
  350. }
  351. }
  352. return false;
  353. }
  354. /*
  355. * Initialization.
  356. * Initialize the bus and set the PIN orientation
  357. */
  358. void setup()
  359. {
  360. // PA15 / PB3 / PB4 Cannot be used
  361. // JTAG Because it is used for debugging.
  362. disableDebugPorts();
  363. // Setup BSRR table
  364. for (unsigned i = 0; i <= 255; i++) {
  365. db_bsrr[i] = DBP(i);
  366. }
  367. // Serial initialization
  368. #if DEBUG > 0
  369. Serial.begin(9600);
  370. // If using a USB->TTL monitor instead of USB serial monitor - you can uncomment this.
  371. //while (!Serial);
  372. #endif
  373. // PIN initialization
  374. gpio_mode(LED2, GPIO_OUTPUT_PP);
  375. gpio_mode(LED, GPIO_OUTPUT_OD);
  376. // Image Set Select Init
  377. gpio_mode(IMAGE_SELECT1, GPIO_INPUT_PU);
  378. gpio_mode(IMAGE_SELECT2, GPIO_INPUT_PU);
  379. pinMode(IMAGE_SELECT1, INPUT);
  380. pinMode(IMAGE_SELECT2, INPUT);
  381. int image_file_set = ((digitalRead(IMAGE_SELECT1) == LOW) ? 1 : 0) | ((digitalRead(IMAGE_SELECT2) == LOW) ? 2 : 0);
  382. LED_OFF();
  383. #ifdef XCVR
  384. // Transceiver Pin Initialization
  385. pinMode(TR_TARGET, OUTPUT);
  386. pinMode(TR_INITIATOR, OUTPUT);
  387. pinMode(TR_DBP, OUTPUT);
  388. TRANSCEIVER_IO_SET(vTR_INITIATOR,TR_INPUT);
  389. #endif
  390. //GPIO(SCSI BUS)Initialization
  391. //Port setting register (lower)
  392. // GPIOB->regs->CRL |= 0x000000008; // SET INPUT W/ PUPD on PAB-PB0
  393. //Port setting register (upper)
  394. //GPIOB->regs->CRH = 0x88888888; // SET INPUT W/ PUPD on PB15-PB8
  395. // GPIOB->regs->ODR = 0x0000FF00; // SET PULL-UPs on PB15-PB8
  396. // DB and DP are input modes
  397. SCSI_DB_INPUT()
  398. #ifdef XCVR
  399. TRANSCEIVER_IO_SET(vTR_DBP,TR_INPUT);
  400. // Initiator port
  401. pinMode(ATN, INPUT);
  402. pinMode(BSY, INPUT);
  403. pinMode(ACK, INPUT);
  404. pinMode(RST, INPUT);
  405. pinMode(SEL, INPUT);
  406. TRANSCEIVER_IO_SET(vTR_INITIATOR,TR_INPUT);
  407. // Target port
  408. pinMode(MSG, INPUT);
  409. pinMode(CD, INPUT);
  410. pinMode(REQ, INPUT);
  411. pinMode(IO, INPUT);
  412. TRANSCEIVER_IO_SET(vTR_TARGET,TR_INPUT);
  413. #else
  414. // Input port
  415. gpio_mode(ATN, GPIO_INPUT_PU);
  416. gpio_mode(BSY, GPIO_INPUT_PU);
  417. gpio_mode(ACK, GPIO_INPUT_PU);
  418. gpio_mode(RST, GPIO_INPUT_PU);
  419. gpio_mode(SEL, GPIO_INPUT_PU);
  420. // Output port
  421. gpio_mode(MSG, GPIO_OUTPUT_OD);
  422. gpio_mode(CD, GPIO_OUTPUT_OD);
  423. gpio_mode(REQ, GPIO_OUTPUT_OD);
  424. gpio_mode(IO, GPIO_OUTPUT_OD);
  425. // Turn off the output port
  426. SCSI_TARGET_INACTIVE()
  427. #endif
  428. //Occurs when the RST pin state changes from HIGH to LOW
  429. //attachInterrupt(RST, onBusReset, FALLING);
  430. // Try different clock speeds till we find one that is stable.
  431. LED_ON();
  432. int mhz = 50;
  433. bool sd_ready = false;
  434. while (mhz >= 32 && !sd_ready) {
  435. if(SD.begin(SdSpiConfig(PA4, DEDICATED_SPI, SD_SCK_MHZ(mhz), &SPI))) {
  436. sd_ready = true;
  437. }
  438. else {
  439. mhz--;
  440. }
  441. }
  442. LED_OFF();
  443. if(!sd_ready) {
  444. #if DEBUG > 0
  445. Serial.println("SD initialization failed!");
  446. #endif
  447. noSDCardFound();
  448. }
  449. initFileLog(mhz);
  450. readSCSIDeviceConfig();
  451. readSDCardInfo();
  452. //HD image file open
  453. scsi_id_mask = 0x00;
  454. // Iterate over the root path in the SD card looking for candidate image files.
  455. FsFile root;
  456. char image_set_dir_name[] = "/ImageSetX/";
  457. image_set_dir_name[9] = char(image_file_set) + 0x30;
  458. root.open(image_set_dir_name);
  459. if (root.isDirectory()) {
  460. LOG_FILE.print("Looking for images in: ");
  461. LOG_FILE.println(image_set_dir_name);
  462. LOG_FILE.sync();
  463. } else {
  464. root.close();
  465. root.open("/");
  466. }
  467. findDriveImages(root);
  468. root.close();
  469. FsFile images_all_dir;
  470. images_all_dir.open("/ImageSetAll/");
  471. if (images_all_dir.isDirectory()) {
  472. LOG_FILE.println("Looking for images in: /ImageSetAll/");
  473. LOG_FILE.sync();
  474. findDriveImages(images_all_dir);
  475. }
  476. images_all_dir.close();
  477. // Error if there are 0 image files
  478. if(scsi_id_mask==0) {
  479. LOG_FILE.println("ERROR: No valid images found!");
  480. onFalseInit();
  481. }
  482. finalizeFileLog();
  483. LED_OFF();
  484. //Occurs when the RST pin state changes from HIGH to LOW
  485. attachInterrupt(RST, onBusReset, FALLING);
  486. }
  487. void findDriveImages(FsFile root) {
  488. bool image_ready;
  489. FsFile file;
  490. char path_name[MAX_FILE_PATH+1];
  491. root.getName(path_name, sizeof(path_name));
  492. SD.chdir(path_name);
  493. while (1) {
  494. // Directories can not be opened RDWR, so it will fail, but fails the same way with no file/dir, so we need to peek at the file first.
  495. FsFile file_test = root.openNextFile(O_RDONLY);
  496. char name[MAX_FILE_PATH+1];
  497. file_test.getName(name, MAX_FILE_PATH+1);
  498. String file_name = String(name);
  499. // Skip directories and already open files.
  500. if(file_test.isDir() || file_name.startsWith("LOG.txt")) {
  501. file_test.close();
  502. continue;
  503. }
  504. // If error there is no next file to open.
  505. if(file_test.getError() > 0) {
  506. file_test.close();
  507. break;
  508. }
  509. // Valid file, open for reading/writing.
  510. file = SD.open(name, O_RDWR);
  511. if(file && file.isFile()) {
  512. file_name.toLowerCase();
  513. if(file_name.startsWith("hd")) {
  514. // Defaults for Hard Disks
  515. int id = 1; // 0 and 3 are common in Macs for physical HD and CD, so avoid them.
  516. int lun = 0;
  517. int blk = 512;
  518. // Positionally read in and coerase the chars to integers.
  519. // We only require the minimum and read in the next if provided.
  520. int file_name_length = file_name.length();
  521. if(file_name_length > 2) { // HD[N]
  522. int tmp_id = name[HDIMG_ID_POS] - '0';
  523. // If valid id, set it, else use default
  524. if(tmp_id > -1 && tmp_id < 8) {
  525. id = tmp_id;
  526. } else {
  527. LOG_FILE.print(name);
  528. LOG_FILE.println(" - bad SCSI id in filename, Using default ID 1");
  529. }
  530. }
  531. if(file_name_length > 3) { // HDN[N]
  532. int tmp_lun = name[HDIMG_LUN_POS] - '0';
  533. // If valid lun, set it, else use default
  534. if(tmp_lun == 0 || tmp_lun == 1) {
  535. lun = tmp_lun;
  536. } else {
  537. LOG_FILE.print(name);
  538. LOG_FILE.println(" - bad SCSI LUN in filename, Using default LUN ID 0");
  539. }
  540. }
  541. int blk1 = 0, blk2, blk3, blk4 = 0;
  542. if(file_name_length > 8) { // HD00_[111]
  543. blk1 = name[HDIMG_BLK_POS] - '0';
  544. blk2 = name[HDIMG_BLK_POS+1] - '0';
  545. blk3 = name[HDIMG_BLK_POS+2] - '0';
  546. if(file_name_length > 9) // HD00_NNN[1]
  547. blk4 = name[HDIMG_BLK_POS+3] - '0';
  548. }
  549. if(blk1 == 2 && blk2 == 5 && blk3 == 6) {
  550. blk = 256;
  551. } else if(blk1 == 1 && blk2 == 0 && blk3 == 2 && blk4 == 4) {
  552. blk = 1024;
  553. } else if(blk1 == 2 && blk2 == 0 && blk3 == 4 && blk4 == 8) {
  554. blk = 2048;
  555. }
  556. if(id < NUM_SCSIID && lun < NUM_SCSILUN) {
  557. HDDIMG *h = &img[id][lun];
  558. LOG_FILE.print(" - ");
  559. LOG_FILE.print(name);
  560. image_ready = hddimageOpen(h, file, id, lun, blk);
  561. if(image_ready) { // Marked as a responsive ID
  562. scsi_id_mask |= 1<<id;
  563. }
  564. }
  565. }
  566. } else {
  567. file.close();
  568. LOG_FILE.print("Not an image: ");
  569. LOG_FILE.println(name);
  570. }
  571. LOG_FILE.sync();
  572. }
  573. // cd .. before going back.
  574. SD.chdir("/");
  575. }
  576. /*
  577. * Setup initialization logfile
  578. */
  579. void initFileLog(int success_mhz) {
  580. LOG_FILE = SD.open(LOG_FILENAME, O_WRONLY | O_CREAT | O_TRUNC);
  581. LOG_FILE.println("BlueSCSI <-> SD - https://github.com/erichelgeson/BlueSCSI");
  582. LOG_FILE.print("VERSION: ");
  583. LOG_FILE.print(VERSION);
  584. LOG_FILE.println(BUILD_TAGS);
  585. LOG_FILE.print("DEBUG:");
  586. LOG_FILE.print(DEBUG);
  587. LOG_FILE.print(" SDFAT_FILE_TYPE:");
  588. LOG_FILE.println(SDFAT_FILE_TYPE);
  589. LOG_FILE.print("SdFat version: ");
  590. LOG_FILE.println(SD_FAT_VERSION_STR);
  591. LOG_FILE.print("SPI speed: ");
  592. LOG_FILE.print(success_mhz);
  593. LOG_FILE.println("Mhz");
  594. if(success_mhz < 40) {
  595. LOG_FILE.println("SPI under 40Mhz - read https://github.com/erichelgeson/BlueSCSI/wiki/Slow-SPI");
  596. }
  597. LOG_FILE.print("SdFat Max FileName Length: ");
  598. LOG_FILE.println(MAX_FILE_PATH);
  599. LOG_FILE.println("Initialized SD Card - lets go!");
  600. LOG_FILE.sync();
  601. }
  602. /*
  603. * Finalize initialization logfile
  604. */
  605. void finalizeFileLog() {
  606. // View support drive map
  607. LOG_FILE.print("ID");
  608. for(int lun=0;lun<NUM_SCSILUN;lun++)
  609. {
  610. LOG_FILE.print(":LUN");
  611. LOG_FILE.print(lun);
  612. }
  613. LOG_FILE.println(":");
  614. //
  615. for(int id=0;id<NUM_SCSIID;id++)
  616. {
  617. LOG_FILE.print(" ");
  618. LOG_FILE.print(id);
  619. for(int lun=0;lun<NUM_SCSILUN;lun++)
  620. {
  621. HDDIMG *h = &img[id][lun];
  622. if( (lun<NUM_SCSILUN) && (h->m_file))
  623. {
  624. LOG_FILE.print((h->m_blocksize<1000) ? ": " : ":");
  625. LOG_FILE.print(h->m_blocksize);
  626. }
  627. else
  628. LOG_FILE.print(":----");
  629. }
  630. LOG_FILE.println(":");
  631. }
  632. LOG_FILE.println("Finished initialization of SCSI Devices - Entering main loop.");
  633. LOG_FILE.sync();
  634. LOG_FILE.close();
  635. }
  636. /*
  637. * Initialization failed, blink 3x fast
  638. */
  639. void onFalseInit(void)
  640. {
  641. LOG_FILE.sync();
  642. while(true) {
  643. for(int i = 0; i < 3; i++) {
  644. LED_ON();
  645. delay(250);
  646. LED_OFF();
  647. delay(250);
  648. }
  649. delay(3000);
  650. }
  651. }
  652. /*
  653. * No SC Card found, blink 5x fast
  654. */
  655. void noSDCardFound(void)
  656. {
  657. while(true) {
  658. for(int i = 0; i < 5; i++) {
  659. LED_ON();
  660. delay(250);
  661. LED_OFF();
  662. delay(250);
  663. }
  664. delay(3000);
  665. }
  666. }
  667. /*
  668. * Return from exception and call longjmp
  669. */
  670. void __attribute__ ((noinline)) longjmpFromInterrupt(jmp_buf jmpb, int retval) __attribute__ ((noreturn));
  671. void longjmpFromInterrupt(jmp_buf jmpb, int retval) {
  672. // Address of longjmp with the thumb bit cleared
  673. const uint32_t longjmpaddr = ((uint32_t)longjmp) & 0xfffffffe;
  674. const uint32_t zero = 0;
  675. // Default PSR value, function calls don't require any particular value
  676. const uint32_t PSR = 0x01000000;
  677. // For documentation on what this is doing, see:
  678. // https://developer.arm.com/documentation/dui0552/a/the-cortex-m3-processor/exception-model/exception-entry-and-return
  679. // Stack frame needs to have R0-R3, R12, LR, PC, PSR (from bottom to top)
  680. // This is being set up to have R0 and R1 contain the parameters passed to longjmp, and PC is the address of the longjmp function.
  681. // This is using existing stack space, rather than allocating more, as longjmp is just going to unroll the stack even further.
  682. // 0xfffffff9 is the EXC_RETURN value to return to thread mode.
  683. asm (
  684. "str %0, [sp];\
  685. str %1, [sp, #4];\
  686. str %2, [sp, #8];\
  687. str %2, [sp, #12];\
  688. str %2, [sp, #16];\
  689. str %2, [sp, #20];\
  690. str %3, [sp, #24];\
  691. str %4, [sp, #28];\
  692. ldr lr, =0xfffffff9;\
  693. bx lr"
  694. :: "r"(jmpb),"r"(retval),"r"(zero), "r"(longjmpaddr), "r"(PSR)
  695. );
  696. }
  697. /*
  698. * Bus reset interrupt.
  699. */
  700. void onBusReset(void)
  701. {
  702. if(isHigh(gpio_read(RST))) {
  703. delayMicroseconds(20);
  704. if(isHigh(gpio_read(RST))) {
  705. // BUS FREE is done in the main process
  706. // gpio_mode(MSG, GPIO_OUTPUT_OD);
  707. // gpio_mode(CD, GPIO_OUTPUT_OD);
  708. // gpio_mode(REQ, GPIO_OUTPUT_OD);
  709. // gpio_mode(IO, GPIO_OUTPUT_OD);
  710. // Should I enter DB and DBP once?
  711. SCSI_DB_INPUT()
  712. LOGN("BusReset!");
  713. if (m_resetJmp) {
  714. m_resetJmp = false;
  715. // Jumping out of the interrupt handler, so need to clear the interupt source.
  716. uint8 exti = PIN_MAP[RST].gpio_bit;
  717. EXTI_BASE->PR = (1U << exti);
  718. longjmpFromInterrupt(m_resetJmpBuf, 1);
  719. } else {
  720. m_isBusReset = true;
  721. }
  722. }
  723. }
  724. }
  725. /*
  726. * Enable the reset longjmp, and check if reset fired while it was disabled.
  727. */
  728. void enableResetJmp(void) {
  729. m_resetJmp = true;
  730. if (m_isBusReset) {
  731. longjmp(m_resetJmpBuf, 1);
  732. }
  733. }
  734. /*
  735. * Read by handshake.
  736. */
  737. inline byte readHandshake(void)
  738. {
  739. SCSI_OUT(vREQ,active)
  740. //SCSI_DB_INPUT()
  741. while( ! SCSI_IN(vACK));
  742. byte r = readIO();
  743. SCSI_OUT(vREQ,inactive)
  744. while( SCSI_IN(vACK));
  745. return r;
  746. }
  747. /*
  748. * Write with a handshake.
  749. */
  750. inline void writeHandshake(byte d)
  751. {
  752. // This has a 400ns bus settle delay built in. Not optimal for multi-byte transfers.
  753. GPIOB->regs->BSRR = db_bsrr[d]; // setup DB,DBP (160ns)
  754. #ifdef XCVR
  755. TRANSCEIVER_IO_SET(vTR_DBP,TR_OUTPUT)
  756. #endif
  757. SCSI_DB_OUTPUT() // (180ns)
  758. // ACK.Fall to DB output delay 100ns(MAX) (DTC-510B)
  759. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  760. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  761. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  762. SCSI_OUT(vREQ,active) // (30ns)
  763. //while(!SCSI_IN(vACK)) { if(m_isBusReset){ SCSI_DB_INPUT() return; }}
  764. while(!SCSI_IN(vACK));
  765. // ACK.Fall to REQ.Raise delay 500ns(typ.) (DTC-510B)
  766. GPIOB->regs->BSRR = DBP(0xff); // DB=0xFF , SCSI_OUT(vREQ,inactive)
  767. // REQ.Raise to DB hold time 0ns
  768. SCSI_DB_INPUT() // (150ns)
  769. #ifdef XCVR
  770. TRANSCEIVER_IO_SET(vTR_DBP,TR_INPUT)
  771. #endif
  772. while( SCSI_IN(vACK));
  773. }
  774. #pragma GCC push_options
  775. #pragma GCC optimize ("-Os")
  776. /*
  777. * This loop is tuned to repeat the following pattern:
  778. * 1) Set REQ
  779. * 2) 5 cycles of work/delay
  780. * 3) Wait for ACK
  781. * Cycle time tunings are for 72MHz STM32F103
  782. * Alignment matters. For the 3 instruction wait loops,it looks like crossing
  783. * an 8 byte prefetch buffer can add 2 cycles of wait every branch taken.
  784. */
  785. void writeDataLoop(uint32_t blocksize, const byte* srcptr) __attribute__ ((aligned(8)));
  786. void writeDataLoop(uint32_t blocksize, const byte* srcptr)
  787. {
  788. #define REQ_ON() (port_b->BRR = req_bit);
  789. #define FETCH_BSRR_DB() (bsrr_val = bsrr_tbl[*srcptr++])
  790. #define REQ_OFF_DB_SET(BSRR_VAL) port_b->BSRR = BSRR_VAL;
  791. #define WAIT_ACK_ACTIVE() while((*port_a_idr>>(vACK&15)&1))
  792. #define WAIT_ACK_INACTIVE() while(!(*port_a_idr>>(vACK&15)&1))
  793. register const byte *endptr= srcptr + blocksize; // End pointer
  794. register const uint32_t *bsrr_tbl = db_bsrr; // Table to convert to BSRR
  795. register uint32_t bsrr_val; // BSRR value to output (DB, DBP, REQ = ACTIVE)
  796. register uint32_t req_bit = BITMASK(vREQ);
  797. register gpio_reg_map *port_b = PBREG;
  798. register volatile uint32_t *port_a_idr = &(GPIOA->regs->IDR);
  799. // Start the first bus cycle.
  800. FETCH_BSRR_DB();
  801. REQ_OFF_DB_SET(bsrr_val);
  802. #ifdef XCVR
  803. TRANSCEIVER_IO_SET(vTR_DBP,TR_OUTPUT)
  804. #endif
  805. REQ_ON();
  806. FETCH_BSRR_DB();
  807. WAIT_ACK_ACTIVE();
  808. REQ_OFF_DB_SET(bsrr_val);
  809. // Align the starts of the do/while and WAIT loops to an 8 byte prefetch.
  810. asm("nop.w;nop");
  811. do{
  812. WAIT_ACK_INACTIVE();
  813. REQ_ON();
  814. // 4 cycles of work
  815. FETCH_BSRR_DB();
  816. // Extra 1 cycle delay while keeping the loop within an 8 byte prefetch.
  817. asm("nop");
  818. WAIT_ACK_ACTIVE();
  819. REQ_OFF_DB_SET(bsrr_val);
  820. // Extra 1 cycle delay, plus 4 cycles for the branch taken with prefetch.
  821. asm("nop");
  822. }while(srcptr < endptr);
  823. WAIT_ACK_INACTIVE();
  824. // Finish the last bus cycle, byte is already on DB.
  825. REQ_ON();
  826. WAIT_ACK_ACTIVE();
  827. REQ_OFF_DB_SET(bsrr_val);
  828. WAIT_ACK_INACTIVE();
  829. }
  830. #pragma GCC pop_options
  831. /*
  832. * Data in phase.
  833. * Send len bytes of data array p.
  834. */
  835. void writeDataPhase(int len, const byte* p)
  836. {
  837. LOGN("DATAIN PHASE");
  838. SCSI_PHASE_CHANGE(SCSI_PHASE_DATAIN);
  839. // Bus settle delay 400ns. Following code was measured at 800ns before REQ asserted. STM32F103.
  840. SCSI_DB_OUTPUT()
  841. writeDataLoop(len, p);
  842. SCSI_DB_INPUT()
  843. }
  844. /*
  845. * Data in phase.
  846. * Send len block while reading from SD card.
  847. */
  848. void writeDataPhaseSD(uint32_t adds, uint32_t len)
  849. {
  850. LOGN("DATAIN PHASE(SD)");
  851. SCSI_PHASE_CHANGE(SCSI_PHASE_DATAIN);
  852. //Bus settle delay 400ns, file.seek() measured at over 1000ns.
  853. uint64_t pos = (uint64_t)adds * m_img->m_blocksize;
  854. m_img->m_file.seekSet(pos);
  855. SCSI_DB_OUTPUT()
  856. for(uint32_t i = 0; i < len; i++) {
  857. // Asynchronous reads will make it faster ...
  858. m_resetJmp = false;
  859. m_img->m_file.read(m_buf, m_img->m_blocksize);
  860. enableResetJmp();
  861. writeDataLoop(m_img->m_blocksize, m_buf);
  862. }
  863. SCSI_DB_INPUT()
  864. #ifdef XCVR
  865. TRANSCEIVER_IO_SET(vTR_DBP,TR_INPUT)
  866. #endif
  867. }
  868. #pragma GCC push_options
  869. #pragma GCC optimize ("-Os")
  870. /*
  871. * See writeDataLoop for optimization info.
  872. */
  873. void readDataLoop(uint32_t blockSize, byte* dstptr) __attribute__ ((aligned(16)));
  874. void readDataLoop(uint32_t blockSize, byte* dstptr)
  875. {
  876. register byte *endptr= dstptr + blockSize - 1;
  877. #define REQ_ON() (port_b->BRR = req_bit);
  878. #define REQ_OFF() (port_b->BSRR = req_bit);
  879. #define WAIT_ACK_ACTIVE() while((*port_a_idr>>(vACK&15)&1))
  880. #define WAIT_ACK_INACTIVE() while(!(*port_a_idr>>(vACK&15)&1))
  881. register uint32_t req_bit = BITMASK(vREQ);
  882. register gpio_reg_map *port_b = PBREG;
  883. register volatile uint32_t *port_a_idr = &(GPIOA->regs->IDR);
  884. REQ_ON();
  885. // Fastest alignment obtained by trial and error.
  886. // Wait loop is within an 8 byte prefetch buffer.
  887. asm("nop");
  888. do {
  889. WAIT_ACK_ACTIVE();
  890. uint32_t ret = port_b->IDR;
  891. REQ_OFF();
  892. *dstptr++ = ~(ret >> 8);
  893. // Move wait loop in to a single 8 byte prefetch buffer
  894. asm("nop;nop;nop");
  895. WAIT_ACK_INACTIVE();
  896. REQ_ON();
  897. // Extra 1 cycle delay
  898. asm("nop");
  899. } while(dstptr<endptr);
  900. WAIT_ACK_ACTIVE();
  901. uint32_t ret = GPIOB->regs->IDR;
  902. REQ_OFF();
  903. *dstptr = ~(ret >> 8);
  904. WAIT_ACK_INACTIVE();
  905. }
  906. #pragma GCC pop_options
  907. /*
  908. * Data out phase.
  909. * len block read
  910. */
  911. void readDataPhase(int len, byte* p)
  912. {
  913. LOGN("DATAOUT PHASE");
  914. SCSI_PHASE_CHANGE(SCSI_PHASE_DATAOUT);
  915. // Bus settle delay 400ns. The following code was measured at 450ns before REQ asserted. STM32F103.
  916. readDataLoop(len, p);
  917. }
  918. /*
  919. * Data out phase.
  920. * Write to SD card while reading len block.
  921. */
  922. void readDataPhaseSD(uint32_t adds, uint32_t len)
  923. {
  924. LOGN("DATAOUT PHASE(SD)");
  925. SCSI_PHASE_CHANGE(SCSI_PHASE_DATAOUT);
  926. //Bus settle delay 400ns, file.seek() measured at over 1000ns.
  927. uint64_t pos = (uint64_t)adds * m_img->m_blocksize;
  928. m_img->m_file.seekSet(pos);
  929. for(uint32_t i = 0; i < len; i++) {
  930. m_resetJmp = true;
  931. readDataLoop(m_img->m_blocksize, m_buf);
  932. m_resetJmp = false;
  933. m_img->m_file.write(m_buf, m_img->m_blocksize);
  934. // If a reset happened while writing, break and let the flush happen before it is handled.
  935. if (m_isBusReset) {
  936. break;
  937. }
  938. }
  939. m_img->m_file.flush();
  940. enableResetJmp();
  941. }
  942. /*
  943. * Data out phase.
  944. * Compare to SD card while reading len block.
  945. */
  946. void verifyDataPhaseSD(uint32_t adds, uint32_t len)
  947. {
  948. LOGN("DATAOUT PHASE(SD)");
  949. SCSI_PHASE_CHANGE(SCSI_PHASE_DATAOUT);
  950. //Bus settle delay 400ns, file.seek() measured at over 1000ns.
  951. uint64_t pos = (uint64_t)adds * m_img->m_blocksize;
  952. m_img->m_file.seekSet(pos);
  953. for(uint32_t i = 0; i < len; i++) {
  954. readDataLoop(m_img->m_blocksize, m_buf);
  955. // This has just gone through the transfer to make things work, a compare would go here.
  956. }
  957. }
  958. /*
  959. * INQUIRY command processing.
  960. */
  961. byte onInquiryCommand(byte len)
  962. {
  963. writeDataPhase(len < 36 ? len : 36, SCSI_INFO_BUF);
  964. return SCSI_STATUS_GOOD;
  965. }
  966. /*
  967. * REQUEST SENSE command processing.
  968. */
  969. void onRequestSenseCommand(byte len)
  970. {
  971. byte buf[18] = {
  972. 0x70, //CheckCondition
  973. 0, //Segment number
  974. m_senseKey, //Sense key
  975. 0, 0, 0, 0, //information
  976. 10, //Additional data length
  977. 0, 0, 0, 0, // command specific information bytes
  978. (byte)(m_addition_sense >> 8),
  979. (byte)m_addition_sense,
  980. 0, 0, 0, 0,
  981. };
  982. m_senseKey = 0;
  983. m_addition_sense = 0;
  984. writeDataPhase(len < 18 ? len : 18, buf);
  985. }
  986. /*
  987. * READ CAPACITY command processing.
  988. */
  989. byte onReadCapacityCommand(byte pmi)
  990. {
  991. if(!m_img) {
  992. m_senseKey = SCSI_SENSE_NOT_READY;
  993. m_addition_sense = SCSI_ASC_LUN_NOT_READY_MANUAL_INTERVENTION_REQUIRED;
  994. return SCSI_STATUS_CHECK_CONDITION;
  995. }
  996. uint32_t bl = m_img->m_blocksize;
  997. uint32_t bc = m_img->m_fileSize / bl - 1; // Points to last LBA
  998. uint8_t buf[8] = {
  999. bc >> 24, bc >> 16, bc >> 8, bc,
  1000. bl >> 24, bl >> 16, bl >> 8, bl
  1001. };
  1002. writeDataPhase(8, buf);
  1003. return SCSI_STATUS_GOOD;
  1004. }
  1005. /*
  1006. * Check that the image file is present and the block range is valid.
  1007. */
  1008. byte checkBlockCommand(uint32_t adds, uint32_t len)
  1009. {
  1010. // Check that image file is present
  1011. if(!m_img) {
  1012. m_senseKey = SCSI_SENSE_NOT_READY;
  1013. m_addition_sense = SCSI_ASC_LUN_NOT_READY_MANUAL_INTERVENTION_REQUIRED;
  1014. return SCSI_STATUS_CHECK_CONDITION;
  1015. }
  1016. // Check block range is valid
  1017. uint32_t bc = m_img->m_fileSize / m_img->m_blocksize;
  1018. if (adds >= bc || (adds + len) > bc) {
  1019. m_senseKey = SCSI_SENSE_ILLEGAL_REQUEST;
  1020. m_addition_sense = SCSI_ASC_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1021. return SCSI_STATUS_CHECK_CONDITION;
  1022. }
  1023. return SCSI_STATUS_GOOD;
  1024. }
  1025. /*
  1026. * READ6 / 10 Command processing.
  1027. */
  1028. byte onReadCommand(uint32_t adds, uint32_t len)
  1029. {
  1030. LOGN("-R");
  1031. LOGHEXN(adds);
  1032. LOGHEXN(len);
  1033. byte sts = checkBlockCommand(adds, len);
  1034. if (sts) {
  1035. return sts;
  1036. }
  1037. LED_ON();
  1038. writeDataPhaseSD(adds, len);
  1039. LED_OFF();
  1040. return SCSI_STATUS_GOOD;
  1041. }
  1042. /*
  1043. * WRITE6 / 10 Command processing.
  1044. */
  1045. byte onWriteCommand(uint32_t adds, uint32_t len)
  1046. {
  1047. LOGN("-W");
  1048. LOGHEXN(adds);
  1049. LOGHEXN(len);
  1050. byte sts = checkBlockCommand(adds, len);
  1051. if (sts) {
  1052. return sts;
  1053. }
  1054. LED_ON();
  1055. readDataPhaseSD(adds, len);
  1056. LED_OFF();
  1057. return SCSI_STATUS_GOOD;
  1058. }
  1059. /*
  1060. * VERIFY10 Command processing.
  1061. */
  1062. byte onVerifyCommand(byte flags, uint32_t adds, uint32_t len)
  1063. {
  1064. byte sts = checkBlockCommand(adds, len);
  1065. if (sts) {
  1066. return sts;
  1067. }
  1068. int bytchk = (flags >> 1) & 0x03;
  1069. if (bytchk != 0) {
  1070. if (bytchk == 3) {
  1071. // Data-Out buffer is single logical block for repeated verification.
  1072. len = m_img->m_blocksize;
  1073. }
  1074. LED_ON();
  1075. verifyDataPhaseSD(adds, len);
  1076. LED_OFF();
  1077. }
  1078. return SCSI_STATUS_GOOD;
  1079. }
  1080. /*
  1081. * MODE SENSE command processing.
  1082. */
  1083. byte onModeSenseCommand(byte scsi_cmd, byte dbd, byte cmd2, uint32_t len)
  1084. {
  1085. if(!m_img) {
  1086. m_senseKey = SCSI_SENSE_NOT_READY;
  1087. m_addition_sense = SCSI_ASC_LUN_NOT_READY_MANUAL_INTERVENTION_REQUIRED;
  1088. return SCSI_STATUS_CHECK_CONDITION;
  1089. }
  1090. uint32_t bl = m_img->m_blocksize;
  1091. uint32_t bc = m_img->m_fileSize / bl;
  1092. memset(m_buf, 0, sizeof(m_buf));
  1093. int pageCode = cmd2 & 0x3F;
  1094. int pageControl = cmd2 >> 6;
  1095. int a = 4;
  1096. if(scsi_cmd == 0x5A) a = 8;
  1097. if(dbd == 0) {
  1098. byte c[8] = {
  1099. 0,//Density code
  1100. bc >> 16, bc >> 8, bc,
  1101. 0, //Reserve
  1102. bl >> 16, bl >> 8, bl
  1103. };
  1104. memcpy(&m_buf[a], c, 8);
  1105. a += 8;
  1106. }
  1107. switch(pageCode) {
  1108. case 0x3F:
  1109. case 0x01: // Read/Write Error Recovery
  1110. m_buf[a + 0] = 0x01;
  1111. m_buf[a + 1] = 0x0A;
  1112. a += 0x0C;
  1113. if(pageCode != 0x3F) break;
  1114. case 0x02: // Disconnect-Reconnect page
  1115. m_buf[a + 0] = 0x02;
  1116. m_buf[a + 1] = 0x0A;
  1117. a += 0x0C;
  1118. if(pageCode != 0x3f) break;
  1119. case 0x03: //Drive parameters
  1120. m_buf[a + 0] = 0x03; //Page code
  1121. m_buf[a + 1] = 0x16; // Page length
  1122. if(pageControl != 1) {
  1123. m_buf[a + 11] = 0x3F;//Number of sectors / track
  1124. m_buf[a + 12] = (byte)(m_img->m_blocksize >> 8);
  1125. m_buf[a + 13] = (byte)m_img->m_blocksize;
  1126. m_buf[a + 15] = 0x1; // Interleave
  1127. }
  1128. a += 0x18;
  1129. if(pageCode != 0x3F) break;
  1130. case 0x04: //Drive parameters
  1131. m_buf[a + 0] = 0x04; //Page code
  1132. m_buf[a + 1] = 0x16; // Page length
  1133. if(pageControl != 1) {
  1134. unsigned cylinders = bc / (16 * 63);
  1135. m_buf[a + 2] = (byte)(cylinders >> 16); // Cylinders
  1136. m_buf[a + 3] = (byte)(cylinders >> 8);
  1137. m_buf[a + 4] = (byte)cylinders;
  1138. m_buf[a + 5] = 16; //Number of heads
  1139. }
  1140. a += 0x18;
  1141. if(pageCode != 0x3F) break;
  1142. case 0x30:
  1143. {
  1144. const byte page30[0x14] = {0x41, 0x50, 0x50, 0x4C, 0x45, 0x20, 0x43, 0x4F, 0x4D, 0x50, 0x55, 0x54, 0x45, 0x52, 0x2C, 0x20, 0x49, 0x4E, 0x43, 0x20};
  1145. m_buf[a + 0] = 0x30; // Page code
  1146. m_buf[a + 1] = sizeof(page30); // Page length
  1147. if(pageControl != 1) {
  1148. memcpy(&m_buf[a + 2], page30, sizeof(page30));
  1149. }
  1150. a += 2 + sizeof(page30);
  1151. if(pageCode != 0x3F) break;
  1152. }
  1153. break; // Don't want 0x3F falling through to error condition
  1154. default:
  1155. m_senseKey = SCSI_SENSE_ILLEGAL_REQUEST;
  1156. m_addition_sense = SCSI_ASC_INVALID_FIELD_IN_CDB;
  1157. return SCSI_STATUS_CHECK_CONDITION;
  1158. break;
  1159. }
  1160. if(scsi_cmd == SCSI_MODE_SENSE10)
  1161. {
  1162. m_buf[1] = a - 2;
  1163. m_buf[7] = 0x08;
  1164. }
  1165. else
  1166. {
  1167. m_buf[0] = a - 1;
  1168. m_buf[3] = 0x08;
  1169. }
  1170. writeDataPhase(len < a ? len : a, m_buf);
  1171. return SCSI_STATUS_GOOD;
  1172. }
  1173. byte onModeSelectCommand(byte scsi_cmd, byte flags, uint32_t len)
  1174. {
  1175. if (len > MAX_BLOCKSIZE) {
  1176. m_senseKey = SCSI_SENSE_ILLEGAL_REQUEST;
  1177. m_addition_sense = SCSI_ASC_INVALID_FIELD_IN_CDB;
  1178. return SCSI_STATUS_CHECK_CONDITION;
  1179. }
  1180. readDataPhase(len, m_buf);
  1181. //Apple HD SC Setup sends:
  1182. //0 0 0 8 0 0 0 0 0 0 2 0 0 2 10 0 1 6 24 10 8 0 0 0
  1183. //I believe mode page 0 set to 10 00 is Disable Unit Attention
  1184. //Mode page 1 set to 24 10 08 00 00 00 is TB and PER set, read retry count 16, correction span 8
  1185. for (unsigned i = 0; i < len; i++) {
  1186. LOGHEX(m_buf[i]);LOG(" ");
  1187. }
  1188. LOGN("");
  1189. return SCSI_STATUS_GOOD;
  1190. }
  1191. /*
  1192. * Test Unit Ready command processing.
  1193. */
  1194. byte onTestUnitReady()
  1195. {
  1196. // Check that image file is present
  1197. if(!m_img) {
  1198. m_senseKey = SCSI_SENSE_NOT_READY;
  1199. m_addition_sense = SCSI_ASC_MEDIUM_NOT_PRESENT;
  1200. return SCSI_STATUS_CHECK_CONDITION;
  1201. }
  1202. return SCSI_STATUS_GOOD;
  1203. }
  1204. /*
  1205. * MsgIn2.
  1206. */
  1207. void MsgIn2(int msg)
  1208. {
  1209. LOGN("MsgIn2");
  1210. SCSI_PHASE_CHANGE(SCSI_PHASE_MESSAGEIN);
  1211. // Bus settle delay 400ns built in to writeHandshake
  1212. writeHandshake(msg);
  1213. }
  1214. /*
  1215. * Main loop.
  1216. */
  1217. void loop()
  1218. {
  1219. #ifdef XCVR
  1220. // Reset all DB and Target pins, switch transceivers to input
  1221. // Precaution against bugs or jumps which don't clean up properly
  1222. SCSI_DB_INPUT();
  1223. TRANSCEIVER_IO_SET(vTR_DBP,TR_INPUT)
  1224. SCSI_TARGET_INACTIVE();
  1225. TRANSCEIVER_IO_SET(vTR_INITIATOR,TR_INPUT)
  1226. #endif
  1227. //int msg = 0;
  1228. m_msg = 0;
  1229. // Wait until RST = H, BSY = H, SEL = L
  1230. do {} while( SCSI_IN(vBSY) || !SCSI_IN(vSEL) || SCSI_IN(vRST));
  1231. // BSY+ SEL-
  1232. // If the ID to respond is not driven, wait for the next
  1233. //byte db = readIO();
  1234. //byte scsiid = db & scsi_id_mask;
  1235. byte scsiid = readIO() & scsi_id_mask;
  1236. if((scsiid) == 0) {
  1237. delayMicroseconds(1);
  1238. return;
  1239. }
  1240. LOGN("Selection");
  1241. m_isBusReset = false;
  1242. if (setjmp(m_resetJmpBuf) == 1) {
  1243. LOGN("Reset, going to BusFree");
  1244. goto BusFree;
  1245. }
  1246. enableResetJmp();
  1247. // Set BSY to-when selected
  1248. SCSI_BSY_ACTIVE(); // Turn only BSY output ON, ACTIVE
  1249. // Ask for a TARGET-ID to respond
  1250. m_id = 31 - __builtin_clz(scsiid);
  1251. // Wait until SEL becomes inactive
  1252. while(isHigh(gpio_read(SEL)) && isLow(gpio_read(BSY))) {
  1253. }
  1254. #ifdef XCVR
  1255. // Reconfigure target pins to output mode, after resetting their values
  1256. GPIOB->regs->BSRR = 0x000000E8; // MSG, CD, REQ, IO
  1257. // GPIOA->regs->BSRR = 0x00000200; // BSY
  1258. #endif
  1259. SCSI_TARGET_ACTIVE() // (BSY), REQ, MSG, CD, IO output turned on
  1260. //
  1261. if(isHigh(gpio_read(ATN))) {
  1262. SCSI_PHASE_CHANGE(SCSI_PHASE_MESSAGEOUT);
  1263. // Bus settle delay 400ns. Following code was measured at 350ns before REQ asserted. Added another 50ns. STM32F103.
  1264. SCSI_PHASE_CHANGE(SCSI_PHASE_MESSAGEOUT);// 28ns delay STM32F103
  1265. SCSI_PHASE_CHANGE(SCSI_PHASE_MESSAGEOUT);// 28ns delay STM32F103
  1266. bool syncenable = false;
  1267. int syncperiod = 50;
  1268. int syncoffset = 0;
  1269. int msc = 0;
  1270. while(isHigh(gpio_read(ATN)) && msc < 255) {
  1271. m_msb[msc++] = readHandshake();
  1272. }
  1273. for(int i = 0; i < msc; i++) {
  1274. // ABORT
  1275. if (m_msb[i] == 0x06) {
  1276. goto BusFree;
  1277. }
  1278. // BUS DEVICE RESET
  1279. if (m_msb[i] == 0x0C) {
  1280. syncoffset = 0;
  1281. goto BusFree;
  1282. }
  1283. // IDENTIFY
  1284. if (m_msb[i] >= 0x80) {
  1285. }
  1286. // Extended message
  1287. if (m_msb[i] == 0x01) {
  1288. // Check only when synchronous transfer is possible
  1289. if (!syncenable || m_msb[i + 2] != 0x01) {
  1290. MsgIn2(0x07);
  1291. break;
  1292. }
  1293. // Transfer period factor(50 x 4 = Limited to 200ns)
  1294. syncperiod = m_msb[i + 3];
  1295. if (syncperiod > 50) {
  1296. syncperiod = 50;
  1297. }
  1298. // REQ/ACK offset(Limited to 16)
  1299. syncoffset = m_msb[i + 4];
  1300. if (syncoffset > 16) {
  1301. syncoffset = 16;
  1302. }
  1303. // STDR response message generation
  1304. MsgIn2(0x01);
  1305. MsgIn2(0x03);
  1306. MsgIn2(0x01);
  1307. MsgIn2(syncperiod);
  1308. MsgIn2(syncoffset);
  1309. break;
  1310. }
  1311. }
  1312. }
  1313. LOG("Command:");
  1314. SCSI_PHASE_CHANGE(SCSI_PHASE_COMMAND);
  1315. // Bus settle delay 400ns. The following code was measured at 20ns before REQ asserted. Added another 380ns. STM32F103.
  1316. asm("nop;nop;nop;nop;nop;nop;nop;nop");// This asm causes some code reodering, which adds 270ns, plus 8 nop cycles for an additional 110ns. STM32F103
  1317. int len;
  1318. byte cmd[12];
  1319. cmd[0] = readHandshake();
  1320. LOGHEX(cmd[0]);
  1321. // Command length selection, reception
  1322. static const int cmd_class_len[8]={6,10,10,6,6,12,6,6};
  1323. len = cmd_class_len[cmd[0] >> 5];
  1324. cmd[1] = readHandshake(); LOG(":");LOGHEX(cmd[1]);
  1325. cmd[2] = readHandshake(); LOG(":");LOGHEX(cmd[2]);
  1326. cmd[3] = readHandshake(); LOG(":");LOGHEX(cmd[3]);
  1327. cmd[4] = readHandshake(); LOG(":");LOGHEX(cmd[4]);
  1328. cmd[5] = readHandshake(); LOG(":");LOGHEX(cmd[5]);
  1329. // Receive the remaining commands
  1330. for(int i = 6; i < len; i++ ) {
  1331. cmd[i] = readHandshake();
  1332. LOG(":");
  1333. LOGHEX(cmd[i]);
  1334. }
  1335. // LUN confirmation
  1336. m_sts = cmd[1]&0xe0; // Preset LUN in status byte
  1337. m_lun = m_sts>>5;
  1338. // HDD Image selection
  1339. m_img = (HDDIMG *)0; // None
  1340. if( (m_lun <= NUM_SCSILUN) )
  1341. {
  1342. m_img = &(img[m_id][m_lun]); // There is an image
  1343. if(!(m_img->m_file.isOpen()))
  1344. m_img = (HDDIMG *)0; // Image absent
  1345. }
  1346. // if(!m_img) m_sts |= 0x02; // Missing image file for LUN
  1347. //LOGHEX(((uint32_t)m_img));
  1348. LOG(":ID ");
  1349. LOG(m_id);
  1350. LOG(":LUN ");
  1351. LOG(m_lun);
  1352. LOGN("");
  1353. switch(cmd[0]) {
  1354. case SCSI_TEST_UNIT_READY:
  1355. LOGN("[Test Unit Ready]");
  1356. m_sts |= onTestUnitReady();
  1357. break;
  1358. case SCSI_REZERO_UNIT: // TODO: Implement me!
  1359. LOGN("[Rezero Unit]");
  1360. break;
  1361. case SCSI_REQUEST_SENSE:
  1362. LOGN("[RequestSense]");
  1363. onRequestSenseCommand(cmd[4]);
  1364. break;
  1365. case SCSI_FORMAT_UNIT4: // TODO: Implement me!
  1366. LOGN("[FormatUnit4]");
  1367. break;
  1368. case SCSI_FORMAT_UNIT6: // TODO: Implement me!
  1369. LOGN("[FormatUnit6]");
  1370. break;
  1371. case SCSI_REASSIGN_BLOCKS: // TODO: Implement me!
  1372. LOGN("[ReassignBlocks]");
  1373. break;
  1374. case SCSI_READ6:
  1375. LOGN("[Read6]");
  1376. m_sts |= onReadCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
  1377. break;
  1378. case SCSI_WRITE6:
  1379. LOGN("[Write6]");
  1380. m_sts |= onWriteCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
  1381. break;
  1382. case SCSI_SEEK6: // TODO: Implement me!
  1383. LOGN("[Seek6]");
  1384. break;
  1385. case SCSI_INQUIRY:
  1386. LOGN("[Inquiry]");
  1387. m_sts |= onInquiryCommand(cmd[4]);
  1388. break;
  1389. case SCSI_MODE_SELECT6:
  1390. LOGN("[ModeSelect6]");
  1391. m_sts |= onModeSelectCommand(cmd[0], cmd[1], cmd[4]);
  1392. break;
  1393. case SCSI_MODE_SENSE6:
  1394. LOGN("[ModeSense6]");
  1395. m_sts |= onModeSenseCommand(cmd[0], cmd[1]&0x80, cmd[2], cmd[4]);
  1396. break;
  1397. case SCSI_START_STOP_UNIT: // TODO: Implement me!
  1398. LOGN("[StartStopUnit]");
  1399. break;
  1400. case SCSI_PREVENT_ALLOW_REMOVAL: // TODO: Implement me!
  1401. LOGN("[PreAllowMed.Removal]");
  1402. break;
  1403. case SCSI_READ_CAPACITY:
  1404. LOGN("[ReadCapacity]");
  1405. m_sts |= onReadCapacityCommand(cmd[8]);
  1406. break;
  1407. case SCSI_READ10:
  1408. LOGN("[Read10]");
  1409. m_sts |= onReadCommand(((uint32_t)cmd[2] << 24) | ((uint32_t)cmd[3] << 16) | ((uint32_t)cmd[4] << 8) | cmd[5], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1410. break;
  1411. case SCSI_WRITE10:
  1412. LOGN("[Write10]");
  1413. m_sts |= onWriteCommand(((uint32_t)cmd[2] << 24) | ((uint32_t)cmd[3] << 16) | ((uint32_t)cmd[4] << 8) | cmd[5], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1414. break;
  1415. case SCSI_SEEK10: // TODO: Implement me!
  1416. LOGN("[Seek10]");
  1417. break;
  1418. case SCSI_VERIFY10:
  1419. LOGN("[Verify10]");
  1420. m_sts |= onVerifyCommand(cmd[1], ((uint32_t)cmd[2] << 24) | ((uint32_t)cmd[3] << 16) | ((uint32_t)cmd[4] << 8) | cmd[5], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1421. break;
  1422. case SCSI_SYNCHRONIZE_CACHE: // TODO: Implement me!
  1423. LOGN("[SynchronizeCache10]");
  1424. break;
  1425. case SCSI_MODE_SELECT10:
  1426. LOGN("[ModeSelect10");
  1427. m_sts |= onModeSelectCommand(cmd[0], cmd[1], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1428. break;
  1429. case SCSI_MODE_SENSE10:
  1430. LOGN("[ModeSense10]");
  1431. m_sts |= onModeSenseCommand(cmd[0], cmd[1] & 0x80, cmd[2], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1432. break;
  1433. default:
  1434. LOGN("[*Unknown]");
  1435. m_sts |= SCSI_STATUS_CHECK_CONDITION;
  1436. m_senseKey = SCSI_SENSE_ILLEGAL_REQUEST;
  1437. m_addition_sense = SCSI_ASC_INVALID_OPERATION_CODE;
  1438. break;
  1439. }
  1440. LOGN("Sts");
  1441. SCSI_PHASE_CHANGE(SCSI_PHASE_STATUS);
  1442. // Bus settle delay 400ns built in to writeHandshake
  1443. writeHandshake(m_sts);
  1444. LOGN("MsgIn");
  1445. SCSI_PHASE_CHANGE(SCSI_PHASE_MESSAGEIN);
  1446. // Bus settle delay 400ns built in to writeHandshake
  1447. writeHandshake(m_msg);
  1448. BusFree:
  1449. LOGN("BusFree");
  1450. m_isBusReset = false;
  1451. //SCSI_OUT(vREQ,inactive) // gpio_write(REQ, low);
  1452. //SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  1453. //SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  1454. //SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  1455. //SCSI_OUT(vBSY,inactive)
  1456. SCSI_TARGET_INACTIVE() // Turn off BSY, REQ, MSG, CD, IO output
  1457. #ifdef XCVR
  1458. TRANSCEIVER_IO_SET(vTR_TARGET,TR_INPUT);
  1459. #endif
  1460. }