Kaynağa Gözat

Support for SdFatLite library.

Bill Greiman 6 yıl önce
ebeveyn
işleme
3b79f38cb5

+ 7 - 7
examples/DirectoryFunctions/DirectoryFunctions.ino

@@ -1,7 +1,7 @@
 /*
  * Example use of chdir(), ls(), mkdir(), and  rmdir().
  */
-#include <SPI.h> 
+#include <SPI.h>
 #include "SdFat.h"
 #include "sdios.h"
 // SD card chip select pin.
@@ -31,8 +31,8 @@ ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
 //------------------------------------------------------------------------------
 void setup() {
   Serial.begin(9600);
-  
-  // Wait for USB Serial 
+
+  // Wait for USB Serial
   while (!Serial) {
     SysCall::yield();
   }
@@ -42,22 +42,22 @@ void setup() {
   // Wait for input line and discard.
   cin.readline();
   cout << endl;
-  
+
   // Initialize at the highest speed supported by the board that is
   // not over 50 MHz. Try a lower speed if SPI errors occur.
   if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
     sd.initErrorHalt();
   }
-  if (sd.exists("Folder1") 
+  if (sd.exists("Folder1")
     || sd.exists("Folder1/file1.txt")
     || sd.exists("Folder1/File2.txt")) {
     error("Please remove existing Folder1, file1.txt, and File2.txt");
   }
 
   int rootFileCount = 0;
-  if (!root.open("/")){
+  if (!root.open("/")) {
     error("open root failed");
-  }    
+  }
   while (file.openNext(&root, O_RDONLY)) {
     if (!file.isHidden()) {
       rootFileCount++;

+ 0 - 1
examples/rename/rename.ino

@@ -53,7 +53,6 @@ void setup() {
   file.println("A test line for Name1.txt");
 
   // rename the file name2.txt and add a line.
-  // Use current working directory, root.
   if (!file.rename("name2.txt")) {
     error("name2.txt");
   }

+ 6 - 0
extras/changes.txt

@@ -1,3 +1,9 @@
+9 Mar 2019
+
+Add startCluste argument to createContiguous().
+
+Functions to support SdFatLite library.
+
 28 Dec 2018
 
 Support for multiple SPI ports now uses a pointer to a SPIClass object.

+ 1 - 1
library.properties

@@ -1,5 +1,5 @@
 name=SdFat
-version=1.0.16
+version=1.1.0
 license=MIT
 author=Bill Greiman <fat16lib@sbcglobal.net>
 maintainer=Bill Greiman <fat16lib@sbcglobal.net>

+ 7 - 7
src/FatLib/FatApiConstants.h

@@ -69,19 +69,19 @@ inline bool isWriteMode(oflag_t oflag) {
 // FatFile class static and const definitions
 // flags for ls()
 /** ls() flag for list all files including hidden. */
-const uint8_t LS_A = 1;
+#define LS_A 1
 /** ls() flag to print modify. date */
-const uint8_t LS_DATE = 2;
+#define LS_DATE 2
 /** ls() flag to print file size. */
-const uint8_t LS_SIZE = 4;
+#define LS_SIZE 4
 /** ls() flag for recursive list of subdirectories */
-const uint8_t LS_R = 8;
+#define LS_R 8
 
 // flags for timestamp
 /** set the file's last access date */
-const uint8_t T_ACCESS = 1;
+#define T_ACCESS 1
 /** set the file's creation date and time */
-const uint8_t T_CREATE = 2;
+#define T_CREATE 2
 /** Set the file's write date and time */
-const uint8_t T_WRITE = 4;
+#define T_WRITE 4
 #endif  // FatApiConstants_h

+ 3 - 3
src/FatLib/FatFile.cpp

@@ -129,8 +129,8 @@ fail:
   return false;
 }
 //------------------------------------------------------------------------------
-bool FatFile::createContiguous(FatFile* dirFile,
-                               const char* path, uint32_t size) {
+bool FatFile::createContiguous(FatFile* dirFile, const char* path,
+                               uint32_t size, uint32_t startCluster) {
   uint32_t count;
 
   // don't allow zero length file
@@ -146,7 +146,7 @@ bool FatFile::createContiguous(FatFile* dirFile,
   count = ((size - 1) >> (m_vol->clusterSizeShift() + 9)) + 1;
 
   // allocate clusters
-  if (!m_vol->allocContiguous(count, &m_firstCluster)) {
+  if (!m_vol->allocContiguous(count, &m_firstCluster, startCluster)) {
     remove();
     DBG_FAIL_MACRO;
     goto fail;

+ 9 - 6
src/FatLib/FatFile.h

@@ -224,24 +224,27 @@ class FatFile {
   /** Create and open a new contiguous file of a specified size.
    *
    * \param[in] dirFile The directory where the file will be created.
-   * \param[in] path A path with a validfile name.
+   * \param[in] path A path with a valid file name.
    * \param[in] size The desired file size.
+   * \param[in] startCluster The desired startCluster.
    *
    * \return The value true is returned for success and
    * the value false, is returned for failure.
    */
-  bool createContiguous(FatFile* dirFile,
-                        const char* path, uint32_t size);
+  bool createContiguous(FatFile* dirFile, const char* path,
+                        uint32_t size, uint32_t startCluster = 0);
   /** Create and open a new contiguous file of a specified size.
    *
-   * \param[in] path A path with a validfile name.
+   * \param[in] path A path with a valid file name.
    * \param[in] size The desired file size.
+   * \param[in] startCluster The desired startCluster.
    *
    * \return The value true is returned for success and
    * the value false, is returned for failure.
    */
-  bool createContiguous(const char* path, uint32_t size) {
-    return createContiguous(m_cwd, path, size);
+  bool createContiguous(const char* path,
+                        uint32_t size, uint32_t startCluster = 0) {
+    return createContiguous(m_cwd, path, size, startCluster);
   }
   /** \return The current cluster number for a file or directory. */
   uint32_t curCluster() const {

+ 16 - 5
src/FatLib/FatVolume.cpp

@@ -132,16 +132,23 @@ fail:
 }
 //------------------------------------------------------------------------------
 // find a contiguous group of clusters
-bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster) {
+bool FatVolume::allocContiguous(uint32_t count,
+                                uint32_t* firstCluster, uint32_t startCluster) {
   // flag to save place to start next search
-  bool setStart = true;
+  bool setStart;
   // start of group
   uint32_t bgnCluster;
   // end of group
   uint32_t endCluster;
-  // Start at cluster after last allocated cluster.
-  endCluster = bgnCluster = m_allocSearchStart + 1;
-
+  if (startCluster != 0) {
+    bgnCluster = startCluster;
+    setStart = false;
+  } else {
+    // Start at cluster after last allocated cluster.
+    bgnCluster = m_allocSearchStart + 1;
+    setStart = true;
+  }
+  endCluster = bgnCluster;
   // search the FAT for free clusters
   while (1) {
     if (endCluster > m_lastCluster) {
@@ -156,6 +163,10 @@ bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster) {
       goto fail;
     }
     if (f || fg == 0) {
+      if (startCluster) {
+        DBG_FAIL_MACRO;
+        goto fail;
+      }
       // don't update search start if unallocated clusters before endCluster.
       if (bgnCluster != endCluster) {
         setStart = false;

+ 22 - 1
src/FatLib/FatVolume.h

@@ -196,6 +196,10 @@ class FatVolume {
   uint32_t dataStartBlock() const {
     return m_dataStartBlock;
   }
+  /** \return The sector number for the start of file data. */
+  uint32_t dataStartSector() const {
+    return m_dataStartBlock;
+  }
   /** \return The number of File Allocation Tables. */
   uint8_t fatCount() {
     return 2;
@@ -204,6 +208,10 @@ class FatVolume {
   uint32_t fatStartBlock() const {
     return m_fatStartBlock;
   }
+  /** \return The sector number for the start of the first FAT. */
+  uint32_t fatStartSector() const {
+    return m_fatStartBlock;
+  }
   /** \return The FAT type of the volume. Values are 12, 16 or 32. */
   uint8_t fatType() const {
     return m_fatType;
@@ -233,6 +241,10 @@ class FatVolume {
    * the value false is returned for failure.
    */
   bool init(uint8_t part);
+  /** \return The cluster number of last cluster in the volume. */
+  uint32_t lastCluster() const {
+    return m_lastCluster;
+  }
   /** \return The number of entries in the root directory for FAT16 volumes. */
   uint16_t rootDirEntryCount() const {
     return m_rootDirEntryCount;
@@ -242,10 +254,18 @@ class FatVolume {
   uint32_t rootDirStart() const {
     return m_rootDirStart;
   }
+  /** \return The volume's cluster size in sectors. */
+  uint8_t sectorsPerCluster() const {
+    return m_blocksPerCluster;
+  }
   /** \return The number of blocks in the volume */
   uint32_t volumeBlockCount() const {
     return blocksPerCluster()*clusterCount();
   }
+  /** \return The number of sectors in the volume */
+  uint32_t volumeSectorCount() const {
+    return sectorsPerCluster()*clusterCount();
+  }
   /** Wipe all data from the volume.
    * \param[in] pr print stream for status dots.
    * \return true for success else false.
@@ -357,7 +377,8 @@ class FatVolume {
   }
 //------------------------------------------------------------------------------
   bool allocateCluster(uint32_t current, uint32_t* next);
-  bool allocContiguous(uint32_t count, uint32_t* firstCluster);
+  bool allocContiguous(uint32_t count,
+                       uint32_t* firstCluster, uint32_t startCluster = 0);
   uint8_t blockOfCluster(uint32_t position) const {
     return (position >> 9) & m_clusterBlockMask;
   }

+ 1 - 1
src/SdCard/SdSpiCard.cpp

@@ -367,7 +367,7 @@ uint8_t SdSpiCard::cardCommand(uint8_t cmd, uint32_t arg) {
   return m_status;
 }
 //------------------------------------------------------------------------------
-uint32_t SdSpiCard::cardSize() {
+uint32_t SdSpiCard::cardCapacity() {
   csd_t csd;
   return readCSD(&csd) ? sdCardCapacity(&csd) : 0;
 }

+ 4 - 2
src/SdCard/SdSpiCard.h

@@ -56,10 +56,12 @@ class SdSpiCard {
   /**
    * Determine the size of an SD flash memory card.
    *
-   * \return The number of 512 byte data blocks in the card
+   * \return The number of 512 byte sectors in the card
    *         or zero if an error occurs.
    */
-  uint32_t cardSize();
+  uint32_t cardCapacity();
+  /** \return Card size in sectors or zero if an error occurs. */
+  uint32_t cardSize() {return cardCapacity();}
   /** Clear debug stats. */
   void dbgClearStats();
   /** Print debug stats. */

+ 4 - 2
src/SdCard/SdioCard.h

@@ -39,10 +39,12 @@ class SdioCard : public BaseBlockDriver {
   /**
    * Determine the size of an SD flash memory card.
    *
-   * \return The number of 512 byte data blocks in the card
+   * \return The number of 512 byte sectors in the card
    *         or zero if an error occurs.
    */
-  uint32_t cardSize();
+  uint32_t cardCapacity();
+  /** \return Card size in sectors or zero if an error occurs. */
+  uint32_t cardSize() {return cardCapacity();}
   /** Erase a range of blocks.
    *
    * \param[in] firstBlock The address of the first block in the range.

+ 1 - 1
src/SdCard/SdioTeensy.cpp

@@ -536,7 +536,7 @@ bool SdioCard::begin() {
   return true;
 }
 //-----------------------------------------------------------------------------
-uint32_t SdioCard::cardSize() {
+uint32_t SdioCard::cardCapacity() {
   return sdCardCapacity(&m_csd);
 }
 //-----------------------------------------------------------------------------

+ 2 - 2
src/SdFat.h

@@ -36,8 +36,8 @@
 #include "sdios.h"
 #endif  // INCLUDE_SDIOS
 //------------------------------------------------------------------------------
-/** SdFat version */
-#define SD_FAT_VERSION "1.0.15"
+/** SdFat version 1.1.0 */
+#define SD_FAT_VERSION 10100
 //==============================================================================
 /**
  * \class SdBaseFile

+ 13 - 2
src/SpiDriver/SdSpiESP8266.cpp

@@ -64,8 +64,19 @@ uint8_t SdSpiAltDriver::receive() {
  * \return Zero for no error or nonzero error code.
  */
 uint8_t SdSpiAltDriver::receive(uint8_t* buf, size_t n) {
-  // Works without 32-bit alignment of buf.
-  SPI.transferBytes(0, buf, n);
+  // Adjust to 32-bit alignment.
+  while ((reinterpret_cast<uintptr_t>(buf) & 0X3) && n) {
+    *buf++ = SPI.transfer(0xff);
+    n--;
+  }
+  // Do multiple of four byte transfers.
+  size_t n4 = 4*(n/4);
+  SPI.transferBytes(0, buf, n4);
+
+  // Transfer up to three remaining bytes.
+  for (buf += n4, n -= n4; n; n--) {
+    *buf++ = SPI.transfer(0xff);
+  }
   return 0;
 }
 //------------------------------------------------------------------------------