ZuluSCSI_msc.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2023,2024 zigzagjoe
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #ifdef PLATFORM_MASS_STORAGE
  22. #include <SdFat.h>
  23. #include "ZuluSCSI_platform.h"
  24. #include "ZuluSCSI_log.h"
  25. #include "ZuluSCSI_msc.h"
  26. // external global SD variable
  27. extern SdFs SD;
  28. // public globals
  29. volatile MSC_LEDState MSC_LEDMode;
  30. // card reader operation loop
  31. // assumption that SD card was enumerated and is working
  32. void zuluscsi_msc_loop() {
  33. // turn LED on to indicate entering card reader mode.
  34. LED_ON();
  35. logmsg("Entering USB Mass storage mode. Eject the USB disk to exit.");
  36. platform_enter_msc();
  37. uint32_t sd_card_check_time = 0;
  38. uint16_t syncCounter = 0;
  39. // steady state operation / indication loop
  40. // led remains steady on
  41. while(platform_run_msc()) {
  42. platform_reset_watchdog(); // also sends log to USB serial
  43. if ((uint32_t)(millis() - sd_card_check_time) > 5000) {
  44. sd_card_check_time = millis();
  45. uint32_t ocr;
  46. if (!SD.card()->readOCR(&ocr)) {
  47. if (!SD.card()->readOCR(&ocr)) {
  48. logmsg("SD card presence check failed! Card unexpectedly removed?");
  49. break;
  50. }
  51. }
  52. }
  53. // blink LED according to access type
  54. switch (MSC_LEDMode) {
  55. case LED_BLINK_FAST:
  56. LED_OFF();
  57. delay(30);
  58. break;
  59. case LED_BLINK_SLOW:
  60. delay(30);
  61. LED_OFF();
  62. delay(100);
  63. syncCounter = 1;
  64. break;
  65. default:
  66. // sync sd card ~ 500ms after writes stop
  67. if (syncCounter && (++syncCounter > 8)) {
  68. syncCounter = 0;
  69. SD.card()->syncDevice();
  70. }
  71. }
  72. // LED always on in card reader mode
  73. MSC_LEDMode = LED_SOLIDON;
  74. LED_ON();
  75. delay(30);
  76. }
  77. // turn the LED off to indicate exiting MSC
  78. LED_OFF();
  79. logmsg("USB Mass Storage mode exited: resuming standard functionality.");
  80. platform_exit_msc();
  81. SD.card()->syncDevice();
  82. // leave the LED off for a moment, before any blinks from the main firmware occur
  83. delay(1000);
  84. }
  85. #endif