My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sd2Card_FlashDrive.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfigPre.h"
  23. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  24. #include "../../core/serial.h"
  25. #include "lib/Usb.h"
  26. #include "lib/masstorage.h"
  27. #include "Sd2Card_FlashDrive.h"
  28. #if EITHER(ULTRA_LCD, EXTENSIBLE_UI)
  29. #include "../../lcd/ultralcd.h"
  30. #endif
  31. USB usb;
  32. BulkOnly bulk(&usb);
  33. Sd2Card::state_t Sd2Card::state;
  34. // The USB library needs to be called periodically to detect USB thumbdrive
  35. // insertion and removals. Call this idle() function periodically to allow
  36. // the USB library to monitor for such events. This function also takes care
  37. // of initializing the USB library for the first time.
  38. void Sd2Card::idle() {
  39. static uint32_t next_retry;
  40. switch (state) {
  41. case USB_HOST_DELAY_INIT:
  42. next_retry = millis() + 2000;
  43. state = USB_HOST_WAITING;
  44. break;
  45. case USB_HOST_WAITING:
  46. if (ELAPSED(millis(), next_retry)) {
  47. next_retry = millis() + 2000;
  48. state = USB_HOST_UNINITIALIZED;
  49. }
  50. break;
  51. case USB_HOST_UNINITIALIZED:
  52. SERIAL_ECHOPGM("Starting USB host...");
  53. if (!usb.start()) {
  54. SERIAL_ECHOPGM(" Failed. Retrying in 2s.");
  55. #if EITHER(ULTRA_LCD, EXTENSIBLE_UI)
  56. LCD_MESSAGEPGM("USB start failed");
  57. #endif
  58. state = USB_HOST_DELAY_INIT;
  59. }
  60. else
  61. state = USB_HOST_INITIALIZED;
  62. SERIAL_EOL();
  63. break;
  64. case USB_HOST_INITIALIZED:
  65. const uint8_t lastUsbTaskState = usb.getUsbTaskState();
  66. usb.Task();
  67. const uint8_t newUsbTaskState = usb.getUsbTaskState();
  68. if (lastUsbTaskState == USB_STATE_RUNNING && newUsbTaskState != USB_STATE_RUNNING) {
  69. // the user pulled the flash drive. Make sure the bulk storage driver releases the address
  70. #ifdef USB_DEBUG
  71. SERIAL_ECHOLNPGM("USB drive removed");
  72. #endif
  73. //bulk.Release();
  74. }
  75. if (lastUsbTaskState != USB_STATE_RUNNING && newUsbTaskState == USB_STATE_RUNNING) {
  76. #ifdef USB_DEBUG
  77. SERIAL_ECHOLNPGM("USB drive inserted");
  78. #endif
  79. }
  80. break;
  81. }
  82. }
  83. // Marlin calls this function to check whether an USB drive is inserted.
  84. // This is equivalent to polling the SD_DETECT when using SD cards.
  85. bool Sd2Card::isInserted() {
  86. return usb.getUsbTaskState() == USB_STATE_RUNNING;
  87. }
  88. // Marlin calls this to initialize an SD card once it is inserted.
  89. bool Sd2Card::init(const uint8_t sckRateID/*=0*/, const pin_t chipSelectPin/*=SD_CHIP_SELECT_PIN*/) {
  90. if (!ready()) return false;
  91. if (!bulk.LUNIsGood(0)) {
  92. SERIAL_ECHOLNPGM("LUN zero is not good");
  93. return false;
  94. }
  95. const uint32_t sectorSize = bulk.GetSectorSize(0);
  96. if (sectorSize != 512) {
  97. SERIAL_ECHOLNPAIR("Expecting sector size of 512. Got: ", sectorSize);
  98. return false;
  99. }
  100. #ifdef USB_DEBUG
  101. lun0_capacity = bulk.GetCapacity(0);
  102. SERIAL_ECHOLNPAIR("LUN Capacity (in blocks): ", lun0_capacity);
  103. #endif
  104. return true;
  105. }
  106. // Returns the capacity of the card in blocks.
  107. uint32_t Sd2Card::cardSize() {
  108. if (!ready()) return 0;
  109. #ifndef USB_DEBUG
  110. const uint32_t
  111. #endif
  112. lun0_capacity = bulk.GetCapacity(0);
  113. return lun0_capacity;
  114. }
  115. bool Sd2Card::readBlock(uint32_t block, uint8_t* dst) {
  116. if (!ready()) return false;
  117. #ifdef USB_DEBUG
  118. if (block >= lun0_capacity) {
  119. SERIAL_ECHOLNPAIR("Attempt to read past end of LUN: ", block);
  120. return false;
  121. }
  122. #if USB_DEBUG > 1
  123. SERIAL_ECHOLNPAIR("Read block ", block);
  124. #endif
  125. #endif
  126. return bulk.Read(0, block, 512, 1, dst) == 0;
  127. }
  128. bool Sd2Card::writeBlock(uint32_t block, const uint8_t* src) {
  129. if (!ready()) return false;
  130. #ifdef USB_DEBUG
  131. if (block >= lun0_capacity) {
  132. SERIAL_ECHOLNPAIR("Attempt to write past end of LUN: ", block);
  133. return false;
  134. }
  135. #if USB_DEBUG > 1
  136. SERIAL_ECHOLNPAIR("Write block ", block);
  137. #endif
  138. #endif
  139. return bulk.Write(0, block, 512, 1, src) == 0;
  140. }
  141. #endif // USB_FLASH_DRIVE_SUPPORT