Skip to content

Commit 67cf01b

Browse files
2 parents f84b1a5 + 3b79f38 commit 67cf01b

File tree

15 files changed

+96
-41
lines changed

15 files changed

+96
-41
lines changed

examples/DirectoryFunctions/DirectoryFunctions.ino

Lines changed: 7 additions & 7 deletions
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

@@ -34,8 +34,8 @@ ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
3434
//------------------------------------------------------------------------------
3535
void setup() {
3636
Serial.begin(9600);
37-
38-
// Wait for USB Serial
37+
38+
// Wait for USB Serial
3939
while (!Serial) {
4040
SysCall::yield();
4141
}
@@ -45,22 +45,22 @@ void setup() {
4545
// Wait for input line and discard.
4646
cin.readline();
4747
cout << endl;
48-
48+
4949
// Initialize at the highest speed supported by the board that is
5050
// not over 50 MHz. Try a lower speed if SPI errors occur.
5151
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
5252
sd.initErrorHalt();
5353
}
54-
if (sd.exists("Folder1")
54+
if (sd.exists("Folder1")
5555
|| sd.exists("Folder1/file1.txt")
5656
|| sd.exists("Folder1/File2.txt")) {
5757
error("Please remove existing Folder1, file1.txt, and File2.txt");
5858
}
5959

6060
int rootFileCount = 0;
61-
if (!root.open("/")){
61+
if (!root.open("/")) {
6262
error("open root failed");
63-
}
63+
}
6464
while (file.openNext(&root, O_RDONLY)) {
6565
if (!file.isHidden()) {
6666
rootFileCount++;

examples/rename/rename.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ void setup() {
5555
file.println("A test line for Name1.txt");
5656

5757
// rename the file name2.txt and add a line.
58-
// Use current working directory, root.
5958
if (!file.rename("name2.txt")) {
6059
error("name2.txt");
6160
}

extras/changes.txt

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP8266SdFat
2-
version=1.0.16
2+
version=1.1.0
33
license=MIT
44
author=Bill Greiman <[email protected]>
55
maintainer=Earle F. Philhower, III <[email protected]>

src/FatLib/FatApiConstants.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,21 @@ inline bool isWriteMode(oflag_t oflag) {
112112
// FatFile class static and const definitions
113113
// flags for ls()
114114
/** ls() flag for list all files including hidden. */
115-
const uint8_t LS_A = 1;
115+
#define LS_A 1
116116
/** ls() flag to print modify. date */
117-
const uint8_t LS_DATE = 2;
117+
#define LS_DATE 2
118118
/** ls() flag to print file size. */
119-
const uint8_t LS_SIZE = 4;
119+
#define LS_SIZE 4
120120
/** ls() flag for recursive list of subdirectories */
121-
const uint8_t LS_R = 8;
121+
#define LS_R 8
122122

123123
// flags for timestamp
124124
/** set the file's last access date */
125-
const uint8_t T_ACCESS = 1;
125+
#define T_ACCESS 1
126126
/** set the file's creation date and time */
127-
const uint8_t T_CREATE = 2;
127+
#define T_CREATE 2
128128
/** Set the file's write date and time */
129-
const uint8_t T_WRITE = 4;
129+
#define T_WRITE 4
130130

131131
}; // namespace sdfat
132132

src/FatLib/FatFile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ bool FatFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock) {
132132
return false;
133133
}
134134
//------------------------------------------------------------------------------
135-
bool FatFile::createContiguous(FatFile* dirFile,
136-
const char* path, uint32_t size) {
135+
bool FatFile::createContiguous(FatFile* dirFile, const char* path,
136+
uint32_t size, uint32_t startCluster) {
137137
uint32_t count;
138138

139139
// don't allow zero length file
@@ -149,7 +149,7 @@ bool FatFile::createContiguous(FatFile* dirFile,
149149
count = ((size - 1) >> (m_vol->clusterSizeShift() + 9)) + 1;
150150

151151
// allocate clusters
152-
if (!m_vol->allocContiguous(count, &m_firstCluster)) {
152+
if (!m_vol->allocContiguous(count, &m_firstCluster, startCluster)) {
153153
remove();
154154
DBG_FAIL_MACRO;
155155
goto fail;

src/FatLib/FatFile.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,24 +227,27 @@ class FatFile {
227227
/** Create and open a new contiguous file of a specified size.
228228
*
229229
* \param[in] dirFile The directory where the file will be created.
230-
* \param[in] path A path with a validfile name.
230+
* \param[in] path A path with a valid file name.
231231
* \param[in] size The desired file size.
232+
* \param[in] startCluster The desired startCluster.
232233
*
233234
* \return The value true is returned for success and
234235
* the value false, is returned for failure.
235236
*/
236-
bool createContiguous(FatFile* dirFile,
237-
const char* path, uint32_t size);
237+
bool createContiguous(FatFile* dirFile, const char* path,
238+
uint32_t size, uint32_t startCluster = 0);
238239
/** Create and open a new contiguous file of a specified size.
239240
*
240-
* \param[in] path A path with a validfile name.
241+
* \param[in] path A path with a valid file name.
241242
* \param[in] size The desired file size.
243+
* \param[in] startCluster The desired startCluster.
242244
*
243245
* \return The value true is returned for success and
244246
* the value false, is returned for failure.
245247
*/
246-
bool createContiguous(const char* path, uint32_t size) {
247-
return createContiguous(m_cwd, path, size);
248+
bool createContiguous(const char* path,
249+
uint32_t size, uint32_t startCluster = 0) {
250+
return createContiguous(m_cwd, path, size, startCluster);
248251
}
249252
/** \return The current cluster number for a file or directory. */
250253
uint32_t curCluster() const {

src/FatLib/FatVolume.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,23 @@ bool FatVolume::allocateCluster(uint32_t current, uint32_t* next) {
135135
}
136136
//------------------------------------------------------------------------------
137137
// find a contiguous group of clusters
138-
bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster) {
138+
bool FatVolume::allocContiguous(uint32_t count,
139+
uint32_t* firstCluster, uint32_t startCluster) {
139140
// flag to save place to start next search
140-
bool setStart = true;
141+
bool setStart;
141142
// start of group
142143
uint32_t bgnCluster;
143144
// end of group
144145
uint32_t endCluster;
145-
// Start at cluster after last allocated cluster.
146-
endCluster = bgnCluster = m_allocSearchStart + 1;
147-
146+
if (startCluster != 0) {
147+
bgnCluster = startCluster;
148+
setStart = false;
149+
} else {
150+
// Start at cluster after last allocated cluster.
151+
bgnCluster = m_allocSearchStart + 1;
152+
setStart = true;
153+
}
154+
endCluster = bgnCluster;
148155
// search the FAT for free clusters
149156
while (1) {
150157
if (endCluster > m_lastCluster) {
@@ -159,6 +166,10 @@ bool FatVolume::allocContiguous(uint32_t count, uint32_t* firstCluster) {
159166
goto fail;
160167
}
161168
if (f || fg == 0) {
169+
if (startCluster) {
170+
DBG_FAIL_MACRO;
171+
goto fail;
172+
}
162173
// don't update search start if unallocated clusters before endCluster.
163174
if (bgnCluster != endCluster) {
164175
setStart = false;

src/FatLib/FatVolume.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ class FatVolume {
202202
uint32_t dataStartBlock() const {
203203
return m_dataStartBlock;
204204
}
205+
/** \return The sector number for the start of file data. */
206+
uint32_t dataStartSector() const {
207+
return m_dataStartBlock;
208+
}
205209
/** \return The number of File Allocation Tables. */
206210
uint8_t fatCount() {
207211
return 2;
@@ -210,6 +214,10 @@ class FatVolume {
210214
uint32_t fatStartBlock() const {
211215
return m_fatStartBlock;
212216
}
217+
/** \return The sector number for the start of the first FAT. */
218+
uint32_t fatStartSector() const {
219+
return m_fatStartBlock;
220+
}
213221
/** \return The FAT type of the volume. Values are 12, 16 or 32. */
214222
uint8_t fatType() const {
215223
return m_fatType;
@@ -239,6 +247,10 @@ class FatVolume {
239247
* the value false is returned for failure.
240248
*/
241249
bool init(uint8_t part);
250+
/** \return The cluster number of last cluster in the volume. */
251+
uint32_t lastCluster() const {
252+
return m_lastCluster;
253+
}
242254
/** \return The number of entries in the root directory for FAT16 volumes. */
243255
uint16_t rootDirEntryCount() const {
244256
return m_rootDirEntryCount;
@@ -248,10 +260,18 @@ class FatVolume {
248260
uint32_t rootDirStart() const {
249261
return m_rootDirStart;
250262
}
263+
/** \return The volume's cluster size in sectors. */
264+
uint8_t sectorsPerCluster() const {
265+
return m_blocksPerCluster;
266+
}
251267
/** \return The number of blocks in the volume */
252268
uint32_t volumeBlockCount() const {
253269
return blocksPerCluster()*clusterCount();
254270
}
271+
/** \return The number of sectors in the volume */
272+
uint32_t volumeSectorCount() const {
273+
return sectorsPerCluster()*clusterCount();
274+
}
255275
/** Wipe all data from the volume.
256276
* \param[in] pr print stream for status dots.
257277
* \return true for success else false.
@@ -363,7 +383,8 @@ class FatVolume {
363383
}
364384
//------------------------------------------------------------------------------
365385
bool allocateCluster(uint32_t current, uint32_t* next);
366-
bool allocContiguous(uint32_t count, uint32_t* firstCluster);
386+
bool allocContiguous(uint32_t count,
387+
uint32_t* firstCluster, uint32_t startCluster = 0);
367388
uint8_t blockOfCluster(uint32_t position) const {
368389
return (position >> 9) & m_clusterBlockMask;
369390
}

src/SdCard/SdSpiCard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ uint8_t SdSpiCard::cardCommand(uint8_t cmd, uint32_t arg) {
370370
return m_status;
371371
}
372372
//------------------------------------------------------------------------------
373-
uint32_t SdSpiCard::cardSize() {
373+
uint32_t SdSpiCard::cardCapacity() {
374374
csd_t csd;
375375
return readCSD(&csd) ? sdCardCapacity(&csd) : 0;
376376
}

src/SdCard/SdSpiCard.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ class SdSpiCard {
6464
/**
6565
* Determine the size of an SD flash memory card.
6666
*
67-
* \return The number of 512 byte data blocks in the card
67+
* \return The number of 512 byte sectors in the card
6868
* or zero if an error occurs.
6969
*/
70-
uint32_t cardSize();
70+
uint32_t cardCapacity();
71+
/** \return Card size in sectors or zero if an error occurs. */
72+
uint32_t cardSize() {return cardCapacity();}
7173
/** Clear debug stats. */
7274
void dbgClearStats();
7375
/** Print debug stats. */

src/SdCard/SdioCard.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ class SdioCard : public BaseBlockDriver {
4242
/**
4343
* Determine the size of an SD flash memory card.
4444
*
45-
* \return The number of 512 byte data blocks in the card
45+
* \return The number of 512 byte sectors in the card
4646
* or zero if an error occurs.
4747
*/
48-
uint32_t cardSize();
48+
uint32_t cardCapacity();
49+
/** \return Card size in sectors or zero if an error occurs. */
50+
uint32_t cardSize() {return cardCapacity();}
4951
/** Erase a range of blocks.
5052
*
5153
* \param[in] firstBlock The address of the first block in the range.

src/SdCard/SdioTeensy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ bool SdioCard::begin() {
539539
return true;
540540
}
541541
//-----------------------------------------------------------------------------
542-
uint32_t SdioCard::cardSize() {
542+
uint32_t SdioCard::cardCapacity() {
543543
return sdCardCapacity(&m_csd);
544544
}
545545
//-----------------------------------------------------------------------------

src/SdFat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
namespace sdfat {
4040

4141
//------------------------------------------------------------------------------
42-
/** SdFat version */
43-
#define SD_FAT_VERSION "1.0.15"
42+
/** SdFat version 1.1.0 */
43+
#define SD_FAT_VERSION 10100
4444
//==============================================================================
4545
/**
4646
* \class SdBaseFile

src/SpiDriver/SdSpiESP8266.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,19 @@ uint8_t SdSpiAltDriver::receive() {
6767
* \return Zero for no error or nonzero error code.
6868
*/
6969
uint8_t SdSpiAltDriver::receive(uint8_t* buf, size_t n) {
70-
// Works without 32-bit alignment of buf.
71-
SPI.transferBytes(0, buf, n);
70+
// Adjust to 32-bit alignment.
71+
while ((reinterpret_cast<uintptr_t>(buf) & 0X3) && n) {
72+
*buf++ = SPI.transfer(0xff);
73+
n--;
74+
}
75+
// Do multiple of four byte transfers.
76+
size_t n4 = 4*(n/4);
77+
SPI.transferBytes(0, buf, n4);
78+
79+
// Transfer up to three remaining bytes.
80+
for (buf += n4, n -= n4; n; n--) {
81+
*buf++ = SPI.transfer(0xff);
82+
}
7283
return 0;
7384
}
7485
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)