Browse Source

Fixed SCSI issues. It should now be stable on all systems.

Michael McMaster 9 years ago
parent
commit
3cd303082a

+ 3 - 0
lib/SCSI2SD/CHANGELOG

@@ -1,3 +1,6 @@
+20160716		6.03 (BETA3)
+	- SCSI bug fixes.
+
 20160616		6.01
 	- Improved SD card compatibility
 	- Fixed SCSI interfaces on slower SCSI controllers

+ 3 - 0
lib/SCSI2SD/Makefile

@@ -131,6 +131,7 @@ USBCOMPOSITE_SRC= \
 
 SRC = \
 	src/firmware/bootloader.c \
+	src/firmware/cdrom.c \
 	src/firmware/config.c \
 	src/firmware/disk.c \
 	src/firmware/diagnostic.c \
@@ -140,10 +141,12 @@ SRC = \
 	src/firmware/inquiry.c \
 	src/firmware/led.c \
 	src/firmware/main.c \
+	src/firmware/mo.c \
 	src/firmware/mode.c \
 	src/firmware/scsiPhy.c \
 	src/firmware/scsi.c \
 	src/firmware/sd.c \
+	src/firmware/tape.c \
 	src/firmware/time.c \
 	src/firmware/trace.c \
 	${USBCOMPOSITE_SRC}

+ 2 - 0
lib/SCSI2SD/STM32CubeMX/SCSI2SD-V6/Src/fsmc.c

@@ -66,11 +66,13 @@ void MX_FSMC_Init(void)
   hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
   hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
   /* Timing */
+
 /*
   Timing.AddressSetupTime = 3;
   Timing.AddressHoldTime = 2;
   Timing.DataSetupTime = 5;
 */
+
   Timing.AddressSetupTime = 2;
   Timing.AddressHoldTime = 1;
   Timing.DataSetupTime = 4;

+ 3 - 3
lib/SCSI2SD/STM32CubeMX/SCSI2SD-V6/Src/main.c

@@ -92,8 +92,8 @@ int main(void)
   MX_SDIO_SD_Init();
   MX_SPI1_Init();
   MX_TIM4_Init();
-  MX_USART3_UART_Init();
-  MX_USB_HOST_Init();
+  // TODO re-enable MX_USART3_UART_Init();
+  // TODO re-enable MX_USB_HOST_Init();
 
   /* USER CODE BEGIN 2 */
   mainInit();
