My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SdVolume.cpp 11KB

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