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.

SdVolume.h 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #ifndef SDVOLUME_H
  29. #define SDVOLUME_H
  30. /**
  31. * \file
  32. * \brief SdVolume class
  33. */
  34. #include "SdFatConfig.h"
  35. #include "Sd2Card.h"
  36. #include "SdFatStructs.h"
  37. #include <stdint.h>
  38. //==============================================================================
  39. // SdVolume class
  40. /**
  41. * \brief Cache for an SD data block
  42. */
  43. union cache_t {
  44. /** Used to access cached file data blocks. */
  45. uint8_t data[512];
  46. /** Used to access cached FAT16 entries. */
  47. uint16_t fat16[256];
  48. /** Used to access cached FAT32 entries. */
  49. uint32_t fat32[128];
  50. /** Used to access cached directory entries. */
  51. dir_t dir[16];
  52. /** Used to access a cached Master Boot Record. */
  53. mbr_t mbr;
  54. /** Used to access to a cached FAT boot sector. */
  55. fat_boot_t fbs;
  56. /** Used to access to a cached FAT32 boot sector. */
  57. fat32_boot_t fbs32;
  58. /** Used to access to a cached FAT32 FSINFO sector. */
  59. fat32_fsinfo_t fsinfo;
  60. };
  61. //------------------------------------------------------------------------------
  62. /**
  63. * \class SdVolume
  64. * \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
  65. */
  66. class SdVolume {
  67. public:
  68. /** Create an instance of SdVolume */
  69. SdVolume() : fatType_(0) {}
  70. /** Clear the cache and returns a pointer to the cache. Used by the WaveRP
  71. * recorder to do raw write to the SD card. Not for normal apps.
  72. * \return A pointer to the cache buffer or zero if an error occurs.
  73. */
  74. cache_t* cacheClear() {
  75. if (!cacheFlush()) return 0;
  76. cacheBlockNumber_ = 0xFFFFFFFF;
  77. return &cacheBuffer_;
  78. }
  79. /** Initialize a FAT volume. Try partition one first then try super
  80. * floppy format.
  81. *
  82. * \param[in] dev The Sd2Card where the volume is located.
  83. *
  84. * \return The value one, true, is returned for success and
  85. * the value zero, false, is returned for failure. Reasons for
  86. * failure include not finding a valid partition, not finding a valid
  87. * FAT file system or an I/O error.
  88. */
  89. bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
  90. bool init(Sd2Card* dev, uint8_t part);
  91. // inline functions that return volume info
  92. /** \return The volume's cluster size in blocks. */
  93. uint8_t blocksPerCluster() const {return blocksPerCluster_;}
  94. /** \return The number of blocks in one FAT. */
  95. uint32_t blocksPerFat() const {return blocksPerFat_;}
  96. /** \return The total number of clusters in the volume. */
  97. uint32_t clusterCount() const {return clusterCount_;}
  98. /** \return The shift count required to multiply by blocksPerCluster. */
  99. uint8_t clusterSizeShift() const {return clusterSizeShift_;}
  100. /** \return The logical block number for the start of file data. */
  101. uint32_t dataStartBlock() const {return dataStartBlock_;}
  102. /** \return The number of FAT structures on the volume. */
  103. uint8_t fatCount() const {return fatCount_;}
  104. /** \return The logical block number for the start of the first FAT. */
  105. uint32_t fatStartBlock() const {return fatStartBlock_;}
  106. /** \return The FAT type of the volume. Values are 12, 16 or 32. */
  107. uint8_t fatType() const {return fatType_;}
  108. int32_t freeClusterCount();
  109. /** \return The number of entries in the root directory for FAT16 volumes. */
  110. uint32_t rootDirEntryCount() const {return rootDirEntryCount_;}
  111. /** \return The logical block number for the start of the root directory
  112. on FAT16 volumes or the first cluster number on FAT32 volumes. */
  113. uint32_t rootDirStart() const {return rootDirStart_;}
  114. /** Sd2Card object for this volume
  115. * \return pointer to Sd2Card object.
  116. */
  117. Sd2Card* sdCard() {return sdCard_;}
  118. /** Debug access to FAT table
  119. *
  120. * \param[in] n cluster number.
  121. * \param[out] v value of entry
  122. * \return true for success or false for failure
  123. */
  124. bool dbgFat(uint32_t n, uint32_t* v) {return fatGet(n, v);}
  125. //------------------------------------------------------------------------------
  126. private:
  127. // Allow SdBaseFile access to SdVolume private data.
  128. friend class SdBaseFile;
  129. // value for dirty argument in cacheRawBlock to indicate read from cache
  130. static bool const CACHE_FOR_READ = false;
  131. // value for dirty argument in cacheRawBlock to indicate write to cache
  132. static bool const CACHE_FOR_WRITE = true;
  133. #if USE_MULTIPLE_CARDS
  134. cache_t cacheBuffer_; // 512 byte cache for device blocks
  135. uint32_t cacheBlockNumber_; // Logical number of block in the cache
  136. Sd2Card* sdCard_; // Sd2Card object for cache
  137. bool cacheDirty_; // cacheFlush() will write block if true
  138. uint32_t cacheMirrorBlock_; // block number for mirror FAT
  139. #else // USE_MULTIPLE_CARDS
  140. static cache_t cacheBuffer_; // 512 byte cache for device blocks
  141. static uint32_t cacheBlockNumber_; // Logical number of block in the cache
  142. static Sd2Card* sdCard_; // Sd2Card object for cache
  143. static bool cacheDirty_; // cacheFlush() will write block if true
  144. static uint32_t cacheMirrorBlock_; // block number for mirror FAT
  145. #endif // USE_MULTIPLE_CARDS
  146. uint32_t allocSearchStart_; // start cluster for alloc search
  147. uint8_t blocksPerCluster_; // cluster size in blocks
  148. uint32_t blocksPerFat_; // FAT size in blocks
  149. uint32_t clusterCount_; // clusters in one FAT
  150. uint8_t clusterSizeShift_; // shift to convert cluster count to block count
  151. uint32_t dataStartBlock_; // first data block number
  152. uint8_t fatCount_; // number of FATs on volume
  153. uint32_t fatStartBlock_; // start block for first FAT
  154. uint8_t fatType_; // volume type (12, 16, OR 32)
  155. uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
  156. uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
  157. //----------------------------------------------------------------------------
  158. bool allocContiguous(uint32_t count, uint32_t* curCluster);
  159. uint8_t blockOfCluster(uint32_t position) const {
  160. return (position >> 9) & (blocksPerCluster_ - 1);
  161. }
  162. uint32_t clusterStartBlock(uint32_t cluster) const {
  163. return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);
  164. }
  165. uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
  166. return clusterStartBlock(cluster) + blockOfCluster(position);
  167. }
  168. cache_t* cache() {return &cacheBuffer_;}
  169. uint32_t cacheBlockNumber() {return cacheBlockNumber_;}
  170. #if USE_MULTIPLE_CARDS
  171. bool cacheFlush();
  172. bool cacheRawBlock(uint32_t blockNumber, bool dirty);
  173. #else // USE_MULTIPLE_CARDS
  174. static bool cacheFlush();
  175. static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
  176. #endif // USE_MULTIPLE_CARDS
  177. // used by SdBaseFile write to assign cache to SD location
  178. void cacheSetBlockNumber(uint32_t blockNumber, bool dirty) {
  179. cacheDirty_ = dirty;
  180. cacheBlockNumber_ = blockNumber;
  181. }
  182. void cacheSetDirty() {cacheDirty_ |= CACHE_FOR_WRITE;}
  183. bool chainSize(uint32_t beginCluster, uint32_t* size);
  184. bool fatGet(uint32_t cluster, uint32_t* value);
  185. bool fatPut(uint32_t cluster, uint32_t value);
  186. bool fatPutEOC(uint32_t cluster) {
  187. return fatPut(cluster, 0x0FFFFFFF);
  188. }
  189. bool freeChain(uint32_t cluster);
  190. bool isEOC(uint32_t cluster) const {
  191. if (FAT12_SUPPORT && fatType_ == 12) return cluster >= FAT12EOC_MIN;
  192. if (fatType_ == 16) return cluster >= FAT16EOC_MIN;
  193. return cluster >= FAT32EOC_MIN;
  194. }
  195. bool readBlock(uint32_t block, uint8_t* dst) {
  196. return sdCard_->readBlock(block, dst);
  197. }
  198. bool writeBlock(uint32_t block, const uint8_t* dst) {
  199. return sdCard_->writeBlock(block, dst);
  200. }
  201. //------------------------------------------------------------------------------
  202. // Deprecated functions - suppress cpplint warnings with NOLINT comment
  203. #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
  204. public:
  205. /** \deprecated Use: bool SdVolume::init(Sd2Card* dev);
  206. * \param[in] dev The SD card where the volume is located.
  207. * \return true for success or false for failure.
  208. */
  209. bool init(Sd2Card& dev) {return init(&dev);} // NOLINT
  210. /** \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol);
  211. * \param[in] dev The SD card where the volume is located.
  212. * \param[in] part The partition to be used.
  213. * \return true for success or false for failure.
  214. */
  215. bool init(Sd2Card& dev, uint8_t part) { // NOLINT
  216. return init(&dev, part);
  217. }
  218. #endif // ALLOW_DEPRECATED_FUNCTIONS
  219. };
  220. #endif // SDVOLUME_H