Quellcode durchsuchen

Start spliting files

Per Mårtensson vor 3 Jahren
Ursprung
Commit
9b94cc03af
10 geänderte Dateien mit 440 neuen und 252 gelöschten Zeilen
  1. 144 0
      sw/include/blacksasi.h
  2. 7 0
      sw/include/config.h
  3. 5 0
      sw/include/gpio.h
  4. 10 0
      sw/include/log.h
  5. 10 0
      sw/include/sdcard.h
  6. 35 0
      sw/src/config.cpp
  7. 44 0
      sw/src/gpio.cpp
  8. 85 0
      sw/src/log.cpp
  9. 12 252
      sw/src/main.cpp
  10. 88 0
      sw/src/sdcard.cpp

+ 144 - 0
sw/include/blacksasi.h

@@ -0,0 +1,144 @@
+#ifndef __BLACKSASI_H__
+#define __BLACKSASI_H__
+#include "sdcard.h"
+#include "gpio.h"
+#include "log.h"
+#include "config.h"
+// Log File
+#define VERSION "0.xx-SNAPSHOT-BLACKSASI-2022-03-20-F4"
+
+#define LOG_FILENAME "LOG.txt"
+
+#define SPI_CLOCK SD_SCK_MHZ(50)
+
+#define DEBUG            1      // 0:No debug information output
+                                // 1: Debug information output to USB Serial
+                                // 2: Debug information output to LOG.txt (slow)
+
+#define SCSI_SELECT      0      // 0 for STANDARD
+                                // 1 for SHARP X1turbo
+                                // 2 for NEC PC98
+#define READ_SPEED_OPTIMIZE  1 // Faster reads
+#define WRITE_SPEED_OPTIMIZE 1 // Speeding up writes
+#define USE_DB2ID_TABLE      1 // Use table to get ID from SEL-DB
+
+// SCSI config
+#define NUM_SCSIID  7          // Maximum number of supported SCSI-IDs (The minimum is 0)
+#define NUM_SCSILUN 2          // Maximum number of LUNs supported     (The minimum is 0)
+#define READ_PARITY_CHECK 0    // Perform read parity check (unverified)
+
+// HDD format
+#define MAX_BLOCKSIZE 2048     // Maximum BLOCK size
+
+
+#if DEBUG == 1
+#define serial Serial
+#define LOG(XX)      serial.print(XX)
+#define LOGHEX(XX)   serial.print(XX, HEX)
+#define LOGDEC(XX)   serial.print(XX, DEC)
+#define LOGBIN(XX)   serial.print(XX, BIN)
+#define LOGN(XX)     serial.println(XX)
+#define LOGHEXN(XX)  serial.println(XX, HEX)
+#define LOGDECN(XX)  serial.println(XX, DEC)
+#define LOGBIN_N(XX) serial.println(XX, BIN)
+#elif DEBUG == 2
+#define LOG(XX)      LOG_FILE.print(XX); LOG_FILE.sync();
+#define LOGHEX(XX)   LOG_FILE.print(XX, HEX); LOG_FILE.sync();
+#define LOGDEC(XX)   LOG_FILE.print(XX, DEC); LOG_FILE.sync();
+#define LOGBIN(XX)   LOG_FILE.print(XX, BIN); LOG_FILE.sync();
+#define LOGN(XX)     LOG_FILE.println(XX); LOG_FILE.sync();
+#define LOGHEXN(XX)  LOG_FILE.println(XX, HEX); LOG_FILE.sync();
+#define LOGDECN(XX)  LOG_FILE.println(XX, DEC); LOG_FILE.sync();
+#define LOGBIN_N(XX) LOG_FILE.println(XX, BIN); LOG_FILE.sync();
+#else
+#define LOG(XX)       //serial.print(XX)
+#define LOGHEX(XX)    //serial.print(XX, HEX)
+#define LOGDEC(XX)    //serial.print(XX, DEC)
+#define LOGBIN(XX)    //serial.print(XX, BIN)
+#define LOGN(XX)      //serial.println(XX)
+#define LOGHEXN(XX)   //serial.println(XX, HEX)
+#define LOGDECN(XX)   //serial.println(XX, DEC)
+#define LOGBIN_N(XX)  //serial.println(XX, BIN)
+#endif
+
+#define active   1
+#define inactive 0
+#define high 0
+#define low 1
+
+#define isHigh(XX) ((XX) == high)
+#define isLow(XX) ((XX) != high)
+
+
+// GPIO register port
+#define PAREG GPIOA->regs
+#define PBREG GPIOB->regs
+#define PCREG GPIOC->regs
+#define PDREG GPIOD->regs
+#define PEREG GPIOE->regs
+
+// LED control
+#define LED1_ON()           PAREG->BSRR = 0b0000000000010000;
+#define LED1_OFF()          PAREG->BSRR = 0b0000000000010000 << 16;
+#define LED2_ON()           PAREG->BSRR = 0b0000000000100000;
+#define LED2_OFF()          PAREG->BSRR = 0b0000000000100000 << 16;
+#define LED3_ON()           PAREG->BSRR = 0b0000000001000000;
+#define LED3_OFF()          PAREG->BSRR = 0b0000000001000000 << 16;
+#define LED_EXT_ON()        PAREG->BSRR = 0b0000000000000001;
+#define LED_EXT_OFF()       PAREG->BSRR = 0b0000000000000001 << 16;
+
+// Termination controll
+#define TERMINATION_HIGH()  PBREG->BSRR = 0b0000000100000000 << 16 | 0b0000001000000000 ;
+#define TERMINATION_LOW()   PBREG->BSRR = 0b0000001000000000 << 16 | 0b0000000100000000 ;
+#define TERMINATION_OFF()   PBREG->BSRR = 0b0000001100000000 ;
+
+// Virtual pin (Arduio compatibility is slow, so make it MCU-dependent)
+#define PA(BIT)       (BIT)
+#define PB(BIT)       (BIT + 16)
+#define PC(BIT)       (BIT + 32)
+#define PD(BIT)       (BIT + 48)
+#define PE(BIT)       (BIT + 64)
+// Virtual pin decoding
+#define GPIOREG(VPIN)     ((VPIN) >= 16 ? PBREG : PAREG)
+#define BITMASK(VPIN)     (1 << ((VPIN) & 15))
+
+#define vATN       PB(14)      // SCSI:ATN
+#define vBSY       PB(6)      // SCSI:BSY
+#define vACK       PB(7)      // SCSI:ACK
+#define vRST       PA(15)     // SCSI:RST
+#define vMSG       PE(2)      // SCSI:MSG
+#define vSEL       PE(3)      // SCSI:SEL
+#define vCD        PE(4)      // SCSI:C/D
+#define vREQ       PE(5)      // SCSI:REQ
+#define vIO        PE(6)      // SCSI:I/O
+#define vSD_CS     PB(1)      // SDCARD:CS
+#define vDTD       PC(0)      // SCSI:DTD
+#define vIND       PC(1)      // SCSI:IND
+#define vTAD       PC(2)      // SCSI:TAD
+
+// SCSI output pin control: opendrain active LOW (direct pin drive)
+#define SCSI_OUT(VPIN,ACTIVE) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN) << ((ACTIVE) ? 16 : 0); }
+
+// SCSI input pin check (inactive=0,active=1)
+#define SCSI_IN(VPIN) ((~GPIOREG(VPIN)->IDR >> (VPIN & 15)) & 1)
+
+// HDDiamge file
+#define HDIMG_ID_POS  2                 // Position to embed ID number
+#define HDIMG_LUN_POS 3                 // Position to embed LUN numbers
+#define HDIMG_BLK_POS 5                 // Position to embed block size numbers
+#define MAX_FILE_PATH 32                // Maximum file name length
+
+typedef struct hddimg_struct
+{
+	FsFile      m_file;                 // File object
+	uint64_t    m_fileSize;             // File size
+	size_t      m_blocksize;            // SCSI BLOCK size
+}HDDIMG;
+
+// Declare functions
+void onFalseInit(void);
+void onBusReset(void);
+void switchImage(void);
+void initFileLog(void);
+void finalizeFileLog(void);
+#endif // __BLACKSASI_H__

