Subversion Repositories MK-Marlin

Rev

Details | Last modification | View Log | RSS feed

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