BlueSCSI.cpp 46 KB

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