Fix terrible performance
[SCSI2SD-V6.git] / src / firmware / config.c
index 1015ae3..eb6b369 100755 (executable)
@@ -16,7 +16,6 @@
 //     along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.\r
 \r
 #include "config.h"\r
-#include "debug.h"\r
 #include "led.h"\r
 \r
 #include "scsi.h"\r
@@ -38,7 +37,7 @@
 \r
 #include <string.h>\r
 \r
-static const uint16_t FIRMWARE_VERSION = 0x0600;\r
+static const uint16_t FIRMWARE_VERSION = 0x0606;\r
 \r
 // 1 flash row\r
 static const uint8_t DEFAULT_CONFIG[128] =\r
@@ -68,19 +67,6 @@ static int usbDebugEpState;
 #endif\r
 static int usbReady; // TODO MM REMOVE. Unused ?\r
 \r
-static void initS2S_BoardCfg(S2S_BoardCfg* config) {\r
-       if (memcmp(config->magic, "BCFG", 4)) {\r
-               config->selectionDelay = 255; // auto\r
-               config->flags6 = S2S_CFG_ENABLE_TERMINATOR;\r
-\r
-               memcpy(\r
-                       s2s_cfg + sizeof(S2S_BoardCfg),\r
-                       DEFAULT_CONFIG,\r
-                       sizeof(S2S_TargetCfg));\r
-       }\r
-}\r
-\r
-\r
 void s2s_configInit(S2S_BoardCfg* config)\r
 {\r
 \r
@@ -88,7 +74,7 @@ void s2s_configInit(S2S_BoardCfg* config)
        usbReady = 0; // We don't know if host is connected yet.\r
 \r
 \r
-       if (blockDev.state & DISK_PRESENT && sdDev.capacity)\r
+       if ((blockDev.state & DISK_PRESENT) && sdDev.capacity)\r
        {\r
                int cfgSectors = (S2S_CFG_SIZE + 511) / 512;\r
                BSP_SD_ReadBlocks_DMA(\r
@@ -98,11 +84,35 @@ void s2s_configInit(S2S_BoardCfg* config)
                        cfgSectors);\r
 \r
                memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));\r
-       }\r
 \r
-       initS2S_BoardCfg(config);\r
+               if (memcmp(config->magic, "BCFG", 4))\r
+               {\r
+                       // Invalid SD card config, use default.\r
+                       memset(&s2s_cfg[0], 0, S2S_CFG_SIZE);\r
+                       memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));\r
+                       memcpy(config->magic, "BCFG", 4);\r
+                       config->selectionDelay = 255; // auto\r
+                       config->flags6 = S2S_CFG_ENABLE_TERMINATOR;\r
+\r
+                       memcpy(\r
+                               &s2s_cfg[0] + sizeof(S2S_BoardCfg),\r
+                               DEFAULT_CONFIG,\r
+                               sizeof(S2S_TargetCfg));\r
+               }\r
+       }\r
+       else\r
+       {\r
+               // No SD card, use existing config if valid\r
+               if (memcmp(config->magic, "BCFG", 4))\r
+               {\r
+                       // Not valid, use empty config with no disks.\r
+                       memset(&s2s_cfg[0], 0, S2S_CFG_SIZE);\r
+                       memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));\r
+                       config->selectionDelay = 255; // auto\r
+                       config->flags6 = S2S_CFG_ENABLE_TERMINATOR;\r
+               }\r
+       }\r
 \r
-       scsiPhyConfig();\r
 }\r
 \r
 \r
@@ -130,7 +140,7 @@ sdInfoCommand()
 static void\r
 scsiTestCommand()\r
 {\r
-       int resultCode = 0; // TODO scsiSelfTest();\r
+       int resultCode = scsiSelfTest();\r
        uint8_t response[] =\r
        {\r
                resultCode == 0 ? S2S_CFG_STATUS_GOOD : S2S_CFG_STATUS_ERR,\r
@@ -154,6 +164,33 @@ scsiDevInfoCommand()
        hidPacket_send(response, sizeof(response));\r
 }\r
 \r
+static void\r
+debugCommand()\r
+{\r
+       uint8_t response[32];\r
+       memcpy(&response, &scsiDev.cdb, 12);\r
+       response[12] = scsiDev.msgIn;\r
+       response[13] = scsiDev.msgOut;\r
+       response[14] = scsiDev.lastStatus;\r
+       response[15] = scsiDev.lastSense;\r
+       response[16] = scsiDev.phase;\r
+       response[17] = scsiStatusBSY();\r
+       response[18] = scsiStatusSEL();\r
+       response[19] = scsiStatusATN();\r
+       response[20] = scsiStatusRST();\r
+       response[21] = scsiDev.rstCount;\r
+       response[22] = scsiDev.selCount;\r
+       response[23] = scsiDev.msgCount;\r
+       response[24] = scsiDev.cmdCount;\r
+       response[25] = scsiDev.watchdogTick;\r
+       response[26] = blockDev.state;\r
+       response[27] = scsiDev.lastSenseASC >> 8;\r
+       response[28] = scsiDev.lastSenseASC;\r
+       response[29] = *SCSI_STS_DBX;\r
+       response[30] = LastTrace;\r
+       response[31] = scsiStatusACK();\r
+       hidPacket_send(response, sizeof(response));\r
+}\r
 \r
 static void\r
 sdWriteCommand(const uint8_t* cmd, size_t cmdSize)\r
@@ -230,6 +267,10 @@ processCommand(const uint8_t* cmd, size_t cmdSize)
                sdReadCommand(cmd, cmdSize);\r
                break;\r
 \r
+       case S2S_CMD_DEBUG:\r
+               debugCommand();\r
+               break;\r
+\r
        case S2S_CMD_NONE: // invalid\r
        default:\r
                break;\r
@@ -289,116 +330,7 @@ void s2s_configPoll()
 \r
 }\r
 \r
