Soldered SD Card Arduino Library 1.0.0
Easily read and write files to and form the SD card breakout! A fork of the original SDFat library by Bill Greiman.
Loading...
Searching...
No Matches
SdFat.h
Go to the documentation of this file.
1
25#ifndef SdFat_h
26#define SdFat_h
31#include "ExFatLib/ExFatLib.h"
32#include "FatLib/FatLib.h"
33#include "FsLib/FsLib.h"
34#include "SdCard/SdCard.h"
35#include "common/SysCall.h"
36#if INCLUDE_SDIOS
37#include "sdios.h"
38#endif // INCLUDE_SDIOS
39//------------------------------------------------------------------------------
41#define SD_FAT_VERSION 20007
43#define SD_FAT_VERSION_STR "2.0.7"
44//==============================================================================
49template <class Vol> class SdBase : public Vol
50{
51 public:
52 //----------------------------------------------------------------------------
58 bool begin(SdCsPin_t csPin = SS)
59 {
60#ifdef BUILTIN_SDCARD
61 if (csPin == BUILTIN_SDCARD)
62 {
63 return begin(SdioConfig(FIFO_SDIO));
64 }
65#endif // BUILTIN_SDCARD
66 return begin(SdSpiConfig(csPin, SHARED_SPI));
67 }
68 //----------------------------------------------------------------------------
75 bool begin(SdCsPin_t csPin, uint32_t maxSck)
76 {
77 return begin(SdSpiConfig(csPin, SHARED_SPI, maxSck));
78 }
79 //----------------------------------------------------------------------------
85 bool begin(SdSpiConfig spiConfig)
86 {
87 return cardBegin(spiConfig) && Vol::begin(m_card);
88 }
89 //---------------------------------------------------------------------------
95 bool begin(SdioConfig sdioConfig)
96 {
97 return cardBegin(sdioConfig) && Vol::begin(m_card);
98 }
99 //----------------------------------------------------------------------------
102 {
103 return m_card;
104 }
105 //----------------------------------------------------------------------------
111 bool cardBegin(SdSpiConfig spiConfig)
112 {
113 m_card = m_cardFactory.newCard(spiConfig);
114 return m_card && !m_card->errorCode();
115 }
116 //----------------------------------------------------------------------------
122 bool cardBegin(SdioConfig sdioConfig)
123 {
124 m_card = m_cardFactory.newCard(sdioConfig);
125 return m_card && !m_card->errorCode();
126 }
127 //----------------------------------------------------------------------------
133 {
134 if (sdErrorCode())
135 {
136 pr->print(F("SdError: 0X"));
137 pr->print(sdErrorCode(), HEX);
138 pr->print(F(",0X"));
139 pr->println(sdErrorData(), HEX);
140 }
141 else if (!Vol::fatType())
142 {
143 pr->println(F("Check SD format."));
144 }
146 }
147 //----------------------------------------------------------------------------
153 void errorHalt(print_t *pr, const char *msg)
154 {
155 pr->print(F("error: "));
156 pr->println(msg);
157 errorHalt(pr);
158 }
159 //----------------------------------------------------------------------------
165 void errorHalt(print_t *pr, const __FlashStringHelper *msg)
166 {
167 pr->print(F("error: "));
168 pr->println(msg);
169 errorHalt(pr);
170 }
171 //----------------------------------------------------------------------------
177 {
178 initErrorPrint(pr);
180 }
181 //----------------------------------------------------------------------------
187 void initErrorHalt(print_t *pr, const char *msg)
188 {
189 pr->println(msg);
190 initErrorHalt(pr);
191 }
192 //----------------------------------------------------------------------------
198 void initErrorHalt(Print *pr, const __FlashStringHelper *msg)
199 {
200 pr->println(msg);
201 initErrorHalt(pr);
202 }
203 //----------------------------------------------------------------------------
208 void initErrorPrint(Print *pr)
209 {
210 pr->println(F("begin() failed"));
211 if (sdErrorCode())
212 {
213 pr->println(F("Do not reformat the SD."));
214 if (sdErrorCode() == SD_CARD_ERROR_CMD0)
215 {
216 pr->println(F("No card, wrong chip select pin, or wiring error?"));
217 }
218 }
219 errorPrint(pr);
220 }
221 //----------------------------------------------------------------------------
227 {
228 if (Vol::fatType() == FAT_TYPE_EXFAT)
229 {
230 pr->print(F("exFAT"));
231 }
232 else
233 {
234 pr->print(F("FAT"));
235 pr->print(Vol::fatType());
236 }
237 }
238 //----------------------------------------------------------------------------
244 {
245 if (sdErrorCode())
246 {
247 pr->print(F("SdError: 0X"));
248 pr->print(sdErrorCode(), HEX);
249 pr->print(F(",0X"));
250 pr->println(sdErrorData(), HEX);
251 }
252 else if (!Vol::fatType())
253 {
254 pr->println(F("Check SD format."));
255 }
256 }
257 //----------------------------------------------------------------------------
263 void errorPrint(print_t *pr, char const *msg)
264 {
265 pr->print(F("error: "));
266 pr->println(msg);
267 errorPrint(pr);
268 }
269
275 void errorPrint(Print *pr, const __FlashStringHelper *msg)
276 {
277 pr->print(F("error: "));
278 pr->println(msg);
279 errorPrint(pr);
280 }
281 //----------------------------------------------------------------------------
287 {
288 if (sdErrorCode())
289 {
290 if (sdErrorCode() == SD_CARD_ERROR_CMD0)
291 {
292 pr->println(F("No card, wrong chip select pin, or wiring error?"));
293 }
294 pr->print(F("SD error: "));
296 pr->print(F(" = 0x"));
297 pr->print(sdErrorCode(), HEX);
298 pr->print(F(",0x"));
299 pr->println(sdErrorData(), HEX);
300 }
301 else if (!Vol::fatType())
302 {
303 pr->println(F("Check SD format."));
304 }
305 }
306 //----------------------------------------------------------------------------
308 uint8_t sdErrorCode()
309 {
310 if (m_card)
311 {
312 return m_card->errorCode();
313 }
314 return SD_CARD_ERROR_INVALID_CARD_CONFIG;
315 }
316 //----------------------------------------------------------------------------
318 uint8_t sdErrorData()
319 {
320 return m_card ? m_card->errorData() : 0;
321 }
322 //----------------------------------------------------------------------------
324 Vol *vol()
325 {
326 return reinterpret_cast<Vol *>(this);
327 }
328 //----------------------------------------------------------------------------
334 {
335 return Vol::begin(m_card);
336 }
337#if ENABLE_ARDUINO_SERIAL
340 {
341 initErrorPrint(&Serial);
342 }
343 //----------------------------------------------------------------------------
348 void errorHalt(const __FlashStringHelper *msg)
349 {
350 errorHalt(&Serial, msg);
351 }
352 //----------------------------------------------------------------------------
355 {
356 errorHalt(&Serial);
357 }
358 //----------------------------------------------------------------------------
363 void errorHalt(const char *msg)
364 {
365 errorHalt(&Serial, msg);
366 }
367 //----------------------------------------------------------------------------
370 {
371 initErrorHalt(&Serial);
372 }
373 //----------------------------------------------------------------------------
378 void errorPrint(const char *msg)
379 {
380 errorPrint(&Serial, msg);
381 }
386 void errorPrint(const __FlashStringHelper *msg)
387 {
388 errorPrint(&Serial, msg);
389 }
390 //----------------------------------------------------------------------------
395 void initErrorHalt(const char *msg)
396 {
397 initErrorHalt(&Serial, msg);
398 }
399 //----------------------------------------------------------------------------
404 void initErrorHalt(const __FlashStringHelper *msg)
405 {
406 initErrorHalt(&Serial, msg);
407 }
408#endif // ENABLE_ARDUINO_SERIAL
409 //----------------------------------------------------------------------------
410 private:
413};
414//------------------------------------------------------------------------------
419class SdFat32 : public SdBase<FatVolume>
420{
421 public:
427 bool format(print_t *pr = nullptr)
428 {
429 FatFormatter fmt;
430 uint8_t *cache = cacheClear();
431 if (!cache)
432 {
433 return false;
434 }
435 return fmt.format(card(), cache, pr);
436 }
437};
438//------------------------------------------------------------------------------
443class SdExFat : public SdBase<ExFatVolume>
444{
445 public:
451 bool format(print_t *pr = nullptr)
452 {
453 ExFatFormatter fmt;
454 uint8_t *cache = cacheClear();
455 if (!cache)
456 {
457 return false;
458 }
459 return fmt.format(card(), cache, pr);
460 }
461};
462//------------------------------------------------------------------------------
467class SdFs : public SdBase<FsVolume>
468{
469 public:
475 bool format(print_t *pr = nullptr)
476 {
477 static_assert(sizeof(m_volMem) >= 512, "m_volMem too small");
478 uint32_t sectorCount = card()->sectorCount();
479 if (sectorCount == 0)
480 {
481 return false;
482 }
483 end();
484 if (sectorCount > 67108864)
485 {
486 ExFatFormatter fmt;
487 return fmt.format(card(), reinterpret_cast<uint8_t *>(m_volMem), pr);
488 }
489 else
490 {
491 FatFormatter fmt;
492 return fmt.format(card(), reinterpret_cast<uint8_t *>(m_volMem), pr);
493 }
494 }
495};
496//------------------------------------------------------------------------------
497#if SDFAT_FILE_TYPE == 1
501#if !defined(__has_include) || !__has_include(<FS.h>)
502typedef File32 File;
503#endif
506#elif SDFAT_FILE_TYPE == 2
507typedef SdExFat SdFat;
508#if !defined(__has_include) || !__has_include(<FS.h>)
509typedef ExFile File;
510#endif
511typedef ExFatFile SdBaseFile;
512#elif SDFAT_FILE_TYPE == 3
513typedef SdFs SdFat;
514#if !defined(__has_include) || !__has_include(<FS.h>)
515typedef FsFile File;
516#endif
517typedef FsBaseFile SdBaseFile;
518#else // SDFAT_FILE_TYPE
519#error Invalid SDFAT_FILE_TYPE
520#endif // SDFAT_FILE_TYPE
525class SdFile : public PrintFile<SdBaseFile>
526{
527 public:
529 {
530 }
535 SdFile(const char *path, oflag_t oflag)
536 {
537 open(path, oflag);
538 }
565 static void dateTimeCallback(void (*dateTime)(uint16_t *date, uint16_t *time))
566 {
567 FsDateTime::setCallback(dateTime);
568 }
571 {
573 }
574};
575#endif // SdFat_h
const uint8_t FAT_TYPE_EXFAT
Definition ExFatPartition.h:37
int oflag_t
Definition FsApiConstants.h:45
FsLib include file.
void printSdErrorSymbol(print_t *pr, uint8_t code)
Definition SdCardInfo.cpp:32
File32 File
Definition SdFat.h:502
FatFile SdBaseFile
Definition SdFat.h:505
SdFat32 SdFat
Definition SdFat.h:499
uint8_t SdCsPin_t
Definition SdFatConfig.h:139
const uint8_t SHARED_SPI
Definition SdSpiDriver.h:47
SysCall class.
Print print_t
Definition SysCall.h:66
Basic file class.
Definition ExFatFile.h:95
Format an exFAT volume.
Definition ExFatFormatter.h:36
bool format(BlockDevice *dev, uint8_t *secBuf, print_t *pr=nullptr)
Definition ExFatFormatter.cpp:52
uint8_t * cacheClear()
Definition ExFatPartition.h:76
exFAT file with Arduino Stream.
Definition ExFatFile.h:895
Basic file class.
Definition FatFile.h:112
Format a FAT volume.
Definition FatFormatter.h:36
bool format(BlockDevice *dev, uint8_t *secBuffer, print_t *pr=nullptr)
Definition FatFormatter.cpp:51
uint8_t * cacheClear()
Definition FatPartition.h:120
FAT16/FAT32 file with Arduino Stream.
Definition FatFile.h:1104
FsBaseFile class.
Definition FsFile.h:39
FsBaseFile file with Arduino Stream.
Definition FsFile.h:865
void end()
Definition FsVolume.h:103
newalign_t m_volMem[FS_ALIGN_DIM(ExFatVolume, FatVolume)]
Definition FsVolume.h:393
base SD file system template class.
Definition SdFat.h:50
void errorHalt(print_t *pr, const char *msg)
Definition SdFat.h:153
bool volumeBegin()
Definition SdFat.h:333
void errorPrint(print_t *pr)
Definition SdFat.h:243
void printFatType(print_t *pr)
Definition SdFat.h:226
void errorHalt(const __FlashStringHelper *msg)
Definition SdFat.h:348
SdCardFactory m_cardFactory
Definition SdFat.h:412
SdCard * m_card
Definition SdFat.h:411
void initErrorHalt(const __FlashStringHelper *msg)
Definition SdFat.h:404
bool begin(SdSpiConfig spiConfig)
Definition SdFat.h:85
void initErrorHalt()
Definition SdFat.h:369
uint8_t sdErrorData()
Definition SdFat.h:318
bool cardBegin(SdioConfig sdioConfig)
Definition SdFat.h:122
void errorHalt(const char *msg)
Definition SdFat.h:363
void errorPrint(Print *pr, const __FlashStringHelper *msg)
Definition SdFat.h:275
void printSdError(print_t *pr)
Definition SdFat.h:286
bool cardBegin(SdSpiConfig spiConfig)
Definition SdFat.h:111
void errorPrint(print_t *pr, char const *msg)
Definition SdFat.h:263
void errorHalt()
Definition SdFat.h:354
void initErrorHalt(Print *pr, const __FlashStringHelper *msg)
Definition SdFat.h:198
void initErrorPrint(Print *pr)
Definition SdFat.h:208
bool begin(SdCsPin_t csPin, uint32_t maxSck)
Definition SdFat.h:75
void initErrorHalt(const char *msg)
Definition SdFat.h:395
void errorPrint(const char *msg)
Definition SdFat.h:378
bool begin(SdioConfig sdioConfig)
Definition SdFat.h:95
void initErrorPrint()
Definition SdFat.h:339
bool begin(SdCsPin_t csPin=SS)
Definition SdFat.h:58
Vol * vol()
Definition SdFat.h:324
void errorHalt(print_t *pr, const __FlashStringHelper *msg)
Definition SdFat.h:165
SdCard * card()
Definition SdFat.h:101
void errorHalt(print_t *pr)
Definition SdFat.h:132
void initErrorHalt(print_t *pr)
Definition SdFat.h:176
void initErrorHalt(print_t *pr, const char *msg)
Definition SdFat.h:187
uint8_t sdErrorCode()
Definition SdFat.h:308
void errorPrint(const __FlashStringHelper *msg)
Definition SdFat.h:386
Setup a SPI card or SDIO card.
Definition SdCard.h:59
SdCard * newCard(SdSpiConfig config)
Definition SdCard.h:66
Abstract interface for an SD card.
Definition SdCardInterface.h:34
virtual uint32_t sectorCount()=0
virtual uint32_t errorData() const =0
virtual uint8_t errorCode() const =0
SD file system class for exFAT volumes.
Definition SdFat.h:444
bool format(print_t *pr=nullptr)
Definition SdFat.h:451
SD file system class for FAT volumes.
Definition SdFat.h:420
bool format(print_t *pr=nullptr)
Definition SdFat.h:427
static void dateTimeCallback(void(*dateTime)(uint16_t *date, uint16_t *time))
Definition SdFat.h:565
SdFile(const char *path, oflag_t oflag)
Definition SdFat.h:535
static void dateTimeCallbackCancel()
Definition SdFat.h:570
SdFile()
Definition SdFat.h:528
SD file system class for FAT16, FAT32, and exFAT volumes.
Definition SdFat.h:468
bool format(print_t *pr=nullptr)
Definition SdFat.h:475
SPI card configuration.
Definition SdSpiDriver.h:112
SDIO card configuration.
Definition SdioCard.h:37
static void halt()
Definition SysCall.h:53
void clearCallback()
Definition FsDateTime.cpp:40
void setCallback(void(*dateTime)(uint16_t *date, uint16_t *time))
Definition FsDateTime.cpp:44
C++ IO Streams features.