Fix bug in using nor flash storage without SD
[SCSI2SD.git] / software / SCSI2SD / src / main.c
1 //      Copyright (C) 2014 Michael McMaster <michael@codesrc.com>\r
2 //\r
3 //      This file is part of SCSI2SD.\r
4 //\r
5 //      SCSI2SD is free software: you can redistribute it and/or modify\r
6 //      it under the terms of the GNU General Public License as published by\r
7 //      the Free Software Foundation, either version 3 of the License, or\r
8 //      (at your option) any later version.\r
9 //\r
10 //      SCSI2SD is distributed in the hope that it will be useful,\r
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 //      GNU General Public License for more details.\r
14 //\r
15 //      You should have received a copy of the GNU General Public License\r
16 //      along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.\r
17 #include "device.h"\r
18 #include "scsi.h"\r
19 #include "scsiPhy.h"\r
20 #include "config.h"\r
21 #include "disk.h"\r
22 #include "led.h"\r
23 #include "time.h"\r
24 #include "trace.h"\r
25 \r
26 const char* Notice = "Copyright (C) 2015-2018 Michael McMaster <michael@codesrc.com>";\r
27 \r
28 int main()\r
29 {\r
30         timeInit();\r
31         ledInit();\r
32         s2s_deviceEarlyInit();\r
33         traceInit();\r
34 \r
35         // Enable global interrupts.\r
36         // Needed for RST and ATN interrupt handlers.\r
37         CyGlobalIntEnable;\r
38 \r
39         // Set interrupt handlers.\r
40         scsiPhyInit();\r
41 \r
42         configInit(&scsiDev.boardCfg);\r
43         debugInit();\r
44 \r
45         scsiPhyConfig();\r
46 \r
47         scsiInit();\r
48         scsiDiskInit();\r
49 \r
50         // Optional bootup delay\r
51         int delaySeconds = 0;\r
52         while (delaySeconds < scsiDev.boardCfg.startupDelay) {\r
53                 // Keep the USB connection working, otherwise it's very hard to revert\r
54                 // silly extra-long startup delay settings.\r
55                 int i;\r
56                 for (i = 0; i < 200; i++) {\r
57                         CyDelay(5);\r
58                         scsiDev.watchdogTick++;\r
59                         configPoll();\r
60                 }\r
61                 ++delaySeconds;\r
62         }\r
63 \r
64     s2s_deviceInit();\r
65 \r
66         while (1)\r
67         {\r
68                 scsiDev.watchdogTick++;\r
69 \r
70                 scsiPoll();\r
71                 scsiDiskPoll();\r
72                 configPoll();\r
73                 sdPoll();\r
74 \r
75                 if (unlikely(scsiDev.phase == BUS_FREE))\r
76                 {\r
77                         if (s2s_pollMediaChange())\r
78                         {\r
79                                 scsiPhyConfig();\r
80                                 scsiInit();\r
81                         }\r
82                         else\r
83                         {\r
84                                 // Wait for our 1ms timer to save some power.\r
85                                 // There's an interrupt on the SEL signal to ensure we respond\r
86                                 // quickly to any SCSI commands. The selection abort time is\r
87                                 // only 250us, and new SCSI-3 controllers time-out very\r
88                                 // not long after that, so we need to ensure we wake up quickly.\r
89                                 uint8_t interruptState = CyEnterCriticalSection();\r
90                                 if (!SCSI_ReadFilt(SCSI_Filt_SEL))\r
91                                 {\r
92                                         __WFI(); // Will wake on interrupt, regardless of mask\r
93                                 }\r
94                                 CyExitCriticalSection(interruptState);\r
95                         }\r
96                 }\r
97                 else if ((scsiDev.phase >= 0) &&\r
98                         scsiDev.target &&\r
99                         (scsiDev.target->device->mediaState & MEDIA_PRESENT))\r
100                 {\r
101                         scsiDev.target->device->pollMediaBusy(scsiDev.target->device);\r
102                 }\r
103         }\r
104         return 0;\r
105 }\r
106 \r