Skip to content

Commit

Permalink
Support for SdFatLite library.
Browse files Browse the repository at this point in the history
  • Loading branch information
greiman committed Mar 9, 2019
1 parent 635d3fd commit 3b79f38
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 41 deletions.
14 changes: 7 additions & 7 deletions examples/DirectoryFunctions/DirectoryFunctions.ino
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
}
Expand All @@ -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++;
Expand Down
1 change: 0 additions & 1 deletion examples/rename/rename.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
6 changes: 6 additions & 0 deletions extras/changes.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
14 changes: 7 additions & 7 deletions src/FatLib/FatApiConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions src/FatLib/FatFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ bool FatFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock) {
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
Expand All @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions src/FatLib/FatFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 16 additions & 5 deletions src/FatLib/FatVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,23 @@ bool FatVolume::allocateCluster(uint32_t current, uint32_t* next) {
}
//------------------------------------------------------------------------------
// 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) {
Expand All @@ -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;
Expand Down
23 changes: 22 additions & 1 deletion src/FatLib/FatVolume.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SdCard/SdSpiCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/SdCard/SdSpiCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
6 changes: 4 additions & 2 deletions src/SdCard/SdioCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/SdCard/SdioTeensy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ bool SdioCard::begin() {
return true;
}
//-----------------------------------------------------------------------------
uint32_t SdioCard::cardSize() {
uint32_t SdioCard::cardCapacity() {
return sdCardCapacity(&m_csd);
}
//-----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/SdFat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/SpiDriver/SdSpiESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
//------------------------------------------------------------------------------
Expand Down

0 comments on commit 3b79f38

Please sign in to comment.