SdVolume.h 9.0 KB

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