@@ -105,7 +105,7 @@ int main(void)
   while (1)
   {
   /* USER CODE END WHILE */
-    MX_USB_HOST_Process();
+    // TODO re-enable MX_USB_HOST_Process();
 
   /* USER CODE BEGIN 3 */
   mainLoop();

BIN
lib/SCSI2SD/rtl/fpga_bitmap.o


+ 3 - 5
lib/SCSI2SD/src/firmware/cdrom.c

@@ -14,14 +14,13 @@
 //
 //	You should have received a copy of the GNU General Public License
 //	along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.
-#pragma GCC push_options
-#pragma GCC optimize("-flto")
 
-#include "device.h"
 #include "scsi.h"
 #include "config.h"
 #include "cdrom.h"
 
+#include <string.h>
+
 static const uint8_t SimpleTOC[] =
 {
 	0x00, // toc length, MSB
@@ -276,7 +275,7 @@ int scsiCDRomCommand()
 {
 	int commandHandled = 1;
 
-	uint8 command = scsiDev.cdb[0];
+	uint8_t command = scsiDev.cdb[0];
 	if (command == 0x43)
 	{
 		// CD-ROM Read TOC
@@ -323,4 +322,3 @@ int scsiCDRomCommand()
 	return commandHandled;
 }
 
-#pragma GCC pop_options

+ 3 - 112
lib/SCSI2SD/src/firmware/config.c

@@ -16,7 +16,6 @@
 //	along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "config.h"
-#include "debug.h"
 #include "led.h"
 
 #include "scsi.h"
@@ -38,7 +37,7 @@
 
 #include <string.h>
 
-static const uint16_t FIRMWARE_VERSION = 0x0601;
+static const uint16_t FIRMWARE_VERSION = 0x0603;
 
 // 1 flash row
 static const uint8_t DEFAULT_CONFIG[128] =
@@ -176,7 +175,7 @@ debugCommand()
 	response[15] = scsiDev.lastSense;
 	response[16] = scsiDev.phase;
 	response[17] = scsiStatusBSY();
-	response[18] = *SCSI_STS_SELECTED;
+	response[18] = scsiStatusSEL();
 	response[19] = scsiStatusATN();
 	response[20] = scsiStatusRST();
 	response[21] = scsiDev.rstCount;
@@ -189,6 +188,7 @@ debugCommand()
 	response[28] = scsiDev.lastSenseASC;
 	response[29] = *SCSI_STS_DBX;
 	response[30] = LastTrace;
+	response[31] = scsiStatusACK();
 	hidPacket_send(response, sizeof(response));
 }
 
@@ -330,116 +330,7 @@ void s2s_configPoll()
 
 }
 
-void debugPoll()
-{
-#if 0
-	if (!usbReady)
-	{
-		return;
-	}
-
-	if(USBFS_GetEPState(USB_EP_COMMAND) == USBFS_OUT_BUFFER_FULL)
-	{
-		// The host sent us some data!
-		int byteCount = USBFS_GetEPCount(USB_EP_COMMAND);
-		USBFS_ReadOutEP(USB_EP_COMMAND, (uint8 *)&hidBuffer, byteCount);
-
-		if (byteCount >= 1 &&
-			hidBuffer[0] == 0x01)
-		{
-			// Reboot command.
-			Bootloadable_1_Load();
-		}
-
-		// Allow the host to send us another command.
-		// (assuming we didn't reboot outselves)
-		USBFS_EnableOutEP(USB_EP_COMMAND);
-	}
-
-	switch (usbDebugEpState)
-	{
-	case USB_IDLE:
-		memcpy(&hidBuffer, &scsiDev.cdb, 12);
-		hidBuffer[12] = scsiDev.msgIn;
-		hidBuffer[13] = scsiDev.msgOut;
-		hidBuffer[14] = scsiDev.lastStatus;
-		hidBuffer[15] = scsiDev.lastSense;
-		hidBuffer[16] = scsiDev.phase;
-		hidBuffer[17] = SCSI_ReadFilt(SCSI_Filt_BSY);
-		hidBuffer[18] = SCSI_ReadFilt(SCSI_Filt_SEL);
-		hidBuffer[19] = SCSI_ReadFilt(SCSI_Filt_ATN);
-		hidBuffer[20] = SCSI_ReadFilt(SCSI_Filt_RST);
-		hidBuffer[21] = scsiDev.rstCount;
-		hidBuffer[22] = scsiDev.selCount;
-		hidBuffer[23] = scsiDev.msgCount;
-		hidBuffer[24] = scsiDev.cmdCount;
-		hidBuffer[25] = scsiDev.watchdogTick;
-		hidBuffer[26] = blockDev.state;
-		hidBuffer[27] = scsiDev.lastSenseASC >> 8;
-		hidBuffer[28] = scsiDev.lastSenseASC;
-		hidBuffer[29] = scsiReadDBxPins();
-		hidBuffer[30] = LastTrace;
-
-		hidBuffer[58] = sdDev.capacity >> 24;
-		hidBuffer[59] = sdDev.capacity >> 16;
-		hidBuffer[60] = sdDev.capacity >> 8;
-		hidBuffer[61] = sdDev.capacity;
-
-		hidBuffer[62] = FIRMWARE_VERSION >> 8;
-		hidBuffer[63] = FIRMWARE_VERSION;
-
-		USBFS_LoadInEP(USB_EP_DEBUG, (uint8 *)&hidBuffer, sizeof(hidBuffer));
-		usbDebugEpState = USB_DATA_SENT;
-		break;
-
-	case USB_DATA_SENT:
-		if (USBFS_bGetEPAckState(USB_EP_DEBUG))
-		{
-			// Data accepted.
-			usbDebugEpState = USB_IDLE;
-		}
-		break;
-	}
-#endif
-}
-
-#if 0
-CY_ISR(debugTimerISR)
-{
-	Debug_Timer_ReadStatusRegister();
-	Debug_Timer_Interrupt_ClearPending();
-	uint8 savedIntrStatus = CyEnterCriticalSection();
-	debugPoll();
-	CyExitCriticalSection(savedIntrStatus);
-}
-#endif
-
-void s2s_debugInit()
-{
-#if 0
-	Debug_Timer_Interrupt_StartEx(debugTimerISR);
-	Debug_Timer_Start();
-#endif
-}
 
-void debugPause()
-{
-#if 0
-	Debug_Timer_Stop();
-#endif
-}
-
-void debugResume()
-{
-#if 0
-	Debug_Timer_Start();
-#endif
-}
-
-int isDebugEnabled()
-{
-	return usbReady;
-}
 
 // Public method for storing MODE SELECT results.
 void s2s_configSave(int scsiId, uint16_t bytesPerSector)

+ 0 - 26
lib/SCSI2SD/src/firmware/debug.h

@@ -1,26 +0,0 @@
-//	Copyright (C) 2015 Michael McMaster <michael@codesrc.com>
-//
-//	This file is part of SCSI2SD.
-//
-//	SCSI2SD is free software: you can redistribute it and/or modify
-//	it under the terms of the GNU General Public License as published by
-//	the Free Software Foundation, either version 3 of the License, or
-//	(at your option) any later version.
-//
-//	SCSI2SD is distributed in the hope that it will be useful,
-//	but WITHOUT ANY WARRANTY; without even the implied warranty of
-//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//	GNU General Public License for more details.
-//
-//	You should have received a copy of the GNU General Public License
-//	along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.
-#ifndef Debug_H
-#define Debug_H
-
-void debugInit(void);
-void debugPause(void);
-void debugResume(void);
-int isDebugEnabled(void);
-
-#endif
-

+ 11 - 2
lib/SCSI2SD/src/firmware/disk.c

@@ -19,7 +19,6 @@
 #include "scsi.h"
 #include "scsiPhy.h"
 #include "config.h"
-#include "debug.h"
 #include "disk.h"
 #include "sd.h"
 #include "time.h"
@@ -597,6 +596,15 @@ void scsiDiskPoll()
 				}
 				sdActive = 1;
 			}