+ 7 - 0
sw/include/config.h

@@ -0,0 +1,7 @@
+#ifndef __BLACKSASI_CONFIG_H__
+#define __BLACKSASI_CONFIG_H__
+#include <SdFat.h>
+#include "sdios.h"
+#include "blacksasi.h"
+void readSCSIDeviceConfig();
+#endif // __BLACKSASI_CONFIG_H__

+ 5 - 0
sw/include/gpio.h

@@ -0,0 +1,5 @@
+#ifndef __BLACKSASI_GPIO_H__
+#define __BLACKSASI_GPIO_H__
+bool gpioInit();
+
+#endif // __BLACKSASI_GPIO_H__

+ 10 - 0
sw/include/log.h

@@ -0,0 +1,10 @@
+#ifndef __BLACKSASI_LOG_H__
+#define __BLACKSASI_LOG_H__
+#include <SdFat.h>
+#include "sdios.h"
+#include "blacksasi.h"
+void initFileLog();
+void errorPrint();
+void dmpVol();
+void finalizeFileLog();
+#endif // __BLACKSASI_LOG_H__

+ 10 - 0
sw/include/sdcard.h

@@ -0,0 +1,10 @@
+#ifndef __BLACKSASI_SDCARD_H__
+#define __BLACKSASI_SDCARD_H__
+#include <SdFat.h>
+#include "sdios.h"
+#include "blacksasi.h"
+void readSDCardInfo();
+void readSDCardInfo2();
+void sdCardInsert();
+void noSDCardFound();
+#endif // __BLACKSASI_SDCARD_H__

