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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 "cardreader.h"
  23. #include "ultralcd.h"
  24. #include "stepper.h"
  25. #include "temperature.h"
  26. #include "language.h"
  27. #include "Marlin.h"
  28. #if ENABLED(SDSUPPORT)
  29. CardReader::CardReader() {
  30. sdprinting = cardOK = saving = logging = false;
  31. filesize = 0;
  32. sdpos = 0;
  33. workDirDepth = 0;
  34. file_subcall_ctr = 0;
  35. memset(workDirParents, 0, sizeof(workDirParents));
  36. autostart_stilltocheck = true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
  37. autostart_index = 0;
  38. //power to SD reader
  39. #if SDPOWER > -1
  40. OUT_WRITE(SDPOWER, HIGH);
  41. #endif //SDPOWER
  42. next_autostart_ms = millis() + 5000;
  43. }
  44. char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
  45. char *pos = buffer;
  46. for (uint8_t i = 0; i < 11; i++) {
  47. if (p.name[i] == ' ') continue;
  48. if (i == 8) *pos++ = '.';
  49. *pos++ = p.name[i];
  50. }
  51. *pos++ = 0;
  52. return buffer;
  53. }
  54. /**
  55. * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction:
  56. * LS_Count - Add +1 to nrFiles for every file within the parent
  57. * LS_GetFilename - Get the filename of the file indexed by nrFiles
  58. * LS_SerialPrint - Print the full path of each file to serial output
  59. */
  60. void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
  61. dir_t p;
  62. uint8_t cnt = 0;
  63. // Read the next entry from a directory
  64. while (parent.readDir(p, longFilename) > 0) {
  65. // If the entry is a directory and the action is LS_SerialPrint
  66. if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
  67. // Get the short name for the item, which we know is a folder
  68. char lfilename[FILENAME_LENGTH];
  69. createFilename(lfilename, p);
  70. // Allocate enough stack space for the full path to a folder, trailing slash, and nul
  71. boolean prepend_is_empty = (prepend[0] == '\0');
  72. int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1;
  73. char path[len];
  74. // Append the FOLDERNAME12/ to the passed string.
  75. // It contains the full path to the "parent" argument.
  76. // We now have the full path to the item in this folder.
  77. strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
  78. strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum
  79. strcat(path, "/"); // 1 character
  80. // Serial.print(path);
  81. // Get a new directory object using the full path
  82. // and dive recursively into it.
  83. SdFile dir;
  84. if (!dir.open(parent, lfilename, O_READ)) {
  85. if (lsAction == LS_SerialPrint) {
  86. SERIAL_ECHO_START;
  87. SERIAL_ECHOPGM(MSG_SD_CANT_OPEN_SUBDIR);
  88. SERIAL_ECHOLN(lfilename);
  89. }
  90. }
  91. lsDive(path, dir);
  92. // close() is done automatically by destructor of SdFile
  93. }
  94. else {
  95. uint8_t pn0 = p.name[0];
  96. if (pn0 == DIR_NAME_FREE) break;
  97. if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue;
  98. if (longFilename[0] == '.') continue;
  99. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  100. filenameIsDir = DIR_IS_SUBDIR(&p);
  101. if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
  102. switch (lsAction) {
  103. case LS_Count:
  104. nrFiles++;
  105. break;
  106. case LS_SerialPrint:
  107. createFilename(filename, p);
  108. SERIAL_PROTOCOL(prepend);
  109. SERIAL_PROTOCOLLN(filename);
  110. break;
  111. case LS_GetFilename:
  112. createFilename(filename, p);
  113. if (match != NULL) {
  114. if (strcasecmp(match, filename) == 0) return;
  115. }
  116. else if (cnt == nrFiles) return;
  117. cnt++;
  118. break;
  119. }
  120. }
  121. } // while readDir
  122. }
  123. void CardReader::ls() {
  124. lsAction = LS_SerialPrint;
  125. root.rewind();
  126. lsDive("", root);
  127. }
  128. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  129. /**
  130. * Get a long pretty path based on a DOS 8.3 path
  131. */
  132. void CardReader::printLongPath(char *path) {
  133. lsAction = LS_GetFilename;
  134. int i, pathLen = strlen(path);
  135. // SERIAL_ECHOPGM("Full Path: "); SERIAL_ECHOLN(path);
  136. // Zero out slashes to make segments
  137. for (i = 0; i < pathLen; i++) if (path[i] == '/') path[i] = '\0';
  138. SdFile diveDir = root; // start from the root for segment 1
  139. for (i = 0; i < pathLen;) {
  140. if (path[i] == '\0') i++; // move past a single nul
  141. char *segment = &path[i]; // The segment after most slashes
  142. // If a segment is empty (extra-slash) then exit
  143. if (!*segment) break;
  144. // Go to the next segment
  145. while (path[++i]) { }
  146. // SERIAL_ECHOPGM("Looking for segment: "); SERIAL_ECHOLN(segment);
  147. // Find the item, setting the long filename
  148. diveDir.rewind();
  149. lsDive("", diveDir, segment);
  150. // Print /LongNamePart to serial output
  151. SERIAL_PROTOCOLCHAR('/');
  152. SERIAL_PROTOCOL(longFilename[0] ? longFilename : "???");
  153. // If the filename was printed then that's it
  154. if (!filenameIsDir) break;
  155. // SERIAL_ECHOPGM("Opening dir: "); SERIAL_ECHOLN(segment);
  156. // Open the sub-item as the new dive parent
  157. SdFile dir;
  158. if (!dir.open(diveDir, segment, O_READ)) {
  159. SERIAL_EOL;
  160. SERIAL_ECHO_START;
  161. SERIAL_ECHOPGM(MSG_SD_CANT_OPEN_SUBDIR);
  162. SERIAL_ECHO(segment);
  163. break;
  164. }
  165. diveDir.close();
  166. diveDir = dir;
  167. } // while i<pathLen
  168. SERIAL_EOL;
  169. }
  170. #endif // LONG_FILENAME_HOST_SUPPORT
  171. void CardReader::initsd() {
  172. cardOK = false;
  173. if (root.isOpen()) root.close();
  174. #ifndef SPI_SPEED
  175. #define SPI_SPEED SPI_FULL_SPEED
  176. #endif
  177. if (!card.init(SPI_SPEED,SDSS)
  178. #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
  179. && !card.init(SPI_SPEED, LCD_SDSS)
  180. #endif
  181. ) {
  182. //if (!card.init(SPI_HALF_SPEED,SDSS))
  183. SERIAL_ECHO_START;
  184. SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
  185. }
  186. else if (!volume.init(&card)) {
  187. SERIAL_ERROR_START;
  188. SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
  189. }
  190. else if (!root.openRoot(&volume)) {
  191. SERIAL_ERROR_START;
  192. SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
  193. }
  194. else {
  195. cardOK = true;
  196. SERIAL_ECHO_START;
  197. SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
  198. }
  199. workDir = root;
  200. curDir = &root;
  201. /**
  202. if (!workDir.openRoot(&volume)) {
  203. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  204. }
  205. */
  206. }
  207. void CardReader::setroot() {
  208. /*if (!workDir.openRoot(&volume)) {
  209. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  210. }*/
  211. workDir = root;
  212. curDir = &workDir;
  213. }
  214. void CardReader::release() {
  215. sdprinting = false;
  216. cardOK = false;
  217. }
  218. void CardReader::openAndPrintFile(const char *name) {
  219. char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
  220. sprintf_P(cmd, PSTR("M23 %s"), name);
  221. for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
  222. enqueue_and_echo_command(cmd);
  223. enqueue_and_echo_commands_P(PSTR("M24"));
  224. }
  225. void CardReader::startFileprint() {
  226. if (cardOK) sdprinting = true;
  227. }
  228. void CardReader::stopSDPrint() {
  229. sdprinting = false;
  230. if (isFileOpen()) file.close();
  231. }
  232. void CardReader::openLogFile(char* name) {
  233. logging = true;
  234. openFile(name, false);
  235. }
  236. void CardReader::getAbsFilename(char *t) {
  237. uint8_t cnt = 0;
  238. *t = '/'; t++; cnt++;
  239. for (uint8_t i = 0; i < workDirDepth; i++) {
  240. workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
  241. while (*t && cnt < MAXPATHNAMELENGTH) { t++; cnt++; } //crawl counter forward.
  242. }
  243. if (cnt < MAXPATHNAMELENGTH - (FILENAME_LENGTH))
  244. file.getFilename(t);
  245. else
  246. t[0] = 0;
  247. }
  248. void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
  249. if (!cardOK) return;
  250. uint8_t doing = 0;
  251. if (isFileOpen()) { //replacing current file by new file, or subfile call
  252. if (push_current) {
  253. if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
  254. SERIAL_ERROR_START;
  255. SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
  256. SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
  257. kill(PSTR(MSG_KILLED));
  258. return;
  259. }
  260. // Store current filename and position
  261. getAbsFilename(proc_filenames[file_subcall_ctr]);
  262. SERIAL_ECHO_START;
  263. SERIAL_ECHOPAIR("SUBROUTINE CALL target:\"", name);
  264. SERIAL_ECHOPAIR("\" parent:\"", proc_filenames[file_subcall_ctr]);
  265. SERIAL_ECHOLNPAIR("\" pos", sdpos);
  266. filespos[file_subcall_ctr] = sdpos;
  267. file_subcall_ctr++;
  268. }
  269. else {
  270. doing = 1;
  271. }
  272. }
  273. else { // Opening fresh file
  274. doing = 2;
  275. file_subcall_ctr = 0; // Reset procedure depth in case user cancels print while in procedure
  276. }
  277. if (doing) {
  278. SERIAL_ECHO_START;
  279. SERIAL_ECHOPGM("Now ");
  280. SERIAL_ECHO(doing == 1 ? "doing" : "fresh");
  281. SERIAL_ECHOLNPAIR(" file: ", name);
  282. }
  283. stopSDPrint();
  284. SdFile myDir;
  285. curDir = &root;
  286. char *fname = name;
  287. char *dirname_start, *dirname_end;
  288. if (name[0] == '/') {
  289. dirname_start = &name[1];
  290. while (dirname_start != NULL) {
  291. dirname_end = strchr(dirname_start, '/');
  292. //SERIAL_ECHOPGM("start:");SERIAL_ECHOLN((int)(dirname_start - name));
  293. //SERIAL_ECHOPGM("end :");SERIAL_ECHOLN((int)(dirname_end - name));
  294. if (dirname_end != NULL && dirname_end > dirname_start) {
  295. char subdirname[FILENAME_LENGTH];
  296. strncpy(subdirname, dirname_start, dirname_end - dirname_start);
  297. subdirname[dirname_end - dirname_start] = 0;
  298. SERIAL_ECHOLN(subdirname);
  299. if (!myDir.open(curDir, subdirname, O_READ)) {
  300. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  301. SERIAL_PROTOCOL(subdirname);
  302. SERIAL_PROTOCOLCHAR('.');
  303. return;
  304. }
  305. else {
  306. //SERIAL_ECHOLNPGM("dive ok");
  307. }
  308. curDir = &myDir;
  309. dirname_start = dirname_end + 1;
  310. }
  311. else { // the remainder after all /fsa/fdsa/ is the filename
  312. fname = dirname_start;
  313. //SERIAL_ECHOLNPGM("remainder");
  314. //SERIAL_ECHOLN(fname);
  315. break;
  316. }
  317. }
  318. }
  319. else { //relative path
  320. curDir = &workDir;
  321. }
  322. if (read) {
  323. if (file.open(curDir, fname, O_READ)) {
  324. filesize = file.fileSize();
  325. SERIAL_PROTOCOLPAIR(MSG_SD_FILE_OPENED, fname);
  326. SERIAL_PROTOCOLLNPAIR(MSG_SD_SIZE, filesize);
  327. sdpos = 0;
  328. SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
  329. getfilename(0, fname);
  330. lcd_setstatus(longFilename[0] ? longFilename : fname);
  331. }
  332. else {
  333. SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, fname);
  334. SERIAL_PROTOCOLCHAR('.');
  335. SERIAL_EOL;
  336. }
  337. }
  338. else { //write
  339. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
  340. SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, fname);
  341. SERIAL_PROTOCOLCHAR('.');
  342. SERIAL_EOL;
  343. }
  344. else {
  345. saving = true;
  346. SERIAL_PROTOCOLLNPAIR(MSG_SD_WRITE_TO_FILE, name);
  347. lcd_setstatus(fname);
  348. }
  349. }
  350. }
  351. void CardReader::removeFile(char* name) {
  352. if (!cardOK) return;
  353. stopSDPrint();
  354. SdFile myDir;
  355. curDir = &root;
  356. char *fname = name;
  357. char *dirname_start, *dirname_end;
  358. if (name[0] == '/') {
  359. dirname_start = strchr(name, '/') + 1;
  360. while (dirname_start != NULL) {
  361. dirname_end = strchr(dirname_start, '/');
  362. //SERIAL_ECHOPGM("start:");SERIAL_ECHOLN((int)(dirname_start - name));
  363. //SERIAL_ECHOPGM("end :");SERIAL_ECHOLN((int)(dirname_end - name));
  364. if (dirname_end != NULL && dirname_end > dirname_start) {
  365. char subdirname[FILENAME_LENGTH];
  366. strncpy(subdirname, dirname_start, dirname_end - dirname_start);
  367. subdirname[dirname_end - dirname_start] = 0;
  368. SERIAL_ECHOLN(subdirname);
  369. if (!myDir.open(curDir, subdirname, O_READ)) {
  370. SERIAL_PROTOCOLPAIR("open failed, File: ", subdirname);
  371. SERIAL_PROTOCOLCHAR('.');
  372. SERIAL_EOL;
  373. return;
  374. }
  375. else {
  376. //SERIAL_ECHOLNPGM("dive ok");
  377. }
  378. curDir = &myDir;
  379. dirname_start = dirname_end + 1;
  380. }
  381. else { // the remainder after all /fsa/fdsa/ is the filename
  382. fname = dirname_start;
  383. //SERIAL_ECHOLNPGM("remainder");
  384. //SERIAL_ECHOLN(fname);
  385. break;
  386. }
  387. }
  388. }
  389. else { // relative path
  390. curDir = &workDir;
  391. }
  392. if (file.remove(curDir, fname)) {
  393. SERIAL_PROTOCOLPGM("File deleted:");
  394. SERIAL_PROTOCOLLN(fname);
  395. sdpos = 0;
  396. }
  397. else {
  398. SERIAL_PROTOCOLPGM("Deletion failed, File: ");
  399. SERIAL_PROTOCOL(fname);
  400. SERIAL_PROTOCOLCHAR('.');
  401. }
  402. }
  403. void CardReader::getStatus() {
  404. if (cardOK) {
  405. SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
  406. SERIAL_PROTOCOL(sdpos);
  407. SERIAL_PROTOCOLCHAR('/');
  408. SERIAL_PROTOCOLLN(filesize);
  409. }
  410. else {
  411. SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
  412. }
  413. }
  414. void CardReader::write_command(char *buf) {
  415. char* begin = buf;
  416. char* npos = 0;
  417. char* end = buf + strlen(buf) - 1;
  418. file.writeError = false;
  419. if ((npos = strchr(buf, 'N')) != NULL) {
  420. begin = strchr(npos, ' ') + 1;
  421. end = strchr(npos, '*') - 1;
  422. }
  423. end[1] = '\r';
  424. end[2] = '\n';
  425. end[3] = '\0';
  426. file.write(begin);
  427. if (file.writeError) {
  428. SERIAL_ERROR_START;
  429. SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
  430. }
  431. }
  432. void CardReader::checkautostart(bool force) {
  433. if (!force && (!autostart_stilltocheck || ELAPSED(millis(), next_autostart_ms)))
  434. return;
  435. autostart_stilltocheck = false;
  436. if (!cardOK) {
  437. initsd();
  438. if (!cardOK) return; // fail
  439. }
  440. char autoname[10];
  441. sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
  442. for (int8_t i = 0; i < (int8_t)strlen(autoname); i++) autoname[i] = tolower(autoname[i]);
  443. dir_t p;
  444. root.rewind();
  445. bool found = false;
  446. while (root.readDir(p, NULL) > 0) {
  447. for (int8_t i = 0; i < (int8_t)strlen((char*)p.name); i++) p.name[i] = tolower(p.name[i]);
  448. if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
  449. openAndPrintFile(autoname);
  450. found = true;
  451. }
  452. }
  453. if (!found)
  454. autostart_index = -1;
  455. else
  456. autostart_index++;
  457. }
  458. void CardReader::closefile(bool store_location) {
  459. file.sync();
  460. file.close();
  461. saving = logging = false;
  462. if (store_location) {
  463. //future: store printer state, filename and position for continuing a stopped print
  464. // so one can unplug the printer and continue printing the next day.
  465. }
  466. }
  467. /**
  468. * Get the name of a file in the current directory by index
  469. */
  470. void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
  471. curDir = &workDir;
  472. lsAction = LS_GetFilename;
  473. nrFiles = nr;
  474. curDir->rewind();
  475. lsDive("", *curDir, match);
  476. }
  477. uint16_t CardReader::getnrfilenames() {
  478. curDir = &workDir;
  479. lsAction = LS_Count;
  480. nrFiles = 0;
  481. curDir->rewind();
  482. lsDive("", *curDir);
  483. //SERIAL_ECHOLN(nrFiles);
  484. return nrFiles;
  485. }
  486. void CardReader::chdir(const char * relpath) {
  487. SdFile newfile;
  488. SdFile *parent = &root;
  489. if (workDir.isOpen()) parent = &workDir;
  490. if (!newfile.open(*parent, relpath, O_READ)) {
  491. SERIAL_ECHO_START;
  492. SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
  493. SERIAL_ECHOLN(relpath);
  494. }
  495. else {
  496. if (workDirDepth < MAX_DIR_DEPTH)
  497. workDirParents[workDirDepth++] = *parent;
  498. workDir = newfile;
  499. }
  500. }
  501. void CardReader::updir() {
  502. if (workDirDepth > 0)
  503. workDir = workDirParents[--workDirDepth];
  504. }
  505. void CardReader::printingHasFinished() {
  506. stepper.synchronize();
  507. file.close();
  508. if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
  509. file_subcall_ctr--;
  510. openFile(proc_filenames[file_subcall_ctr], true, true);
  511. setIndex(filespos[file_subcall_ctr]);
  512. startFileprint();
  513. }
  514. else {
  515. sdprinting = false;
  516. if (SD_FINISHED_STEPPERRELEASE)
  517. enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  518. print_job_timer.stop();
  519. if (print_job_timer.duration() > 60)
  520. enqueue_and_echo_commands_P(PSTR("M31"));
  521. }
  522. }
  523. #endif //SDSUPPORT