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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Arduino SdFat Library
  24. * Copyright (c) 2009 by William Greiman
  25. *
  26. * This file is part of the Arduino Sd2Card Library
  27. */
  28. #include "../inc/MarlinConfig.h"
  29. #if ENABLED(SDSUPPORT)
  30. #include "SdVolume.h"
  31. #include "../MarlinCore.h"
  32. #if !USE_MULTIPLE_CARDS
  33. // raw block cache
  34. uint32_t SdVolume::cacheBlockNumber_; // current block number
  35. cache_t SdVolume::cacheBuffer_; // 512 byte cache for Sd2Card
  36. Sd2Card* SdVolume::sdCard_; // pointer to SD card object
  37. bool SdVolume::cacheDirty_; // cacheFlush() will write block if true
  38. uint32_t SdVolume::cacheMirrorBlock_; // mirror block for second FAT
  39. #endif // USE_MULTIPLE_CARDS
  40. // find a contiguous group of clusters
  41. bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
  42. // start of group
  43. uint32_t bgnCluster;
  44. // end of group
  45. uint32_t endCluster;
  46. // last cluster of FAT
  47. uint32_t fatEnd = clusterCount_ + 1;
  48. // flag to save place to start next search
  49. bool setStart;
  50. // set search start cluster
  51. if (*curCluster) {
  52. // try to make file contiguous
  53. bgnCluster = *curCluster + 1;
  54. // don't save new start location
  55. setStart = false;
  56. }
  57. else {
  58. // start at likely place for free cluster
  59. bgnCluster = allocSearchStart_;
  60. // save next search start if one cluster
  61. setStart = count == 1;
  62. }
  63. // end of group
  64. endCluster = bgnCluster;
  65. // search the FAT for free clusters
  66. for (uint32_t n = 0;; n++, endCluster++) {
  67. // can't find space checked all clusters
  68. if (n >= clusterCount_) return false;
  69. // past end - start from beginning of FAT
  70. if (endCluster > fatEnd) {
  71. bgnCluster = endCluster = 2;
  72. }
  73. uint32_t f;
  74. if (!fatGet(endCluster, &f)) return false;
  75. if (f != 0) {
  76. // cluster in use try next cluster as bgnCluster
  77. bgnCluster = endCluster + 1;
  78. }
  79. else if ((endCluster - bgnCluster + 1) == count) {
  80. // done - found space
  81. break;
  82. }
  83. }
  84. // mark end of chain
  85. if (!fatPutEOC(endCluster)) return false;
  86. // link clusters
  87. while (endCluster > bgnCluster) {
  88. if (!fatPut(endCluster - 1, endCluster)) return false;
  89. endCluster--;
  90. }
  91. if (*curCluster != 0) {
  92. // connect chains
  93. if (!fatPut(*curCluster, bgnCluster)) return false;
  94. }
  95. // return first cluster number to caller
  96. *curCluster = bgnCluster;
  97. // remember possible next free cluster
  98. if (setStart) allocSearchStart_ = bgnCluster + 1;
  99. return true;
  100. }
  101. bool SdVolume::cacheFlush() {
  102. if (cacheDirty_) {
  103. if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data))
  104. return false;
  105. // mirror FAT tables
  106. if (cacheMirrorBlock_) {
  107. if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data))
  108. return false;
  109. cacheMirrorBlock_ = 0;
  110. }
  111. cacheDirty_ = 0;
  112. }
  113. return true;
  114. }
  115. bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
  116. if (cacheBlockNumber_ != blockNumber) {
  117. if (!cacheFlush()) return false;
  118. if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) return false;
  119. cacheBlockNumber_ = blockNumber;
  120. }
  121. if (dirty) cacheDirty_ = true;
  122. return true;
  123. }
  124. // return the size in bytes of a cluster chain
  125. bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
  126. uint32_t s = 0;
  127. do {
  128. if (!fatGet(cluster, &cluster)) return false;
  129. s += 512UL << clusterSizeShift_;
  130. } while (!isEOC(cluster));
  131. *size = s;
  132. return true;
  133. }
  134. // Fetch a FAT entry
  135. bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
  136. uint32_t lba;
  137. if (cluster > (clusterCount_ + 1)) return false;
  138. if (FAT12_SUPPORT && fatType_ == 12) {
  139. uint16_t index = cluster;
  140. index += index >> 1;
  141. lba = fatStartBlock_ + (index >> 9);
  142. if (!cacheRawBlock(lba, CACHE_FOR_READ)) return false;
  143. index &= 0x1FF;
  144. uint16_t tmp = cacheBuffer_.data[index];
  145. index++;
  146. if (index == 512) {
  147. if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) return false;
  148. index = 0;
  149. }
  150. tmp |= cacheBuffer_.data[index] << 8;
  151. *value = cluster & 1 ? tmp >> 4 : tmp & 0xFFF;
  152. return true;
  153. }
  154. if (fatType_ == 16)
  155. lba = fatStartBlock_ + (cluster >> 8);
  156. else if (fatType_ == 32)
  157. lba = fatStartBlock_ + (cluster >> 7);
  158. else
  159. return false;
  160. if (lba != cacheBlockNumber_ && !cacheRawBlock(lba, CACHE_FOR_READ))
  161. return false;
  162. *value = (fatType_ == 16) ? cacheBuffer_.fat16[cluster & 0xFF] : (cacheBuffer_.fat32[cluster & 0x7F] & FAT32MASK);
  163. return true;
  164. }
  165. // Store a FAT entry
  166. bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
  167. uint32_t lba;
  168. // error if reserved cluster
  169. if (cluster < 2) return false;
  170. // error if not in FAT
  171. if (cluster > (clusterCount_ + 1)) return false;
  172. if (FAT12_SUPPORT && fatType_ == 12) {
  173. uint16_t index = cluster;
  174. index += index >> 1;
  175. lba = fatStartBlock_ + (index >> 9);
  176. if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
  177. // mirror second FAT
  178. if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
  179. index &= 0x1FF;
  180. uint8_t tmp = value;
  181. if (cluster & 1) {
  182. tmp = (cacheBuffer_.data[index] & 0xF) | tmp << 4;
  183. }
  184. cacheBuffer_.data[index] = tmp;
  185. index++;
  186. if (index == 512) {
  187. lba++;
  188. index = 0;
  189. if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
  190. // mirror second FAT
  191. if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
  192. }
  193. tmp = value >> 4;
  194. if (!(cluster & 1)) {
  195. tmp = ((cacheBuffer_.data[index] & 0xF0)) | tmp >> 4;
  196. }
  197. cacheBuffer_.data[index] = tmp;
  198. return true;
  199. }
  200. if (fatType_ == 16)
  201. lba = fatStartBlock_ + (cluster >> 8);
  202. else if (fatType_ == 32)
  203. lba = fatStartBlock_ + (cluster >> 7);
  204. else
  205. return false;
  206. if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
  207. // store entry
  208. if (fatType_ == 16)
  209. cacheBuffer_.fat16[cluster & 0xFF] = value;
  210. else
  211. cacheBuffer_.fat32[cluster & 0x7F] = value;
  212. // mirror second FAT
  213. if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
  214. return true;
  215. }
  216. // free a cluster chain
  217. bool SdVolume::freeChain(uint32_t cluster) {
  218. // clear free cluster location
  219. allocSearchStart_ = 2;
  220. do {
  221. uint32_t next;
  222. if (!fatGet(cluster, &next)) return false;
  223. // free cluster
  224. if (!fatPut(cluster, 0)) return false;
  225. cluster = next;
  226. } while (!isEOC(cluster));
  227. return true;
  228. }
  229. /** Volume free space in clusters.
  230. *
  231. * \return Count of free clusters for success or -1 if an error occurs.
  232. */
  233. int32_t SdVolume::freeClusterCount() {
  234. uint32_t free = 0;
  235. uint16_t n;
  236. uint32_t todo = clusterCount_ + 2;
  237. if (fatType_ == 16)
  238. n = 256;
  239. else if (fatType_ == 32)
  240. n = 128;
  241. else // put FAT12 here
  242. return -1;
  243. for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) {
  244. if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1;
  245. NOMORE(n, todo);
  246. if (fatType_ == 16) {
  247. for (uint16_t i = 0; i < n; i++)
  248. if (cacheBuffer_.fat16[i] == 0) free++;
  249. }
  250. else {
  251. for (uint16_t i = 0; i < n; i++)
  252. if (cacheBuffer_.fat32[i] == 0) free++;
  253. }
  254. #ifdef ESP32
  255. // Needed to reset the idle task watchdog timer on ESP32 as reading the complete FAT may easily
  256. // block for 10+ seconds. yield() is insufficient since it blocks lower prio tasks (e.g., idle).
  257. static millis_t nextTaskTime = 0;
  258. const millis_t ms = millis();
  259. if (ELAPSED(ms, nextTaskTime)) {
  260. vTaskDelay(1); // delay 1 tick (Minimum. Usually 10 or 1 ms depending on skdconfig.h)
  261. nextTaskTime = ms + 1000; // tickle the task manager again in 1 second
  262. }
  263. #endif // ESP32
  264. }
  265. return free;
  266. }
  267. /** Initialize a FAT volume.
  268. *
  269. * \param[in] dev The SD card where the volume is located.
  270. *
  271. * \param[in] part The partition to be used. Legal values for \a part are
  272. * 1-4 to use the corresponding partition on a device formatted with
  273. * a MBR, Master Boot Record, or zero if the device is formatted as
  274. * a super floppy with the FAT boot sector in block zero.
  275. *
  276. * \return true for success, false for failure.
  277. * Reasons for failure include not finding a valid partition, not finding a valid
  278. * FAT file system in the specified partition or an I/O error.
  279. */
  280. bool SdVolume::init(Sd2Card* dev, uint8_t part) {
  281. uint32_t totalBlocks, volumeStartBlock = 0;
  282. fat32_boot_t* fbs;
  283. sdCard_ = dev;
  284. fatType_ = 0;
  285. allocSearchStart_ = 2;
  286. cacheDirty_ = 0; // cacheFlush() will write block if true
  287. cacheMirrorBlock_ = 0;
  288. cacheBlockNumber_ = 0xFFFFFFFF;
  289. // if part == 0 assume super floppy with FAT boot sector in block zero
  290. // if part > 0 assume mbr volume with partition table
  291. if (part) {
  292. if (part > 4) return false;
  293. if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
  294. part_t* p = &cacheBuffer_.mbr.part[part - 1];
  295. if ((p->boot & 0x7F) != 0 || p->totalSectors < 100 || p->firstSector == 0)
  296. return false; // not a valid partition
  297. volumeStartBlock = p->firstSector;
  298. }
  299. if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
  300. fbs = &cacheBuffer_.fbs32;
  301. if (fbs->bytesPerSector != 512 ||
  302. fbs->fatCount == 0 ||
  303. fbs->reservedSectorCount == 0 ||
  304. fbs->sectorsPerCluster == 0) {
  305. // not valid FAT volume
  306. return false;
  307. }
  308. fatCount_ = fbs->fatCount;
  309. blocksPerCluster_ = fbs->sectorsPerCluster;
  310. // determine shift that is same as multiply by blocksPerCluster_
  311. clusterSizeShift_ = 0;
  312. while (blocksPerCluster_ != _BV(clusterSizeShift_)) {
  313. // error if not power of 2
  314. if (clusterSizeShift_++ > 7) return false;
  315. }
  316. blocksPerFat_ = fbs->sectorsPerFat16 ?
  317. fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
  318. fatStartBlock_ = volumeStartBlock + fbs->reservedSectorCount;
  319. // count for FAT16 zero for FAT32
  320. rootDirEntryCount_ = fbs->rootDirEntryCount;
  321. // directory start for FAT16 dataStart for FAT32
  322. rootDirStart_ = fatStartBlock_ + fbs->fatCount * blocksPerFat_;
  323. // data start for FAT16 and FAT32
  324. dataStartBlock_ = rootDirStart_ + ((32 * fbs->rootDirEntryCount + 511) / 512);
  325. // total blocks for FAT16 or FAT32
  326. totalBlocks = fbs->totalSectors16 ?
  327. fbs->totalSectors16 : fbs->totalSectors32;
  328. // total data blocks
  329. clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
  330. // divide by cluster size to get cluster count
  331. clusterCount_ >>= clusterSizeShift_;
  332. // FAT type is determined by cluster count
  333. if (clusterCount_ < 4085) {
  334. fatType_ = 12;
  335. if (!FAT12_SUPPORT) return false;
  336. }
  337. else if (clusterCount_ < 65525)
  338. fatType_ = 16;
  339. else {
  340. rootDirStart_ = fbs->fat32RootCluster;
  341. fatType_ = 32;
  342. }
  343. return true;
  344. }
  345. #endif // SDSUPPORT