+ 35 - 0
sw/src/config.cpp

@@ -0,0 +1,35 @@
+#include <Arduino.h>
+#include "config.h"
+#include "sdcard.h"
+extern SdFs SD;
+extern FsFile LOG_FILE;
+extern byte SCSI_INFO_BUF[36];
+// If config file exists, read the first three lines and copy the contents.
+// File must be well formed or you will get junk in the SCSI Vendor fields.
+void readSCSIDeviceConfig() {
+  FsFile config_file = SD.open("scsi-config.txt", O_RDONLY);
+  if (!config_file.isOpen()) {
+    return;
+  }
+  char vendor[9];
+  memset(vendor, 0, sizeof(vendor));
+  config_file.readBytes(vendor, sizeof(vendor));
+  LOG_FILE.print("SCSI VENDOR: ");
+  LOG_FILE.println(vendor);
+  memcpy(&(SCSI_INFO_BUF[8]), vendor, 8);
+
+  char product[17];
+  memset(product, 0, sizeof(product));
+  config_file.readBytes(product, sizeof(product));
+  LOG_FILE.print("SCSI PRODUCT: ");
+  LOG_FILE.println(product);
+  memcpy(&(SCSI_INFO_BUF[16]), product, 16);
+
+  char version[5];
+  memset(version, 0, sizeof(version));
+  config_file.readBytes(version, sizeof(version));
+  LOG_FILE.print("SCSI VERSION: ");
+  LOG_FILE.println(version);
+  memcpy(&(SCSI_INFO_BUF[32]), version, 4);
+  config_file.close();
+}

+ 44 - 0
sw/src/gpio.cpp

@@ -0,0 +1,44 @@
+#include <Arduino.h>
+#include "gpio.h"
+
+extern void sdCardInsert();
+extern void switchImage();
+bool gpioInit(void){
+    // PIN initialization
+    pinMode(BOARD_LED1_PIN , OUTPUT);
+    pinMode(BOARD_LED2_PIN , OUTPUT);
+    pinMode(BOARD_LED3_PIN , OUTPUT);
+    pinMode(BOARD_SDCARD_INSERT, INPUT_PULLUP);
+    pinMode(BOARD_SWITCH1_PIN , INPUT_PULLUP);
+    pinMode(BOARD_SWITCH2_PIN , INPUT_PULLUP);
+    pinMode(BOARD_SWITCH3_PIN , INPUT_PULLUP);
+    pinMode(BOARD_SWITCH4_PIN , INPUT_PULLUP);
+    pinMode(BOARD_SCSI_TERM_HIGH, OUTPUT);
+    pinMode(BOARD_SCSI_TERM_LOW, OUTPUT);
+    pinMode(BOARD_SCSI_TERM_POWER, INPUT); //Check if external termination power is provided
+
+    attachInterrupt(BOARD_SDCARD_INSERT,sdCardInsert, CHANGE);
+    attachInterrupt(BOARD_SWITCH1_PIN,switchImage, CHANGE);
+    attachInterrupt(BOARD_SWITCH2_PIN,switchImage, CHANGE);
+    attachInterrupt(BOARD_SWITCH3_PIN,switchImage, CHANGE);
+    attachInterrupt(BOARD_SWITCH4_PIN,switchImage, CHANGE);
+
+    pinMode(BOARD_SCSI_ATN, INPUT_PULLUP);
+    pinMode(BOARD_SCSI_BSY, INPUT_PULLUP);
+    pinMode(BOARD_SCSI_ACK, INPUT_PULLUP);
+    pinMode(BOARD_SCSI_RST, INPUT_PULLUP);
+    pinMode(BOARD_SCSI_SEL, INPUT_PULLUP);
+
+    // Output port
+    pinMode(BOARD_SCSI_MSG, OUTPUT_OPEN_DRAIN);
+    pinMode(BOARD_SCSI_CD, OUTPUT_OPEN_DRAIN);
+    pinMode(BOARD_SCSI_REQ, OUTPUT_OPEN_DRAIN);
+    pinMode(BOARD_SCSI_IO, OUTPUT_OPEN_DRAIN);
+
+    // Control SCSI drivers
+    pinMode(BOARD_SCSI_DTD, OUTPUT_OPEN_DRAIN);
+    pinMode(BOARD_SCSI_IND, OUTPUT_OPEN_DRAIN);
+    pinMode(BOARD_SCSI_TAD, OUTPUT_OPEN_DRAIN);
+    pinMode(BOARD_TRANS_OE, OUTPUT_OPEN_DRAIN);
+}
+  