+#endif
+#if 0
+			uint32_t maxSectors = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
+			uint32_t rem = totalSDSectors - i;
+			uint32_t sectors =
+				rem < maxSectors ? rem : maxSectors;
+			sdTmpRead(&scsiDev.data[0], i + sdLBA, sectors);
+			scsiWrite(&scsiDev.data[0], sectors * SD_SECTOR_SIZE);
+			i += sectors;
 #endif
 			if ((prep - i < buffers) &&
 				(prep < totalSDSectors))
@@ -673,8 +681,9 @@ void scsiDiskPoll()
 			// do this in a half-duplex fashion. We need to write as much as
 			// possible in each SD card transaction.
 			uint32_t maxSectors = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
+			uint32_t rem = totalSDSectors - i;
 			uint32_t sectors =
-				totalSDSectors < maxSectors ? totalSDSectors : maxSectors;
+				rem < maxSectors ? rem : maxSectors;
 			scsiRead(&scsiDev.data[0], sectors * SD_SECTOR_SIZE);
 			sdTmpWrite(&scsiDev.data[0], i + sdLBA, sectors);
 			i += sectors;

+ 0 - 3
lib/SCSI2SD/src/firmware/main.c

@@ -55,9 +55,6 @@ void mainInit()
 	scsiPhyConfig();
 	scsiInit();
 
-	s2s_debugInit();
-
-
 	MX_USB_DEVICE_Init(); // USB lun config now available.
 
 	// Optional bootup delay

+ 1 - 5
lib/SCSI2SD/src/firmware/mo.c

@@ -14,10 +14,7 @@
 //
 //	You should have received a copy of the GNU General Public License
 //	along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.
-#pragma GCC push_options
-#pragma GCC optimize("-flto")
 
-#include "device.h"
 #include "scsi.h"
 #include "config.h"
 #include "mo.h"
