My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Sd2Card_FlashDrive.cpp 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfigPre.h"
  23. /**
  24. * Adjust USB_DEBUG to select debugging verbosity.
  25. * 0 - no debug messages
  26. * 1 - basic insertion/removal messages
  27. * 2 - show USB state transitions
  28. * 3 - perform block range checking
  29. * 4 - print each block access
  30. */
  31. #define USB_DEBUG 1
  32. #define USB_STARTUP_DELAY 0
  33. // uncomment to get 'printf' console debugging. NOT FOR UNO!
  34. //#define HOST_DEBUG(...) {char s[255]; sprintf(s,__VA_ARGS__); SERIAL_ECHOLNPGM("UHS:",s);}
  35. //#define BS_HOST_DEBUG(...) {char s[255]; sprintf(s,__VA_ARGS__); SERIAL_ECHOLNPGM("UHS:",s);}
  36. //#define MAX_HOST_DEBUG(...) {char s[255]; sprintf(s,__VA_ARGS__); SERIAL_ECHOLNPGM("UHS:",s);}
  37. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  38. #include "../../MarlinCore.h"
  39. #include "../../core/serial.h"
  40. #include "../../module/temperature.h"
  41. #if DISABLED(USE_OTG_USB_HOST) && !PINS_EXIST(USB_CS, USB_INTR)
  42. #error "USB_FLASH_DRIVE_SUPPORT requires USB_CS_PIN and USB_INTR_PIN to be defined."
  43. #endif
  44. #if ENABLED(USE_UHS3_USB)
  45. #define NO_AUTO_SPEED
  46. #define UHS_MAX3421E_SPD 8000000 >> SD_SPI_SPEED
  47. #define UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE 1
  48. #define UHS_HOST_MAX_INTERFACE_DRIVERS 2
  49. #define MASS_MAX_SUPPORTED_LUN 1
  50. #define USB_HOST_SERIAL MYSERIAL1
  51. // Workaround for certain issues with UHS3
  52. #define SKIP_PAGE3F // Required for IOGEAR media adapter
  53. #define USB_NO_TEST_UNIT_READY // Required for removable media adapter
  54. #define USB_HOST_MANUAL_POLL // Optimization to shut off IRQ automatically
  55. // Workarounds to keep Marlin's watchdog timer from barking...
  56. void marlin_yield() { thermalManager.task(); }
  57. #define SYSTEM_OR_SPECIAL_YIELD(...) marlin_yield();
  58. #define delay(x) safe_delay(x)
  59. #define LOAD_USB_HOST_SYSTEM
  60. #define LOAD_USB_HOST_SHIELD
  61. #define LOAD_UHS_BULK_STORAGE
  62. #define MARLIN_UHS_WRITE_SS(v) WRITE(USB_CS_PIN, v)
  63. #define MARLIN_UHS_READ_IRQ() READ(USB_INTR_PIN)
  64. #include "lib-uhs3/UHS_host/UHS_host.h"
  65. MAX3421E_HOST usb(USB_CS_PIN, USB_INTR_PIN);
  66. UHS_Bulk_Storage bulk(&usb);
  67. #define UHS_START (usb.Init() == 0)
  68. #define UHS_STATE(state) UHS_USB_HOST_STATE_##state
  69. #elif ENABLED(USE_OTG_USB_HOST)
  70. #if HAS_SD_HOST_DRIVE
  71. #include HAL_PATH(../../HAL, msc_sd.h)
  72. #endif
  73. #include HAL_PATH(../../HAL, usb_host.h)
  74. #define UHS_START usb.start()
  75. #define rREVISION 0
  76. #define UHS_STATE(state) USB_STATE_##state
  77. #else
  78. #include "lib-uhs2/Usb.h"
  79. #include "lib-uhs2/masstorage.h"
  80. USB usb;
  81. BulkOnly bulk(&usb);
  82. #define UHS_START usb.start()
  83. #define UHS_STATE(state) USB_STATE_##state
  84. #endif
  85. #include "Sd2Card_FlashDrive.h"
  86. #include "../../lcd/marlinui.h"
  87. static enum {
  88. UNINITIALIZED,
  89. DO_STARTUP,
  90. WAIT_FOR_DEVICE,
  91. WAIT_FOR_LUN,
  92. MEDIA_READY,
  93. MEDIA_ERROR
  94. } state;
  95. #if USB_DEBUG >= 3
  96. uint32_t lun0_capacity;
  97. #endif
  98. bool DiskIODriver_USBFlash::usbStartup() {
  99. if (state <= DO_STARTUP) {
  100. SERIAL_ECHOPGM("Starting USB host...");
  101. if (!UHS_START) {
  102. SERIAL_ECHOLNPGM(" failed.");
  103. LCD_MESSAGE(MSG_MEDIA_USB_FAILED);
  104. return false;
  105. }
  106. // SPI quick test - check revision register
  107. switch (usb.regRd(rREVISION)) {
  108. case 0x01: SERIAL_ECHOLNPGM("rev.01 started"); break;
  109. case 0x12: SERIAL_ECHOLNPGM("rev.02 started"); break;
  110. case 0x13: SERIAL_ECHOLNPGM("rev.03 started"); break;
  111. default: SERIAL_ECHOLNPGM("started. rev unknown."); break;
  112. }
  113. state = WAIT_FOR_DEVICE;
  114. }
  115. return true;
  116. }
  117. // The USB library needs to be called periodically to detect USB thumbdrive
  118. // insertion and removals. Call this idle() function periodically to allow
  119. // the USB library to monitor for such events. This function also takes care
  120. // of initializing the USB library for the first time.
  121. void DiskIODriver_USBFlash::idle() {
  122. usb.Task();
  123. const uint8_t task_state = usb.getUsbTaskState();
  124. #if USB_DEBUG >= 2
  125. if (state > DO_STARTUP) {
  126. static uint8_t laststate = 232;
  127. if (task_state != laststate) {
  128. laststate = task_state;
  129. #define UHS_USB_DEBUG(x,y) case UHS_STATE(x): SERIAL_ECHOLNPGM(y); break
  130. switch (task_state) {
  131. UHS_USB_DEBUG(IDLE, "IDLE");
  132. UHS_USB_DEBUG(RESET_DEVICE, "RESET_DEVICE");
  133. UHS_USB_DEBUG(RESET_NOT_COMPLETE, "RESET_NOT_COMPLETE");
  134. UHS_USB_DEBUG(DEBOUNCE, "DEBOUNCE");
  135. UHS_USB_DEBUG(DEBOUNCE_NOT_COMPLETE, "DEBOUNCE_NOT_COMPLETE");
  136. UHS_USB_DEBUG(WAIT_SOF, "WAIT_SOF");
  137. UHS_USB_DEBUG(ERROR, "ERROR");
  138. UHS_USB_DEBUG(CONFIGURING, "CONFIGURING");
  139. UHS_USB_DEBUG(CONFIGURING_DONE, "CONFIGURING_DONE");
  140. UHS_USB_DEBUG(RUNNING, "RUNNING");
  141. default:
  142. SERIAL_ECHOLNPGM("UHS_USB_HOST_STATE: ", task_state);
  143. break;
  144. }
  145. }
  146. }
  147. #endif
  148. static millis_t next_state_ms = millis();
  149. #define GOTO_STATE_AFTER_DELAY(STATE, DELAY) do{ state = STATE; next_state_ms = millis() + DELAY; }while(0)
  150. if (ELAPSED(millis(), next_state_ms)) {
  151. GOTO_STATE_AFTER_DELAY(state, 250); // Default delay
  152. switch (state) {
  153. case UNINITIALIZED:
  154. #ifndef MANUAL_USB_STARTUP
  155. GOTO_STATE_AFTER_DELAY( DO_STARTUP, USB_STARTUP_DELAY );
  156. #endif
  157. break;
  158. case DO_STARTUP: usbStartup(); break;
  159. case WAIT_FOR_DEVICE:
  160. if (task_state == UHS_STATE(RUNNING)) {
  161. #if USB_DEBUG >= 1
  162. SERIAL_ECHOLNPGM("USB device inserted");
  163. #endif
  164. GOTO_STATE_AFTER_DELAY( WAIT_FOR_LUN, 250 );
  165. }
  166. break;
  167. case WAIT_FOR_LUN:
  168. /* USB device is inserted, but if it is an SD card,
  169. * adapter it may not have an SD card in it yet. */
  170. if (bulk.LUNIsGood(0)) {
  171. #if USB_DEBUG >= 1
  172. SERIAL_ECHOLNPGM("LUN is good");
  173. #endif
  174. GOTO_STATE_AFTER_DELAY( MEDIA_READY, 100 );
  175. }
  176. else {
  177. #ifdef USB_HOST_MANUAL_POLL
  178. // Make sure we catch disconnect events
  179. usb.busprobe();
  180. usb.VBUS_changed();
  181. #endif
  182. #if USB_DEBUG >= 1
  183. SERIAL_ECHOLNPGM("Waiting for media");
  184. #endif
  185. LCD_MESSAGE(MSG_MEDIA_WAITING);
  186. GOTO_STATE_AFTER_DELAY(state, 2000);
  187. }
  188. break;
  189. case MEDIA_READY: break;
  190. case MEDIA_ERROR: break;
  191. }
  192. if (state > WAIT_FOR_DEVICE && task_state != UHS_STATE(RUNNING)) {
  193. // Handle device removal events
  194. #if USB_DEBUG >= 1
  195. SERIAL_ECHOLNPGM("USB device removed");
  196. #endif
  197. if (state != MEDIA_READY)
  198. LCD_MESSAGE(MSG_MEDIA_USB_REMOVED);
  199. GOTO_STATE_AFTER_DELAY(WAIT_FOR_DEVICE, 0);
  200. }
  201. else if (state > WAIT_FOR_LUN && !bulk.LUNIsGood(0)) {
  202. // Handle media removal events
  203. #if USB_DEBUG >= 1
  204. SERIAL_ECHOLNPGM("Media removed");
  205. #endif
  206. LCD_MESSAGE(MSG_MEDIA_REMOVED);
  207. GOTO_STATE_AFTER_DELAY(WAIT_FOR_DEVICE, 0);
  208. }
  209. else if (task_state == UHS_STATE(ERROR)) {
  210. LCD_MESSAGE(MSG_MEDIA_READ_ERROR);
  211. GOTO_STATE_AFTER_DELAY(MEDIA_ERROR, 0);
  212. }
  213. }
  214. }
  215. // Marlin calls this function to check whether an USB drive is inserted.
  216. // This is equivalent to polling the SD_DETECT when using SD cards.
  217. bool DiskIODriver_USBFlash::isInserted() {
  218. return state == MEDIA_READY;
  219. }
  220. bool DiskIODriver_USBFlash::isReady() {
  221. return state > DO_STARTUP && usb.getUsbTaskState() == UHS_STATE(RUNNING);
  222. }
  223. // Marlin calls this to initialize an SD card once it is inserted.
  224. bool DiskIODriver_USBFlash::init(const uint8_t, const pin_t) {
  225. if (!isInserted()) return false;
  226. #if USB_DEBUG >= 1
  227. const uint32_t sectorSize = bulk.GetSectorSize(0);
  228. if (sectorSize != 512) {
  229. SERIAL_ECHOLNPGM("Expecting sector size of 512. Got: ", sectorSize);
  230. return false;
  231. }
  232. #endif
  233. #if USB_DEBUG >= 3
  234. lun0_capacity = bulk.GetCapacity(0);
  235. SERIAL_ECHOLNPGM("LUN Capacity (in blocks): ", lun0_capacity);
  236. #endif
  237. return true;
  238. }
  239. // Returns the capacity of the card in blocks.
  240. uint32_t DiskIODriver_USBFlash::cardSize() {
  241. if (!isInserted()) return false;
  242. #if USB_DEBUG < 3
  243. const uint32_t
  244. #endif
  245. lun0_capacity = bulk.GetCapacity(0);
  246. return lun0_capacity;
  247. }
  248. bool DiskIODriver_USBFlash::readBlock(uint32_t block, uint8_t *dst) {
  249. if (!isInserted()) return false;
  250. #if USB_DEBUG >= 3
  251. if (block >= lun0_capacity) {
  252. SERIAL_ECHOLNPGM("Attempt to read past end of LUN: ", block);
  253. return false;
  254. }
  255. #if USB_DEBUG >= 4
  256. SERIAL_ECHOLNPGM("Read block ", block);
  257. #endif
  258. #endif
  259. return bulk.Read(0, block, 512, 1, dst) == 0;
  260. }
  261. bool DiskIODriver_USBFlash::writeBlock(uint32_t block, const uint8_t *src) {
  262. if (!isInserted()) return false;
  263. #if USB_DEBUG >= 3
  264. if (block >= lun0_capacity) {
  265. SERIAL_ECHOLNPGM("Attempt to write past end of LUN: ", block);
  266. return false;
  267. }
  268. #if USB_DEBUG >= 4
  269. SERIAL_ECHOLNPGM("Write block ", block);
  270. #endif
  271. #endif
  272. return bulk.Write(0, block, 512, 1, src) == 0;
  273. }
  274. #endif // USB_FLASH_DRIVE_SUPPORT