/
MakeFiles.ino
1389 lines (1310 loc) · 40.3 KB
/
MakeFiles.ino
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SD card basic file example
This example shows how to create and destroy an SD card file
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11, pin 7 on Teensy with audio board
** MISO - pin 12
** CLK - pin 13, pin 14 on Teensy with audio board
** CS - pin 4, pin 10 on Teensy with audio board
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
//#define LFS_MTPINDEX
#ifdef LFS_MTPINDEX
#include "LittleFS.h" // MJS513 :: https://forum.pjrc.com/threads/68139-Teensyduino-File-System-Integration-including-MTP-and-MSC?p=306205&viewfull=1#post306205
LittleFS_Program lfsProg; // Used to create FS on the Flash memory of the chip
static const uint32_t file_system_size = 1024 * 1024 * 1;
#endif
#include <SD.h>
#include <SPI.h>
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
#include <MTP_Teensy.h>
#endif
#define USE_SDIO_SD
//#define USE_PSRAM
//#define TEST_QSPI // Typical NOR FLASH
//#define TEST_QSPI_NAND // NAND Flash
#ifdef USE_SDIO_SD
#define DISK SD
const int chipSelect = BUILTIN_SDCARD;
//const int chipSelect = 10; // SFUN T_MM Carriers
#else // LittleFS
#include "LittleFS.h"
#define DISK myfs
#if defined TEST_QSPI
LittleFS_QSPIFlash myfs;
#elif defined(TEST_QSPI_NAND)
LittleFS_QPINAND myfs;
#elif defined USE_PSRAM
LittleFS_RAM myfs;
#define MYPSRAM 8 // compile time PSRAM size and is T_4.1 specific either 8 or 16, or smaller portion
EXTMEM char buf[MYPSRAM * 1024 * 1024];
#else
LittleFS_SPINAND myfs;
const int chipSelect = 4;
#endif
#endif
elapsedMillis seeSer;
void setup()
{
Serial.begin( 9600);
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
#ifdef LFS_MTPINDEX
// MJS513 :: https://forum.pjrc.com/threads/68139-Teensyduino-File-System-Integration-including-MTP-and-MSC?p=306205&viewfull=1#post306205
// Lets add the Program memory version:
// checks that the LittFS program has started with the disk size specified
if (lfsProg.begin(file_system_size)) {
MTP.addFilesystem(lfsProg, "PgmIndx");
} else {
Serial.println("Error starting Program Flash storage");
}
// MTP.begin();
#endif
#endif
while (!Serial && millis() < 5000 );
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
#ifdef USE_SDIO_SD
Serial.print("Initializing MEDIA... SD CARD ... ");
if (!DISK.begin(chipSelect)) {
#elif defined USE_PSRAM
Serial.print("Initializing MEDIA... PSRAM ... ");
if (!DISK.begin(buf, sizeof(buf))) {
#else
Serial.print("Initializing MEDIA... LFS DRIVE ... ");
if (!DISK.begin()) {
#endif
while (!Serial && millis() < 5000 );
Serial.println("\ninitialization failed!");
Serial.println("\ninitialization failed!");
Serial.println("\ninitialization failed!");
while (1) yield();
return;
}
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
else {
MTP.addFilesystem(DISK, "MakeFiles 2206");
Serial.println("Added SD card using built in SDIO, or given SPI CS");
}
#endif
delay(2000);
Serial.println("initialization done.");
#ifdef USE_SDIO_SD
Serial.print("Initialized MEDIA... SD CARD ... ");
#elif defined USE_PSRAM
Serial.print("Initialized MEDIA... PSRAM ... ");
#else
Serial.print("Initialized MEDIA... LFS DRIVE ... ");
#endif
seeSer = 101;
while ( millis() < 2500) {
if ( seeSer > 100 ) {
Serial.print(" SerOn: ");
Serial.print(millis());
seeSer = 0;
}
}
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
//Serial.println("\n2206: " __FILE__ " " __DATE__ " " __TIME__);
Serial.println("\nSetup done");
dSetup();
menu();
dLoop();
}
void MakeDeepDirs( char* szRoot, int numDirs, int numFiles, uint32_t startSize, uint32_t growSize, int compoundGrow = 1 );
void indexedDataWrite( char *szBlock, char* szPath, uint32_t xx, bool addFNum = false );
void directoryVerify(File dir, int numTabs, uint32_t numFiles = 0);
void MakeDataFiles( char* szRoot, int numFiles, int startSize, int growSize );
void MakeData( char* szRoot );
void MakeNames( char* szRoot );
void makeSome( int ii ) {
char szStart[][32] = { "", "ManyF", "ManyD10", "ManyD8", "MoreFD", // 0,1,2,3,4
"Ascii", "4K", "россиянин", "Huge", "TEST", // 5,6,7,8,9
"User" // 10,
};
// MAKE YOUR OWN - add a szStart name as desired and index it - only each ONCE or with unique file count
// prototypes above :: EDIT #5 'USER' to maintain the known tests.
if ( ii == 1 ) { // 2206 : as posted 6/14/22 (2204: ~1/24/22)
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 20, 4096, 0 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 40, 122 * 1024, 0 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 60, 63 * 1024, 0 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 30, 31 * 1024, 0 );
showMediaSpace(); MakeDeepDirs( szStart[2], 10, 5, 500, 512 );
showMediaSpace(); MakeDeepDirs( szStart[9], 1, 28, 495, 1 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 28, 495, 1 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 25, 500, 1 );
showMediaSpace(); MakeDeepDirs( szStart[8], 1, 10, 400000, 0 );
showMediaSpace();
}
else if ( ii == 2 ) { // SMALL
// EDIT HERE - for more files, Dirs, larger or alternate file sizes
// Multiple use of same FOLDER will mess up the file count - if they have same # files
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 3, 2048, 0 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 4, 100, 400 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 5, 100, 400 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 6, 4096, 0 );
showMediaSpace();
}
else if ( ii == 3 ) { // BIG
// EDIT HERE - for more files, Dirs, larger or alternate file sizes
showMediaSpace(); MakeDataFiles( szStart[1], 128, 100, 1 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 7, 2048, 0 );
showMediaSpace(); MakeDeepDirs( szStart[0], 1, 8, 100, 400 ); // Multiple use of same FOLDER will mess up the file count - if they have same # files
showMediaSpace(); MakeData( szStart[0] );
showMediaSpace(); MakeDataFiles( szStart[1], 124, 200, 1 );
showMediaSpace(); MakeDeepDirs( szStart[3], 8, 6, 100, 400 );
showMediaSpace(); MakeDeepDirs( szStart[4], 4, 6, 125, 250, 8 ); // LAST PARAM is 'GROW factor' Good for arbitrarily LARGE files
showMediaSpace(); MakeDeepDirs( szStart[6], 1, 6, 4096, 0 );
showMediaSpace();
}
else if ( ii == 4 ) { // NAME TEST
showMediaSpace(); MakeNames( szStart[5] ); // ASCII filename char list
showMediaSpace(); dbMakeNames( szStart[7] ); // UTF8 filename char list
}
else if ( ii == 5 ) { // USER
// EDIT HERE - for more files, Dirs, larger or alternate file sizes
// Multiple use of same FOLDER will mess up the file count - if they have same # files
showMediaSpace(); MakeDataFiles( szStart[0], 2, 100, 100 );
showMediaSpace(); MakeDataFiles( szStart[10], 2, 100, 100 );
}
else if ( ii == 6 ) { // ROOT MANY
showMediaSpace(); MakeDataFiles( szStart[0], 2, 100, 100 );
}
Serial.println("MakeData done.");
}
bool Abort = false;
uint32_t totBWrite = 0;
void loop() {
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
MTP.loop(); //This is mandatory to be placed in the loop code.
#endif
char CLRC;
char szNone[] = "";
#ifndef USE_SDIO_SD
uint32_t nextBlock = 0;
#endif
if ( Serial.available() ) {
if ( '.' == Serial.peek() ) {
CLRC = '.';
Serial.read();
}
else
CLRC = CommandLineReadChar();
switch ( CLRC ) {
case '.':
dLoop();
break;
case 'v':
directoryVerify();
break;
case 'l':
printDirectory();
break;
case 't':
makeSome( 1 );
break;
case 'r': // make many on root
MakeRoot( szNone, 490, 100 );
break;
case 's':
makeSome( 2 );
break;
case 'b':
makeSome( 3 );
break;
case 'n':
makeSome( 4 );
break;
case 'o': // Open Write Close timed test
timeOWC();
break;
case 'u': // USER
makeSome( 5 );
break;
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
case 'U': // USB Reset
Serial.println("USB reset: Reconnect serial port or restart Serial Monitor after.");
delay(100);
// usb_init(); // shuts down USB if already started, then restarts
// Serial.println("Send Device Reset Event");
MTP.send_DeviceResetEvent();
delay(200);
Serial.begin(9600);
delay(200);
Serial.println("USB reset Completed.");
break;
#endif
case 'W':
deleteAllDirectory(DISK.open("/"), szNone );
break;
case 'R':
Serial.print(" RESTART Teensy ...");
delay(100);
SCB_AIRCR = 0x05FA0004;
break;
#ifndef USE_SDIO_SD
case 'C': // Copy LFS Media to SD
xferSD( );
break;
case 'F': // Low Level Format Disk
Serial.print("Starting Format Low Level:\n\t>");
DISK.lowLevelFormat('.');
Serial.println();
break;
case 'S': // Unused block Format
Serial.print("Starting SAFE Format of unused blocks:\n\t>");
do {
nextBlock = DISK.formatUnused( 10, nextBlock ); // COUNT, where to start
Serial.print('.');
} while ( 0 != nextBlock );
Serial.println();
break;
#else
case 'F': // Low Level Format Disk
Serial.print("Starting SD Format:\n\t>");
DISK.format(0, '.');
Serial.println();
break;
#endif
case 'L': // Copy LFS Media to SD
logCMD( 0 );
break;
default:
menu();
dLoop(); // no Serial shows Menu
CLRC = 0;
break;
}
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
MTP.loop(); //This is mandatory to be placed in the loop code.
#endif
if ( CLRC != 0 ) {
Serial.printf("\n----\tTask '%c' complete!", CLRC );
showMediaSpace();
logCMD( CLRC );
}
if ( (DISK.totalSize() - DISK.usedSize()) < 2048) Serial.printf("\n DISK FULL?\n" );
if ( Abort ) Serial.printf("\n\n\t!!!! PRIOR OPERATION WAS ABORTED !!!!!!!\n" );
Abort = false;
}
} // end loop()
void menu() // any single alpha or numeral char
{
Serial.println();
Serial.println("Menu Options:");
Serial.println("\tt - Test Files write 2206");
Serial.println("\tb - Test Files write BIG");
Serial.println("\ts - Test Files write small");
Serial.println("\tu - Test Files write USER edit");
Serial.println("\tn - Test ASCII and UTF8 Filenames");
Serial.println("\tr - Root files - make Many");
Serial.println("\to - OpenWriteClose timed test");
Serial.println("\tv - Verify Files");
Serial.println("\tl - List files on media");
Serial.printf("\n\t%s\n", "R - Restart Teensy");
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
Serial.println("\tU - MTP USB reset");
#endif
Serial.println("\tW - Remove ALL media content");
Serial.println("\tF - Format Disk SD / LFS Low Level");
#ifndef USE_SDIO_SD
Serial.println("\tC - Copy LFS media content to SD");
Serial.println("\tS - SAFE Unused block Format");
#endif
Serial.println("\tL - List cmd log file");
Serial.println("\n\t. - '.?' menu for DEBUG {cpsd}");
showMediaSpace();
Serial.println();
}
char CommandLineReadChar() {
uint32_t return_value = 0;
char ch;
while ( Serial.available() ) {
ch = Serial.read();
if ( ((ch >= '0') && (ch <= '9')) || ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) )
return_value = ch;
}
return return_value;
}
uint32_t startSize = 244;
uint32_t growSize = 256;
char dirL[3][4] = {"111", "222", "333"};
char fileL[3][8] = {"aaa", "bbb", "ccc"};
#define BLOCK_SIZE 16
char dataL[6][BLOCK_SIZE + 1] = { "zaaa567890!@#$%^", "zbbbefghijklmnop", "zcccEFGHIJKLMNOP", "zDDdEFGHIJKLMNOP", "zE123456789LMNOP", "zRootFileManyxyz"};
void MakeData( char* szRoot ) {
char szPath[128];
char szTmp[128];
if ( Abort ) {
Serial.printf("\n\n\t!!!! PRIOR OPERATION WAS ABORTED !!!!!!!\n" );
return;
}
Abort = false;
for ( int ii = 0; ii < 3; ii++ ) {
if ( Abort ) break;
strcpy( szPath, szRoot );
strcat( szPath, "/" );
strcat( szPath, dirL[ii] );
sprintf( szTmp, ".%d", 3);
strcat( szPath, szTmp );
DISK.mkdir( szPath );
if (!DISK.exists(szPath)) {
Serial.print("\tMake MD Dir failed!");
Serial.println(szPath);
return;
}
else {
Serial.print("\tMade MD Dir:");
Serial.println(szPath);
}
uint32_t xx = startSize;
for ( int jj = 0; jj < 3; jj++ ) {
strcpy( szPath, szRoot );
strcat( szPath, "/" );
strcat( szPath, dirL[ii] );
sprintf( szTmp, ".%d", 3);
strcat( szPath, szTmp );
strcat( szPath, "/" );
strcat( szPath, fileL[jj] );
Serial.print("MakeD File:");
Serial.print(szPath);
indexedDataWrite( dataL[ii], szPath, xx );
if ( Abort ) break;
xx += growSize;
if ( Serial.available() ) {
while ( Serial.available() ) {
Serial.read();
}
return;
}
}
}
} // end MakeData()
void MakeRoot( char* szRoot, int numFiles, int startSize ) {
char szPath[128];
char szFile[12];
int ii = 0;
Serial.print("Make Root File:");
if ( Abort ) {
Serial.printf("\n\n\t!!!! PRIOR OPERATION WAS ABORTED !!!!!!!\n" );
return;
}
Abort = false;
while ( ii < numFiles && !Abort ) {
ii++;
strcpy( szPath, szRoot );
strcat( szPath, "/" );
sprintf( szFile, "%d_Mrt%3d", startSize, ii);
strcat( szPath, szFile );
indexedDataWrite( dataL[5], szPath, startSize );
if ( Serial.available() ) {
while ( Serial.available() ) {
Serial.read();
}
return;
}
}
} // end MakeRoot()
void MakeNames( char* szRoot ) {
char szPath[128];
strcpy( szPath, szRoot );
if ( 0 != szPath[0] ) {
DISK.mkdir( szPath );
if (!DISK.exists(szPath)) {
Serial.print("\tMake MN Dir failed!");
Serial.println(szPath);
return;
}
else {
Serial.print("\tMade MN Dir:");
Serial.println(szPath);
}
}
char jj[2] = "-";
for ( jj[0] = 33; jj[0] < 127; jj[0]++ ) {
bool ok;
switch ( jj[0] ) { // Compiler take these away
case '=':
case ';':
case ',':
case '.':
case '>':
case '<':
case '\"':
case '|':
case ':':
case '*':
case '%':
case '?':
case '\\':
case '/':
ok = false;
break;
default:
ok = true;
break;
}
if ( ok ) {
strcpy( szPath, szRoot );
strcat( szPath, "/" );
if ( *jj >= '0' && *jj <= '9')
strcat( szPath, "_" );
strcat( szPath, jj );
Serial.print("MakeN File:");
Serial.print(szPath);
// Run Once - don't delete to show file name re-use
File myFile;
myFile = DISK.open(szPath, FILE_WRITE);
if ( !myFile ) {
Serial.print(" >> MN Fail Open << \t");
Serial.println(szPath);
break;
}
uint32_t kk;
for ( kk = 0; kk < 32; kk++ ) {
myFile.print( jj );
}
Serial.print("=");
Serial.print(myFile.size());
totBWrite += 32;
if ( 32 != myFile.size() )
Serial.print(" >> NAME REUSED << ");
Serial.println();
myFile.close();
}
}
} // end MakeNames()
void logCMD( char theCMD ) {
File myFile;
if ( 0 == theCMD ) {
myFile = DISK.open("mfLog.txt", FILE_READ);
if ( myFile ) {
char chRd;
while ( myFile.available() ) {
myFile.read( &chRd , 1 );
Serial.print( chRd );
}
myFile.close();
}
}
else {
myFile = DISK.open("mfLog.txt", FILE_WRITE);
if ( myFile ) {
myFile.print( "theCMD: " );
myFile.print( theCMD );
myFile.print( "\n" );
}
myFile.close();
}
}
void dbMakeNames( char* szRoot ) { // Double Byte content names
char szPath[128];
strcpy( szPath, szRoot );
if ( 0 != szPath[0] )
DISK.mkdir( szPath );
const char *jj;
for ( int ii = 0; ii < 4; ii++ ) {
if ( 0 == ii ) jj = "😀";
if ( 1 == ii ) jj = "👍";
if ( 2 == ii ) jj = "α";
if ( 3 == ii ) jj = "β";
strcpy( szPath, szRoot );
strcat( szPath, "/" );
strcat( szPath, jj );
strcat( szPath, ".txt" );
Serial.print("dbMakeN File:");
Serial.print(szPath);
// Run Once - don't delete to show file name re-use
File myFile;
if (DISK.exists(szPath)) DISK.remove(szPath);
myFile = DISK.open(szPath, FILE_WRITE);
if ( !myFile ) {
Serial.print(" >> db Fail Open << \t");
Serial.println(szPath);
break;
}
uint32_t kk = 0;
if ( 2 > ii ) kk = 8; // emoji's are 4 byte
for ( ; kk < 16; kk++ ) {
myFile.print( jj );
}
Serial.print("=");
Serial.print(myFile.size());
totBWrite += 32;
if ( 32 != myFile.size() )
Serial.print(" >> NAME REUSED << ");
Serial.println();
myFile.close();
}
} // dbMakeNames()
// void MakeDataFiles( "ManyF", 1024, 400, 1 );
void MakeDataFiles( char* szRoot, int numFiles, int startSize, int growSize ) {
char szPath[128];
char szFile[128];
char szTmp[128];
if ( Abort ) {
Serial.printf("\n\n\t!!!! PRIOR OPERATION WAS ABORTED !!!!!!!\n" );
return;
}
Abort = false;
for ( int ii = 0; ii < 3; ii++ ) {
if ( Abort ) break;
strcpy( szPath, szRoot );
if (!DISK.exists(szPath)) {
DISK.mkdir( szPath );
}
strcat( szPath, "/" );
strcat( szPath, dirL[ii] );
sprintf( szTmp, ".%d", numFiles);
strcat( szPath, szTmp );
DISK.mkdir( szPath );
if (!DISK.exists(szPath)) {
Serial.print("\tMake DF Dir failed!");
Serial.println(szPath);
return;
}
else {
Serial.print("\tMade DF Dir:");
Serial.println(szPath);
}
int xx = startSize;
for ( int jj = 0; jj < numFiles; jj++ ) {
sprintf( szFile, "%d", xx);
strcpy( szPath, szRoot );
strcat( szPath, "/" );
strcat( szPath, dirL[ii] );
sprintf( szTmp, ".%d", numFiles);
strcat( szPath, szTmp );
strcat( szPath, "/" );
strcat( szPath, szFile );
Serial.print("Make DF File:");
// Serial.print(szPath);
indexedDataWrite( dataL[ii], szPath, xx, growSize == 0 );
if ( Abort ) break;
xx += growSize;
if ( Serial.available() ) {
while ( Serial.available() ) {
Serial.read();
}
return;
}
}
}
} // end MakeDataFiles()
// void MakeDeepDirs( "ManyD", 10, 5, 500, 512, [1] );
void MakeDeepDirs( char* szRoot, int numDirs, int numFiles, uint32_t startSize, uint32_t growSize, int compoundGrow ) {
char szCurPath[256];
char szPath[256];
char szFile[128];
char szTmp[128];
strcpy( szCurPath, szRoot );
if ( 0 != szCurPath[0] ) {
DISK.mkdir( szCurPath );
if (!DISK.exists(szCurPath)) {
Serial.print("\tMake DD Dir failed!");
Serial.println(szCurPath);
return;
}
else {
Serial.print("\tMade DD Dir:");
Serial.println(szCurPath);
}
}
Serial.print("\tMake DeepDir :");
Serial.println(szCurPath);
if ( Abort ) {
Serial.printf("\n\n\t!!!! PRIOR OPERATION WAS ABORTED !!!!!!!\n" );
return;
}
Abort = false;
for ( int ii = 0; ii < numDirs; ii++ ) {
if ( Abort ) break;
sprintf( szTmp, "/D%d", ii);
strcat( szCurPath, szTmp );
sprintf( szTmp, ".%d", numFiles);
strcat( szCurPath, szTmp );
if ( !DISK.mkdir( szCurPath ) ) {
if (!DISK.exists(szCurPath)) {
Serial.print("\tMake dd Dir failed!");
Serial.println(szCurPath);
}
else {
Serial.print("\tMade dd Dir:");
Serial.println(szCurPath);
}
}
uint32_t xx = startSize;
for ( int jj = 0; jj < numFiles; jj++ ) {
strcpy( szPath, szCurPath );
strcat( szPath, "/" );
sprintf( szFile, "%lu", xx);
strcat( szPath, szFile );
Serial.print("Make DD File:");
// Serial.print(szPath);
indexedDataWrite( dataL[4], szPath, xx, growSize == 0 );
if ( Abort ) break;
xx += growSize * compoundGrow;
if ( Serial.available() ) {
while ( Serial.available() ) {
Serial.read();
}
return;
}
}
}
} // end MakeDeepDirs()
void indexedDataWrite( char *szBlock, char* szInPath, uint32_t xx, bool addFNum )
{
static uint32_t gNumFiles = 0;
static uint32_t gNumBlocks = 0;
File myFile;
char writeData[ 2 * BLOCK_SIZE + 10 ];
char szPath[256];
gNumFiles++;
totBWrite += xx;
uint32_t timeHere = micros();
if ( addFNum ) {
sprintf( szPath, "%s_%lu.txt", szInPath, gNumFiles );
}
else {
sprintf( szPath, "%s.txt", szInPath );
}
if (DISK.exists(szPath)) DISK.remove(szPath);
myFile = DISK.open(szPath, FILE_WRITE);
if ( !myFile ) {
Serial.print(" >> iDW Fail Open << \t");
Serial.println(szPath);
gNumFiles--;
return;
}
strncpy( writeData, szBlock, BLOCK_SIZE + 1 );
Serial.print(" ");
Serial.print(szPath);
Serial.print("\tFile size=");
Serial.print(xx);
uint32_t kk;
for ( kk = 0; kk < xx; kk += BLOCK_SIZE ) {
if ( BLOCK_SIZE >= 16 ) { // UPDATE writeData[] HERE
if ( kk + 2 * BLOCK_SIZE >= xx) break; // Early exit to write szPath
// 16 CHARS == 4 file chars, '.', #### 4 dig numFile, 'x', 0x##### 5 char Hex numBlock, '\n'
sprintf( &writeData[2], ".%4lux%7lX\n", gNumFiles, gNumBlocks );
}
if ( kk + BLOCK_SIZE <= xx) {
myFile.print( writeData );
}
else {
for ( uint32_t ll = 0; ll + kk < xx; ll++ ) {
myFile.print( writeData[ll] );
}
}
gNumBlocks++;
}
if ( kk + BLOCK_SIZE <= xx) { // End file with szPath
uint32_t ii = strlen( szPath );
uint32_t jj = xx - kk;
while ( jj > ii ) {
myFile.print( '*' );
jj--;
kk++;
}
uint32_t ll = 0;
if ( ii > jj) ll = ii - jj;
for ( ; kk < xx; kk++, ll++ ) {
myFile.print( szPath[ll] );
}
}
Serial.print("=");
Serial.print(myFile.size());
Serial.print("\t us=");
Serial.print(micros() - timeHere);
Serial.print(" end:");
if ( xx != myFile.size() ) {
Abort = true;
Serial.print(" >> WRONG << ");
}
Serial.print(writeData);
myFile.close();
timeHere = micros() - timeHere;
if (!DISK.exists(szPath)) {
Abort = true;
Serial.print("\tFile WRITE failed!\n");
}
} // end indexedDataWrite()
void showMediaSpace()
{
static uint32_t timeHere = micros();
timeHere = micros() - timeHere;
Serial.print("\t========================= ");
Serial.print("Media Size=");
Serial.print(DISK.totalSize());
Serial.print("\tUsed Size=");
Serial.print(DISK.usedSize());
if ( timeHere > 5 ) {
Serial.print("\t us=");
Serial.print(timeHere);
if ( totBWrite > 0 ) {
Serial.print(" KB/sec=");
Serial.print((totBWrite / 1024.0) / (timeHere / 1000000.0));
Serial.print(" Bytes written=");
Serial.print(totBWrite);
}
}
Serial.println();
timeHere = micros();
totBWrite = 0;
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
MTP.loop(); //This is mandatory to be placed in the loop code.
#endif
} // end showMediaSpace())
uint32_t fTot, totSize, errTot;
void printDirectory() {
fTot = 0, totSize = 0;
printDirectory(DISK.open("/"), 0);
Serial.printf("\n%Total %u files of Size %u Bytes\n", fTot, totSize);
Serial.printf("Bytes Used: %llu, Bytes Total:%llu\n", DISK.usedSize(), DISK.totalSize());
}
void directoryVerify() {
fTot = 0, totSize = 0, errTot = 0;
double verCyc = ARM_DWT_CYCCNT;
directoryVerify(DISK.open("/"), 0);
verCyc = 1000.0 * (ARM_DWT_CYCCNT - verCyc) / F_CPU_ACTUAL;
Serial.printf("\n%Total %u files of Size %u Bytes\t Verify Errors %u\t%lf msec\n", fTot, totSize, errTot, verCyc);
Serial.printf("Bytes Used: %llu, Bytes Total:%llu\n", DISK.usedSize(), DISK.totalSize());
}
void deleteAllDirectory( File dir, char *fullPath ) {
char myPath[ 256 ] = "";
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
MTP.loop(); //This is mandatory to be placed in the loop code.
#endif
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
uint32_t tRem = micros();
if (entry.isDirectory()) {
strcpy( myPath, fullPath);
strcat( myPath, entry.name());
strcat( myPath, "/");
deleteAllDirectory(entry, myPath);
entry.close();
if ( !DISK.rmdir( myPath ) )
Serial.print( " Fail remove DIR>\t");
else
Serial.print( " Removed DIR>\t");
Serial.printf( "%s in %d us\n", myPath, micros() - tRem );
} else {
strcpy( myPath, fullPath);
strcat( myPath, entry.name());
entry.close();
if ( !DISK.remove( myPath ) )
Serial.print( "\tFail remove>\t");
else
Serial.print( "\tRemoved>\t");
Serial.printf( "%s in %d us\n", myPath, micros() - tRem );
}
}
} // deleteAllDirectory()
void printDirectory( File dir, int numTabs) {
uint64_t fSize = 0;
uint32_t dCnt = 0, fCnt = 0;
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
MTP.loop(); //This is mandatory to be placed in the loop code.
#endif
if ( 0 == dir ) {
Serial.printf( "\t>>>\t>>>>> No Dir\n" );
return;
}
while (true) {
File entry = dir.openNextFile();
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print(" ");
}
if (! entry) {
// no more files
Serial.printf("\n %u dirs with %u files of Size %llu Bytes\n", dCnt, fCnt, fSize);
fTot += fCnt;
totSize += fSize;
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print(" ");
}
if (entry.isDirectory()) {
Serial.print("DIR ");
dCnt++;
} else {
Serial.print("FILE ");
fCnt++;
fSize += entry.size();
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println(" / ");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t");
Serial.println(entry.size(), DEC);
}
entry.close();
//Serial.flush();
delay(1);
}
} // end printDirectory()
void xferSD( ) { // do MediaTransfer to SD
#ifndef USE_SDIO_SD
static bool initSD = true;
if ( initSD && !SD.begin(BUILTIN_SDCARD)) { // see if the card is present and can be initialized:
Serial.println("\n\n SD Card failed, or not present - Cannot do Xfer\n");
return;
}
else {
if ( initSD ) Serial.print("Initialized SD card.\n");
initSD = false;
// char szSDdir[] = "/"; // COPY to SD ROOT
char szSDdir[16] = "LFS_CPY0/"; // COPY to SD subdirectory
char fileID = '0';;
if ( '/' != szSDdir[0] ) {
do {
szSDdir[7] = fileID;
File fDir = SD.open( szSDdir );
if ( !fDir )
break;
fDir.close();
fileID++;
} while ( fileID < '9' );
if ( fileID == '9' ) {
Serial.print( "Disk has 9 folders 0-8! DO :: clean the SD card");
return;
}
SD.mkdir( szSDdir );
}
Serial.println("\n STARTING :: LittleFS copy to SD card XFER ...\n\n");
mediaTransfer( myfs.open("/"), szSDdir ); // TO SD
Serial.printf("\n LittleFS copy to SD card XFER COMPLETE in folder: %s\n\n", szSDdir);
}
#endif
} // end xferSD()
void mediaTransfer(File dir, char* szDir) {
char szNewDir[256];
while (true) {
File entry = dir.openNextFile();
if (! entry) {
break;
}
if (entry.isDirectory()) {
strcpy( szNewDir, szDir);
SD.mkdir( szNewDir );
strcat( szNewDir, entry.name());
SD.mkdir( szNewDir );
strcat( szNewDir, "/");
mediaTransfer(entry, szNewDir);
} else {
uint64_t fileSize, sizeCnt = 0, xfSize = 1;
char mm[512];
strcpy( szNewDir, szDir);
strcat( szNewDir, entry.name() );
File dataFile;
dataFile = SD.open(szNewDir, FILE_WRITE_BEGIN);
if ( !dataFile ) {
Serial.printf("\td_FILE: NOT open : %s [d:%s :: f:%s]\n", szNewDir, szDir, entry.name() );
}
fileSize = entry.size();
while ( entry.available() ) {
if ( fileSize < sizeCnt ) break;
if ( fileSize - sizeCnt >= sizeof(mm) ) xfSize = sizeof(mm);
else xfSize = fileSize - sizeCnt;
entry.read( &mm , xfSize );
dataFile.write( &mm , xfSize );
sizeCnt += xfSize;
}
if (fileSize != sizeCnt ) {
Serial.print("\n File Size Error:: ");
Serial.println( entry.name() );
}
dataFile.close();
}
entry.close();
}
} // end mediaTransfer()
void directoryVerify( File dir, int numTabs, uint32_t numFiles) {
static char szTmp[100];
uint64_t fSize = 0;
uint64_t fSizeDir = 0;
uint32_t dCnt = 0, fCnt = 0;
uint32_t fErrs = 0;
uint32_t verCnt = 0;
double verCyc = ARM_DWT_CYCCNT;
#if defined(USB_MTPDISK) || defined(USB_MTPDISK_SERIAL)
MTP.loop(); //This is mandatory to be placed in the loop code.
#endif
if ( 0 == dir ) {
Serial.printf( "\t>>>\t>>>>> No Dir\n" );
return;
}
while (true) {
File entry = dir.openNextFile();
Serial.print(" ");
if (! entry) { // no more files
fTot += fCnt;
totSize += fSizeDir;