@@ -28,7 +25,7 @@ int scsiMOCommand()
 {
 	int commandHandled = 0;
 
-	uint8 command = scsiDev.cdb[0];
+	uint8_t command = scsiDev.cdb[0];
 	if ((command == 0x2C) || // ERASE(10)
 		(command == 0xAC)) // ERASE(12)
 	{
@@ -40,4 +37,3 @@ int scsiMOCommand()
 	return commandHandled;
 }
 
-#pragma GCC pop_options

+ 6 - 13
lib/SCSI2SD/src/firmware/scsi.c

@@ -25,10 +25,10 @@
 #include "mode.h"
 #include "time.h"
 #include "bsp.h"
-//#include "cdrom.h"
+#include "cdrom.h"
 //#include "debug.h"
-//#include "tape.h"
-//#include "mo.h"
+#include "tape.h"
+#include "mo.h"
 
 #include <string.h>
 
@@ -157,7 +157,6 @@ void process_Status()
 	scsiDev.lastSense = scsiDev.target->sense.code;
 	scsiDev.lastSenseASC = scsiDev.target->sense.asc;
 
-
 	// Command Complete occurs AFTER a valid status has been
 	// sent. then we go bus-free.
 	enter_MessageIn(message);
@@ -263,7 +262,7 @@ static void process_Command()
 	control = scsiDev.cdb[scsiDev.cdbLen - 1];
 
 	scsiDev.cmdCount++;
-	// TODO const S2S_TargetCfg* cfg = scsiDev.target->cfg;
+	const S2S_TargetCfg* cfg = scsiDev.target->cfg;
 
 	if (unlikely(scsiDev.resetFlag))
 	{
@@ -352,7 +351,6 @@ static void process_Command()
 	{
 		enter_Status(CONFLICT);
 	}
-#if 0
 	// Handle odd device types first that may override basic read and
 	// write commands. Will fall-through to generic disk handling.
 	else if (((cfg->deviceType == S2S_CFG_OPTICAL) && scsiCDRomCommand()) ||
@@ -361,7 +359,6 @@ static void process_Command()
 	{
 		// Already handled.
 	}
-#endif
 	else if (scsiDiskCommand())
 	{
 		// Already handled.
@@ -605,13 +602,9 @@ static void process_SelectionPhase()
 		// SCSI1/SASI initiators may not set their own ID.
 		scsiDev.initiatorId = (selStatus >> 3) & 0x7;
 
-		// Wait until the end of the selection phase.
-		while (likely(!scsiDev.resetFlag))
+		while (likely(!scsiDev.resetFlag) && scsiStatusSEL())
 		{
-			if ((*SCSI_STS_SELECTED & 0x40) == 0)
-			{
-				break;
-			}
+			// Wait until the end of the selection phase.
 		}
 
 		scsiDev.phase = COMMAND;

+ 86 - 27
lib/SCSI2SD/src/firmware/scsiPhy.c

@@ -76,21 +76,16 @@ void EXTI4_IRQHandler()
 	}
 }
 
-#if 0
-uint8_t
-scsiReadDBxPins()
+static void assertFail()
 {
-	return
-		(SCSI_ReadPin(SCSI_In_DBx_DB7) << 7) |
-		(SCSI_ReadPin(SCSI_In_DBx_DB6) << 6) |
-		(SCSI_ReadPin(SCSI_In_DBx_DB5) << 5) |
-		(SCSI_ReadPin(SCSI_In_DBx_DB4) << 4) |
-		(SCSI_ReadPin(SCSI_In_DBx_DB3) << 3) |
-		(SCSI_ReadPin(SCSI_In_DBx_DB2) << 2) |
-		(SCSI_ReadPin(SCSI_In_DBx_DB1) << 1) |
-		SCSI_ReadPin(SCSI_In_DBx_DB0);
+	while (1)
+	{
+		s2s_ledOn();
+		s2s_delay_ms(100);
+		s2s_ledOff();
+		s2s_delay_ms(100);
+	}
 }
-#endif
 
 static void
 startScsiRx(uint32_t count)
@@ -103,6 +98,12 @@ startScsiRx(uint32_t count)
 uint8_t
 scsiReadByte(void)
 {
+#if FIFODEBUG
+	if (!scsiPhyFifoAltEmpty()) {
+		// Force a lock-up.
+		assertFail();
+	}
+#endif
 	startScsiRx(1);
 
 	trace(trace_spinPhyRxFifo);
@@ -111,6 +112,16 @@ scsiReadByte(void)
 	uint8_t val = scsiPhyRx();
 	// TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
 
+#if FIFODEBUG
+	if (!scsiPhyFifoEmpty()) {
+		int j = 0;
+		uint8_t k __attribute((unused));
+		while (!scsiPhyFifoEmpty()) { k = scsiPhyRx(); ++j; }
+
+		// Force a lock-up.
+		assertFail();
+	}
+#endif
 	return val;
 }
 
@@ -176,6 +187,13 @@ scsiRead(uint8_t* data, uint32_t count)
 			chunk = chunk & 0xFFFFFFF8;
 		}
 
+#if FIFODEBUG
+		if (!scsiPhyFifoAltEmpty()) {
+			// Force a lock-up.
+			assertFail();
+		}
+#endif
+
 		startScsiRx(chunk);
 		// Wait for the next scsi interrupt (or the 1ms systick)
 		__WFI();
@@ -208,6 +226,14 @@ scsiRead(uint8_t* data, uint32_t count)
 			};
 		}
 
