Skip to content

Commit 3b79f38

Browse files
committed
Support for SdFatLite library.
1 parent 635d3fd commit 3b79f38

15 files changed

+96
-41
lines changed

examples/DirectoryFunctions/DirectoryFunctions.ino

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Example use of chdir(), ls(), mkdir(), and rmdir().
33
*/
4-
#include <SPI.h>
4+
#include <SPI.h>
55
#include "SdFat.h"
66
#include "sdios.h"
77
// SD card chip select pin.
@@ -31,8 +31,8 @@ ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
3131
//------------------------------------------------------------------------------
3232
void setup() {
3333
Serial.begin(9600);
34-
35-
// Wait for USB Serial
34+
35+
// Wait for USB Serial
3636
while (!Serial) {
3737
SysCall::yield();
3838
}
@@ -42,22 +42,22 @@ void setup() {
4242
// Wait for input line and discard.
4343
cin.readline();
4444
cout << endl;
45-
45+
4646
// Initialize at the highest speed supported by the board that is
4747
// not over 50 MHz. Try a lower speed if SPI errors occur.
4848
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
4949
sd.initErrorHalt();
5050
}
51-
if (sd.exists("Folder1")
51+
if (sd.exists("Folder1")
5252
|| sd.exists("Folder1/file1.txt")
5353
|| sd.exists("Folder1/File2.txt")) {
5454
error("Please remove existing Folder1, file1.txt, and File2.txt");
5555
}
5656

5757
int rootFileCount = 0;
58-
if (!root.open("/")){
58+
if (!root.open("/")) {
5959
error("open root failed");
60-
}
60+
}
6161
while (file.openNext(&root, O_RDONLY)) {
6262
if (!file.isHidden()) {
6363
rootFileCount++;

examples/rename/rename.ino

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void setup() {
5353
file.println("A test line for Name1.txt");
5454

5555
// rename the file name2.txt and add a line.
56-
// Use current working directory, root.
5756
if (!file.rename("name2.txt")) {
5857
error("name2.txt");
5958
}

extras/changes.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
9 Mar 2019
2+
3+
Add startCluste argument to createContiguous().
4+
5+
Functions to support SdFatLite library.
6+
17
28 Dec 2018
28

39
Support for multiple SPI ports now uses a pointer to a SPIClass object.

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SdFat
2-
version=1.0.16
2+
version=1.1.0
33
license=MIT
44
author=Bill Greiman <[email protected]>
55
maintainer=Bill Greiman <[email protected]>

src/FatLib/FatApiConstants.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ inline bool isWriteMode(oflag_t oflag) {
6969
// FatFile class static and const definitions
7070
// flags for ls()
7171
/** ls() flag for list all files including hidden. */
72-
const uint8_t LS_A = 1;
72+
#define LS_A 1
7373
/** ls() flag to print modify. date */
74-
const uint8_t LS_DATE = 2;
74+
#define LS_DATE 2
7575
/** ls() flag to print file size. */
76-
const uint8_t LS_SIZE = 4;
76+
#define LS_SIZE 4
7777
/** ls() flag for recursive list of subdirectories */
78-
const uint8_t LS_R = 8;
78+
#define LS_R 8
7979

8080
// flags for timestamp
8181
/** set the file's last access date */
82-
const uint8_t T_ACCESS = 1;
82+
#define T_ACCESS 1
8383
/** set the file's creation date and time */
84-
const uint8_t T_CREATE = 2;
84+
#define T_CREATE 2
8585
/** Set the file's write date and time */
86-
const uint8_t T_WRITE = 4;
86+
#define T_WRITE 4
8787
#endif // FatApiConstants_h

src/FatLib/FatFile.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ bool FatFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock) {
129129
return false;
130130
}
131131
//------------------------------------------------------------------------------
132-
bool FatFile::createContiguous(FatFile* dirFile,
133-
const char* path, uint32_t size) {
132+
bool FatFile::createContiguous(FatFile* dirFile, const char* path,
133+
uint32_t size, uint32_t startCluster) {
134134
uint32_t count;
135135

136136
// don't allow zero length file
@@ -146,7 +146,7 @@ bool FatFile::createContiguous(FatFile* dirFile,
146146
count = ((size - 1) >> (m_vol->clusterSizeShift() + 9)) + 1;
147147

148148
// allocate clusters
149-
if (!m_vol->allocContiguous(count, &m_firstCluster)) {
149+
if (!m_vol->allocContiguous(count, &m_firstCluster, startCluster)) {
150150
remove();
151151
DBG_FAIL_MACRO;
152152
goto fail;

src/FatLib/FatFile.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -224,24 +224,27 @@ class FatFile {
224224
/** Create and open a new contiguous file of a specified size.
225225
*
226226
* \param[in] dirFile The directory where the file will be created.
227-
* \param[in] path A path with a validfile name.
227+
* \param[in] path A path with a valid file name.
228228
* \param[in] size The desired file size.
229+
* \param[in] startCluster The desired startCluster.
229230
*
230231
* \return The value true is returned for success and
231232
* the value false, is returned for failure.
232233
*/
233-
bool createContiguous(FatFile* dirFile,
234-
const char* path, uint32_t size);
234+
bool createContiguous(FatFile* dirFile, const char* path,
235+
uint32_t size, uint32_t startCluster = 0);
235236
/** Create and open a new contiguous file of a specified size.
236237
*
237-
* \param[in] path A path with a validfile name.
238+
* \param[in] path A path with a valid file name.
238239
* \param[in] size The desired file size.
240+
* \param[in] startCluster The desired startCluster.
239241
*
240242
* \return The value true is returned for success and
241243
* the value false, is returned for failure.
242244
*/
243-
bool createContiguous(const char* path, uint32_t size) {
244-
return createContiguous(m_cwd, path, size);
245+
bool createContiguous(const char* path,
246+
uint32_t size, uint32_t startCluster = 0) {
247+
return createContiguous(m_cwd, path, size, startCluster);
245248
}
246249
/** \return The current cluster number for a file or directory. */
247250
uint32_t curCluster() const {

src/FatLib/FatVolume.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,23 @@ bool FatVolume::allocateCluster(uint32_t current, uint32_t* next) {
132132
}
133133
//------------------------------------------------------------------------------
134134
// find a contiguous group of clusters
135-
bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster) {
135+
bool FatVolume::allocContiguous(uint32_t count,
136+
uint32_t* firstCluster, uint32_t startCluster) {
136137
// flag to save place to start next search
137-
bool setStart = true;
138+
bool setStart;
138139
// start of group
139140
uint32_t bgnCluster;
140141
// end of group
141142
uint32_t endCluster;
142-
// Start at cluster after last allocated cluster.
143-
endCluster = bgnCluster = m_allocSearchStart + 1;
144-
143+
if (startCluster != 0) {
144+
bgnCluster = startCluster;
145+
setStart = false;
146+
} else {
147+
// Start at cluster after last allocated cluster.
148+
bgnCluster = m_allocSearchStart + 1;
149+
setStart = true;
150+
}
151+
endCluster = bgnCluster;
145152
// search the FAT for free clusters
146153
while (1) {
147154
if (endCluster > m_lastCluster) {
@@ -156,6 +163,10 @@ bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster) {
156163
goto fail;
157164
}
158165
if (f || fg == 0) {
166+
if (startCluster) {
167+
DBG_FAIL_MACRO;
168+
goto fail;
169+
}
159170
// don't update search start if unallocated clusters before endCluster.
160171
if (bgnCluster != endCluster) {
161172
setStart = false;

src/FatLib/FatVolume.h

+22-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ class FatVolume {
196196
uint32_t dataStartBlock() const {
197197
return m_dataStartBlock;
198198
}
199+
/** \return The sector number for the start of file data. */
200+
uint32_t dataStartSector() const {
201+
return m_dataStartBlock;
202+
}
199203
/** \return The number of File Allocation Tables. */
200204
uint8_t fatCount() {
201205
return 2;
@@ -204,6 +208,10 @@ class FatVolume {
204208
uint32_t fatStartBlock() const {
205209
return m_fatStartBlock;
206210
}
211+
/** \return The sector number for the start of the first FAT. */
212+
uint32_t fatStartSector() const {
213+
return m_fatStartBlock;
214+
}
207215
/** \return The FAT type of the volume. Values are 12, 16 or 32. */
208216
uint8_t fatType() const {
209217
return m_fatType;
@@ -233,6 +241,10 @@ class FatVolume {
233241
* the value false is returned for failure.
234242
*/
235243
bool init(uint8_t part);
244+
/** \return The cluster number of last cluster in the volume. */
245+
uint32_t lastCluster() const {
246+
return m_lastCluster;
247+
}
236248
/** \return The number of entries in the root directory for FAT16 volumes. */
237249
uint16_t rootDirEntryCount() const {
238250
return m_rootDirEntryCount;
@@ -242,10 +254,18 @@ class FatVolume {
242254
uint32_t rootDirStart() const {
243255
return m_rootDirStart;
244256
}
257+
/** \return The volume's cluster size in sectors. */
258+
uint8_t sectorsPerCluster() const {
259+
return m_blocksPerCluster;
260+
}
245261
/** \return The number of blocks in the volume */
246262
uint32_t volumeBlockCount() const {
247263
return blocksPerCluster()*clusterCount();
248264
}
265+
/** \return The number of sectors in the volume */
266+
uint32_t volumeSectorCount() const {
267+
return sectorsPerCluster()*clusterCount();
268+
}
249269
/** Wipe all data from the volume.
250270
* \param[in] pr print stream for status dots.
251271
* \return true for success else false.
@@ -357,7 +377,8 @@ class FatVolume {
357377
}
358378
//------------------------------------------------------------------------------
359379
bool allocateCluster(uint32_t current, uint32_t* next);
360-
bool allocContiguous(uint32_t count, uint32_t* firstCluster);
380+
bool allocContiguous(uint32_t count,
381+
uint32_t* firstCluster, uint32_t startCluster = 0);
361382
uint8_t blockOfCluster(uint32_t position) const {
362383
return (position >> 9) & m_clusterBlockMask;
363384
}

src/SdCard/SdSpiCard.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ uint8_t SdSpiCard::cardCommand(uint8_t cmd, uint32_t arg) {
367367
return m_status;
368368
}
369369
//------------------------------------------------------------------------------
370-
uint32_t SdSpiCard::cardSize() {
370+
uint32_t SdSpiCard::cardCapacity() {
371371
csd_t csd;
372372
return readCSD(&csd) ? sdCardCapacity(&csd) : 0;
373373
}

src/SdCard/SdSpiCard.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ class SdSpiCard {
5656
/**
5757
* Determine the size of an SD flash memory card.
5858
*
59-
* \return The number of 512 byte data blocks in the card
59+
* \return The number of 512 byte sectors in the card
6060
* or zero if an error occurs.
6161
*/
62-
uint32_t cardSize();
62+
uint32_t cardCapacity();
63+
/** \return Card size in sectors or zero if an error occurs. */
64+
uint32_t cardSize() {return cardCapacity();}
6365
/** Clear debug stats. */
6466
void dbgClearStats();
6567
/** Print debug stats. */

src/SdCard/SdioCard.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ class SdioCard : public BaseBlockDriver {
3939
/**
4040
* Determine the size of an SD flash memory card.
4141
*
42-
* \return The number of 512 byte data blocks in the card
42+
* \return The number of 512 byte sectors in the card
4343
* or zero if an error occurs.
4444
*/
45-
uint32_t cardSize();
45+
uint32_t cardCapacity();
46+
/** \return Card size in sectors or zero if an error occurs. */
47+
uint32_t cardSize() {return cardCapacity();}
4648
/** Erase a range of blocks.
4749
*
4850
* \param[in] firstBlock The address of the first block in the range.

src/SdCard/SdioTeensy.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ bool SdioCard::begin() {
536536
return true;
537537
}
538538
//-----------------------------------------------------------------------------
539-
uint32_t SdioCard::cardSize() {
539+
uint32_t SdioCard::cardCapacity() {
540540
return sdCardCapacity(&m_csd);
541541
}
542542
//-----------------------------------------------------------------------------

src/SdFat.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
#include "sdios.h"
3737
#endif // INCLUDE_SDIOS
3838
//------------------------------------------------------------------------------
39-
/** SdFat version */
40-
#define SD_FAT_VERSION "1.0.15"
39+
/** SdFat version 1.1.0 */
40+
#define SD_FAT_VERSION 10100
4141
//==============================================================================
4242
/**
4343
* \class SdBaseFile

src/SpiDriver/SdSpiESP8266.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,19 @@ uint8_t SdSpiAltDriver::receive() {
6464
* \return Zero for no error or nonzero error code.
6565
*/
6666
uint8_t SdSpiAltDriver::receive(uint8_t* buf, size_t n) {
67-
// Works without 32-bit alignment of buf.
68-
SPI.transferBytes(0, buf, n);
67+
// Adjust to 32-bit alignment.
68+
while ((reinterpret_cast<uintptr_t>(buf) & 0X3) && n) {
69+
*buf++ = SPI.transfer(0xff);
70+
n--;
71+
}
72+
// Do multiple of four byte transfers.
73+
size_t n4 = 4*(n/4);
74+
SPI.transferBytes(0, buf, n4);
75+
76+
// Transfer up to three remaining bytes.
77+
for (buf += n4, n -= n4; n; n--) {
78+
*buf++ = SPI.transfer(0xff);
79+
}
6980
return 0;
7081
}
7182
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)