aa144bde2c676cc8256d4196d6ce9f3ca1dc588d
[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         traceInit();\r
33 \r
34         // Enable global interrupts.\r
35         // Needed for RST and ATN interrupt handlers.\r
36         CyGlobalIntEnable;\r
37 \r
38         // Set interrupt handlers.\r
39         scsiPhyInit();\r
40 \r
41         configInit(&scsiDev.boardCfg);\r
42         debugInit();\r
43 \r
44         scsiPhyConfig();\r
45 \r
46         scsiInit();\r
47         scsiDiskInit();\r
48 \r
49         // Optional bootup delay\r
50         int delaySeconds = 0;\r
51         while (delaySeconds < scsiDev.boardCfg.startupDelay) {\r
52                 // Keep the USB connection working, otherwise it's very hard to revert\r
53                 // silly extra-long startup delay settings.\r
54                 int i;\r
55                 for (i = 0; i < 200; i++) {\r
56                         CyDelay(5);\r
57                         scsiDev.watchdogTick++;\r
58                         configPoll();\r
59                 }\r
60                 ++delaySeconds;\r
61         }\r
62 \r
63         uint32_t lastSDPoll = getTime_ms();\r
64         sdCheckPresent();\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 (unlikely(elapsedTime_ms(lastSDPoll) > 200))\r
78                         {\r
79                                 lastSDPoll = getTime_ms();\r
80                                 sdCheckPresent();\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) && (blockDev.state & DISK_PRESENT))\r
98                 {\r
99                         // don't waste time scanning SD cards while we're doing disk IO\r
100                         lastSDPoll = getTime_ms();\r
101                 }\r
102         }\r
103         return 0;\r
104 }\r
105 \r