+ 85 - 0
sw/src/log.cpp

@@ -0,0 +1,85 @@
+#include <Arduino.h>
+#include "log.h"
+
+extern FsFile LOG_FILE;
+extern SdFs SD;
+extern ArduinoOutStream cout(Serial);
+extern HDDIMG  img[NUM_SCSIID][NUM_SCSILUN]; 
+/*
+ * Setup initialization logfile
+ */
+void initFileLog() {
+  LOG_FILE = SD.open(LOG_FILENAME, O_WRONLY | O_CREAT | O_TRUNC);
+  LOG_FILE.println("BlackSASI F411 <-> SD ");
+  LOG_FILE.print("VERSION: ");
+  LOG_FILE.println(VERSION);
+  LOG_FILE.print("DEBUG:");
+  LOG_FILE.print(DEBUG);
+  LOG_FILE.print(" SCSI_SELECT:");
+  LOG_FILE.print(SCSI_SELECT);
+  LOG_FILE.print(" SDFAT_FILE_TYPE:");
+  LOG_FILE.println(SDFAT_FILE_TYPE);
+  LOG_FILE.print("SdFat version: ");
+  LOG_FILE.println(SD_FAT_VERSION_STR);
+  LOG_FILE.print("SdFat Max FileName Length: ");
+  LOG_FILE.println(MAX_FILE_PATH);
+  LOG_FILE.println("Initialized SD Card - lets go!");
+  LOG_FILE.sync();
+}
+void errorPrint() {
+  if (SD.sdErrorCode()) {
+    cout << F("SD errorCode: ") << hex << showbase;
+    printSdErrorSymbol(&Serial, SD.sdErrorCode());
+    cout << F(" = ") << int(SD.sdErrorCode()) << endl;
+    cout << F("SD errorData = ") << int(SD.sdErrorData()) << endl;
+  }
+}
+void dmpVol() {
+  cout << F("\nScanning FAT, please wait.\n");
+  uint32_t freeClusterCount = SD.freeClusterCount();
+  if (SD.fatType() <= 32) {
+    cout << F("\nVolume is FAT") << int(SD.fatType()) << endl;
+  } else {
+    cout << F("\nVolume is exFAT\n");
+  }
+  cout << F("sectorsPerCluster: ") << SD.sectorsPerCluster() << endl;
+  cout << F("clusterCount:      ") << SD.clusterCount() << endl;
+  cout << F("freeClusterCount:  ") << freeClusterCount << endl;
+  cout << F("fatStartSector:    ") << SD.fatStartSector() << endl;
+  cout << F("dataStartSector:   ") << SD.dataStartSector() << endl;
+
+}
+/*
+ * Finalize initialization logfile
+ */
+void finalizeFileLog() {
+  // View support drive map
+  LOG_FILE.print("ID");
+  for(int lun=0;lun<NUM_SCSILUN;lun++)
+  {
+    LOG_FILE.print(":LUN");
+    LOG_FILE.print(lun);
+  }
+  LOG_FILE.println(":");
+  //
+  for(int id=0;id<NUM_SCSIID;id++)
+  {
+    LOG_FILE.print(" ");
+    LOG_FILE.print(id);
+    for(int lun=0;lun<NUM_SCSILUN;lun++)
+    {
+      HDDIMG *h = &img[id][lun];
+      if( (lun<NUM_SCSILUN) && (h->m_file))
+      {
+        LOG_FILE.print((h->m_blocksize<1000) ? ": " : ":");
+        LOG_FILE.print(h->m_blocksize);
+      }
+      else      
+        LOG_FILE.print(":----");
+    }
+    LOG_FILE.println(":");
+  }
+  LOG_FILE.println("Finished initialization of SCSI Devices - Entering main loop.");
+  LOG_FILE.sync();
+  LOG_FILE.close();
+}

