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 9.4KB

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