62 #if defined(BOARD_SDCARD_CS_PIN) || defined(BOARD_DATAFLASH_FATFS) 66 Sd2Card SDClass::_card;
67 SdFatFs SDClass::_fatFs;
79 if (_card.init(std::move(spi))) {
80 lastError=_fatFs.init(&_card);
81 if(lastError ==
FR_OK)
return true;
82 printf(
"\nSD card error: %s\n", strError(lastError));
85 printf(
"\nSD card init error!\n");
95 uint8_t SDClass::exists(
const char *filepath)
99 lastError=
f_stat(filepath, &fno);
113 return lastError ==
FR_OK;
124 return lastError ==
FR_OK;
134 File file = File(filepath);
138 lastError=
f_stat(filepath, &fno);
140 if(lastError!=
FR_OK){
145 lastError =
f_opendir(&file._d.dir, filepath);
147 if( lastError !=
FR_OK) {
154 if( lastError ==
FR_OK) {
155 File::addOpenFile(&file._d.fil);
171 File file = File(filepath);
175 lastError=
f_stat(filepath, &fno);
178 if(!(mode & FILE_WRITE)){
179 lastError =
f_opendir(&file._d.dir, filepath);
184 if( lastError !=
FR_OK) file.close();
187 if((mode & FILE_WRITE) && lastError==
FR_OK) {
191 lastError =
f_open(&file._d.fil, filepath, mode);
194 if( lastError ==
FR_OK){
198 File::addOpenFile(&file._d.fil);
214 return lastError ==
FR_OK;
217 File SDClass::openRoot(
void)
219 File file = File(_fatFs.getRoot());
221 lastError =
f_opendir(&file._d.dir, _fatFs.getRoot());
223 if(lastError !=
FR_OK) {
224 file._d.dir.obj.fs = 0;
231 uint32_t SDClass::getfree(
const char *filepath, uint32_t * fssize){
233 DWORD fre_clust, fre_sect;
237 lastError =
f_getfree(filepath, &fre_clust, &fs);
238 if (lastError !=
FR_OK)
return -1;
242 fre_sect = fre_clust * fs->
csize;
250 lastError =
f_stat(filepath, fno);
251 if(lastError !=
FR_OK)
return -1;
255 uint8_t SDClass::format(
const char *filepath){
257 lastError = _fatFs.format(filepath, &_card);
259 return lastError ==
FR_OK;
272 File::File(
const char* fname)
274 _name = (
char*)
malloc(strlen(fname) +1);
277 if(_name ==
NULL)
return;
279 strcpy(_name, fname);
297 void File::ls(
cb_putc cb, uint8_t flags, uint8_t indent) {
307 fno.lfsize =
sizeof(lfn);
315 if(fno.
fname[0] ==
'.') {
319 fn = *fno.lfname ? fno.lfname : fno.
fname;
324 for (int8_t i = 0; i < indent; i++) cb(
' ');
329 if (flags & LS_DATE) {
331 printFatDate(fno.
fdate,cb);
333 printFatTime(fno.
ftime,cb);
336 if (flags & LS_SIZE) {
338 printNumber(fno.
fsize, cb);
348 fullPath = (
char*)
malloc(strlen(_name) + 1 + strlen(fn) +1);
349 if (fullPath !=
NULL) {
350 sprintf(fullPath,
"%s/%s", _name, fn);
351 File filtmp = SD.open(fullPath);
353 if (filtmp._name !=
NULL) {
355 filtmp.ls(cb,flags, indent+2);
361 static const char err_s[] =
"Error to open dir: ";
368 static const char err_s[] =
"Error to allocate memory!";
382 void File::printFatDate(uint16_t fatDate,
cb_putc cb) {
383 printNumber(FAT_YEAR(fatDate), cb);
385 printTwoDigits(FAT_MONTH(fatDate),cb);
387 printTwoDigits(FAT_DAY(fatDate),cb);
396 void File::printFatTime(uint16_t fatTime,
cb_putc cb) {
397 printTwoDigits(FAT_HOUR(fatTime), cb);
399 printTwoDigits(FAT_MINUTE(fatTime), cb);
401 printTwoDigits(FAT_SECOND(fatTime), cb);
408 void File::printTwoDigits(uint8_t
v,
cb_putc cb) {
413 void File::printNumber(int16_t n,
cb_putc cb) {
414 const uint8_t base = 10;
421 char buf[8 *
sizeof(long) + 1];
422 char *str = &buf[
sizeof(buf) - 1];
430 *--str = c < 10 ? c +
'0' : c +
'A' - 10;
436 void File::printStr(
const char *s,
cb_putc cb) {
449 SD.lastError =
f_read(&_d.fil, (
void *)&data, 1, &byteread);
463 SD.lastError =
f_read(&_d.fil, buf, len, &bytesread);
475 uint8_t ret =
read(&c,1);
478 if(c==
'\r')
continue;
496 if(_d.dir.obj.fs != 0) {
500 if(_d.fil.obj.fs != 0) {
505 SD.lastError =
f_close(&_d.fil);
508 removeOpenFile(&_d.fil);
524 SD.lastError =
f_sync(&_d.fil);
546 uint32_t File::position()
556 uint8_t File::seek(uint32_t pos)
563 SD.lastError =
f_lseek(&_d.fil, pos);
564 return SD.lastError ==
FR_OK;
573 uint32_t File::size()
587 return write(&data, 1);
601 SD.lastError =
f_write(&_d.fil, (
const void *)buf, sz, (
UINT *)&byteswritten);
607 return write((
const char *)buf, sz);
615 size_t File::print(
const char* data)
617 return write(data, strlen(data));
624 size_t File::println()
626 return write(
"\r\n", 2);
634 size_t File::println(
const char* data)
636 size_t bytewritten =
write(data, strlen(data));
637 bytewritten += println();
646 int File::available()
648 uint32_t n = size() - position();
649 return n > 0x7FFF ? 0x7FFF : n;
655 char *fname = strrchr(_name,
'/');
656 if (fname && fname[0] ==
'/')
665 uint8_t File::isDirectory()
669 if(_name ==
NULL)
return false;
673 if(_d.dir.obj.fs != 0)
return TRUE;
675 if(_d.fil.obj.fs != 0)
return FALSE;
681 SD.lastError =
f_stat(_name, &fno);
682 if (SD.lastError ==
FR_OK) {
697 File File::openNextFile(uint8_t mode)
699 if(!
is_dir)
return File();
704 char *fullPath =
NULL;
705 size_t name_len= strlen(_name);
706 size_t len = name_len;
710 fno.lfsize =
sizeof(lfn);
717 if(fno.
fname[0] ==
'.') {
721 fn = *fno.lfname ? fno.lfname : fno.
fname;
725 len += strlen(fn) +2;
726 fullPath = (
char*)
malloc(len);
727 if (fullPath !=
NULL) {
729 if ((name_len > 0) && (_name[name_len-1] ==
'/')) {
730 sprintf(fullPath,
"%s%s", _name, fn);
732 sprintf(fullPath,
"%s/%s", _name, fn);
734 File filtmp = SD.open(fullPath, mode);
743 void File::rewindDirectory(
void)
746 if(_d.dir.obj.fs != 0) {
753 #define MAX_OPEN_FILES 16 754 FIL* File::openFiles[MAX_OPEN_FILES]= {0};
755 uint8_t File::num_openFiles=0;
757 void File::syncAll(){
758 for(uint8_t i=0; i<num_openFiles;i++){
764 void File::addOpenFile(
FIL *
f){
765 for(uint8_t i=0; i<num_openFiles;i++){
771 if(num_openFiles<MAX_OPEN_FILES){
772 openFiles[num_openFiles++] =
f;
776 void File::removeOpenFile(
FIL *f){
777 for(uint8_t i=0; i<num_openFiles;i++){
778 if(openFiles[i] == f) {
int printf(const char *fmt,...)
FRESULT f_closedir(DIR *dp)
void(* cb_putc)(uint8_t c)
FRESULT f_lseek(FIL *fp, FSIZE_t ofs)
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
FRESULT f_unlink(const TCHAR *path)
int remove(const char *pathname)
ssize_t write(int fd, const void *buf, size_t count)
POSIX Write count bytes from *buf to fileno fd.
int rmdir(const char *pathname)
POSIX delete a directory.
ssize_t read(int fd, void *buf, size_t count)
POSIX read count bytes from *buf to fileno fd.
FRESULT f_readdir(DIR *dp, FILINFO *fno)
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
FRESULT f_getfree(const TCHAR *path, DWORD *nclst, FATFS **fatfs)
int close(int fileno)
POSIX Close a file with fileno handel.
FRESULT f_stat(const TCHAR *path, FILINFO *fno)
int mkdir(const char *pathname, mode_t mode)
POSIX make a directory.
void * malloc(size_t size)
FRESULT f_opendir(DIR *dp, const TCHAR *path)
static int is_dir(const char *path)
int stat(const char *name, struct stat *buf)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
FRESULT f_mkdir(const TCHAR *path)
FRESULT f_write(FIL *fp, const void *buff, UINT btw, UINT *bw)
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)