Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

217 lines
9.3KB

  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 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. #ifndef SdVolume_h
  21. #define SdVolume_h
  22. /**
  23. * \file
  24. * \brief SdVolume class
  25. */
  26. #include <SdFatConfig.h>
  27. #include <Sd2Card.h>
  28. #include <utility/FatStructs.h>
  29. //==============================================================================
  30. // SdVolume class
  31. /**
  32. * \brief Cache for an SD data block
  33. */
  34. union cache_t {
  35. /** Used to access cached file data blocks. */
  36. uint8_t data[512];
  37. /** Used to access cached FAT16 entries. */
  38. uint16_t fat16[256];
  39. /** Used to access cached FAT32 entries. */
  40. uint32_t fat32[128];
  41. /** Used to access cached directory entries. */
  42. dir_t dir[16];
  43. /** Used to access a cached Master Boot Record. */
  44. mbr_t mbr;
  45. /** Used to access to a cached FAT boot sector. */
  46. fat_boot_t fbs;
  47. /** Used to access to a cached FAT32 boot sector. */
  48. fat32_boot_t fbs32;
  49. /** Used to access to a cached FAT32 FSINFO sector. */
  50. fat32_fsinfo_t fsinfo;
  51. };
  52. //------------------------------------------------------------------------------
  53. /**
  54. * \class SdVolume
  55. * \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
  56. */
  57. class SdVolume {
  58. public:
  59. /** Create an instance of SdVolume */
  60. SdVolume() : m_fatType(0) {}
  61. /** Clear the cache and returns a pointer to the cache. Used by the WaveRP
  62. * recorder to do raw write to the SD card. Not for normal apps.
  63. * \return A pointer to the cache buffer or zero if an error occurs.
  64. */
  65. cache_t* cacheClear() {
  66. if (!cacheSync()) return 0;
  67. m_cacheBlockNumber = 0XFFFFFFFF;
  68. return &m_cacheBuffer;
  69. }
  70. /** Initialize a FAT volume. Try partition one first then try super
  71. * floppy format.
  72. *
  73. * \param[in] dev The Sd2Card where the volume is located.
  74. *
  75. * \return The value one, true, is returned for success and
  76. * the value zero, false, is returned for failure. Reasons for
  77. * failure include not finding a valid partition, not finding a valid
  78. * FAT file system or an I/O error.
  79. */
  80. bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
  81. bool init(Sd2Card* dev, uint8_t part);
  82. // inline functions that return volume info
  83. /** \return The volume's cluster size in blocks. */
  84. uint8_t blocksPerCluster() const {return m_blocksPerCluster;}
  85. /** \return The number of blocks in one FAT. */
  86. uint32_t blocksPerFat() const {return m_blocksPerFat;}
  87. /** \return The total number of clusters in the volume. */
  88. uint32_t clusterCount() const {return m_clusterCount;}
  89. /** \return The shift count required to multiply by blocksPerCluster. */
  90. uint8_t clusterSizeShift() const {return m_clusterSizeShift;}
  91. /** \return The logical block number for the start of file data. */
  92. uint32_t dataStartBlock() const {return clusterStartBlock(2);}
  93. /** \return The number of FAT structures on the volume. */
  94. uint8_t fatCount() const {return m_fatCount;}
  95. /** \return The logical block number for the start of the first FAT. */
  96. uint32_t fatStartBlock() const {return m_fatStartBlock;}
  97. /** \return The FAT type of the volume. Values are 12, 16 or 32. */
  98. uint8_t fatType() const {return m_fatType;}
  99. int32_t freeClusterCount();
  100. /** \return The number of entries in the root directory for FAT16 volumes. */
  101. uint32_t rootDirEntryCount() const {return m_rootDirEntryCount;}
  102. /** \return The logical block number for the start of the root directory
  103. on FAT16 volumes or the first cluster number on FAT32 volumes. */
  104. uint32_t rootDirStart() const {return m_rootDirStart;}
  105. /** Sd2Card object for this volume
  106. * \return pointer to Sd2Card object.
  107. */
  108. Sd2Card* sdCard() {return m_sdCard;}
  109. /** Debug access to FAT table
  110. *
  111. * \param[in] n cluster number.
  112. * \param[out] v value of entry
  113. * \return true for success or false for failure
  114. */
  115. bool dbgFat(uint32_t n, uint32_t* v) {return fatGet(n, v);}
  116. //------------------------------------------------------------------------------
  117. private:
  118. // Allow SdBaseFile access to SdVolume private data.
  119. friend class SdBaseFile;
  120. //------------------------------------------------------------------------------
  121. uint32_t m_allocSearchStart; // Start cluster for alloc search.
  122. uint8_t m_blocksPerCluster; // Cluster size in blocks.
  123. uint8_t m_clusterBlockMask; // Mask to extract block of cluster.
  124. uint32_t m_clusterCount; // Clusters in one FAT.
  125. uint8_t m_clusterSizeShift; // Cluster count to block count shift.
  126. uint32_t m_dataStartBlock; // First data block number.
  127. uint32_t m_fatStartBlock; // Start block for first FAT.
  128. uint8_t m_fatType; // Volume type (12, 16, OR 32).
  129. uint16_t m_rootDirEntryCount; // Number of entries in FAT16 root dir.
  130. uint32_t m_rootDirStart; // Start block for FAT16, cluster for FAT32.
  131. //------------------------------------------------------------------------------
  132. // block caches
  133. // use of static functions save a bit of flash - maybe not worth complexity
  134. //
  135. static const uint8_t CACHE_STATUS_DIRTY = 1;
  136. static const uint8_t CACHE_STATUS_FAT_BLOCK = 2;
  137. static const uint8_t CACHE_STATUS_MASK
  138. = CACHE_STATUS_DIRTY | CACHE_STATUS_FAT_BLOCK;
  139. static const uint8_t CACHE_OPTION_NO_READ = 4;
  140. // value for option argument in cacheFetch to indicate read from cache
  141. static uint8_t const CACHE_FOR_READ = 0;
  142. // value for option argument in cacheFetch to indicate write to cache
  143. static uint8_t const CACHE_FOR_WRITE = CACHE_STATUS_DIRTY;
  144. // reserve cache block with no read
  145. static uint8_t const CACHE_RESERVE_FOR_WRITE
  146. = CACHE_STATUS_DIRTY | CACHE_OPTION_NO_READ;
  147. #if USE_MULTIPLE_CARDS
  148. uint8_t m_fatCount; // number of FATs on volume
  149. uint32_t m_blocksPerFat; // FAT size in blocks
  150. cache_t m_cacheBuffer; // 512 byte cache for device blocks
  151. uint32_t m_cacheBlockNumber; // Logical number of block in the cache
  152. Sd2Card* m_sdCard; // Sd2Card object for cache
  153. uint8_t m_cacheStatus; // status of cache block
  154. #if USE_SEPARATE_FAT_CACHE
  155. cache_t m_cacheFatBuffer; // 512 byte cache for FAT
  156. uint32_t m_cacheFatBlockNumber; // current Fat block number
  157. uint8_t m_cacheFatStatus; // status of cache Fatblock
  158. #endif // USE_SEPARATE_FAT_CACHE
  159. #else // USE_MULTIPLE_CARDS
  160. static uint8_t m_fatCount; // number of FATs on volume
  161. static uint32_t m_blocksPerFat; // FAT size in blocks
  162. static cache_t m_cacheBuffer; // 512 byte cache for device blocks
  163. static uint32_t m_cacheBlockNumber; // Logical number of block in the cache
  164. static uint8_t m_cacheStatus; // status of cache block
  165. #if USE_SEPARATE_FAT_CACHE
  166. static cache_t m_cacheFatBuffer; // 512 byte cache for FAT
  167. static uint32_t m_cacheFatBlockNumber; // current Fat block number
  168. static uint8_t m_cacheFatStatus; // status of cache Fatblock
  169. #endif // USE_SEPARATE_FAT_CACHE
  170. static Sd2Card* m_sdCard; // Sd2Card object for cache
  171. #endif // USE_MULTIPLE_CARDS
  172. cache_t *cacheAddress() {return &m_cacheBuffer;}
  173. uint32_t cacheBlockNumber() {return m_cacheBlockNumber;}
  174. #if USE_MULTIPLE_CARDS
  175. cache_t* cacheFetch(uint32_t blockNumber, uint8_t options);
  176. cache_t* cacheFetchData(uint32_t blockNumber, uint8_t options);
  177. cache_t* cacheFetchFat(uint32_t blockNumber, uint8_t options);
  178. void cacheInvalidate();
  179. bool cacheSync();
  180. bool cacheWriteData();
  181. bool cacheWriteFat();
  182. #else // USE_MULTIPLE_CARDS
  183. static cache_t* cacheFetch(uint32_t blockNumber, uint8_t options);
  184. static cache_t* cacheFetchData(uint32_t blockNumber, uint8_t options);
  185. static cache_t* cacheFetchFat(uint32_t blockNumber, uint8_t options);
  186. static void cacheInvalidate();
  187. static bool cacheSync();
  188. static bool cacheWriteData();
  189. static bool cacheWriteFat();
  190. #endif // USE_MULTIPLE_CARDS
  191. //------------------------------------------------------------------------------
  192. bool allocContiguous(uint32_t count, uint32_t* curCluster);
  193. uint8_t blockOfCluster(uint32_t position) const {
  194. return (position >> 9) & m_clusterBlockMask;}
  195. uint32_t clusterStartBlock(uint32_t cluster) const;
  196. bool fatGet(uint32_t cluster, uint32_t* value);
  197. bool fatPut(uint32_t cluster, uint32_t value);
  198. bool fatPutEOC(uint32_t cluster) {
  199. return fatPut(cluster, 0x0FFFFFFF);
  200. }
  201. bool freeChain(uint32_t cluster);
  202. bool isEOC(uint32_t cluster) const {
  203. if (FAT12_SUPPORT && m_fatType == 12) return cluster >= FAT12EOC_MIN;
  204. if (m_fatType == 16) return cluster >= FAT16EOC_MIN;
  205. return cluster >= FAT32EOC_MIN;
  206. }
  207. bool readBlock(uint32_t block, uint8_t* dst) {
  208. return m_sdCard->readBlock(block, dst);}
  209. bool writeBlock(uint32_t block, const uint8_t* dst) {
  210. return m_sdCard->writeBlock(block, dst);
  211. }
  212. };
  213. #endif // SdVolume