main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include "stm32f2xx.h"
  18. #include "config.h"
  19. #include "disk.h"
  20. #include "fpga.h"
  21. #include "led.h"
  22. #include "sd.h"
  23. #include "scsi.h"
  24. #include "scsiPhy.h"
  25. #include "time.h"
  26. #include "trace.h"
  27. #include "usb_device/usb_device.h"
  28. #include "usb_device/usbd_composite.h"
  29. #include "usb_device/usbd_msc_storage_sd.h"
  30. const char* Notice = "Copyright (C) 2016 Michael McMaster <michael@codesrc.com>";
  31. uint32_t lastSDPoll;
  32. void mainEarlyInit()
  33. {
  34. // USB device is initialised before mainInit is called
  35. s2s_initUsbDeviceStorage();
  36. }
  37. void mainInit()
  38. {
  39. traceInit();
  40. s2s_timeInit();
  41. s2s_ledInit();
  42. s2s_fpgaInit();
  43. scsiPhyInit();
  44. scsiDiskInit();
  45. sdInit();
  46. s2s_configInit(&scsiDev.boardCfg);
  47. scsiPhyConfig();
  48. scsiInit();
  49. MX_USB_DEVICE_Init(); // USB lun config now available.
  50. // Optional bootup delay
  51. int delaySeconds = 0;
  52. while (delaySeconds < scsiDev.boardCfg.startupDelay) {
  53. // Keep the USB connection working, otherwise it's very hard to revert
  54. // silly extra-long startup delay settings.
  55. int i;
  56. for (i = 0; i < 200; i++) {
  57. s2s_delay_ms(5);
  58. scsiDev.watchdogTick++;
  59. s2s_configPoll();
  60. }
  61. ++delaySeconds;
  62. }
  63. lastSDPoll = s2s_getTime_ms();
  64. }
  65. void mainLoop()
  66. {
  67. scsiDev.watchdogTick++;
  68. scsiPoll();
  69. scsiDiskPoll();
  70. s2s_configPoll();
  71. s2s_usbDevicePoll();
  72. #if 0
  73. sdPoll();
  74. #endif
  75. if (unlikely(scsiDev.phase == BUS_FREE))
  76. {
  77. if (unlikely(s2s_elapsedTime_ms(lastSDPoll) > 200))
  78. {
  79. lastSDPoll = s2s_getTime_ms();
  80. if (sdInit())
  81. {
  82. s2s_configInit(&scsiDev.boardCfg);
  83. scsiPhyConfig();
  84. scsiInit();
  85. USBD_Stop(&hUsbDeviceFS);
  86. s2s_delay_ms(128);
  87. USBD_Start(&hUsbDeviceFS);
  88. }
  89. }
  90. else
  91. {
  92. // Wait for our 1ms timer to save some power.
  93. // There's an interrupt on the SEL signal to ensure we respond
  94. // quickly to any SCSI commands. The selection abort time is
  95. // only 250us, and new SCSI-3 controllers time-out very
  96. // not long after that, so we need to ensure we wake up quickly.
  97. uint32_t interruptState = __get_PRIMASK();
  98. __disable_irq();
  99. if (!*SCSI_STS_SELECTED)
  100. {
  101. __WFI(); // Will wake on interrupt, regardless of mask
  102. }
  103. if (!interruptState)
  104. {
  105. __enable_irq();
  106. }
  107. }
  108. }
  109. else if (scsiDev.phase >= 0)
  110. {
  111. // don't waste time scanning SD cards while we're doing disk IO
  112. lastSDPoll = s2s_getTime_ms();
  113. }
  114. }