main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #ifdef STM32F2xx
  18. #include "stm32f2xx.h"
  19. #endif
  20. #ifdef STM32F4xx
  21. #include "stm32f4xx.h"
  22. #endif
  23. #include "config.h"
  24. #include "disk.h"
  25. #include "fpga.h"
  26. #include "hwversion.h"
  27. #include "led.h"
  28. #include "sd.h"
  29. #include "scsi.h"
  30. #include "scsiPhy.h"
  31. #include "time.h"
  32. #include "sdio.h"
  33. #include "usb_device/usb_device.h"
  34. #include "usb_device/usbd_composite.h"
  35. #include "usb_device/usbd_msc_storage_sd.h"
  36. const char* Notice = "Copyright (C) 2020 Michael McMaster <michael@codesrc.com>";
  37. uint32_t lastSDPoll;
  38. static int isUsbStarted;
  39. // Note that the chip clocking isn't fully configured at this stage.
  40. void mainEarlyInit()
  41. {
  42. #ifdef nULPI_RESET_GPIO_Port
  43. // Disable the ULPI chip
  44. HAL_GPIO_WritePin(nULPI_RESET_GPIO_Port, nULPI_RESET_Pin, GPIO_PIN_RESET);
  45. #endif
  46. // Sets up function pointers only
  47. s2s_initUsbDeviceStorage();
  48. }
  49. void mainInit()
  50. {
  51. s2s_timeInit();
  52. s2s_checkHwVersion();
  53. s2s_ledInit();
  54. s2s_fpgaInit();
  55. scsiPhyInit();
  56. scsiDiskInit();
  57. sdInit();
  58. s2s_configInit(&scsiDev.boardCfg);
  59. scsiPhyConfig();
  60. scsiInit();
  61. #ifdef S2S_USB_HS
  62. // Enable the ULPI chip
  63. HAL_GPIO_WritePin(nULPI_RESET_GPIO_Port, nULPI_RESET_Pin, GPIO_PIN_SET);
  64. s2s_delay_ms(5);
  65. #endif
  66. MX_USB_DEVICE_Init(); // USB lun config now available.
  67. isUsbStarted = 1;
  68. // Optional bootup delay
  69. int delaySeconds = 0;
  70. while (delaySeconds < scsiDev.boardCfg.startupDelay) {
  71. // Keep the USB connection working, otherwise it's very hard to revert
  72. // silly extra-long startup delay settings.
  73. int i;
  74. for (i = 0; i < 200; i++) {
  75. s2s_delay_ms(5);
  76. scsiDev.watchdogTick++;
  77. s2s_configPoll();
  78. }
  79. ++delaySeconds;
  80. }
  81. lastSDPoll = s2s_getTime_ms();
  82. }
  83. void mainLoop()
  84. {
  85. scsiDev.watchdogTick++;
  86. scsiPoll();
  87. scsiDiskPoll();
  88. s2s_configPoll();
  89. #ifdef S2S_USB_FS
  90. s2s_usbDevicePoll(&hUsbDeviceFS);
  91. #endif
  92. #ifdef S2S_USB_HS
  93. s2s_usbDevicePoll(&hUsbDeviceHS);
  94. #endif
  95. #if 0
  96. sdPoll();
  97. #endif
  98. // TODO test if USB transfer is in progress
  99. if (unlikely(scsiDev.phase == BUS_FREE))
  100. {
  101. if (unlikely(s2s_elapsedTime_ms(lastSDPoll) > 200))
  102. {
  103. lastSDPoll = s2s_getTime_ms();
  104. if (sdInit())
  105. {
  106. s2s_configInit(&scsiDev.boardCfg);
  107. scsiPhyConfig();
  108. scsiInit();
  109. /* TODO DEAL WITH THIS
  110. if (isUsbStarted)
  111. {
  112. USBD_Stop(&hUsbDeviceFS);
  113. s2s_delay_ms(128);
  114. USBD_Start(&hUsbDeviceFS);
  115. }
  116. */
  117. }
  118. }
  119. }
  120. else if ((scsiDev.phase >= 0) && (blockDev.state & DISK_PRESENT))
  121. {
  122. // don't waste time scanning SD cards while we're doing disk IO
  123. lastSDPoll = s2s_getTime_ms();
  124. }
  125. }