+ 12 - 252
sw/src/main.cpp

@@ -60,15 +60,16 @@
 #include <SdFat.h>
 #include "sdios.h"
 #include "blacksasi.h"
+
 #ifdef USE_STM32_DMA
 #warning "warning USE_STM32_DMA"
 #endif
 
-
 // SDFAT
 SdFs SD;
 
-
+// Logfile
+FsFile LOG_FILE;
 
 static const uint32_t scsiDbOutputRegOr = 0x55150011;
 static const uint32_t scsiDbInputOutputAnd = 0x00C0FFCC;
@@ -85,20 +86,11 @@ static const uint32_t scsiDbInputOutputAnd = 0x00C0FFCC;
 // BSY,REQ,MSG,CD,IO Turn off output, BSY is the last input
 #define SCSI_TARGET_INACTIVE() { SCSI_OUT(vREQ,inactive); SCSI_OUT(vMSG,inactive); SCSI_OUT(vCD,inactive); SCSI_OUT(vIO,inactive); SCSI_OUT(vBSY,inactive); pinMode(BOARD_SCSI_BSY, INPUT); }
 
-// HDDiamge file
-#define HDIMG_ID_POS  2                 // Position to embed ID number
-#define HDIMG_LUN_POS 3                 // Position to embed LUN numbers
-#define HDIMG_BLK_POS 5                 // Position to embed block size numbers
-#define MAX_FILE_PATH 32                // Maximum file name length
+
 static ArduinoOutStream cout(Serial);
 // HDD image
 uint16_t imageSelect = 0;
-typedef struct hddimg_struct
-{
-	FsFile      m_file;                 // File object
-	uint64_t    m_fileSize;             // File size
-	size_t      m_blocksize;            // SCSI BLOCK size
-}HDDIMG;
+
 HDDIMG  img[NUM_SCSIID][NUM_SCSILUN]; // Maximum number
 
 uint8_t       m_senseKey = 0;         // Sense key
