Prusa3d Marlin fork
SdVolume.h
Go to the documentation of this file.
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
28 #include "SdFatConfig.h"
29 #include "Sd2Card.h"
30 #include "SdFatStructs.h"
31 
32 //==============================================================================
33 // SdVolume class
37 union cache_t {
39  uint8_t data[512 + 1]; // abuse the last byte for saving '\n' - ugly optimization of read_filtered's inner skipping loop
41  uint16_t fat16[256];
43  uint32_t fat32[128];
45  dir_t dir[16];
54 };
55 //------------------------------------------------------------------------------
60 class SdVolume {
61  public:
63  SdVolume() : fatType_(0) {}
69  if (!cacheFlush()) return 0;
70  cacheBlockNumber_ = 0XFFFFFFFF;
71  return &cacheBuffer_;
72  }
83  bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
84  bool init(Sd2Card* dev, uint8_t part);
85 
86  // inline functions that return volume info
88  uint8_t blocksPerCluster() const {return blocksPerCluster_;}
90  uint32_t blocksPerFat() const {return blocksPerFat_;}
92  uint32_t clusterCount() const {return clusterCount_;}
94  uint8_t clusterSizeShift() const {return clusterSizeShift_;}
96  uint32_t dataStartBlock() const {return dataStartBlock_;}
98  uint8_t fatCount() const {return fatCount_;}
100  uint32_t fatStartBlock() const {return fatStartBlock_;}
102  uint8_t fatType() const {return fatType_;}
103  int32_t freeClusterCount();
105  uint32_t rootDirEntryCount() const {return rootDirEntryCount_;}
108  uint32_t rootDirStart() const {return rootDirStart_;}
112  Sd2Card* sdCard() {return sdCard_;}
119  bool dbgFat(uint32_t n, uint32_t* v) {return fatGet(n, v);}
120 //------------------------------------------------------------------------------
121  private:
122  friend class SdFile;
123  // Allow SdBaseFile access to SdVolume private data.
124  friend class SdBaseFile;
125 
126  // value for dirty argument in cacheRawBlock to indicate read from cache
127  static bool const CACHE_FOR_READ = false;
128  // value for dirty argument in cacheRawBlock to indicate write to cache
129  static bool const CACHE_FOR_WRITE = true;
130 
131 #if USE_MULTIPLE_CARDS
132  cache_t cacheBuffer_; // 512 byte cache for device blocks
133  uint32_t cacheBlockNumber_; // Logical number of block in the cache
134  Sd2Card* sdCard_; // Sd2Card object for cache
135  bool cacheDirty_; // cacheFlush() will write block if true
136  uint32_t cacheMirrorBlock_; // block number for mirror FAT
137 #else // USE_MULTIPLE_CARDS
138  static cache_t cacheBuffer_; // 512 byte cache for device blocks
139  static uint32_t cacheBlockNumber_; // Logical number of block in the cache
140  static Sd2Card* sdCard_; // Sd2Card object for cache
141  static bool cacheDirty_; // cacheFlush() will write block if true
142  static uint32_t cacheMirrorBlock_; // block number for mirror FAT
143 #endif // USE_MULTIPLE_CARDS
144  uint32_t allocSearchStart_; // start cluster for alloc search
145  uint8_t blocksPerCluster_; // cluster size in blocks
146  uint32_t blocksPerFat_; // FAT size in blocks
147  uint32_t clusterCount_; // clusters in one FAT
148  uint8_t clusterSizeShift_; // shift to convert cluster count to block count
149  uint32_t dataStartBlock_; // first data block number
150  uint8_t fatCount_; // number of FATs on volume
151  uint32_t fatStartBlock_; // start block for first FAT
152  uint8_t fatType_; // volume type (12, 16, OR 32)
153  uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
154  uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
155  //----------------------------------------------------------------------------
156  bool allocContiguous(uint32_t count, uint32_t* curCluster);
157  uint8_t blockOfCluster(uint32_t position) const {
158  return (position >> 9) & (blocksPerCluster_ - 1);}
159  uint32_t clusterStartBlock(uint32_t cluster) const {
160  return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);}
161  uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
162  return clusterStartBlock(cluster) + blockOfCluster(position);}
163  cache_t *cache() {return &cacheBuffer_;}
164  uint32_t cacheBlockNumber() {return cacheBlockNumber_;}
165 #if USE_MULTIPLE_CARDS
166  bool cacheFlush();
167  bool cacheRawBlock(uint32_t blockNumber, bool dirty);
168 #else // USE_MULTIPLE_CARDS
169  static bool cacheFlush();
170  static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
171 #endif // USE_MULTIPLE_CARDS
172  // used by SdBaseFile write to assign cache to SD location
173  void cacheSetBlockNumber(uint32_t blockNumber, bool dirty) {
174  cacheDirty_ = dirty;
175  cacheBlockNumber_ = blockNumber;
176  }
177  void cacheSetDirty() {cacheDirty_ |= CACHE_FOR_WRITE;}
178  bool chainSize(uint32_t beginCluster, uint32_t* size);
179  bool fatGet(uint32_t cluster, uint32_t* value);
180  bool fatPut(uint32_t cluster, uint32_t value);
181  bool fatPutEOC(uint32_t cluster) {
182  return fatPut(cluster, 0x0FFFFFFF);
183  }
184  bool freeChain(uint32_t cluster);
185  bool isEOC(uint32_t cluster) const {
186  if (FAT12_SUPPORT && fatType_ == 12) return cluster >= FAT12EOC_MIN;
187  if (fatType_ == 16) return cluster >= FAT16EOC_MIN;
188  return cluster >= FAT32EOC_MIN;
189  }
190  bool readBlock(uint32_t block, uint8_t* dst) {
191  return sdCard_->readBlock(block, dst);}
192  bool writeBlock(uint32_t block, const uint8_t* dst) {
193  return sdCard_->writeBlock(block, dst);
194  }
195 //------------------------------------------------------------------------------
196  // Deprecated functions - suppress cpplint warnings with NOLINT comment
197 #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
198  public:
203  bool init(Sd2Card& dev) {return init(&dev);} // NOLINT
209  bool init(Sd2Card& dev, uint8_t part) { // NOLINT
210  return init(&dev, part);
211  }
212 #endif // ALLOW_DEPRECATED_FUNCTIONS
213 };
214 #endif // SdVolume
215 #endif
Sd2Card class for V2 SD/SDHC cards.
configuration definitions
FAT file structures.
uint16_t const FAT16EOC_MIN
Definition: SdFatStructs.h:458
uint16_t const FAT12EOC_MIN
Definition: SdFatStructs.h:454
uint32_t const FAT32EOC_MIN
Definition: SdFatStructs.h:462
Raw access to SD and SDHC flash memory cards.
Definition: Sd2Card.h:148
bool readBlock(uint32_t block, uint8_t *dst)
Definition: Sd2Card.cpp:393
bool writeBlock(uint32_t blockNumber, const uint8_t *src)
Definition: Sd2Card.cpp:632
Base class for SdFile with Print and C++ streams.
Definition: SdBaseFile.h:182
uint32_t curCluster() const
Definition: SdBaseFile.h:210
SdBaseFile with Print.
Definition: SdFile.h:36
Access FAT16 and FAT32 volumes on SD and SDHC cards.
Definition: SdVolume.h:60
uint32_t rootDirStart() const
Definition: SdVolume.h:108
bool init(Sd2Card *dev)
Definition: SdVolume.h:83
uint8_t fatType() const
Definition: SdVolume.h:102
int32_t freeClusterCount()
Definition: SdVolume.cpp:280
uint8_t fatCount() const
Definition: SdVolume.h:98
SdVolume()
Definition: SdVolume.h:63
bool dbgFat(uint32_t n, uint32_t *v)
Definition: SdVolume.h:119
uint8_t blocksPerCluster() const
Definition: SdVolume.h:88
uint32_t rootDirEntryCount() const
Definition: SdVolume.h:105
uint32_t dataStartBlock() const
Definition: SdVolume.h:96
uint32_t blocksPerFat() const
Definition: SdVolume.h:90
uint8_t clusterSizeShift() const
Definition: SdVolume.h:94
bool init(Sd2Card &dev, uint8_t part)
Definition: SdVolume.h:209
uint32_t fatStartBlock() const
Definition: SdVolume.h:100
cache_t * cacheClear()
Definition: SdVolume.h:68
bool init(Sd2Card &dev)
Definition: SdVolume.h:203
uint32_t clusterCount() const
Definition: SdVolume.h:92
Sd2Card * sdCard()
Definition: SdVolume.h:112
FAT short directory entry.
Definition: SdFatStructs.h:496
Boot sector for a FAT32 volume.
Definition: SdFatStructs.h:262
FSINFO sector for a FAT32 volume.
Definition: SdFatStructs.h:420
Boot sector for a FAT12/FAT16 volume.
Definition: SdFatStructs.h:134
Master Boot Record.
Definition: SdFatStructs.h:111
Cache for an SD data block.
Definition: SdVolume.h:37
uint8_t data[512+1]
Definition: SdVolume.h:39
fat32_fsinfo_t fsinfo
Definition: SdVolume.h:53
uint32_t fat32[128]
Definition: SdVolume.h:43
mbr_t mbr
Definition: SdVolume.h:47
dir_t dir[16]
Definition: SdVolume.h:45
uint16_t fat16[256]
Definition: SdVolume.h:41
fat32_boot_t fbs32
Definition: SdVolume.h:51
fat_boot_t fbs
Definition: SdVolume.h:49