+#if FIFODEBUG
+		if (!scsiPhyFifoEmpty()) {
+			int j = 0;
+			while (!scsiPhyFifoEmpty()) { scsiPhyRx(); ++j; }
+			// Force a lock-up.
+			assertFail();
+		}
+#endif
 		i += chunk;
 	}
 }
@@ -215,12 +241,25 @@ scsiRead(uint8_t* data, uint32_t count)
 void
 scsiWriteByte(uint8_t value)
 {
+#if FIFODEBUG
+	if (!scsiPhyFifoEmpty()) {
+		// Force a lock-up.
+		assertFail();
+	}
+#endif
 	trace(trace_spinPhyTxFifo);
 	scsiPhyTx(value);
 	scsiPhyFifoFlip();
 
 	trace(trace_spinTxComplete);
 	while (!scsiPhyComplete() && likely(!scsiDev.resetFlag)) {}
+
+#if FIFODEBUG
+	if (!scsiPhyFifoAltEmpty()) {
+		// Force a lock-up.
+		assertFail();
+	}
+#endif
 }
 
 static void
@@ -277,6 +316,13 @@ scsiWrite(const uint8_t* data, uint32_t count)
 		uint32_t chunk = ((count - i) > SCSI_FIFO_DEPTH)
 			? SCSI_FIFO_DEPTH : (count - i);
 
+#if FIFODEBUG
+		if (!scsiPhyFifoEmpty()) {
+			// Force a lock-up.
+			assertFail();
+		}
+#endif
+
 		if (chunk < 16)
 		{
 			scsiWritePIO(data + i, chunk);
@@ -305,6 +351,14 @@ scsiWrite(const uint8_t* data, uint32_t count)
 		}
 
 		while (!scsiPhyComplete() && likely(!scsiDev.resetFlag)) {}
+
+#if FIFODEBUG
+		if (!scsiPhyFifoAltEmpty()) {
+			// Force a lock-up.
+			assertFail();
+		}
+#endif
+
 		scsiPhyFifoFlip();
 #if 0
 // TODO NEED SCSI IRQs
@@ -314,6 +368,13 @@ scsiWrite(const uint8_t* data, uint32_t count)
 		i += chunk;
 	}
 	while (!scsiPhyComplete() && likely(!scsiDev.resetFlag)) {}
+
+#if FIFODEBUG
+	if (!scsiPhyFifoAltEmpty()) {
+		// Force a lock-up.
+		assertFail();
+	}
+#endif
 }
 
 static inline void busSettleDelay(void)
@@ -325,22 +386,25 @@ static inline void busSettleDelay(void)
 
 void scsiEnterBusFree()
 {
-	*SCSI_CTRL_PHASE = 0;
 	*SCSI_CTRL_BSY = 0x00;
+	// We now have a Bus Clear Delay of 800ns to release remaining signals.
+	*SCSI_CTRL_PHASE = 0;
 }
 
 void scsiEnterPhase(int phase)
 {
 	// ANSI INCITS 362-2002 SPI-3 10.7.1:
 	// Phase changes are not allowed while REQ or ACK is asserted.
-#if 0
-	while (likely(!scsiDev.resetFlag) &&
-		(SCSI_ReadPin(SCSI_In_REQ) || SCSI_ReadFilt(SCSI_Filt_ACK))
-		) {}
-#endif
+	while (likely(!scsiDev.resetFlag) && scsiStatusACK()) {}
 
 	int newPhase = phase > 0 ? phase : 0;
-	if (newPhase != *SCSI_CTRL_PHASE)
+	int oldPhase = *SCSI_CTRL_PHASE;
+
+	if (!scsiPhyFifoEmpty() || !scsiPhyFifoAltEmpty()) {
+		// Force a lock-up.
+		assertFail();
+	}
+	if (newPhase != oldPhase)
 	{
 		*SCSI_CTRL_PHASE = newPhase;
 		busSettleDelay();
@@ -349,6 +413,7 @@ void scsiEnterPhase(int phase)
 		{
 			s2s_delay_us(100);
 		}
+
 	}
 }
 
