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.1KB

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