BlueSCSI.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #ifndef __BLUESCSI_H__
  2. #define __BLUESCSI_H__
  3. #include <Arduino.h> // For Platform.IO
  4. #include <SdFat.h>
  5. // SCSI config
  6. #define MAX_SCSIID 7 // Maximum number of supported SCSI-IDs (The minimum is 0)
  7. #define MAX_SCSILUN 8 // Maximum number of LUNs supported (The minimum is 0)
  8. #define NUM_SCSIID MAX_SCSIID // Number of enabled SCSI IDs
  9. #define NUM_SCSILUN 1 // Number of enabled LUNs
  10. #define READ_PARITY_CHECK 0 // Perform read parity check (unverified)
  11. #define DEFAULT_SCSI_ID 1
  12. #define DEFAULT_SCSI_LUN 0
  13. #define SCSI_BUF_SIZE 512 // Size of the SCSI Buffer
  14. #define HDD_BLOCK_SIZE 512
  15. #define OPTICAL_BLOCK_SIZE 2048
  16. // HDD format
  17. #define MAX_BLOCKSIZE 4096 // Maximum BLOCK size
  18. // SDFAT
  19. #define SD1_CONFIG SdSpiConfig(PA4, DEDICATED_SPI, SD_SCK_MHZ(SPI_FULL_SPEED), &SPI)
  20. // LED ERRORS
  21. #define ERROR_FALSE_INIT 3
  22. #define ERROR_NO_SDCARD 5
  23. #if DEBUG
  24. #define LOG(XX) Serial.print(XX)
  25. #define LOGHEX(XX) Serial.print(XX, HEX)
  26. #define LOGN(XX) Serial.println(XX)
  27. #define LOGHEXN(XX) Serial.println(XX, HEX)
  28. #else
  29. #define LOG(XX) //Serial.print(XX)
  30. #define LOGHEX(XX) //Serial.print(XX, HEX)
  31. #define LOGN(XX) //Serial.println(XX)
  32. #define LOGHEXN(XX) //Serial.println(XX, HEX)
  33. #endif
  34. #define active 1
  35. #define inactive 0
  36. #define high 0
  37. #define low 1
  38. #define isHigh(XX) ((XX) == high)
  39. #define isLow(XX) ((XX) != high)
  40. #define gpio_mode(pin,val) gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val);
  41. #define gpio_write(pin,val) gpio_write_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val)
  42. #define gpio_read(pin) gpio_read_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit)
  43. //#define DB0 PB8 // SCSI:DB0
  44. //#define DB1 PB9 // SCSI:DB1
  45. //#define DB2 PB10 // SCSI:DB2
  46. //#define DB3 PB11 // SCSI:DB3
  47. //#define DB4 PB12 // SCSI:DB4
  48. //#define DB5 PB13 // SCSI:DB5
  49. //#define DB6 PB14 // SCSI:DB6
  50. //#define DB7 PB15 // SCSI:DB7
  51. //#define DBP PB0 // SCSI:DBP
  52. #define ATN PA8 // SCSI:ATN
  53. #define BSY PA9 // SCSI:BSY
  54. #define ACK PA10 // SCSI:ACK
  55. #define RST PA15 // SCSI:RST
  56. #define MSG PB3 // SCSI:MSG
  57. #define SEL PB4 // SCSI:SEL
  58. #define CD PB5 // SCSI:C/D
  59. #define REQ PB6 // SCSI:REQ
  60. #define IO PB7 // SCSI:I/O
  61. #define SD_CS PA4 // SDCARD:CS
  62. #define LED PC13 // LED
  63. #define LED2 PA0 // External LED
  64. // Image Set Selector
  65. #ifdef XCVR
  66. #define IMAGE_SELECT1 PC14
  67. #define IMAGE_SELECT2 PC15
  68. #else
  69. #define IMAGE_SELECT1 PA1
  70. #define IMAGE_SELECT2 PB1
  71. #endif
  72. // GPIO register port
  73. #define PAREG GPIOA->regs
  74. #define PBREG GPIOB->regs
  75. #define PCREG GPIOC->regs
  76. // LED control
  77. #define LED_ON() PCREG->BSRR = 0b00100000000000000000000000000000; PAREG->BSRR = 0b00000000000000000000000000000001;
  78. #define LED_OFF() PCREG->BSRR = 0b00000000000000000010000000000000; PAREG->BSRR = 0b00000000000000010000000000000000;
  79. // Virtual pin (Arduio compatibility is slow, so make it MCU-dependent)
  80. #define PA(BIT) (BIT)
  81. #define PB(BIT) (BIT+16)
  82. // Virtual pin decoding
  83. #define GPIOREG(VPIN) ((VPIN)>=16?PBREG:PAREG)
  84. #define BITMASK(VPIN) (1<<((VPIN)&15))
  85. #define vATN PA(8) // SCSI:ATN
  86. #define vBSY PA(9) // SCSI:BSY
  87. #define vACK PA(10) // SCSI:ACK
  88. #define vRST PA(15) // SCSI:RST
  89. #define vMSG PB(3) // SCSI:MSG
  90. #define vSEL PB(4) // SCSI:SEL
  91. #define vCD PB(5) // SCSI:C/D
  92. #define vREQ PB(6) // SCSI:REQ
  93. #define vIO PB(7) // SCSI:I/O
  94. #define vSD_CS PA(4) // SDCARD:CS
  95. // SCSI output pin control: opendrain active LOW (direct pin drive)
  96. #define SCSI_OUT(VPIN,ACTIVE) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN)<<((ACTIVE)?16:0); }
  97. // SCSI input pin check (inactive=0,avtive=1)
  98. #define SCSI_IN(VPIN) ((~GPIOREG(VPIN)->IDR>>(VPIN&15))&1)
  99. #define NOP(x) for(unsigned _nopcount = x; _nopcount; _nopcount--) { asm("NOP"); }
  100. /* SCSI Timing delays */
  101. // Due to limitations in timing granularity all of these are "very" rough estimates
  102. #define SCSI_BUS_SETTLE() NOP(30); // spec 400ns ours ~420us
  103. #define SCSI_DATA_RELEASE() NOP(30); // spec 400ns ours ~420us
  104. #define SCSI_HOLD_TIME() asm("NOP"); asm("NOP"); asm("NOP"); // spec 45ns ours ~42ns
  105. #define SCSI_DESKEW() // asm("NOP"); asm("NOP"); asm("NOP"); // spec 45ns ours ~42ns
  106. #define SCSI_CABLE_SKEW() // asm("NOP"); // spec 10ns ours ~14ns
  107. #define SCSI_RESET_HOLD() asm("NOP"); asm("NOP"); // spec 25ns ours ~28ns
  108. #define SCSI_DISCONNECTION_DELAY() NOP(15); // spec 200ns ours ~210ns
  109. /* SCSI phases
  110. +=============-===============-==================================-============+
  111. | Signal | Phase name | Direction of transfer | Comment |
  112. |-------------| | | |
  113. | MSG|C/D|I/O | | | |
  114. |----+---+----+---------------+----------------------------------+------------|
  115. | 0 | 0 | 0 | DATA OUT | Initiator to target \ | Data |
  116. | 0 | 0 | 1 | DATA IN | Initiator from target / | phase |
  117. | 0 | 1 | 0 | COMMAND | Initiator to target | |
  118. | 0 | 1 | 1 | STATUS | Initiator from target | |
  119. | 1 | 0 | 0 | * | | |
  120. | 1 | 0 | 1 | * | | |
  121. | 1 | 1 | 0 | MESSAGE OUT | Initiator to target \ | Message |
  122. | 1 | 1 | 1 | MESSAGE IN | Initiator from target / | phase |
  123. |-----------------------------------------------------------------------------|
  124. | Key: 0 = False, 1 = True, * = Reserved for future standardization |
  125. +=============================================================================+
  126. */
  127. // SCSI phase change as single write to port B
  128. #define SCSIPHASEMASK(MSGACTIVE, CDACTIVE, IOACTIVE) ((BITMASK(vMSG)<<((MSGACTIVE)?16:0)) | (BITMASK(vCD)<<((CDACTIVE)?16:0)) | (BITMASK(vIO)<<((IOACTIVE)?16:0)))
  129. #define SCSI_PHASE_DATAOUT SCSIPHASEMASK(inactive, inactive, inactive)
  130. #define SCSI_PHASE_DATAIN SCSIPHASEMASK(inactive, inactive, active)
  131. #define SCSI_PHASE_COMMAND SCSIPHASEMASK(inactive, active, inactive)
  132. #define SCSI_PHASE_STATUS SCSIPHASEMASK(inactive, active, active)
  133. #define SCSI_PHASE_MESSAGEOUT SCSIPHASEMASK(active, active, inactive)
  134. #define SCSI_PHASE_MESSAGEIN SCSIPHASEMASK(active, active, active)
  135. #define SCSI_PHASE_CHANGE(MASK) { PBREG->BSRR = (MASK); }
  136. #ifdef XCVR
  137. #define TR_TARGET PA1 // Target Transceiver Control Pin
  138. #define TR_DBP PA2 // Data Pins Transceiver Control Pin
  139. #define TR_INITIATOR PA3 // Initiator Transciever Control Pin
  140. #define vTR_TARGET PA(1) // Target Transceiver Control Pin
  141. #define vTR_DBP PA(2) // Data Pins Transceiver Control Pin
  142. #define vTR_INITIATOR PA(3) // Initiator Transciever Control Pin
  143. #define TR_INPUT 1
  144. #define TR_OUTPUT 0
  145. // Transceiver control definitions
  146. #define TRANSCEIVER_IO_SET(VPIN,TR_INPUT) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN) << ((TR_INPUT) ? 16 : 0); }
  147. // Turn on the output only for BSY
  148. #define SCSI_BSY_ACTIVE() { gpio_mode(BSY, GPIO_OUTPUT_PP); SCSI_OUT(vBSY, active) }
  149. #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);}
  150. // BSY,REQ,MSG,CD,IO Turn off output, BSY is the last input
  151. #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); }
  152. #define DB_MODE_OUT 1 // push-pull mode
  153. #define DB_MODE_IN 4 // floating inputs
  154. #else
  155. // GPIO mode
  156. // IN , FLOAT : 4
  157. // IN , PU/PD : 8
  158. // OUT, PUSH/PULL : 3
  159. // OUT, OD : 1
  160. #define DB_MODE_OUT 3
  161. //#define DB_MODE_OUT 7
  162. #define DB_MODE_IN 8
  163. // Turn on the output only for BSY
  164. #define SCSI_BSY_ACTIVE() { gpio_mode(BSY, GPIO_OUTPUT_OD); SCSI_OUT(vBSY, active) }
  165. // BSY,REQ,MSG,CD,IO Turn on the output (no change required for OD)
  166. #define SCSI_TARGET_ACTIVE() { if (DB_MODE_OUT != 7) gpio_mode(REQ, GPIO_OUTPUT_PP); }
  167. // BSY,REQ,MSG,CD,IO Turn off output, BSY is the last input
  168. #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)} PBREG->BSRR = 0b000000000000000011101000; SCSI_OUT(vBSY,inactive); gpio_mode(BSY, GPIO_INPUT_PU); }
  169. #endif
  170. // Put DB and DP in output mode
  171. #define SCSI_DB_OUTPUT() { PBREG->CRL=(PBREG->CRL &0xfffffff0)|DB_MODE_OUT; PBREG->CRH = 0x11111111*DB_MODE_OUT; }
  172. // Put DB and DP in input mode
  173. #define SCSI_DB_INPUT() { PBREG->CRL=(PBREG->CRL &0xfffffff0)|DB_MODE_IN ; PBREG->CRH = 0x11111111*DB_MODE_IN; }
  174. // HDDiamge file
  175. #define HDIMG_ID_POS 2 // Position to embed ID number
  176. #define HDIMG_LUN_POS 3 // Position to embed LUN numbers
  177. #define HDIMG_BLK_POS 5 // Position to embed block size numbers
  178. #define MAX_FILE_PATH 32 // Maximum file name length
  179. /*
  180. * Data byte to BSRR register setting value and parity table
  181. */
  182. // Parity bit generation
  183. #define PTY(V) (1^((V)^((V)>>1)^((V)>>2)^((V)>>3)^((V)>>4)^((V)>>5)^((V)>>6)^((V)>>7))&1)
  184. // Data byte to BSRR register setting value conversion table
  185. // BSRR[31:24] = DB[7:0]
  186. // BSRR[ 16] = PTY(DB)
  187. // BSRR[15: 8] = ~DB[7:0]
  188. // BSRR[ 0] = ~PTY(DB)
  189. // Set DBP, set REQ = inactive
  190. #define DBP(D) ((((((uint32_t)(D)<<8)|PTY(D))*0x00010001)^0x0000ff01)|BITMASK(vREQ))
  191. //#define DBP(D) ((((((uint32_t)(D)<<8)|PTY(D))*0x00010001)^0x0000ff01))
  192. //#define DBP8(D) DBP(D),DBP(D+1),DBP(D+2),DBP(D+3),DBP(D+4),DBP(D+5),DBP(D+6),DBP(D+7)
  193. //#define DBP32(D) DBP8(D),DBP8(D+8),DBP8(D+16),DBP8(D+24)
  194. // BSRR register control value that simultaneously performs DB set, DP set, and REQ = H (inactrive)
  195. uint32_t db_bsrr[256];
  196. // Parity bit acquisition
  197. #define PARITY(DB) (db_bsrr[DB]&1)
  198. // Macro cleaning
  199. //#undef DBP32
  200. //#undef DBP8
  201. //#undef DBP
  202. //#undef PTY
  203. // #define GET_CDB6_LBA(x) ((x[2] & 01f) << 16) | (x[3] << 8) | x[4]
  204. #define READ_DATA_BUS() (byte)((~(uint32_t)GPIOB->regs->IDR)>>8)
  205. enum SCSI_DEVICE_TYPE
  206. {
  207. SCSI_DEVICE_HDD,
  208. SCSI_DEVICE_OPTICAL,
  209. };
  210. #define CDROM_RAW_SECTORSIZE 2352
  211. #define CDROM_COMMON_SECTORSIZE 2048
  212. struct SCSI_INQUIRY_DATA
  213. {
  214. union
  215. {
  216. struct {
  217. // bitfields are in REVERSE order for ARM
  218. // byte 0
  219. byte peripheral_device_type:5;
  220. byte peripheral_qualifier:3;
  221. // byte 1
  222. byte reserved_byte2:7;
  223. byte rmb:1;
  224. // byte 2
  225. byte ansi_version:3;
  226. byte always_zero_byte3:5;
  227. // byte 3
  228. byte response_format:4;
  229. byte reserved_byte4:2;
  230. byte tiop:1;
  231. byte always_zero_byte4:1;
  232. // byte 4
  233. byte additional_length;
  234. // byte 5-6
  235. byte reserved_byte5;
  236. byte reserved_byte6;
  237. // byte 7
  238. byte sync:1;
  239. byte always_zero_byte7_more:4;
  240. byte always_zero_byte7:3;
  241. // byte 8-15
  242. char vendor[8];
  243. // byte 16-31
  244. char product[16];
  245. // byte 32-35
  246. char revision[4];
  247. // byte 36
  248. byte release;
  249. // 37-46
  250. char revision_date[10];
  251. };
  252. // raw bytes
  253. byte raw[64];
  254. };
  255. };
  256. // HDD image
  257. typedef __attribute__((aligned(4))) struct _SCSI_DEVICE
  258. {
  259. FsFile *m_file; // File object
  260. uint64_t m_fileSize; // File size
  261. uint16_t m_blocksize; // SCSI BLOCK size
  262. uint8_t m_type; // SCSI device type
  263. uint32_t m_blockcount; // blockcount
  264. bool m_raw; // Raw disk
  265. SCSI_INQUIRY_DATA *inquiry_block; // SCSI information
  266. uint8_t m_senseKey; // Sense key
  267. uint16_t m_additional_sense_code; // ASC/ASCQ
  268. bool m_mode2; // MODE2 CDROM
  269. } SCSI_DEVICE;
  270. #endif