Skip to content

Commit e2555d7

Browse files
Earle F. Philhower, IIIEarle F. Philhower, III
Earle F. Philhower, III
authored and
Earle F. Philhower, III
committed
Add a host-mock SdSpiCard to allow host tests
Add a very simple and basic host memory based driver to allow library to run on host systems and be used for CI/etc. testing.
1 parent 6552ebc commit e2555d7

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

library.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name=SdFat-namespace
1+
name=ESP8266SdFat
22
version=1.0.8
33
author=Bill Greiman <[email protected]>
44
maintainer=Earle F. Philhower, III <[email protected]>
5-
sentence=Minor ESP8266 tweak of Bill Greiman's amazing FAT16/FAT32 file system for SD cards with everything enclosed in "namespace sdfat".
6-
paragraph=Minor tweak of Bill Greiman's amazing FAT16/FAT32 file system for SD cards. Modified to enclose all objects, constants, etc. in a unique namespace "sdfat" to make it easier to integrate with the ESP8266's own File class.
5+
sentence=Fork of Bill Greiman's SdFat library <https://github.com/greiman/SdFat> with fixes for ESP8266 Arduino support.
6+
paragraph=Minor tweak of Bill Greiman's SdFat FAT16/FAT32 file system for SD cards. Modified to enclose all objects, constants, etc. in a unique namespace "sdfat" to make it easier to integrate with the ESP8266's own File class.
77
category=Data Storage
8-
url=https://github.com/earlephilhower/SdFat-namespace
8+
url=https://github.com/earlephilhower/ESP8266SdFat
99
architectures=esp8266
1010
dot_a_linkage=true

src/SdCard/SdSpiCard.h

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@
3434
#include "../FatLib/BaseBlockDriver.h"
3535
#include "../SpiDriver/SdSpiDriver.h"
3636

37-
namespace sdfat {
37+
#ifdef HOST_MOCK
38+
extern uint64_t _sdCardSizeB;
39+
extern uint8_t _sdCard[];
40+
#endif
3841

42+
namespace sdfat {
3943
//==============================================================================
4044
/**
4145
* \class SdSpiCard
@@ -46,6 +50,7 @@ class SdSpiCard : public BaseBlockDriver {
4650
#else // ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
4751
class SdSpiCard {
4852
#endif // ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
53+
#ifndef HOST_MOCK
4954
public:
5055
/** Construct an instance of SdSpiCard. */
5156
SdSpiCard() : m_errorCode(SD_CARD_ERROR_INIT_NOT_CALLED), m_type(0) {}
@@ -302,6 +307,83 @@ class SdSpiCard {
302307
bool m_spiActive;
303308
uint8_t m_status;
304309
uint8_t m_type;
310+
#else
311+
public:
312+
SdSpiCard() : m_errorCode(SD_CARD_ERROR_INIT_NOT_CALLED), m_type(0) {
313+
_blocks = _sdCardSizeB / 512LL;
314+
_data = _sdCard;
315+
}
316+
~SdSpiCard() { }
317+
bool begin(SdSpiDriver* spi, uint8_t csPin, SPISettings spiSettings) {
318+
m_errorCode = 0;
319+
m_status = 0;
320+
(void)spi;
321+
(void)csPin;
322+
(void)spiSettings;
323+
return true;
324+
}
325+
uint32_t cardSize() { return _blocks; }
326+
bool erase(uint32_t firstBlock, uint32_t lastBlock) {
327+
memset(_data + firstBlock * 512, 0, (lastBlock - firstBlock) * 512);
328+
return true;
329+
}
330+
bool eraseSingleBlockEnable() { return true; }
331+
void error(uint8_t code) { m_errorCode = code; }
332+
int errorCode() const { return m_errorCode; }
333+
int errorData() const { return m_status; }
334+
bool isBusy() { return false; }
335+
bool readBlock(uint32_t lba, uint8_t* dst) {
336+
memcpy(dst, _data + lba * 512, 512);
337+
return true;
338+
}
339+
bool readBlocks(uint32_t lba, uint8_t* dst, size_t nb) {
340+
memcpy(dst, _data + lba, 512 * nb);
341+
return true;
342+
}
343+
bool readCID(cid_t* cid) { return true; }
344+
bool readCSD(csd_t* csd) { return true; }
345+
bool readData(uint8_t *dst) { return readBlock(_multi++, dst); }
346+
bool readOCR(uint32_t* ocr) { return true; }
347+
bool readStart(uint32_t blockNumber) {
348+
_multi = blockNumber;
349+
return true;
350+
}
351+
bool readStatus(uint8_t* status) { return true; }
352+
bool readStop() { return true; }
353+
bool syncBlocks() { return true; }
354+
int type() const { return m_type; }
355+
bool writeBlock(uint32_t lba, const uint8_t* src) {
356+
memcpy(_data + lba * 512, src, 512);
357+
return true;
358+
}
359+
bool writeBlocks(uint32_t lba, const uint8_t* src, size_t nb) {
360+
memcpy(_data + lba * 512, src, 512 * nb);
361+
return true;
362+
}
363+
bool writeData(const uint8_t* src) { return writeBlock(_multi++, src);
364+
}
365+
bool writeStart(uint32_t blockNumber) {
366+
_multi = blockNumber;
367+
return true;
368+
}
369+
bool writeStart(uint32_t blockNumber, uint32_t eraseCount) {
370+
erase(blockNumber, blockNumber + eraseCount);
371+
_multi = blockNumber;
372+
return true;
373+
}
374+
bool writeStop() { return true; }
375+
void spiStart() { }
376+
void spiStop() { }
377+
378+
private:
379+
int _multi;
380+
uint8_t *_data;
381+
uint64_t _cardSizeB;
382+
int _blocks;
383+
int m_errorCode;
384+
int m_status;
385+
int m_type;
386+
#endif
305387
};
306388
//==============================================================================
307389
/**

src/SdFatConfig.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* sdios.h provides C++ style IO Streams.
4040
*/
4141
#define INCLUDE_SDIOS 1
42+
4243
//------------------------------------------------------------------------------
4344
/**
4445
* Set USE_LONG_FILE_NAMES nonzero to use long file names (LFN).
@@ -211,4 +212,13 @@
211212
#else // USE_STANDARD_SPI_LIBRARY
212213
#define IMPLEMENT_SPI_PORT_SELECTION 1
213214
#endif // USE_STANDARD_SPI_LIBRARY
215+
216+
217+
#ifdef HOST_MOCK
218+
#undef INCLUDE_SDIOS
219+
#define INCLUDE_SDIOS 0
220+
#undef SS
221+
#define SS 0
222+
#endif
223+
214224
#endif // SdFatConfig_h

0 commit comments

Comments
 (0)