-void debugPoll()\r
-{\r
-#if 0\r
-       if (!usbReady)\r
-       {\r
-               return;\r
-       }\r
 \r
-       if(USBFS_GetEPState(USB_EP_COMMAND) == USBFS_OUT_BUFFER_FULL)\r
-       {\r
-               // The host sent us some data!\r
-               int byteCount = USBFS_GetEPCount(USB_EP_COMMAND);\r
-               USBFS_ReadOutEP(USB_EP_COMMAND, (uint8 *)&hidBuffer, byteCount);\r
-\r
-               if (byteCount >= 1 &&\r
-                       hidBuffer[0] == 0x01)\r
-               {\r
-                       // Reboot command.\r
-                       Bootloadable_1_Load();\r
-               }\r
-\r
-               // Allow the host to send us another command.\r
-               // (assuming we didn't reboot outselves)\r
-               USBFS_EnableOutEP(USB_EP_COMMAND);\r
-       }\r
-\r
-       switch (usbDebugEpState)\r
-       {\r
-       case USB_IDLE:\r
-               memcpy(&hidBuffer, &scsiDev.cdb, 12);\r
-               hidBuffer[12] = scsiDev.msgIn;\r
-               hidBuffer[13] = scsiDev.msgOut;\r
-               hidBuffer[14] = scsiDev.lastStatus;\r
-               hidBuffer[15] = scsiDev.lastSense;\r
-               hidBuffer[16] = scsiDev.phase;\r
-               hidBuffer[17] = SCSI_ReadFilt(SCSI_Filt_BSY);\r
-               hidBuffer[18] = SCSI_ReadFilt(SCSI_Filt_SEL);\r
-               hidBuffer[19] = SCSI_ReadFilt(SCSI_Filt_ATN);\r
-               hidBuffer[20] = SCSI_ReadFilt(SCSI_Filt_RST);\r
-               hidBuffer[21] = scsiDev.rstCount;\r
-               hidBuffer[22] = scsiDev.selCount;\r
-               hidBuffer[23] = scsiDev.msgCount;\r
-               hidBuffer[24] = scsiDev.cmdCount;\r
-               hidBuffer[25] = scsiDev.watchdogTick;\r
-               hidBuffer[26] = blockDev.state;\r
-               hidBuffer[27] = scsiDev.lastSenseASC >> 8;\r
-               hidBuffer[28] = scsiDev.lastSenseASC;\r
-               hidBuffer[29] = scsiReadDBxPins();\r
-               hidBuffer[30] = LastTrace;\r
-\r
-               hidBuffer[58] = sdDev.capacity >> 24;\r
-               hidBuffer[59] = sdDev.capacity >> 16;\r
-               hidBuffer[60] = sdDev.capacity >> 8;\r
-               hidBuffer[61] = sdDev.capacity;\r
-\r
-               hidBuffer[62] = FIRMWARE_VERSION >> 8;\r
-               hidBuffer[63] = FIRMWARE_VERSION;\r
-\r
-               USBFS_LoadInEP(USB_EP_DEBUG, (uint8 *)&hidBuffer, sizeof(hidBuffer));\r
-               usbDebugEpState = USB_DATA_SENT;\r
-               break;\r
-\r
-       case USB_DATA_SENT:\r
-               if (USBFS_bGetEPAckState(USB_EP_DEBUG))\r
-               {\r
-                       // Data accepted.\r
-                       usbDebugEpState = USB_IDLE;\r
-               }\r
-               break;\r
-       }\r
-#endif\r
-}\r
-\r
-#if 0\r
-CY_ISR(debugTimerISR)\r
-{\r
-       Debug_Timer_ReadStatusRegister();\r
-       Debug_Timer_Interrupt_ClearPending();\r
-       uint8 savedIntrStatus = CyEnterCriticalSection();\r
-       debugPoll();\r
-       CyExitCriticalSection(savedIntrStatus);\r
-}\r
-#endif\r
-\r
-void s2s_debugInit()\r
-{\r
-#if 0\r
-       Debug_Timer_Interrupt_StartEx(debugTimerISR);\r
-       Debug_Timer_Start();\r
-#endif\r
-}\r
-\r
-void debugPause()\r
-{\r
-#if 0\r
-       Debug_Timer_Stop();\r
-#endif\r
-}\r
-\r
-void debugResume()\r
-{\r
-#if 0\r
-       Debug_Timer_Start();\r
-#endif\r
-}\r
-\r
-int isDebugEnabled()\r
-{\r
-       return usbReady;\r
-}\r
 \r
 // Public method for storing MODE SELECT results.\r
 void s2s_configSave(int scsiId, uint16_t bytesPerSector)\r