LittleFS Test
QSPI flash begin
Flash ID: EF 40 18
Flash size is 16.00 Mbyte
attemping to mount existing media
couldn't mount media, attemping to format
waited 22266 us
waited 119 us
waited 22310 us
waited 121 us
attemping to mount freshly formatted media
success
started
waited 98 us
opened test.txt
wrote to file
waited 101 us
read 0 bytes
done printing, now close the file
try writing more
opened test.txt again for writing
after write
File this=2006fdbc, f=20203650
LittleFSFile this=20203650, refcount=1
waited 116 us
opened file for read
read 34 bytes
This is a test....
another test
printDirectory
--------------
test.txt 34
...
LittleFS Test
QSPI flash begin
Flash ID: 00 EF AA
Error starting ramdisk
Might be a while until I work with the large NAND flash chip. Many details to consider, like checking the ECC status and bad block remapping.
typedef struct {
uint32_t sample_index;
uint32_t sample_time;
} LogData_t;
void process_interval() {
File f;
LogData_t logdata;
if (samples_in_file == SAMPLES_PER_FILE) {
which_file++;
samples_in_file = 0;
if (which_file == (sizeof(file_names)/sizeof(file_names[0])) which_file = 0;
f = myfs.open(file_names[which_file], FILE_WRITE);
file.seek(0, SeekSet);
} else {
f = myfs.open(file_names[which_file], FILE_WRITE);
}
logdate.sample_index = ++sample_index;
logdata.sample_time = millis();
f.write(&logdata, sizeof(logdata));
f.close();
samples_in_file++;
}
File f= myfs.open("test", FILE_WRITE);
f.seek(1000, SeekSeT);
f.seek(0, SeekSet);
f.print("a");
f.close();
#include <LittleFS.h>
#include <IntervalTimer.h>
#include <SPI.h>
#define TEST_RAM
//#define TEST_SPI
//#define TEST_QSPI
#ifdef TEST_RAM
LittleFS_RAM myfs;
char buf[200000];
#elif defined(TEST_SPI)
LittleFS_SPIFlash myfs;
#else
LittleFS_QSPIFlash myfs;
#endif
void setup() {
// pinMode(13, OUTPUT);
// digitalWrite(13, HIGH);
SPI.begin();
while (!Serial) ; // wait
Serial.println("LittleFS Test"); delay(5);
#ifdef TEST_RAM
if (!myfs.begin(buf, sizeof(buf))) {
#elif defined(TEST_SPI)
pinMode(7, OUTPUT);
digitalWriteFast(7, LOW);
SPI2.setMOSI(50);
SPI2.setMISO(54);
SPI2.setSCK(49);
SPI2.begin();
if (!myfs.begin(51, SPI2)) {
#else
if (!myfs.begin()) {
#endif
Serial.println("Error starting LittleFS Disk");
while (1) ;
}
Serial.println("started");
File f = myfs.open("test.txt", FILE_WRITE);
Serial.println("opened test.txt");
// Output 64 zeros...
f.println("0000000000000000000000000000000000000000000000000000000000000000");
Serial.println("wrote to file");
Serial.printf("POS: % u Size: % u\n", f.position(), f.size());
f.close();
delay(10);
printDirectory();
}
uint16_t loop_count = 0;
void loop() {
delay(1000);
uint8_t buffer[100];
File f = myfs.open("test.txt", FILE_READ);
int cbRead = f.read(buffer, sizeof(buffer));
Serial.printf(" % u: % d - % s\n", loop_count, cbRead, buffer);
uint8_t buf_pos = loop_count++ & 0x1f;
f.seek(buf_pos, SeekSet);
buffer[buf_pos] = (buffer[buf_pos] < '9') ? buffer[buf_pos]+1 : '0';
f.write(buffer[buf_pos]);
f.close();
printDirectory();
}
void printDirectory() {
Serial.println("printDirectory\n--------------");
printDirectory(myfs.open(" / "), 0);
Serial.println();
}
void printDirectory(File dir, int numTabs) {
//dir.whoami();
while (true) {
static int count = 0;
if (++count > 20) while (1) ;
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println(" / ");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
LittleFS Test
started
opened test.txt
wrote to file
POS: 66 Size: 66
printDirectory
--------------
0: 66 - 0000000000000000000000000000000000000000000000000000000000000000
printDirectory
--------------
1: 66 - 0000000000000000000000000000000000000000000000000000000000000000
printDirectory
--------------
2: 66 - 0000000000000000000000000000000000000000000000000000000000000000
printDirectory
--------------
buffer[buf_pos] = (buffer[buf_pos] < '9') ? buffer[buf_pos]++ : '0';
buffer[buf_pos] = (buffer[buf_pos] < '9') ? buffer[buf_pos]+1 : '0';
#define FILE_READ 0
#define FILE_WRITE 1
Oh, opps, sorry about that static count I left in the test code. ...
void printDirectory(File dir, int numTabs) {
...
[B] static int count = 0; // BUGBUG remove this test code
if (++count > 20) while (1) ; // BUGBUG remove this test code
[/B]
#include <LittleFS.h>
#include <IntervalTimer.h>
#include <SPI.h>
//#define TEST_RAM
#define TEST_SPI
//#define TEST_QSPI
#ifdef TEST_RAM
LittleFS_RAM myfs;
char buf[200000];
#elif defined(TEST_SPI)
LittleFS_SPIFlash myfs;
#else
LittleFS_QSPIFlash myfs;
#endif
void setup() {
// pinMode(13, OUTPUT);
// digitalWrite(13, HIGH);
SPI.begin();
while (!Serial) ; // wait
Serial.println("LittleFS Test"); delay(5);
#ifdef TEST_RAM
if (!myfs.begin(buf, sizeof(buf))) {
#elif defined(TEST_SPI)
if (!myfs.begin(6)) {
#else
if (!myfs.begin()) {
#endif
Serial.println("Error starting LittleFS Disk");
while (1) ;
}
Serial.println("started");
File f = myfs.open("test.txt", FILE_WRITE);
Serial.println("opened test.txt");
// Output 64 zeros...
f.println("0000000000000000000000000000000000000000000000000000000000000000");
Serial.println("wrote to file");
Serial.printf("POS: % u Size: % u\n", f.position(), f.size());
f.close();
delay(10);
printDirectory();
}
uint16_t loop_count = 0;
void loop() {
delay(1000);
uint8_t buffer[100];
File f = myfs.open("test.txt", FILE_READ);
int cbRead = f.read(buffer, sizeof(buffer));
Serial.printf(" % u: % d - % s\n", loop_count, cbRead, buffer);
uint8_t buf_pos = loop_count++ & 0x1f;
f.seek(buf_pos, SeekSet);
uint8_t ch = buffer[buf_pos];
ch = (ch < '9') ? ch + 1 : '0';
buffer[buf_pos] = ch;
f.write(buffer[buf_pos]);
f.close();
Serial.printf("%d:%s\n", buf_pos, buffer);
printDirectory();
}
void printDirectory() {
Serial.println("printDirectory\n--------------");
printDirectory(myfs.open("/"), 0);
Serial.println();
}
void printDirectory(File dir, int numTabs) {
//dir.whoami();
while (true) {
static int count = 0;
if (++count > 20) while (1) ;
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println(" / ");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
LittleFS Test
flash begin
Flash ID: EF 40 17
Flash size is 8.00 Mbyte
attemping to mount existing media
success
started
opened test.txt
wrote to file
POS: 200 Size: 200
waited 364 us
printDirectory
--------------
myfolder /
morestuff.txt 78
test.txt 200
0: 100 - 0000!00!0!0000////another test
This is a test....
another test
00000000000000000000000000000000
waited 364 us
0:1000!00!0!0000////another test
This is a test....
another test
00000000000000000000000000000000
printDirectory
--------------
myfolder /
morestuff.txt 78
test.txt 200
1: 100 - 1000!00!0!0000////another test
This is a test....
another test
00000000000000000000000000000000
waited 364 us
1:1100!00!0!0000////another test
This is a test....
another test
00000000000000000000000000000000
printDirectory
--------------
myfolder /
morestuff.txt 78
test.txt 200
2: 100 - 1100!00!0!0000////another test
This is a test....
another test
00000000000000000000000000000000
waited 364 us
2:1110!00!0!0000////another test
This is a test....
another test
00000000000000000000000000000000
printDirectory
--------------
myfolder /
morestuff.txt 78
test.txt 200
3: 100 - 1110!00!0!0000////another test
This is a test....
another test
00000000000000000000000000000000
waited 50031 us
waited 408 us
waited 185 us
3:1111!00!0!0000////another test
This is a test....
another test
00000000000000000000000000000000
printDirectory
--------------
Edit: Fixed the above program to at least be able to update ... Silly mistake
should be:Code:buffer[buf_pos] = (buffer[buf_pos] < '9') ? buffer[buf_pos]++ : '0';
Code:buffer[buf_pos] = (buffer[buf_pos] < '9') ? buffer[buf_pos]+1 : '0';
But that implies I can write to a file I opened as Read...
As for open a file for READ/WRITE. The only two defines I see in FS.h are:
So obviously: does not good to do: FILE_READ|FILE_WRITE.Code:#define FILE_READ 0 #define FILE_WRITE 1
And obviously my seek to a position here and do a write did not truncate the file.
Also not sure yet why not showing anything in directory search.
// File open flags
enum lfs_open_flags {
// open flags
LFS_O_RDONLY = 1, // Open a file as read only
LFS_O_WRONLY = 2, // Open a file as write only
LFS_O_RDWR = 3, // Open a file as read and write
LFS_O_CREAT = 0x0100, // Create a file if it does not exist
LFS_O_EXCL = 0x0200, // Fail if a file already exists
LFS_O_TRUNC = 0x0400, // Truncate the existing file to zero size
LFS_O_APPEND = 0x0800, // Move to end of file on every write
// internally used flags
LFS_F_DIRTY = 0x010000, // File does not match storage
LFS_F_WRITING = 0x020000, // File has been written since last flush
LFS_F_READING = 0x040000, // File has been read since last flush
LFS_F_ERRED = 0x080000, // An error occured during write
LFS_F_INLINE = 0x100000, // Currently inlined in directory entry
LFS_F_OPENED = 0x200000, // File has been opened
};
#if USE_FCNTL_H
#include <fcntl.h>
/* values for GNU Arm Embedded Toolchain.
[COLOR="#FF0000"] * O_RDONLY: 0x0
* O_WRONLY: 0x1[/COLOR]
* O_RDWR: 0x2
* O_ACCMODE: 0x3
* O_APPEND: 0x8
* O_CREAT: 0x200
* O_TRUNC: 0x400
* O_EXCL: 0x800
* O_SYNC: 0x2000
* O_NONBLOCK: 0x4000
*/
/** Use O_NONBLOCK for open at EOF */
#define O_AT_END O_NONBLOCK ///< Open at EOF.
typedef int oflag_t;
#else // USE_FCNTL_H
[COLOR="#FF0000"]#define O_RDONLY 0X00 ///< Open for reading only.
#define O_WRONLY 0X01 ///< Open for writing only.[/COLOR]
#define O_RDWR 0X02 ///< Open for reading and writing.
#define O_AT_END 0X04 ///< Open at EOF.
#define O_APPEND 0X08 ///< Set append mode.
#define O_CREAT 0x10 ///< Create file if it does not exist.
#define O_TRUNC 0x20 ///< Truncate file to zero length.
#define O_EXCL 0x40 ///< Fail if the file exists.
#define O_SYNC 0x80 ///< Synchronized write I/O operations.
#define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) ///< Mask for access mode.
typedef uint8_t oflag_t;
#endif // USE_FCNTL_H
#define O_READ O_RDONLY
#define O_WRITE O_WRONLY
Another slight update: I changed from RAMDISK to SPI disk... and running on 4.1 with propshield again...
Fixed the print dir... function, something changed the "/" to " / " ... which did not find anything. I have been having some strange annoying Arduino IDE things... Yeah normally for bigger things use sublime...
...
Note: the code only runs the loop a couple of times at least for this run and hungCode:... void printDirectory(File dir, int numTabs) { //dir.whoami(); while (true) { [B][COLOR="#FF0000"] static int count = 0; if (++count > 20) while (1) ; [/COLOR][/B] ...
Code:LittleFS Test [CODE]another test 00000000000000000000000000000000 printDirectory --------------
Will try again. Again wonder if there is some truncate function or do I need to maybe call remove first on the file name... ?
:0 - Forgot to remove it... Thanks... It is working.@KurtE (all) - in every LittleFS sketch with printDirectory() from original ... COMMENT out or REMOVE those two RED/BOLD lines. It was DEBUG to halt code for early testing after some 20 cycles inside that function.
See p#110 - after Paul's earlier note.
#define FILE_RDWR 0X02 ///< Open for reading and writing.
#define FILE_AT_END 0X04 ///< Open at EOF.
#define FILE_APPEND 0X08 ///< Set append mode.
#define FILE_CREATE 0x10 ///< Create file if it does not exist.
#define FILE_TRUNC 0x20 ///< Truncate file to zero length.
#define FILE_EXCL 0x40 ///< Fail if the file exists.
#define FILE_SYNC 0x80 ///< Synchronized write I/O operations.
#include <LittleFS.h>
#include <IntervalTimer.h>
#include <SPI.h>
#define TEST_RAM
//#define TEST_SPI
//#define TEST_QSPI
#ifdef TEST_RAM
LittleFS_RAM myfs;
char buf[200000];
#elif defined(TEST_SPI)
LittleFS_SPIFlash myfs;
#else
LittleFS_QSPIFlash myfs;
#endif
File f;
void setup() {
// pinMode(13, OUTPUT);
// digitalWrite(13, HIGH);
SPI.begin();
while (!Serial) ; // wait
Serial.println("LittleFS Test"); delay(5);
#ifdef TEST_RAM
if (!myfs.begin(buf, sizeof(buf))) {
#elif defined(TEST_SPI)
pinMode(6, INPUT_PULLUP);
if (!myfs.begin(6)) {
#else
if (!myfs.begin()) {
#endif
Serial.println("Error starting LittleFS Disk");
while (1) ;
}
Serial.println("started");
f = myfs.open("test1.txt", FILE_CREATE | FILE_RDWR);
Serial.println("opened test.txt");
// Output 64 zeros...
f.println("0000000000000000000000000000000000000000000000000000000000000000");
Serial.println("wrote to file");
Serial.printf("POS: % u Size: % u\n", f.position(), f.size());
//f.close();
f.seek(0);
delay(10);
printDirectory();
}
uint16_t loop_count = 0;
void loop() {
delay(1000);
uint8_t buffer[100];
//File f = myfs.open("test.txt", FILE_READ);
int cbRead = f.read(buffer, sizeof(buffer));
Serial.printf(" % u: % d - % s\n", loop_count, cbRead, buffer);
uint8_t buf_pos = loop_count++ & 0x1f;
f.seek(buf_pos, SeekSet);
uint8_t ch = buffer[buf_pos];
ch = (ch < '9') ? ch + 1 : '0';
buffer[buf_pos] = ch;
f.write(buffer[buf_pos]);
f.seek(0);
//f.close();
Serial.printf("%d:%s\n", buf_pos, buffer);
printDirectory();
if(loop_count == 100){
f.close();
Serial.println("Test Finished");
printDirectory();
while(1);
}
}
void printDirectory() {
Serial.println("printDirectory\n--------------");
printDirectory(myfs.open("/"), 0);
Serial.println();
}
void printDirectory(File dir, int numTabs) {
//dir.whoami();
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println(" / ");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
Serial.println("started");
myfs.remove("test.txt");
File f = myfs.open("test.txt", FILE_WRITE);
Serial.println("opened test.txt");
// Output 64 zeros...
f.seek(100, SeekSet);
Serial.printf("POS: %u Size: %u\n", f.position(), f.size());
f.seek(0, SeekSet);
f.println("0000000000000000000000000000000000000000000000000000000000000000");
Serial.println("wrote to file");
Serial.printf("POS: %u Size: %u\n", f.position(), f.size());
f.close();
opened test.txt
POS: 100 Size: 0
wrote to file
POS: 66 Size: 66
c) what does FILE_READ or FILE_WRITE mean.
@mjs513 - Yes I think we should have more defines. Also not sure how much they are or should be enforced.
...
Realistically, we're probably only going to have 2 filesystems on Teensy in the foreseeable future: SdFat and LittleFS. These are just the logical filesystems. I imagine we'll have SdFat accessing SD cards and USB host MSC, and LittleFS accessing NOR & NAND flash on SPI & QSPI, and unused program memory, and of course the ramdisk. So that's a total of 8 supported disk types, 2 using SdFat and 6 using LittleFS. Are there others I'm forgetting?
myfs.remove("test.txt");
File f = myfs.open("test.txt", FILE_WRITE);
Serial.println("opened test.txt");
// Output 64 zeros...
f.seek(100, SeekSet);
Serial.printf("POS: %u Size: %u\n", f.position(), f.size());
f.seek(0, SeekSet);
f.println("0000000000000000000000000000000000000000000000000000000000000000");
Serial.println("wrote to file");
Serial.printf("POS: %u Size: %u\n", f.position(), f.size());
f.close();
// wonderint case sensitive?
File f2 = myfs.open("TEST.txt", FILE_WRITE);
f2.seek(0, SeekSet);
f2.println("0000000000000000000000000000000000000000000000000000000000000000");
f2.close();
printDirectory
--------------
TEST.txt 66
myfolder /
morestuff.txt 78
test.txt 66
.printDirectory RAMDISK
--------------
FILE A_file.txt 0
FILE B_file.txt 200
FILE C_file.txt 400
FILE D_file.txt 600
FILE E_file.txt 400
FILE F_file.txt 500
FILE G_file.txt 600
FILE H_file.txt 700
FILE I_file.txt 800
FILE J_file.txt 900
FILE K_file.txt 1000
FILE L_file.txt 1100
FILE M_file.txt 1200
FILE N_file.txt 1300
FILE O_file.txt 1400
FILE P_file.txt 1500
FILE Q_file.txt 1600
FILE R_file.txt 0
FILE S_file.txt 0
FILE T_file.txt 0
FILE U_file.txt 0
FILE V_file.txt 0
FILE W_file.txt 0
FILE X_file.txt 0
FILE Y_file.txt 0
FILE Z_file.txt 0
.printDirectory RAMDISK
--------------
FILE A_file.txt 1000
FILE B_file.txt 100
FILE C_file.txt 200
FILE D_file.txt 300
FILE E_file.txt 400
FILE F_file.txt 500
FILE G_file.txt 600
FILE H_file.txt 700
FILE I_file.txt 800
FILE J_file.txt 900
FILE K_file.txt 1000
FILE L_file.txt 1100
FILE M_file.txt 1200
FILE N_file.txt 1300
FILE O_file.txt 1400
FILE P_file.txt 1500
FILE Q_file.txt 1600
FILE R_file.txt 1700
FILE S_file.txt 1800
FILE T_file.txt 1900
FILE U_file.txt 2000
FILE V_file.txt 2100
FILE W_file.txt 2200
FILE X_file.txt 2300
FILE Y_file.txt 2400
FILE Z_file.txt 2500
What about LittleFS on PCB Boot Flash in a reserved area?
myflash.begin(36, SPI2);