Browse Source

Fix various bugs, basic communication works now.

Petteri Aimonen 3 years ago
parent
commit
094e9431ce

+ 21 - 10
lib/AzulSCSI_platform_GD32F205/AzulSCSI_platform.cpp

@@ -1,5 +1,6 @@
 #include "AzulSCSI_platform.h"
 #include "gd32f20x_spi.h"
+#include "AzulSCSI_log.h"
 #include <SdFat.h>
 
 extern "C" {
@@ -101,7 +102,17 @@ void azplatform_init()
     // SWO trace pin on PB3
     gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
 
-    azplatform_log("GPIO init complete\n");    
+    azlogn("GPIO init complete");
+
+    if (gpio_input_bit_get(DIP_PORT, DIPSW3_PIN))
+    {
+        azlogn("DIPSW3 is ON: Enabling SCSI termination");
+        gpio_bit_reset(SCSI_TERM_EN_PORT, SCSI_TERM_EN_PIN);
+    }
+    else
+    {
+        azlogn("DIPSW3 is OFF: SCSI termination disabled");
+    }
 }
 
 static void (*g_rst_callback)();
@@ -276,15 +287,15 @@ SdSpiConfig g_sd_spi_config(0, DEDICATED_SPI, SD_SCK_MHZ(25), &g_sd_spi_port);
 
 #define PARITY(n) ((1 ^ (n) ^ ((n)>>1) ^ ((n)>>2) ^ ((n)>>3) ^ ((n)>>4) ^ ((n)>>5) ^ ((n)>>6) ^ ((n)>>7)) & 1)
 #define X(n) (\
-    ((n & 0x01) ? SCSI_OUT_DB0 : (SCSI_OUT_DB0 << 16)) | \
-    ((n & 0x02) ? SCSI_OUT_DB1 : (SCSI_OUT_DB1 << 16)) | \
-    ((n & 0x04) ? SCSI_OUT_DB2 : (SCSI_OUT_DB2 << 16)) | \
-    ((n & 0x08) ? SCSI_OUT_DB3 : (SCSI_OUT_DB3 << 16)) | \
-    ((n & 0x10) ? SCSI_OUT_DB4 : (SCSI_OUT_DB4 << 16)) | \
-    ((n & 0x20) ? SCSI_OUT_DB5 : (SCSI_OUT_DB5 << 16)) | \
-    ((n & 0x40) ? SCSI_OUT_DB6 : (SCSI_OUT_DB6 << 16)) | \
-    ((n & 0x80) ? SCSI_OUT_DB7 : (SCSI_OUT_DB7 << 16)) | \
-    (PARITY(n)  ? SCSI_OUT_DBP : (SCSI_OUT_DBP << 16)) \
+    ((n & 0x01) ? (SCSI_OUT_DB0 << 16) : SCSI_OUT_DB0) | \
+    ((n & 0x02) ? (SCSI_OUT_DB1 << 16) : SCSI_OUT_DB1) | \
+    ((n & 0x04) ? (SCSI_OUT_DB2 << 16) : SCSI_OUT_DB2) | \
+    ((n & 0x08) ? (SCSI_OUT_DB3 << 16) : SCSI_OUT_DB3) | \
+    ((n & 0x10) ? (SCSI_OUT_DB4 << 16) : SCSI_OUT_DB4) | \
+    ((n & 0x20) ? (SCSI_OUT_DB5 << 16) : SCSI_OUT_DB5) | \
+    ((n & 0x40) ? (SCSI_OUT_DB6 << 16) : SCSI_OUT_DB6) | \
+    ((n & 0x80) ? (SCSI_OUT_DB7 << 16) : SCSI_OUT_DB7) | \
+    (PARITY(n)  ? (SCSI_OUT_DBP << 16) : SCSI_OUT_DBP) \
 )
     
 const uint32_t g_scsi_out_byte_to_bop[256] =

