My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #pragma once
  23. /**
  24. * \file
  25. * \brief SdVolume class
  26. */
  27. /**
  28. * Arduino SdFat Library
  29. * Copyright (c) 2009 by William Greiman
  30. *
  31. * This file is part of the Arduino Sd2Card Library
  32. */
  33. #include <stdint.h>
  34. #include "../inc/MarlinConfigPre.h"
  35. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  36. #include "usb_flashdrive/Sd2Card_FlashDrive.h"
  37. #elif ENABLED(SDIO_SUPPORT)
  38. #include "Sd2Card_sdio.h"
  39. #else
  40. #include "Sd2Card.h"
  41. #endif
  42. #include "SdFatConfig.h"
  43. #include "SdFatStructs.h"
  44. //==============================================================================
  45. // SdVolume class
  46. /**
  47. * \brief Cache for an SD data block
  48. */
  49. union cache_t {
  50. uint8_t data[512]; // Used to access cached file data blocks.
  51. uint16_t fat16[256]; // Used to access cached FAT16 entries.
  52. uint32_t fat32[128]; // Used to access cached FAT32 entries.
  53. dir_t dir[16]; // Used to access cached directory entries.
  54. mbr_t mbr; // Used to access a cached Master Boot Record.
  55. fat_boot_t fbs; // Used to access to a cached FAT boot sector.
  56. fat32_boot_t fbs32; // Used to access to a cached FAT32 boot sector.
  57. fat32_fsinfo_t fsinfo; // Used to access to a cached FAT32 FSINFO sector.
  58. };
  59. /**
  60. * \class SdVolume
  61. * \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
  62. */
  63. class SdVolume {
  64. public:
  65. // Create an instance of SdVolume
  66. SdVolume() : fatType_(0) {}
  67. /**
  68. * Clear the cache and returns a pointer to the cache. Used by the WaveRP
  69. * recorder to do raw write to the SD card. Not for normal apps.
  70. * \return A pointer to the cache buffer or zero if an error occurs.
  71. */
  72. cache_t* cacheClear() {
  73. if (!cacheFlush()) return 0;
  74. cacheBlockNumber_ = 0xFFFFFFFF;
  75. return &cacheBuffer_;
  76. }
  77. /**
  78. * Initialize a FAT volume. Try partition one first then try super
  79. * floppy format.
  80. *
  81. * \param[in] dev The Sd2Card where the volume is located.
  82. *
  83. * \return true for success, false for failure.
  84. * Reasons for failure include not finding a valid partition, not finding
  85. * a valid FAT file system or an I/O error.
  86. */
  87. bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0); }
  88. bool init(Sd2Card* dev, uint8_t part);
  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. * \return The logical block number for the start of the root directory
  102. * on FAT16 volumes or the first cluster number on FAT32 volumes.
  103. */
  104. uint32_t rootDirStart() const { return rootDirStart_; }
  105. /**
  106. * Sd2Card object for this volume
  107. * \return pointer to Sd2Card object.
  108. */
  109. Sd2Card* sdCard() { return sdCard_; }
  110. /**
  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. private:
  119. // Allow SdBaseFile access to SdVolume private data.
  120. friend class SdBaseFile;
  121. // value for dirty argument in cacheRawBlock to indicate read from cache
  122. static bool const CACHE_FOR_READ = false;
  123. // value for dirty argument in cacheRawBlock to indicate write to cache
  124. static bool const CACHE_FOR_WRITE = true;
  125. #if USE_MULTIPLE_CARDS
  126. cache_t cacheBuffer_; // 512 byte cache for device blocks
  127. uint32_t cacheBlockNumber_; // Logical number of block in the cache
  128. Sd2Card* sdCard_; // Sd2Card object for cache
  129. bool cacheDirty_; // cacheFlush() will write block if true
  130. uint32_t cacheMirrorBlock_; // block number for mirror FAT
  131. #else
  132. static cache_t cacheBuffer_; // 512 byte cache for device blocks
  133. static uint32_t cacheBlockNumber_; // Logical number of block in the cache
  134. static Sd2Card* sdCard_; // Sd2Card object for cache
  135. static bool cacheDirty_; // cacheFlush() will write block if true
  136. static uint32_t cacheMirrorBlock_; // block number for mirror FAT
  137. #endif
  138. uint32_t allocSearchStart_; // start cluster for alloc search
  139. uint8_t blocksPerCluster_; // cluster size in blocks
  140. uint32_t blocksPerFat_; // FAT size in blocks
  141. uint32_t clusterCount_; // clusters in one FAT
  142. uint8_t clusterSizeShift_; // shift to convert cluster count to block count
  143. uint32_t dataStartBlock_; // first data block number
  144. uint8_t fatCount_; // number of FATs on volume
  145. uint32_t fatStartBlock_; // start block for first FAT
  146. uint8_t fatType_; // volume type (12, 16, OR 32)
  147. uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
  148. uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
  149. bool allocContiguous(uint32_t count, uint32_t* curCluster);
  150. uint8_t blockOfCluster(uint32_t position) const { return (position >> 9) & (blocksPerCluster_ - 1); }
  151. uint32_t clusterStartBlock(uint32_t cluster) const { return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_); }
  152. uint32_t blockNumber(uint32_t cluster, uint32_t position) const { return clusterStartBlock(cluster) + blockOfCluster(position); }
  153. cache_t* cache() { return &cacheBuffer_; }
  154. uint32_t cacheBlockNumber() const { return cacheBlockNumber_; }
  155. #if USE_MULTIPLE_CARDS
  156. bool cacheFlush();
  157. bool cacheRawBlock(uint32_t blockNumber, bool dirty);
  158. #else
  159. static bool cacheFlush();
  160. static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
  161. #endif
  162. // used by SdBaseFile write to assign cache to SD location
  163. void cacheSetBlockNumber(uint32_t blockNumber, bool dirty) {
  164. cacheDirty_ = dirty;
  165. cacheBlockNumber_ = blockNumber;
  166. }
  167. void cacheSetDirty() { cacheDirty_ |= CACHE_FOR_WRITE; }
  168. bool chainSize(uint32_t beginCluster, uint32_t* size);
  169. bool fatGet(uint32_t cluster, uint32_t* value);
  170. bool fatPut(uint32_t cluster, uint32_t value);
  171. bool fatPutEOC(uint32_t cluster) { return fatPut(cluster, 0x0FFFFFFF); }
  172. bool freeChain(uint32_t cluster);
  173. bool isEOC(uint32_t cluster) const {
  174. if (FAT12_SUPPORT && fatType_ == 12) return cluster >= FAT12EOC_MIN;
  175. if (fatType_ == 16) return cluster >= FAT16EOC_MIN;
  176. return cluster >= FAT32EOC_MIN;
  177. }
  178. bool readBlock(uint32_t block, uint8_t* dst) { return sdCard_->readBlock(block, dst); }
  179. bool writeBlock(uint32_t block, const uint8_t* dst) { return sdCard_->writeBlock(block, dst); }
  180. };