Firmware2/Marlin/src/sd/SdVolume.h

202 lines
8.2 KiB
C
Raw Normal View History

/**
2016-03-24 19:01:20 +01:00
* Marlin 3D Printer Firmware
2020-02-03 15:00:57 +01:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2011-11-18 22:17:37 +01:00
*
2016-03-24 19:01:20 +01:00
* Based on Sprinter and grbl.
2019-06-28 06:57:50 +02:00
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
2011-11-18 22:17:37 +01:00
*
2016-03-24 19:01:20 +01:00
* This program is free software: you can redistribute it and/or modify
2011-11-18 22:17:37 +01:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
2016-03-24 19:01:20 +01:00
* This program is distributed in the hope that it will be useful,
2011-11-18 22:17:37 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2016-03-24 19:01:20 +01:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
2016-03-24 19:01:20 +01:00
2017-11-15 07:06:20 +01:00
/**
* \file
* \brief SdVolume class
*/
/**
2016-03-24 19:01:20 +01:00
* Arduino SdFat Library
2019-06-28 06:57:50 +02:00
* Copyright (c) 2009 by William Greiman
2016-03-24 19:01:20 +01:00
*
* This file is part of the Arduino Sd2Card Library
2011-11-18 22:17:37 +01:00
*/
2017-09-06 13:28:32 +02:00
#include <stdint.h>
#include "../inc/MarlinConfigPre.h"
2018-07-02 17:06:17 +02:00
#if ENABLED(USB_FLASH_DRIVE_SUPPORT)
#include "usb_flashdrive/Sd2Card_FlashDrive.h"
2019-01-04 20:38:07 +01:00
#elif ENABLED(SDIO_SUPPORT)
#include "Sd2Card_sdio.h"
2018-07-02 17:06:17 +02:00
#else
#include "Sd2Card.h"
#endif
2011-11-18 22:17:37 +01:00
#include "SdFatConfig.h"
#include "SdFatStructs.h"
2017-09-06 13:28:32 +02:00
2011-11-18 22:17:37 +01:00
//==============================================================================
// SdVolume class
/**
* \brief Cache for an SD data block
*/
union cache_t {
2017-11-15 07:06:20 +01:00
uint8_t data[512]; // Used to access cached file data blocks.
uint16_t fat16[256]; // Used to access cached FAT16 entries.
uint32_t fat32[128]; // Used to access cached FAT32 entries.
dir_t dir[16]; // Used to access cached directory entries.
mbr_t mbr; // Used to access a cached Master Boot Record.
fat_boot_t fbs; // Used to access to a cached FAT boot sector.
fat32_boot_t fbs32; // Used to access to a cached FAT32 boot sector.
fat32_fsinfo_t fsinfo; // Used to access to a cached FAT32 FSINFO sector.
2011-11-18 22:17:37 +01:00
};
2017-11-15 07:06:20 +01:00
2011-11-18 22:17:37 +01:00
/**
* \class SdVolume
* \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
*/
class SdVolume {
public:
2017-11-15 07:06:20 +01:00
// Create an instance of SdVolume
2011-11-18 22:17:37 +01:00
SdVolume() : fatType_(0) {}
2017-11-15 07:06:20 +01:00
/**
* Clear the cache and returns a pointer to the cache. Used by the WaveRP
2011-11-18 22:17:37 +01:00
* recorder to do raw write to the SD card. Not for normal apps.
* \return A pointer to the cache buffer or zero if an error occurs.
*/
cache_t* cacheClear() {
if (!cacheFlush()) return 0;
2017-08-09 20:23:14 +02:00
cacheBlockNumber_ = 0xFFFFFFFF;
2011-11-18 22:17:37 +01:00
return &cacheBuffer_;
}
2017-11-15 07:06:20 +01:00
/**
* Initialize a FAT volume. Try partition one first then try super
2011-11-18 22:17:37 +01:00
* floppy format.
*
* \param[in] dev The Sd2Card where the volume is located.
*
2017-11-15 07:06:20 +01:00
* \return true for success, false for failure.
* Reasons for failure include not finding a valid partition, not finding
* a valid FAT file system or an I/O error.
2011-11-18 22:17:37 +01:00
*/
2017-11-15 07:06:20 +01:00
bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0); }
2011-11-18 22:17:37 +01:00
bool init(Sd2Card* dev, uint8_t part);
// inline functions that return volume info
2017-11-15 07:06:20 +01:00
uint8_t blocksPerCluster() const { return blocksPerCluster_; } //> \return The volume's cluster size in blocks.
uint32_t blocksPerFat() const { return blocksPerFat_; } //> \return The number of blocks in one FAT.
uint32_t clusterCount() const { return clusterCount_; } //> \return The total number of clusters in the volume.
uint8_t clusterSizeShift() const { return clusterSizeShift_; } //> \return The shift count required to multiply by blocksPerCluster.
uint32_t dataStartBlock() const { return dataStartBlock_; } //> \return The logical block number for the start of file data.
uint8_t fatCount() const { return fatCount_; } //> \return The number of FAT structures on the volume.
uint32_t fatStartBlock() const { return fatStartBlock_; } //> \return The logical block number for the start of the first FAT.
uint8_t fatType() const { return fatType_; } //> \return The FAT type of the volume. Values are 12, 16 or 32.
2011-11-18 22:17:37 +01:00
int32_t freeClusterCount();
2017-11-15 07:06:20 +01:00
uint32_t rootDirEntryCount() const { return rootDirEntryCount_; } /** \return The number of entries in the root directory for FAT16 volumes. */
/**
* \return The logical block number for the start of the root directory
* on FAT16 volumes or the first cluster number on FAT32 volumes.
*/
uint32_t rootDirStart() const { return rootDirStart_; }
/**
* Sd2Card object for this volume
2011-11-18 22:17:37 +01:00
* \return pointer to Sd2Card object.
*/
2017-11-15 07:06:20 +01:00
Sd2Card* sdCard() { return sdCard_; }
/**
* Debug access to FAT table
2011-11-18 22:17:37 +01:00
*
* \param[in] n cluster number.
* \param[out] v value of entry
* \return true for success or false for failure
*/
2017-11-15 07:06:20 +01:00
bool dbgFat(uint32_t n, uint32_t* v) { return fatGet(n, v); }
2011-11-18 22:17:37 +01:00
private:
// Allow SdBaseFile access to SdVolume private data.
friend class SdBaseFile;
// value for dirty argument in cacheRawBlock to indicate read from cache
static bool const CACHE_FOR_READ = false;
// value for dirty argument in cacheRawBlock to indicate write to cache
static bool const CACHE_FOR_WRITE = true;
2017-09-06 13:28:32 +02:00
#if USE_MULTIPLE_CARDS
cache_t cacheBuffer_; // 512 byte cache for device blocks
uint32_t cacheBlockNumber_; // Logical number of block in the cache
Sd2Card* sdCard_; // Sd2Card object for cache
bool cacheDirty_; // cacheFlush() will write block if true
uint32_t cacheMirrorBlock_; // block number for mirror FAT
2017-11-15 07:06:20 +01:00
#else
2017-09-06 13:28:32 +02:00
static cache_t cacheBuffer_; // 512 byte cache for device blocks
static uint32_t cacheBlockNumber_; // Logical number of block in the cache
static Sd2Card* sdCard_; // Sd2Card object for cache
static bool cacheDirty_; // cacheFlush() will write block if true
static uint32_t cacheMirrorBlock_; // block number for mirror FAT
2017-11-15 07:06:20 +01:00
#endif
2011-11-18 22:17:37 +01:00
uint32_t allocSearchStart_; // start cluster for alloc search
uint8_t blocksPerCluster_; // cluster size in blocks
uint32_t blocksPerFat_; // FAT size in blocks
uint32_t clusterCount_; // clusters in one FAT
uint8_t clusterSizeShift_; // shift to convert cluster count to block count
uint32_t dataStartBlock_; // first data block number
uint8_t fatCount_; // number of FATs on volume
uint32_t fatStartBlock_; // start block for first FAT
uint8_t fatType_; // volume type (12, 16, OR 32)
uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
2017-11-15 07:06:20 +01:00
2011-11-18 22:17:37 +01:00
bool allocContiguous(uint32_t count, uint32_t* curCluster);
2017-11-15 07:06:20 +01:00
uint8_t blockOfCluster(uint32_t position) const { return (position >> 9) & (blocksPerCluster_ - 1); }
uint32_t clusterStartBlock(uint32_t cluster) const { return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_); }
uint32_t blockNumber(uint32_t cluster, uint32_t position) const { return clusterStartBlock(cluster) + blockOfCluster(position); }
cache_t* cache() { return &cacheBuffer_; }
uint32_t cacheBlockNumber() const { return cacheBlockNumber_; }
2017-09-06 13:28:32 +02:00
#if USE_MULTIPLE_CARDS
bool cacheFlush();
bool cacheRawBlock(uint32_t blockNumber, bool dirty);
2017-11-15 07:06:20 +01:00
#else
2017-09-06 13:28:32 +02:00
static bool cacheFlush();
static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
2017-11-15 07:06:20 +01:00
#endif
2011-11-18 22:17:37 +01:00
// used by SdBaseFile write to assign cache to SD location
void cacheSetBlockNumber(uint32_t blockNumber, bool dirty) {
cacheDirty_ = dirty;
cacheBlockNumber_ = blockNumber;
}
2017-11-15 07:06:20 +01:00
void cacheSetDirty() { cacheDirty_ |= CACHE_FOR_WRITE; }
2011-11-18 22:17:37 +01:00
bool chainSize(uint32_t beginCluster, uint32_t* size);
bool fatGet(uint32_t cluster, uint32_t* value);
bool fatPut(uint32_t cluster, uint32_t value);
2017-11-15 07:06:20 +01:00
bool fatPutEOC(uint32_t cluster) { return fatPut(cluster, 0x0FFFFFFF); }
2011-11-18 22:17:37 +01:00
bool freeChain(uint32_t cluster);
bool isEOC(uint32_t cluster) const {
if (FAT12_SUPPORT && fatType_ == 12) return cluster >= FAT12EOC_MIN;
if (fatType_ == 16) return cluster >= FAT16EOC_MIN;
return cluster >= FAT32EOC_MIN;
}
2017-11-15 07:06:20 +01:00
bool readBlock(uint32_t block, uint8_t* dst) { return sdCard_->readBlock(block, dst); }
bool writeBlock(uint32_t block, const uint8_t* dst) { return sdCard_->writeBlock(block, dst); }
2011-11-18 22:17:37 +01:00
};