@@ -466,13 +531,7 @@ void scsiPhyReset()
 		{
 			if (scsiDev.data[j] != (uint8_t) j)
 			{
-				while (1)
-				{
-					s2s_ledOn();
-					s2s_delay_ms(100);
-					s2s_ledOff();
-					s2s_delay_ms(100);
-				}
+				assertFail();
 			}
 		}
 

+ 3 - 0
lib/SCSI2SD/src/firmware/scsiPhy.h

@@ -38,6 +38,7 @@
 
 #define scsiPhyFifoFull() ((*SCSI_STS_FIFO & 0x01) == 0x01)
 #define scsiPhyFifoEmpty() ((*SCSI_STS_FIFO & 0x02) == 0x02)
+#define scsiPhyFifoAltEmpty() ((*SCSI_STS_ALTFIFO & 0x02) == 0x02)
 
 #define scsiPhyFifoFlip() \
 {\
@@ -52,6 +53,8 @@
 #define scsiStatusATN() ((*SCSI_STS_SCSI & 0x01) == 0x01)
 #define scsiStatusBSY() ((*SCSI_STS_SCSI & 0x02) == 0x02)
 #define scsiStatusRST() ((*SCSI_STS_SCSI & 0x04) == 0x04)
+#define scsiStatusSEL() ((*SCSI_STS_SCSI & 0x08) == 0x08)
+#define scsiStatusACK() ((*SCSI_STS_SCSI & 0x10) == 0x10)
 
 extern uint8_t scsiPhyFifoSel;
 

+ 1 - 1
lib/SCSI2SD/src/firmware/sd.c

@@ -943,7 +943,7 @@ int sdInit()
 		sdInitDMA();
 	}
 
-	if (scsiDev.phase == BUS_FREE)
+	if (firstInit || (scsiDev.phase == BUS_FREE))
 	{
 		uint8_t cs = HAL_GPIO_ReadPin(nSD_CD_GPIO_Port, nSD_CD_Pin) ? 0 : 1;
 		uint8_t wp = HAL_GPIO_ReadPin(nSD_WP_GPIO_Port, nSD_WP_Pin) ? 0 : 1;

+ 0 - 4
lib/SCSI2SD/src/firmware/tape.c

@@ -14,10 +14,7 @@
 //
 //	You should have received a copy of the GNU General Public License
 //	along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.
-#pragma GCC push_options
-#pragma GCC optimize("-flto")
 
-#include "device.h"
 #include "scsi.h"
 #include "config.h"
 #include "tape.h"
@@ -30,4 +27,3 @@ int scsiTapeCommand()
 	return 0;
 }
 
-#pragma GCC pop_options

+ 42 - 5
lib/SCSI2SD/src/scsi2sd-util6/Makefile

@@ -1,4 +1,4 @@
-VPATH=../firmware
+VPATH=../firmware dfu-programmer/src
 
 VERSION=6.0
 NAME=scsi2sd-util6
@@ -7,6 +7,15 @@ ifeq ($(USE_SYSTEM), Yes)
 USE_SYSTEM_HIDAPI = Yes
 USE_SYSTEM_ZLIB = Yes
 USE_SYSTEM_WX = Yes
+USE_SYSTEM_LIBUSB = Yes
+endif
+
+ifeq ($(USE_SYSTEM_LIBUSB), Yes)
+CPPFLAGS_LIBUSB=$(shell pkg-config libusb-1.0 --cflags)
+LDFLAGS_LIBUSB=$(shell pkg-config libusb-1.0 --libs)
+else
+CPPFLAGS_LIBUSB=-I libusb-1.0.9/libusb
+LDFLAGS_LIBUSB=-L $(BUILD)/libusb/libusb/.libs -lusb-1.0
 endif
 
 ifeq ($(USE_SYSTEM_HIDAPI), Yes)
@@ -33,11 +42,20 @@ else
 LDFLAGS_WX=$(shell $(BUILD)/wx/wx-config --libs)
 endif
 
