forked from greiman/SdFat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSdFat.h
509 lines (508 loc) · 16.7 KB
/
SdFat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/**
* Copyright (c) 2011-2024 Bill Greiman
* This file is part of the SdFat library for SD memory cards.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef SdFat_h
#define SdFat_h
/**
* \file
* \brief main SdFs include file.
*/
#include "ExFatLib/ExFatLib.h"
#include "FatLib/FatLib.h"
#include "FsLib/FsLib.h"
#include "SdCard/SdCard.h"
#include "common/SysCall.h"
#if INCLUDE_SDIOS
#include "sdios.h"
#endif // INCLUDE_SDIOS
//------------------------------------------------------------------------------
/** SdFat version for cpp use. */
#define SD_FAT_VERSION 20203
/** SdFat version as string. */
#define SD_FAT_VERSION_STR "2.2.3"
//==============================================================================
/**
* \class SdBase
* \brief base SD file system template class.
*/
template <class Vol, class Fmt>
class SdBase : public Vol {
public:
//----------------------------------------------------------------------------
/** Initialize SD card and file system.
*
* \param[in] csPin SD card chip select pin.
* \return true for success or false for failure.
*/
bool begin(SdCsPin_t csPin = SS) {
#ifdef BUILTIN_SDCARD
if (csPin == BUILTIN_SDCARD) {
return begin(SdioConfig(FIFO_SDIO));
}
#endif // BUILTIN_SDCARD
return begin(SdSpiConfig(csPin, SHARED_SPI));
}
//----------------------------------------------------------------------------
/** Initialize SD card and file system.
*
* \param[in] csPin SD card chip select pin.
* \param[in] maxSck Maximum SCK frequency.
* \return true for success or false for failure.
*/
bool begin(SdCsPin_t csPin, uint32_t maxSck) {
return begin(SdSpiConfig(csPin, SHARED_SPI, maxSck));
}
//----------------------------------------------------------------------------
/** Initialize SD card and file system for SPI mode.
*
* \param[in] spiConfig SPI configuration.
* \return true for success or false for failure.
*/
bool begin(SdSpiConfig spiConfig) {
return cardBegin(spiConfig) && volumeBegin();
}
//---------------------------------------------------------------------------
/** Initialize SD card and file system for SDIO mode.
*
* \param[in] sdioConfig SDIO configuration.
* \return true for success or false for failure.
*/
bool begin(SdioConfig sdioConfig) {
return cardBegin(sdioConfig) && volumeBegin();
}
//----------------------------------------------------------------------------
/** \return Pointer to SD card object. */
SdCard* card() { return m_card; }
//----------------------------------------------------------------------------
/** Initialize SD card in SPI mode.
*
* \param[in] spiConfig SPI configuration.
* \return true for success or false for failure.
*/
bool cardBegin(SdSpiConfig spiConfig) {
m_card = m_cardFactory.newCard(spiConfig);
return m_card && !m_card->errorCode();
}
//----------------------------------------------------------------------------
/** Initialize SD card in SDIO mode.
*
* \param[in] sdioConfig SDIO configuration.
* \return true for success or false for failure.
*/
bool cardBegin(SdioConfig sdioConfig) {
m_card = m_cardFactory.newCard(sdioConfig);
return m_card && !m_card->errorCode();
}
//----------------------------------------------------------------------------
/** End use of card. */
void end() {
Vol::end();
if (m_card) {
m_card->end();
}
}
//----------------------------------------------------------------------------
/** %Print error info and halt.
*
* \param[in] pr Print destination.
*/
void errorHalt(print_t* pr) {
if (sdErrorCode()) {
pr->print(F("SdError: 0X"));
pr->print(sdErrorCode(), HEX);
pr->print(F(",0X"));
pr->println(sdErrorData(), HEX);
} else if (!Vol::fatType()) {
pr->println(F("Check SD format."));
}
while (true) {
}
}
//----------------------------------------------------------------------------
/** %Print error info and halt.
*
* \param[in] pr Print destination.
* \param[in] msg Message to print.
*/
void errorHalt(print_t* pr, const char* msg) {
pr->print(F("error: "));
pr->println(msg);
errorHalt(pr);
}
//----------------------------------------------------------------------------
/** %Print msg and halt.
*
* \param[in] pr Print destination.
* \param[in] msg Message to print.
*/
void errorHalt(print_t* pr, const __FlashStringHelper* msg) {
pr->print(F("error: "));
pr->println(msg);
errorHalt(pr);
}
//----------------------------------------------------------------------------
/** Format SD card
*
* \param[in] pr Print destination.
* \return true for success else false.
*/
bool format(print_t* pr = nullptr) {
Fmt fmt;
uint8_t* mem = Vol::end();
if (!mem) {
return false;
}
bool switchSpi = hasDedicatedSpi() && !isDedicatedSpi();
if (switchSpi && !setDedicatedSpi(true)) {
return false;
}
bool rtn = fmt.format(card(), mem, pr);
if (switchSpi && !setDedicatedSpi(false)) {
return false;
}
return rtn;
}
//----------------------------------------------------------------------------
/** \return the free cluster count. */
uint32_t freeClusterCount() {
bool switchSpi = hasDedicatedSpi() && !isDedicatedSpi();
if (switchSpi && !setDedicatedSpi(true)) {
return 0;
}
uint32_t rtn = Vol::freeClusterCount();
if (switchSpi && !setDedicatedSpi(false)) {
return 0;
}
return rtn;
}
//----------------------------------------------------------------------------
/** \return true if can be in dedicated SPI state */
bool hasDedicatedSpi() { return m_card ? m_card->hasDedicatedSpi() : false; }
//----------------------------------------------------------------------------
/** %Print error info and halt.
*
* \param[in] pr Print destination.
*/
void initErrorHalt(print_t* pr) {
initErrorPrint(pr);
while (true) {
}
}
//----------------------------------------------------------------------------
/** %Print error info and halt.
*
* \param[in] pr Print destination.
* \param[in] msg Message to print.
*/
void initErrorHalt(print_t* pr, const char* msg) {
pr->println(msg);
initErrorHalt(pr);
}
//----------------------------------------------------------------------------
/** %Print error info and halt.
*
* \param[in] pr Print destination.
* \param[in] msg Message to print.
*/
void initErrorHalt(print_t* pr, const __FlashStringHelper* msg) {
pr->println(msg);
initErrorHalt(pr);
}
//----------------------------------------------------------------------------
/** Print error details after begin() fails.
*
* \param[in] pr Print destination.
*/
void initErrorPrint(print_t* pr) {
pr->println(F("begin() failed"));
if (sdErrorCode()) {
pr->println(F("Do not reformat the SD."));
if (sdErrorCode() == SD_CARD_ERROR_CMD0) {
pr->println(F("No card, wrong chip select pin, or wiring error?"));
}
}
errorPrint(pr);
}
//----------------------------------------------------------------------------
/** \return true if in dedicated SPI state. */
bool isDedicatedSpi() { return m_card ? m_card->isDedicatedSpi() : false; }
//----------------------------------------------------------------------------
/** %Print volume FAT/exFAT type.
*
* \param[in] pr Print destination.
*/
void printFatType(print_t* pr) {
if (Vol::fatType() == FAT_TYPE_EXFAT) {
pr->print(F("exFAT"));
} else {
pr->print(F("FAT"));
pr->print(Vol::fatType());
}
}
//----------------------------------------------------------------------------
/** %Print SD errorCode and errorData.
*
* \param[in] pr Print destination.
*/
void errorPrint(print_t* pr) {
if (sdErrorCode()) {
pr->print(F("SdError: 0X"));
pr->print(sdErrorCode(), HEX);
pr->print(F(",0X"));
pr->println(sdErrorData(), HEX);
} else if (!Vol::fatType()) {
pr->println(F("Check SD format."));
}
}
//----------------------------------------------------------------------------
/** %Print msg, any SD error code.
*
* \param[in] pr Print destination.
* \param[in] msg Message to print.
*/
void errorPrint(print_t* pr, char const* msg) {
pr->print(F("error: "));
pr->println(msg);
errorPrint(pr);
}
/** %Print msg, any SD error code.
*
* \param[in] pr Print destination.
* \param[in] msg Message to print.
*/
void errorPrint(print_t* pr, const __FlashStringHelper* msg) {
pr->print(F("error: "));
pr->println(msg);
errorPrint(pr);
}
//----------------------------------------------------------------------------
/** %Print error info and return.
*
* \param[in] pr Print destination.
*/
void printSdError(print_t* pr) {
if (sdErrorCode()) {
if (sdErrorCode() == SD_CARD_ERROR_CMD0) {
pr->println(F("No card, wrong chip select pin, or wiring error?"));
}
pr->print(F("SD error: "));
printSdErrorSymbol(pr, sdErrorCode());
pr->print(F(" = 0x"));
pr->print(sdErrorCode(), HEX);
pr->print(F(",0x"));
pr->println(sdErrorData(), HEX);
} else if (!Vol::fatType()) {
pr->println(F("Check SD format."));
}
}
//----------------------------------------------------------------------------
/** \return SD card error code. */
uint8_t sdErrorCode() {
if (m_card) {
return m_card->errorCode();
}
return SD_CARD_ERROR_INVALID_CARD_CONFIG;
}
//----------------------------------------------------------------------------
/** \return SD card error data. */
uint8_t sdErrorData() { return m_card ? m_card->errorData() : 0; }
//----------------------------------------------------------------------------
/** Set SPI sharing state
* \param[in] value desired state.
* \return true for success else false;
*/
bool setDedicatedSpi(bool value) {
if (m_card) {
return m_card->setDedicatedSpi(value);
}
return false;
}
//----------------------------------------------------------------------------
/** \return pointer to base volume */
Vol* vol() { return reinterpret_cast<Vol*>(this); }
//----------------------------------------------------------------------------
/** Initialize file system after call to cardBegin.
*
* \return true for success or false for failure.
*/
bool volumeBegin() {
return Vol::begin(m_card) || Vol::begin(m_card, true, 0);
}
#if ENABLE_ARDUINO_SERIAL
/** Print error details after begin() fails. */
void initErrorPrint() { initErrorPrint(&Serial); }
//----------------------------------------------------------------------------
/** %Print msg to Serial and halt.
*
* \param[in] msg Message to print.
*/
void errorHalt(const __FlashStringHelper* msg) { errorHalt(&Serial, msg); }
//----------------------------------------------------------------------------
/** %Print error info to Serial and halt. */
void errorHalt() { errorHalt(&Serial); }
//----------------------------------------------------------------------------
/** %Print error info and halt.
*
* \param[in] msg Message to print.
*/
void errorHalt(const char* msg) { errorHalt(&Serial, msg); }
//----------------------------------------------------------------------------
/** %Print error info and halt. */
void initErrorHalt() { initErrorHalt(&Serial); }
//----------------------------------------------------------------------------
/** %Print msg, any SD error code.
*
* \param[in] msg Message to print.
*/
void errorPrint(const char* msg) { errorPrint(&Serial, msg); }
/** %Print msg, any SD error code.
*
* \param[in] msg Message to print.
*/
void errorPrint(const __FlashStringHelper* msg) { errorPrint(&Serial, msg); }
//----------------------------------------------------------------------------
/** %Print error info and halt.
*
* \param[in] msg Message to print.
*/
void initErrorHalt(const char* msg) { initErrorHalt(&Serial, msg); }
//----------------------------------------------------------------------------
/** %Print error info and halt.
*
* \param[in] msg Message to print.
*/
void initErrorHalt(const __FlashStringHelper* msg) {
initErrorHalt(&Serial, msg);
}
#endif // ENABLE_ARDUINO_SERIAL
//----------------------------------------------------------------------------
private:
SdCard* m_card = nullptr;
SdCardFactory m_cardFactory;
};
//------------------------------------------------------------------------------
/**
* \class SdFat32
* \brief SD file system class for FAT volumes.
*/
class SdFat32 : public SdBase<FatVolume, FatFormatter> {
public:
};
//------------------------------------------------------------------------------
/**
* \class SdExFat
* \brief SD file system class for exFAT volumes.
*/
class SdExFat : public SdBase<ExFatVolume, ExFatFormatter> {
public:
};
//------------------------------------------------------------------------------
/**
* \class SdFs
* \brief SD file system class for FAT16, FAT32, and exFAT volumes.
*/
class SdFs : public SdBase<FsVolume, FsFormatter> {
public:
};
//------------------------------------------------------------------------------
#if SDFAT_FILE_TYPE == 1 || defined(DOXYGEN)
/** Select type for SdFat. */
typedef SdFat32 SdFat;
/** Select type for SdBaseFile. */
typedef FatFile SdBaseFile;
#elif SDFAT_FILE_TYPE == 2
typedef SdExFat SdFat;
typedef ExFatFile SdBaseFile;
#elif SDFAT_FILE_TYPE == 3
typedef SdFs SdFat;
typedef FsBaseFile SdBaseFile;
#else // SDFAT_FILE_TYPE
#error Invalid SDFAT_FILE_TYPE
#endif // SDFAT_FILE_TYPE
//
// Only define File if FS.h is not included.
// Line with test for __has_include must not have operators or parentheses.
#if defined __has_include
#if __has_include(<FS.h>)
#define HAS_INCLUDE_FS_H
#endif // __has_include(<FS.h>)
#endif // defined __has_include
#ifndef HAS_INCLUDE_FS_H
#if SDFAT_FILE_TYPE == 1 || defined(DOXYGEN)
/** Select type for File. */
typedef File32 File;
#elif SDFAT_FILE_TYPE == 2
typedef ExFile File;
#elif SDFAT_FILE_TYPE == 3
typedef FsFile File;
#endif // SDFAT_FILE_TYPE
#elif !defined(DISABLE_FS_H_WARNING)
#warning File not defined because __has_include(FS.h)
#endif // HAS_INCLUDE_FS_H
/**
* \class SdFile
* \brief FAT16/FAT32 file with Print.
*/
class SdFile : public PrintFile<SdBaseFile> {
public:
SdFile() {}
/** Create an open SdFile.
* \param[in] path path for file.
* \param[in] oflag open flags.
*/
SdFile(const char* path, oflag_t oflag) { open(path, oflag); }
/** Set the date/time callback function
*
* \param[in] dateTime The user's call back function. The callback
* function is of the form:
*
* \code
* void dateTime(uint16_t* date, uint16_t* time) {
* uint16_t year;
* uint8_t month, day, hour, minute, second;
*
* // User gets date and time from GPS or real-time clock here
*
* // return date using FS_DATE macro to format fields
* *date = FS_DATE(year, month, day);
*
* // return time using FS_TIME macro to format fields
* *time = FS_TIME(hour, minute, second);
* }
* \endcode
*
* Sets the function that is called when a file is created or when
* a file's directory entry is modified by sync(). All timestamps,
* access, creation, and modify, are set when a file is created.
* sync() maintains the last access date and last modify date/time.
*
*/
static void dateTimeCallback(void (*dateTime)(uint16_t* date,
uint16_t* time)) {
FsDateTime::setCallback(dateTime);
}
/** Cancel the date/time callback function. */
static void dateTimeCallbackCancel() { FsDateTime::clearCallback(); }
};
#endif // SdFat_h