@@ -203,7 +195,7 @@ static const byte db2scsiid[256]={
 #endif
 
 
-FsFile LOG_FILE;
+
 
 // SCSI Drive Vendor information
 byte SCSI_INFO_BUF[36] = {
@@ -219,40 +211,7 @@ byte SCSI_INFO_BUF[36] = {
   '1', '.', '0', ' ' // version 4
 };
 
-void onFalseInit(void);
-void noSDCardFound(void);
-void onBusReset(void);
-void sdCardInsert(void);
-void switchImage(void);
-void initFileLog(void);
-void finalizeFileLog(void);
-//------------------------------------------------------------------------------
-
-
-void errorPrint() {
-  if (SD.sdErrorCode()) {
-    cout << F("SD errorCode: ") << hex << showbase;
-    printSdErrorSymbol(&Serial, SD.sdErrorCode());
-    cout << F(" = ") << int(SD.sdErrorCode()) << endl;
-    cout << F("SD errorData = ") << int(SD.sdErrorData()) << endl;
-  }
-}
-//------------------------------------------------------------------------------
-void dmpVol() {
-  cout << F("\nScanning FAT, please wait.\n");
-  uint32_t freeClusterCount = SD.freeClusterCount();
-  if (SD.fatType() <= 32) {
-    cout << F("\nVolume is FAT") << int(SD.fatType()) << endl;
-  } else {
-    cout << F("\nVolume is exFAT\n");
-  }
-  cout << F("sectorsPerCluster: ") << SD.sectorsPerCluster() << endl;
-  cout << F("clusterCount:      ") << SD.clusterCount() << endl;
-  cout << F("freeClusterCount:  ") << freeClusterCount << endl;
-  cout << F("fatStartSector:    ") << SD.fatStartSector() << endl;
-  cout << F("dataStartSector:   ") << SD.dataStartSector() << endl;
 
-}
 /*
  * IO read.
  */
@@ -270,96 +229,8 @@ inline byte readIO(void)
   return bret;
 }
 
-// If config file exists, read the first three lines and copy the contents.
-// File must be well formed or you will get junk in the SCSI Vendor fields.
-void readSCSIDeviceConfig() {
-  FsFile config_file = SD.open("scsi-config.txt", O_RDONLY);
-  if (!config_file.isOpen()) {
-    return;
-  }
-  char vendor[9];
-  memset(vendor, 0, sizeof(vendor));
-  config_file.readBytes(vendor, sizeof(vendor));
-  LOG_FILE.print("SCSI VENDOR: ");
-  LOG_FILE.println(vendor);
-  memcpy(&(SCSI_INFO_BUF[8]), vendor, 8);
-
-  char product[17];
-  memset(product, 0, sizeof(product));
-  config_file.readBytes(product, sizeof(product));
-  LOG_FILE.print("SCSI PRODUCT: ");
-  LOG_FILE.println(product);
-  memcpy(&(SCSI_INFO_BUF[16]), product, 16);
-
-  char version[5];
-  memset(version, 0, sizeof(version));
-  config_file.readBytes(version, sizeof(version));
-  LOG_FILE.print("SCSI VERSION: ");
-  LOG_FILE.println(version);
-  memcpy(&(SCSI_INFO_BUF[32]), version, 4);
-  config_file.close();
-}
-
-// read SD information and print to logfile
-void readSDCardInfo()
-{
-  cid_t sd_cid;
 
-  if(SD.card()->readCID(&sd_cid))
-  {
-    Serial.print("Sd MID:");
-    Serial.print(sd_cid.mid, 16);
-    Serial.print(" OID:");
-    Serial.print(sd_cid.oid[0]);
-    Serial.println(sd_cid.oid[1]);
-
-    Serial.print("Sd Name:");
-    Serial.print(sd_cid.pnm[0]);
-    Serial.print(sd_cid.pnm[1]);
-    Serial.print(sd_cid.pnm[2]);
-    Serial.print(sd_cid.pnm[3]);
-    Serial.println(sd_cid.pnm[4]);
-
-    Serial.print("Sd Date:");
-    Serial.print(sd_cid.mdt_month);
-    Serial.print("/20"); // CID year is 2000 + high/low
-    Serial.print(sd_cid.mdt_year_high);
-    Serial.println(sd_cid.mdt_year_low);
-
-    Serial.print("Sd Serial:");
-    Serial.println(sd_cid.psn);
-  }
-}
-void readSDCardInfo2()
-{
-  cid_t sd_cid;
 
-  if(SD.card()->readCID(&sd_cid))
-  {
-    LOG_FILE.print("Sd MID:");
-    LOG_FILE.print(sd_cid.mid, 16);
-    LOG_FILE.print(" OID:");
-    LOG_FILE.print(sd_cid.oid[0]);
-    LOG_FILE.println(sd_cid.oid[1]);
-
-    LOG_FILE.print("Sd Name:");
-    LOG_FILE.print(sd_cid.pnm[0]);
-    LOG_FILE.print(sd_cid.pnm[1]);
-    LOG_FILE.print(sd_cid.pnm[2]);
-    LOG_FILE.print(sd_cid.pnm[3]);
-    LOG_FILE.println(sd_cid.pnm[4]);
-
-    LOG_FILE.print("Sd Date:");
-    LOG_FILE.print(sd_cid.mdt_month);
-    LOG_FILE.print("/20"); // CID year is 2000 + high/low
-    LOG_FILE.print(sd_cid.mdt_year_high);
-    LOG_FILE.println(sd_cid.mdt_year_low);
-
-    LOG_FILE.print("Sd Serial:");
-    LOG_FILE.println(sd_cid.psn);
-    LOG_FILE.sync();
-  }
-}
 /*
  * Open HDD image file
  */
@@ -411,37 +282,19 @@ void setup()
 
 #endif
 
-  // PIN initialization
-  pinMode(BOARD_LED1_PIN , OUTPUT);
-  pinMode(BOARD_LED2_PIN , OUTPUT);
-  pinMode(BOARD_LED3_PIN , OUTPUT);
-  pinMode(BOARD_SDCARD_INSERT, INPUT_PULLUP);
-  pinMode(BOARD_SWITCH1_PIN , INPUT_PULLUP);
-  pinMode(BOARD_SWITCH2_PIN , INPUT_PULLUP);
-  pinMode(BOARD_SWITCH3_PIN , INPUT_PULLUP);
-  pinMode(BOARD_SWITCH4_PIN , INPUT_PULLUP);
-  pinMode(BOARD_SCSI_TERM_HIGH, OUTPUT);
-  pinMode(BOARD_SCSI_TERM_LOW, OUTPUT);
-  pinMode(BOARD_SCSI_TERM_POWER, INPUT); //Check if external termination power is provided
+  gpioInit();
 
   //Default turn termination off
   TERMINATION_HIGH();
 
   //Turn Power LED ON
   LED2_ON();
-
-  attachInterrupt(BOARD_SDCARD_INSERT,sdCardInsert, CHANGE);
-
   //Check if SD card is inserted
   sdCardInsert();
 
-  attachInterrupt(BOARD_SWITCH1_PIN,switchImage, CHANGE);
-  attachInterrupt(BOARD_SWITCH2_PIN,switchImage, CHANGE);
-  attachInterrupt(BOARD_SWITCH3_PIN,switchImage, CHANGE);
-  attachInterrupt(BOARD_SWITCH4_PIN,switchImage, CHANGE);
-
   //Check which image to load
   switchImage();
+
   /*
   // set up OTYPER for open drain on SCSI pins of PA and PB
   // PA 0-7, 11-14
@@ -456,23 +309,7 @@ void setup()
   uint32_t oTypeB_Or = 0x0000F7FD;
   GPIOB->regs->OTYPER = (GPIOB->regs->OTYPER & oTypeB_And) | oTypeB_Or;
 */
-  pinMode(BOARD_SCSI_ATN, INPUT_PULLUP);
-  pinMode(BOARD_SCSI_BSY, INPUT_PULLUP);
-  pinMode(BOARD_SCSI_ACK, INPUT_PULLUP);
-  pinMode(BOARD_SCSI_RST, INPUT_PULLUP);
-  pinMode(BOARD_SCSI_SEL, INPUT_PULLUP);
-
-  // Output port
-  pinMode(BOARD_SCSI_MSG, OUTPUT_OPEN_DRAIN);
-  pinMode(BOARD_SCSI_CD, OUTPUT_OPEN_DRAIN);
-  pinMode(BOARD_SCSI_REQ, OUTPUT_OPEN_DRAIN);
-  pinMode(BOARD_SCSI_IO, OUTPUT_OPEN_DRAIN);
-
-  // Control SCSI drivers
-  pinMode(BOARD_SCSI_DTD, OUTPUT_OPEN_DRAIN);
-  pinMode(BOARD_SCSI_IND, OUTPUT_OPEN_DRAIN);
-  pinMode(BOARD_SCSI_TAD, OUTPUT_OPEN_DRAIN);
-  pinMode(BOARD_TRANS_OE, OUTPUT_OPEN_DRAIN);
+
 /*
   // Turn off the output port
   SCSI_TARGET_INACTIVE()
@@ -495,9 +332,6 @@ void setup()
   readSCSIDeviceConfig();
   initFileLog();
  
-
-
-
   //Sector data overrun byte setting
   m_buf[MAX_BLOCKSIZE] = 0xff; // DB0 all off,DBP off
   //HD image file open
@@ -589,62 +423,9 @@ void setup()
   attachInterrupt(BOARD_SCSI_RST, onBusReset, FALLING);
 }
 
-/*
- * Setup initialization logfile
- */
-void initFileLog() {
-  LOG_FILE = SD.open(LOG_FILENAME, O_WRONLY | O_CREAT | O_TRUNC);
-  LOG_FILE.println("BlackSASI F411 <-> SD ");
-  LOG_FILE.print("VERSION: ");
-  LOG_FILE.println(VERSION);
-  LOG_FILE.print("DEBUG:");
-  LOG_FILE.print(DEBUG);
-  LOG_FILE.print(" SCSI_SELECT:");
-  LOG_FILE.print(SCSI_SELECT);
-  LOG_FILE.print(" SDFAT_FILE_TYPE:");
-  LOG_FILE.println(SDFAT_FILE_TYPE);
-  LOG_FILE.print("SdFat version: ");
-  LOG_FILE.println(SD_FAT_VERSION_STR);
-  LOG_FILE.print("SdFat Max FileName Length: ");
-  LOG_FILE.println(MAX_FILE_PATH);
-  LOG_FILE.println("Initialized SD Card - lets go!");
-  LOG_FILE.sync();
-}
 
-/*
- * Finalize initialization logfile
- */
-void finalizeFileLog() {
-  // View support drive map
-  LOG_FILE.print("ID");
-  for(int lun=0;lun<NUM_SCSILUN;lun++)
-  {
-    LOG_FILE.print(":LUN");
-    LOG_FILE.print(lun);
-  }
-  LOG_FILE.println(":");
-  //
-  for(int id=0;id<NUM_SCSIID;id++)
-  {
-    LOG_FILE.print(" ");
-    LOG_FILE.print(id);
-    for(int lun=0;lun<NUM_SCSILUN;lun++)
-    {
-      HDDIMG *h = &img[id][lun];
-      if( (lun<NUM_SCSILUN) && (h->m_file))
-      {
-        LOG_FILE.print((h->m_blocksize<1000) ? ": " : ":");
-        LOG_FILE.print(h->m_blocksize);
-      }
-      else      
-        LOG_FILE.print(":----");
-    }
-    LOG_FILE.println(":");
-  }
-  LOG_FILE.println("Finished initialization of SCSI Devices - Entering main loop.");
-  LOG_FILE.sync();
-  LOG_FILE.close();
-}
+
+
 
 /*
  * Initialization failed, blink 3x fast
@@ -663,28 +444,7 @@ void onFalseInit(void)
   }
 }
 
-/*
- * No SC Card found, blink 5x fast
- */
-void noSDCardFound(void)
-{
-  while(!SD.begin(SdSpiConfig(SS, DEDICATED_SPI))) {
-    for(int i = 0; i < 5; i++) {
-      LED3_ON();
-      delay(500);
-      LED3_OFF();
-      delay(500);
-    }
-    delay(1000);
-  }
-}
-void sdCardInsert(void){
-  if (digitalRead(BOARD_SDCARD_INSERT)){
-    LED1_OFF();
-  }else{
-    LED1_ON();
-  }
-}
+
 void switchImage(void){
 
     uint16_t oldimageSelect = imageSelect;

+ 88 - 0
sw/src/sdcard.cpp

@@ -0,0 +1,88 @@
+#include "sdcard.h"
+extern SdFs SD;
+extern FsFile LOG_FILE;
+
+// read SD information and print to logfile
+void readSDCardInfo()
+{
+  cid_t sd_cid;
+
+  if(SD.card()->readCID(&sd_cid))
+  {
+    Serial.print("Sd MID:");
+    Serial.print(sd_cid.mid, 16);
+    Serial.print(" OID:");
+    Serial.print(sd_cid.oid[0]);
+    Serial.println(sd_cid.oid[1]);
+
+    Serial.print("Sd Name:");
+    Serial.print(sd_cid.pnm[0]);
+    Serial.print(sd_cid.pnm[1]);
+    Serial.print(sd_cid.pnm[2]);
+    Serial.print(sd_cid.pnm[3]);
+    Serial.println(sd_cid.pnm[4]);
+
+    Serial.print("Sd Date:");
+    Serial.print(sd_cid.mdt_month);
+    Serial.print("/20"); // CID year is 2000 + high/low
+    Serial.print(sd_cid.mdt_year_high);
+    Serial.println(sd_cid.mdt_year_low);
+
+    Serial.print("Sd Serial:");
+    Serial.println(sd_cid.psn);
+  }
+}
+void readSDCardInfo2()
+{
+  cid_t sd_cid;
+
+  if(SD.card()->readCID(&sd_cid))
+  {
+    LOG_FILE.print("Sd MID:");
+    LOG_FILE.print(sd_cid.mid, 16);
+    LOG_FILE.print(" OID:");
+    LOG_FILE.print(sd_cid.oid[0]);
+    LOG_FILE.println(sd_cid.oid[1]);
+
+    LOG_FILE.print("Sd Name:");
+    LOG_FILE.print(sd_cid.pnm[0]);
+    LOG_FILE.print(sd_cid.pnm[1]);
+    LOG_FILE.print(sd_cid.pnm[2]);
+    LOG_FILE.print(sd_cid.pnm[3]);
+    LOG_FILE.println(sd_cid.pnm[4]);
+
+    LOG_FILE.print("Sd Date:");
+    LOG_FILE.print(sd_cid.mdt_month);
+    LOG_FILE.print("/20"); // CID year is 2000 + high/low
+    LOG_FILE.print(sd_cid.mdt_year_high);
+    LOG_FILE.println(sd_cid.mdt_year_low);
+
+    LOG_FILE.print("Sd Serial:");
+    LOG_FILE.println(sd_cid.psn);
+    LOG_FILE.sync();
+  }
+}
+
+/*
+ * No SC Card found, blink 5x fast
+ */
+void noSDCardFound(void)
+{
+  while(!SD.begin(SdSpiConfig(SS, DEDICATED_SPI))) {
+    for(int i = 0; i < 5; i++) {
+      LED3_ON();
+      delay(500);
+      LED3_OFF();
+      delay(500);
+    }
+    delay(1000);
+  }
+}
+
+void sdCardInsert(void){
+  if (digitalRead(BOARD_SDCARD_INSERT)){
+    LED1_OFF();
+  }else{
+    LED1_ON();
+  }
+}