My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SdVolume.cpp 12KB

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