-CPPFLAGS = $(CPPFLAGS_HIDAPI) -I. -I ../../include -Ilibzipper-1.0.4 $(CPPFLAGS_ZLIB)
+CPPFLAGS = $(CPPFLAGS_HIDAPI) -I. -I ../../include -Idfu-programmer/src \
+	-Ilibzipper-1.0.4 \
+	$(CPPFLAGS_ZLIB) \
+	$(CPPFLAGS_LIBUSB) \
+	-DHAVE_LIBUSB_1_0 \
+
+
 CFLAGS += -Wall -Wno-pointer-sign -O2 -g
 CXXFLAGS += -Wall -O2 -g -std=c++0x
 
-LDFLAGS += -L$(BUILD)/libzipper/.libs -lzipper $(LDFLAGS_ZLIB) $(LDFLAGS_HIDAPI)
+LDFLAGS += -L$(BUILD)/libzipper/.libs -lzipper \
+	$(LDFLAGS_ZLIB) \
+	$(LDFLAGS_HIDAPI) \
+	$(LDFLAGS_LIBUSB)
 
 
 # wxWidgets 3.0.2 uses broken Webkit headers under OSX Yosemeti
@@ -56,6 +74,7 @@ ifeq ($(TARGET),Win32)
 	LIBZIPPER_CONFIG+=--host=i686-w64-mingw32
 	EXE=.exe
 	WX_CONFIG+=--host=i686-w64-mingw32
+	LIBUSB_CONFIG+=--host=i686-w64-mingw32 --disable-shared
 endif
 ifeq ($(TARGET),Win64)
 	VPATH += hidapi/windows
@@ -66,12 +85,13 @@ ifeq ($(TARGET),Win64)
 	LIBZIPPER_CONFIG+=--host=x86_64-w64-mingw32
 	EXE=.exe
 	WX_CONFIG+=--host=x86_64-w64-mingw32
+	LIBUSB_CONFIG+=--host=x86_64-w64-mingw32 --disable-shared
 endif
 ifeq ($(TARGET),Linux)
 	VPATH += hidapi/linux
-	CPPFLAGS += -DHAS_LIBUSB
 	LDFLAGS += -ludev -lexpat -lusb-1.0
 	BUILD := $(PWD)/build/linux
+	LIBUSB_CONFIG+=--disable-shared
 endif
 ifeq ($(TARGET),Darwin)
 	# Should match OSX
@@ -80,6 +100,7 @@ ifeq ($(TARGET),Darwin)
 	CC=clang -mmacosx-version-min=10.7
 	CXX=clang++ -stdlib=libc++ -mmacosx-version-min=10.7
 	WX_CONFIG += --with-macosx-version-min=10.7
+	LIBUSB_CONFIG += --with-macosx-version-min=10.7 --disable-shared
 	BUILD := $(PWD)/build/mac
 all: $(BUILD)/scsi2sd-util6.dmg
 
@@ -109,11 +130,14 @@ OBJ = \
 	$(BUILD)/SCSI2SD_HID.o \
 	$(BUILD)/hidpacket.o \
 
+#	$(BUILD)/Dfu.o \
+#	$(BUILD)/dfu.o \
+
+
 EXEOBJ = \
 	$(BUILD)/scsi2sd-util6.o \
 
 
-
 ifneq ($(USE_SYSTEM_ZLIB),Yes)
 $(OBJ): $(BUILD)/zlib/buildstamp
 $(EXEOBJ): $(BUILD)/zlib/buildstamp
@@ -128,6 +152,19 @@ $(BUILD)/zlib/buildstamp:
 	touch $@
 endif
 
+ifneq ($(USE_SYSTEM_LIBUSB),Yes)
+$(OBJ): $(BUILD)/libusb/buildstamp
+$(EXEOBJ): $(BUILD)/libusb/buildstamp
+$(BUILD)/libusb/buildstamp:
+	mkdir -p $(dir $@)
+	( \
+		cd $(dir $@) && \
+		$(CURDIR)/libusb-1.0.9/configure $(LIBUSB_CONFIG) && \
+		$(MAKE) \
+	) && \
+	touch $@
+endif
+
 ifneq ($(USE_SYSTEM_WX),Yes)
 $(OBJ): $(BUILD)/wx/buildstamp
 $(EXEOBJ): $(BUILD)/wx/buildstamp