+ 1 - 1
lib/AzulSCSI_platform_GD32F205/AzulSCSI_platform.h

@@ -143,7 +143,7 @@ extern const uint32_t g_scsi_out_byte_to_bop[256];
 
 // Read SCSI data bus
 #define SCSI_IN_DATA(data) \
-    ((GPIO_ISTAT(SCSI_IN_PORT) & SCSI_IN_MASK) >> SCSI_IN_SHIFT)
+    (((~GPIO_ISTAT(SCSI_IN_PORT)) & SCSI_IN_MASK) >> SCSI_IN_SHIFT)
 
 #ifdef __cplusplus
 }

+ 19 - 11
src/AzulSCSI.cpp

@@ -666,7 +666,7 @@ int readSCSICommand(uint8_t cmd[12])
   static const int cmd_class_len[8]={6,10,10,6,6,12,6,6};
   int cmdlen = cmd_class_len[cmd[0] >> 5];
 
-  for (int i = 0; i < cmdlen; i++)
+  for (int i = 1; i < cmdlen; i++)
   {
     cmd[i] = readHandshake();
     if (g_busreset) return 0;
@@ -681,7 +681,11 @@ bool onATNMessage()
   bool syncenable = false;
   int syncperiod = 50;
   int syncoffset = 0;
-  
+
+  SCSI_OUT(MSG,  active);
+  SCSI_OUT(CD ,  active);
+  SCSI_OUT(IO ,inactive);  
+
   uint8_t msg[256];
   memset(msg, 0x00, sizeof(msg));
 
@@ -691,11 +695,11 @@ bool onATNMessage()
 
   int msg_bytes = 0;
   while(SCSI_IN(ATN) && msg_bytes < (int)sizeof(msg)) {
-    if (g_busreset) break;
-
-    SCSI_OUT(MSG,  active);
-    SCSI_OUT(CD ,  active);
-    SCSI_OUT(IO ,inactive);
+    if (g_busreset)
+    {
+      return false;
+    }
+    
     msg[msg_bytes] = readHandshake();
     msg_bytes++;
   }
@@ -705,7 +709,7 @@ bool onATNMessage()
     return false;
   }
 
-  azlogn("Received MSG, ", (int)msg_bytes, " bytes, first byte ", (int)msg[0]);
+  azlogn("Received MSG, ", (int)msg_bytes, " bytes, first byte ", msg[0]);
 
   for (int i = 0; i < msg_bytes; i++)
   {
@@ -759,7 +763,7 @@ bool onATNMessage()
 
   if (responselen > 0)
   {
-    azlogn("Sending MSG response, ", (int)responselen, " bytes, first byte ", (int)response[0]);
+    azlogn("Sending MSG response, ", (int)responselen, " bytes, first byte ", response[0]);
     SCSI_OUT(MSG,  active);
     SCSI_OUT(CD ,  active);
     SCSI_OUT(IO ,  active);
@@ -779,9 +783,10 @@ bool onATNMessage()
 
 void scsi_loop()
 {
+  SCSI_RELEASE_OUTPUTS();
+
   if (g_busreset)
   {
-    SCSI_RELEASE_OUTPUTS();
     g_busreset = false;
   }
 
@@ -795,11 +800,11 @@ void scsi_loop()
     return; // Not for us
   }
 
-  azlog("SCSI device selected");
   g_busreset = false;
   
   // Set BSY to-when selected
   SCSI_OUT(BSY, active);
+  azlogn("SCSI device selected");
   
   // Ask for a TARGET-ID to respond
   g_scsi_id = 0;
@@ -943,6 +948,9 @@ void scsi_loop()
   SCSI_OUT(CD ,  active);
   SCSI_OUT(IO ,  active);
   writeHandshake(0);
+
+  azlogn("Command complete");
+  SCSI_RELEASE_OUTPUTS();
 }
 
 void onBusReset(void)