main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "bsp.h"
  20. #include "disk.h"
  21. #include "fpga.h"
  22. #include "led.h"
  23. #include "sd.h"
  24. #include "scsi.h"
  25. #include "scsiPhy.h"
  26. #include "time.h"
  27. #include "sdio.h"
  28. #include "usb_device/usb_device.h"
  29. #include "usb_device/usbd_composite.h"
  30. #include "usb_device/usbd_msc_storage_sd.h"
  31. const char* Notice = "Copyright (C) 2016 Michael McMaster <michael@codesrc.com>";
  32. uint32_t lastSDPoll;
  33. static int isUsbStarted;
  34. void mainEarlyInit()
  35. {
  36. // USB device is initialised before mainInit is called
  37. s2s_initUsbDeviceStorage();
  38. }
  39. void mainInit()
  40. {
  41. s2s_timeInit();
  42. s2s_ledInit();
  43. s2s_fpgaInit();
  44. scsiPhyInit();
  45. scsiDiskInit();
  46. sdInit();
  47. s2s_configInit(&scsiDev.boardCfg);
  48. scsiPhyConfig();
  49. scsiInit();
  50. MX_USB_DEVICE_Init(); // USB lun config now available.
  51. isUsbStarted = 1;
  52. // Optional bootup delay
  53. int delaySeconds = 0;
  54. while (delaySeconds < scsiDev.boardCfg.startupDelay) {
  55. // Keep the USB connection working, otherwise it's very hard to revert
  56. // silly extra-long startup delay settings.
  57. int i;
  58. for (i = 0; i < 200; i++) {
  59. s2s_delay_ms(5);
  60. scsiDev.watchdogTick++;
  61. s2s_configPoll();
  62. }
  63. ++delaySeconds;
  64. }
  65. lastSDPoll = s2s_getTime_ms();
  66. }
  67. void mainLoop()
  68. {
  69. scsiDev.watchdogTick++;
  70. scsiPoll();
  71. scsiDiskPoll();
  72. s2s_configPoll();
  73. s2s_usbDevicePoll();
  74. #if 0
  75. sdPoll();
  76. #endif
  77. if (unlikely(scsiDev.phase == BUS_FREE))
  78. {
  79. if (unlikely(s2s_elapsedTime_ms(lastSDPoll) > 200))
  80. {
  81. lastSDPoll = s2s_getTime_ms();
  82. if (sdInit())
  83. {
  84. s2s_configInit(&scsiDev.boardCfg);
  85. scsiPhyConfig();
  86. scsiInit();
  87. // Is a USB host connected ?
  88. if (isUsbStarted)
  89. {
  90. USBD_Stop(&hUsbDeviceFS);
  91. s2s_delay_ms(128);
  92. USBD_Start(&hUsbDeviceFS);
  93. }
  94. }
  95. // Can we speed up the SD card ?
  96. // Don't combine with the above block because that won't
  97. // run if the SD card is present at startup.
  98. // Don't use VBUS monitoring because that just tells us about
  99. // power, which could be from a charger
  100. if ((blockDev.state & DISK_PRESENT) &&
  101. isUsbStarted &&
  102. (scsiDev.cmdCount > 0) && // no need for speed without scsi
  103. !USBD_Composite_IsConfigured(&hUsbDeviceFS) &&
  104. (scsiDev.boardCfg.scsiSpeed == S2S_CFG_SPEED_TURBO))
  105. {
  106. if (HAL_SD_HighSpeed(&hsd) == SD_OK)
  107. {
  108. USBD_Stop(&hUsbDeviceFS);
  109. s2s_setFastClock();
  110. isUsbStarted = 0;
  111. }
  112. }
  113. else if (!(blockDev.state & DISK_PRESENT) && !isUsbStarted)
  114. {
  115. // Good time to restart USB.
  116. s2s_setNormalClock();
  117. USBD_Start(&hUsbDeviceFS);
  118. isUsbStarted = 1;
  119. }
  120. }
  121. else
  122. {
  123. // TODO this hurts performance significantly! Work out why __WFI()
  124. // doesn't wake up immediately !
  125. #if 0
  126. // Wait for our 1ms timer to save some power.
  127. // There's an interrupt on the SEL signal to ensure we respond
  128. // quickly to any SCSI commands. The selection abort time is
  129. // only 250us, and new SCSI-3 controllers time-out very
  130. // not long after that, so we need to ensure we wake up quickly.
  131. uint32_t interruptState = __get_PRIMASK();
  132. __disable_irq();
  133. if (!*SCSI_STS_SELECTED)
  134. {
  135. //__WFI(); // Will wake on interrupt, regardless of mask
  136. }
  137. if (!interruptState)
  138. {
  139. __enable_irq();
  140. }
  141. #endif
  142. }
  143. }
  144. else if (scsiDev.phase >= 0)
  145. {
  146. // don't waste time scanning SD cards while we're doing disk IO
  147. lastSDPoll = s2s_getTime_ms();
  148. }
  149. }