+ 3 - 21
lib/SCSI2SD/src/scsi2sd-util6/SCSI2SD_HID.cc

@@ -138,27 +138,9 @@ HID::writeSector(uint32_t sector, const std::vector<uint8_t>& in)
 bool
 HID::readSCSIDebugInfo(std::vector<uint8_t>& buf)
 {
-#if 0
-	buf[0] = 0; // report id
-	hid_set_nonblocking(myDebugHandle, 1);
-	int result =
-		hid_read_timeout(
-			myDebugHandle,
-			&buf[0],
-			HID_PACKET_SIZE,
-			HID_TIMEOUT_MS);
-	hid_set_nonblocking(myDebugHandle, 0);
-
-	if (result <= 0)
-	{
-		const wchar_t* err = hid_error(myDebugHandle);
-		std::stringstream ss;
-		ss << "USB HID read failure: " << err;
-		throw std::runtime_error(ss.str());
-	}
-	return result > 0;
-#endif
-	return false;
+	std::vector<uint8_t> cmd { S2S_CMD_DEBUG };
+	sendHIDPacket(cmd, buf, 1);
+	return buf.size() > 0;
 }
 
 

+ 7 - 37
lib/SCSI2SD/src/scsi2sd-util6/scsi2sd-util.cc

@@ -41,6 +41,7 @@
 #include "BoardPanel.hh"
 #include "TargetPanel.hh"
 #include "SCSI2SD_HID.hh"
+//#include "Dfu.hh"
 
 #include <algorithm>
 #include <iomanip>
@@ -58,10 +59,6 @@ using std::shared_ptr;
 using std::tr1::shared_ptr;
 #endif
 
-#ifdef HAS_LIBUSB
-#include <libusb-1.0/libusb.h>
-#endif
-
 using namespace SCSI2SD;
 
 class ProgressWrapper
@@ -111,33 +108,6 @@ void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
 
 namespace
 {
-bool hasDFUdevice() {
-#ifdef HAS_LIBUSB
-	bool found = false;
-
-	libusb_device **list;
-	ssize_t cnt = libusb_get_device_list(NULL, &list);
-	ssize_t i = 0;
-	if (cnt < 0) return false;
-
-	for (i = 0; i < cnt; i++) {
-		libusb_device *device = list[i];
-		libusb_device_descriptor desc;
-		libusb_get_device_descriptor(device, &desc);
-		if (desc.idVendor == 0x0483 && desc.idProduct == 0xdf11) {
-			found = true;
-			break;
-		}
-	}
-
-	libusb_free_device_list(list, 1);
-
-	return found;
-#else
-	return false;
-#endif
-}
-
 
 static uint8_t sdCrc7(uint8_t* chr, uint8_t cnt, uint8_t crc)
 {
@@ -285,10 +255,11 @@ public:
 		myLogWindow->PassMessages(false); // Prevent messagebox popups
 
 		myTimer = new wxTimer(this, ID_Timer);
-		myTimer->Start(16); //ms, suitable for scsi debug logging
+		myTimer->Start(64); //ms, suitable for scsi debug logging
 	}
 
 private:
+	//Dfu myDfu;
 	wxLogWindow* myLogWindow;
 	BoardPanel* myBoardPanel;
 	std::vector<TargetPanel*> myTargets;
@@ -532,13 +503,15 @@ private:
 				}
 
 
-				if (hasDFUdevice())
+/*
+				if (myDfu.hasDevice())
 				{
 					mmLogStatus("STM DFU Bootloader found");
 					progress->Show(0);
 					doDFUUpdate(filename);
 					return;
 				}
+*/
 			}
 			catch (std::exception& e)
 			{
@@ -606,7 +579,7 @@ private:
 		}
 		try
 		{
-			std::vector<uint8_t> info(HID::HID_PACKET_SIZE);
+			std::vector<uint8_t> info;
 			if (myHID->readSCSIDebugInfo(info))
 			{
 				dumpSCSICommand(info);
@@ -940,9 +913,6 @@ class App : public wxApp
 public:
 	virtual bool OnInit()
 	{
-#ifdef HAS_LIBUSB
-		libusb_init(NULL);
-#endif
 		AppFrame* frame = new AppFrame();
 		frame->Show(true);
 		SetTopWindow(frame);